From 11808e32332fad8aec0b007f304b142707259c66 Mon Sep 17 00:00:00 2001 From: maxwell Date: Thu, 6 Jan 2011 11:47:16 -0800 Subject: [PATCH 1/8] adding tests for pubsub client' --- lib/pubsubhubbub.rb | 14 ++++++-------- spec/lib/pubsubhubbub_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 spec/lib/pubsubhubbub_spec.rb diff --git a/lib/pubsubhubbub.rb b/lib/pubsubhubbub.rb index bb0bcf2a3..2610027cf 100644 --- a/lib/pubsubhubbub.rb +++ b/lib/pubsubhubbub.rb @@ -4,16 +4,14 @@ class PubSubHubbub - H = {"User-Agent" => "PubSubHubbub Ruby", "Content-Type" => "application/x-www-form-urlencoded"} def initialize(hub, options={}) - @headers = H.merge(options[:head]) if options[:head] - @hub = hub.kind_of?(URI) ? hub : URI::parse(hub) - end + @headers = H.merge(options[:head]) if options[:head] + @hub = hub + end - def publish(feed) - RestClient.post @hub, :headers => H, 'hub.url' => feed, 'hub.mode' => 'publish' - end + def publish(feed) + RestClient.post(@hub, :headers => @headers, 'hub.url' => feed, 'hub.mode' => 'publish') + end end - diff --git a/spec/lib/pubsubhubbub_spec.rb b/spec/lib/pubsubhubbub_spec.rb new file mode 100644 index 000000000..fadf9b445 --- /dev/null +++ b/spec/lib/pubsubhubbub_spec.rb @@ -0,0 +1,23 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe PubSubHubbub do + describe '#initialize' do + end + + describe '#publish' do + it 'posts the feed to the given hub' do + hub = "http://hubzord.com/" + feed = 'http://rss.com/dom.atom' + body = {'hub.url' => feed, 'hub.mode' => 'publish'} + + stub_request(:post, "http://hubzord.com/"). + with(:body => "hub.url=http%3A%2F%2Frss.com%2Fdom.atom&headers=&hub.mode=publish", + :headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'65', 'Content-Type'=>'application/x-www-form-urlencoded'}).to_return(:status => [202, 'you are awesome']) + PubSubHubbub.new(hub).publish(feed).code.should == 202 + end + end +end From f02ed1e3c48efa15db1a644fe7772a04faaa91f6 Mon Sep 17 00:00:00 2001 From: maxwell Date: Thu, 6 Jan 2011 12:19:36 -0800 Subject: [PATCH 2/8] unstubing and restubbing before doing pubsubhubub --- spec/lib/pubsubhubbub_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/lib/pubsubhubbub_spec.rb b/spec/lib/pubsubhubbub_spec.rb index fadf9b445..1a915266d 100644 --- a/spec/lib/pubsubhubbub_spec.rb +++ b/spec/lib/pubsubhubbub_spec.rb @@ -5,6 +5,15 @@ require 'spec_helper' describe PubSubHubbub do + before :all do + RestClient.unstub!(:post) + end + + after :all do + + RestClient.stub!(:post).and_return(FakeHttpRequest.new(:success)) + end + describe '#initialize' do end From 0284f0fd6d161b4fa24da95274b6ff6661562db4 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 6 Jan 2011 11:35:58 -0800 Subject: [PATCH 3/8] Write fixture for mysql export --- lib/tasks/fixtures.rake | 52 ++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 44 ------------------------------ spec/support/user_methods.rb | 42 +++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 44 deletions(-) create mode 100644 spec/support/user_methods.rb diff --git a/lib/tasks/fixtures.rake b/lib/tasks/fixtures.rake index e10882cf5..5c4f18edc 100644 --- a/lib/tasks/fixtures.rake +++ b/lib/tasks/fixtures.rake @@ -9,4 +9,56 @@ namespace :fixtures do UserFixer.regenerate_user_fixtures puts "Fixture regeneration complete." end + task :for_mysql_export do + puts "Populating DB for export." + require File.join(Rails.root,"config/environment") + require File.join(Rails.root,"spec/helper_methods") + require File.join(Rails.root,"spec/factories") + require File.join(Rails.root,"spec/support/user_methods") + include HelperMethods + + fantasy_resque do + Factory(:person) + Factory(:person) + Factory(:person) + user1 = Factory(:user_with_aspect) + user2 = Factory(:user_with_aspect) + connect_users(user1, user1.aspects.first, user2, user2.aspects.first) + user2.activate_contact(Factory(:person), user2.aspects.first) + + user3 = Factory(:user_with_aspect) + user4 = Factory(:user_with_aspect) + user3.send_contact_request_to(user4.person, user3.aspects.first) + user3.send_contact_request_to(Factory(:person), user3.aspects.first) + + batch_invitee = Invitation.create_invitee(:email => "random@example.com", :name => "Curious George", :invites => 3) + invitee = user1.invite_user("random2@example.net", user1.aspects.first.id, "Hello!") + + u1post = user1.post(:status_message, :message => "User2 can see this", :to => [user1.aspects.first.id]) + u3post = user3.post(:status_message, :message => "User3 can see this", :to => [user3.aspects.first.id]) + user3.comment("Hey me!", :on => u3post) + user2.comment("Hey you!", :on => u1post) + + user2.post(:photo, :user_file => uploaded_photo, :to => [user2.aspects.first.id]) + user3.post(:photo, :user_file => uploaded_photo, :to => [user3.aspects.first.id]) + + remote_user = Factory(:user_with_aspect) + user4.activate_contact(remote_user.person, user4.aspects.first) + remote_message = remote_user.build_post(:photo, :user_file => uploaded_photo, :to => remote_user.aspects.first.id) + remote_photo = remote_user.build_post(:status_message, :message => "from another server!", :to => remote_user.aspects.first.id) + request = remote_user.send_contact_request_to(user2.person, remote_user.aspects.first) + remote_user.delete + Contact.where(:user_id => remote_user.id).each{|c| c.delete} + Aspect.where(:user_id => remote_user.id).each{|c| c.delete} + + user4.receive(remote_message.to_diaspora_xml, remote_user.person) + user4.receive(remote_photo.to_diaspora_xml, remote_user.person) + remote_person = remote_user.person + remote_person.owner_id = nil + remote_person.save + + end + puts "Done generating fixtures, you can now export" + + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2d9901dae..ad8332b49 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -65,50 +65,6 @@ module Resque end ImageUploader.enable_processing = false -class User - def send_contact_request_to(desired_contact, aspect) - fantasy_resque do - contact = Contact.new(:person => desired_contact, - :user => self, - :pending => true) - contact.aspects << aspect - - if contact.save! - contact.dispatch_request - else - nil - end - end - end - - def post(class_name, opts = {}) - fantasy_resque do - p = build_post(class_name, opts) - if p.save! - raise 'MongoMapper failed to catch a failed save' unless p.id - - self.aspects.reload - - add_to_streams(p, opts[:to]) - dispatch_post(p, :to => opts[:to]) - end - p - end - end - - def comment(text, options = {}) - fantasy_resque do - c = build_comment(text, options) - if c.save! - raise 'MongoMapper failed to catch a failed save' unless c.id - dispatch_comment(c) - end - c - end - end -end - - class FakeHttpRequest def initialize(callback_wanted) diff --git a/spec/support/user_methods.rb b/spec/support/user_methods.rb new file mode 100644 index 000000000..d0288d7fa --- /dev/null +++ b/spec/support/user_methods.rb @@ -0,0 +1,42 @@ +class User + def send_contact_request_to(desired_contact, aspect) + fantasy_resque do + contact = Contact.new(:person => desired_contact, + :user => self, + :pending => true) + contact.aspects << aspect + + if contact.save! + contact.dispatch_request + else + nil + end + end + end + + def post(class_name, opts = {}) + fantasy_resque do + p = build_post(class_name, opts) + if p.save! + raise 'MongoMapper failed to catch a failed save' unless p.id + + self.aspects.reload + + add_to_streams(p, opts[:to]) + dispatch_post(p, :to => opts[:to]) + end + p + end + end + + def comment(text, options = {}) + fantasy_resque do + c = build_comment(text, options) + if c.save! + raise 'MongoMapper failed to catch a failed save' unless c.id + dispatch_comment(c) + end + c + end + end +end From 8d62ad8896510f993fdf7f44ac8c9ba4c89cde8f Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 6 Jan 2011 11:48:23 -0800 Subject: [PATCH 4/8] Add some reloads --- lib/tasks/fixtures.rake | 79 ++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/lib/tasks/fixtures.rake b/lib/tasks/fixtures.rake index 5c4f18edc..b54b4896f 100644 --- a/lib/tasks/fixtures.rake +++ b/lib/tasks/fixtures.rake @@ -18,45 +18,58 @@ namespace :fixtures do include HelperMethods fantasy_resque do - Factory(:person) - Factory(:person) - Factory(:person) - user1 = Factory(:user_with_aspect) - user2 = Factory(:user_with_aspect) - connect_users(user1, user1.aspects.first, user2, user2.aspects.first) - user2.activate_contact(Factory(:person), user2.aspects.first) + models = [] + Factory(:person) + Factory(:person) + Factory(:person) + user1 = Factory(:user_with_aspect) + user2 = Factory(:user_with_aspect) + connect_users(user1, user1.aspects.first, user2, user2.aspects.first) + user2.activate_contact(Factory(:person), user2.aspects.first) + user2.reload - user3 = Factory(:user_with_aspect) - user4 = Factory(:user_with_aspect) - user3.send_contact_request_to(user4.person, user3.aspects.first) - user3.send_contact_request_to(Factory(:person), user3.aspects.first) + user3 = Factory(:user_with_aspect) + user4 = Factory(:user_with_aspect) + user3.send_contact_request_to(user4.person, user3.aspects.first) + user3.reload; user4.reload + user3.send_contact_request_to(Factory(:person), user3.aspects.first) - batch_invitee = Invitation.create_invitee(:email => "random@example.com", :name => "Curious George", :invites => 3) - invitee = user1.invite_user("random2@example.net", user1.aspects.first.id, "Hello!") + batch_invitee = Invitation.create_invitee(:email => "random@example.com", :name => "Curious George", :invites => 3) + invitee = user1.invite_user("random2@example.net", user1.aspects.first.id, "Hello!") + user1.reload - u1post = user1.post(:status_message, :message => "User2 can see this", :to => [user1.aspects.first.id]) - u3post = user3.post(:status_message, :message => "User3 can see this", :to => [user3.aspects.first.id]) - user3.comment("Hey me!", :on => u3post) - user2.comment("Hey you!", :on => u1post) + u1post = user1.post(:status_message, :message => "User2 can see this", :to => [user1.aspects.first.id]) + user1.reload + u3post = user3.post(:status_message, :message => "User3 can see this", :to => [user3.aspects.first.id]) + user3.reload + user3.comment("Hey me!", :on => u3post) + user3.reload + user2.comment("Hey you!", :on => u1post) + user2.reload - user2.post(:photo, :user_file => uploaded_photo, :to => [user2.aspects.first.id]) - user3.post(:photo, :user_file => uploaded_photo, :to => [user3.aspects.first.id]) + user2.post(:photo, :user_file => uploaded_photo, :to => [user2.aspects.first.id]) + user2.reload + user3.post(:photo, :user_file => uploaded_photo, :to => [user3.aspects.first.id]) + user3.reload - remote_user = Factory(:user_with_aspect) - user4.activate_contact(remote_user.person, user4.aspects.first) - remote_message = remote_user.build_post(:photo, :user_file => uploaded_photo, :to => remote_user.aspects.first.id) - remote_photo = remote_user.build_post(:status_message, :message => "from another server!", :to => remote_user.aspects.first.id) - request = remote_user.send_contact_request_to(user2.person, remote_user.aspects.first) - remote_user.delete - Contact.where(:user_id => remote_user.id).each{|c| c.delete} - Aspect.where(:user_id => remote_user.id).each{|c| c.delete} - - user4.receive(remote_message.to_diaspora_xml, remote_user.person) - user4.receive(remote_photo.to_diaspora_xml, remote_user.person) - remote_person = remote_user.person - remote_person.owner_id = nil - remote_person.save + remote_user = Factory(:user_with_aspect) + user4.activate_contact(remote_user.person, user4.aspects.first) + user4.reload + remote_message = remote_user.build_post(:photo, :user_file => uploaded_photo, :to => remote_user.aspects.first.id) + remote_photo = remote_user.build_post(:status_message, :message => "from another server!", :to => remote_user.aspects.first.id) + request = remote_user.send_contact_request_to(user2.person, remote_user.aspects.first) + remote_user.reload + remote_user.delete + Contact.where(:user_id => remote_user.id).each{|c| c.delete} + Aspect.where(:user_id => remote_user.id).each{|c| c.delete} + remote_person = remote_user.person + remote_person.owner_id = nil + remote_person.save + user4.receive(remote_message.to_diaspora_xml, remote_person) + user3.reload + user4.receive(remote_photo.to_diaspora_xml, remote_person) + user3.reload end puts "Done generating fixtures, you can now export" From 9ba43b148f0b1580c89272002c5c84ad2f2d5533 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 6 Jan 2011 12:07:39 -0800 Subject: [PATCH 5/8] Move resque modifications out to spec/support --- lib/tasks/fixtures.rake | 6 ++++++ spec/spec_helper.rb | 9 --------- spec/support/fake_resque.rb | 9 +++++++++ 3 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 spec/support/fake_resque.rb diff --git a/lib/tasks/fixtures.rake b/lib/tasks/fixtures.rake index b54b4896f..4d41da041 100644 --- a/lib/tasks/fixtures.rake +++ b/lib/tasks/fixtures.rake @@ -15,8 +15,14 @@ namespace :fixtures do require File.join(Rails.root,"spec/helper_methods") require File.join(Rails.root,"spec/factories") require File.join(Rails.root,"spec/support/user_methods") + require File.join(Rails.root,"spec/support/fake_resque") + include HelperMethods + Jobs::HttpPost.class_eval do + def self.perform(*args) + end + end fantasy_resque do models = [] Factory(:person) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ad8332b49..cf8572166 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -54,15 +54,6 @@ class FakeRedis true end end -module Resque - def enqueue(klass, *args) - if $process_queue - klass.send(:perform, *args) - else - true - end - end -end ImageUploader.enable_processing = false diff --git a/spec/support/fake_resque.rb b/spec/support/fake_resque.rb new file mode 100644 index 000000000..165d9ea10 --- /dev/null +++ b/spec/support/fake_resque.rb @@ -0,0 +1,9 @@ +module Resque + def enqueue(klass, *args) + if $process_queue + klass.send(:perform, *args) + else + true + end + end +end From 1d5b6fa6752847266f79ae7e5e107630acb00721 Mon Sep 17 00:00:00 2001 From: maxwell Date: Thu, 6 Jan 2011 12:19:36 -0800 Subject: [PATCH 6/8] unstubing and restubbing before doing pubsubhubub --- spec/lib/pubsubhubbub_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/lib/pubsubhubbub_spec.rb b/spec/lib/pubsubhubbub_spec.rb index 1a915266d..1a90c29c4 100644 --- a/spec/lib/pubsubhubbub_spec.rb +++ b/spec/lib/pubsubhubbub_spec.rb @@ -10,7 +10,6 @@ describe PubSubHubbub do end after :all do - RestClient.stub!(:post).and_return(FakeHttpRequest.new(:success)) end From 5818227904d4baa6c49505252026990f35d96d76 Mon Sep 17 00:00:00 2001 From: maxwell Date: Thu, 6 Jan 2011 13:13:09 -0800 Subject: [PATCH 7/8] adding job for posting to services --- app/models/jobs/post_to_service.rb | 16 ++++++++++++++++ spec/models/jobs/post_to_service_spec.rb | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 app/models/jobs/post_to_service.rb create mode 100644 spec/models/jobs/post_to_service_spec.rb diff --git a/app/models/jobs/post_to_service.rb b/app/models/jobs/post_to_service.rb new file mode 100644 index 000000000..20b695fd2 --- /dev/null +++ b/app/models/jobs/post_to_service.rb @@ -0,0 +1,16 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. +# +module Jobs + class PostToService + extend ResqueJobLogging + @queue = :http + + def self.perform(service_id, post_id, url) + service = Service.find_by_id(service_id) + post = Post.find_by_id(post_id) + service.post(post, url) + end + end +end diff --git a/spec/models/jobs/post_to_service_spec.rb b/spec/models/jobs/post_to_service_spec.rb new file mode 100644 index 000000000..59cb062c6 --- /dev/null +++ b/spec/models/jobs/post_to_service_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Jobs::PostToService do + it 'calls service#post with the given service' do + user = make_user + aspect = user.aspects.create(:name => "yeah") + post = user.post(:status_message, :message => 'foo', :to => aspect.id) + User.stub!(:find_by_id).with(user.id.to_s).and_return(user) + m = mock() + url = "foobar" + m.should_receive(:post).with(anything, url) + Service.stub!(:find_by_id).and_return(m) + Jobs::PostToService.perform("123", post.id.to_s, url) + end +end + From 8074e481a32c17bfe41998fd01b8b1c62a3c779c Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 6 Jan 2011 13:51:59 -0800 Subject: [PATCH 8/8] stream fix on aspects/show --- app/views/aspects/show.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/aspects/show.html.haml b/app/views/aspects/show.html.haml index 99f7567ab..99ff874f9 100644 --- a/app/views/aspects/show.html.haml +++ b/app/views/aspects/show.html.haml @@ -23,6 +23,6 @@ = render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @aspect_contacts_count, :options => false = render 'aspects/no_posts_message', :post_count => @post_count, :contact_count=> @aspect_contacts_count - %ul{:class => 'stream', :id => 'main_stream'} + #main_stream.stream = render 'shared/stream', :posts => @post_hashes =will_paginate @posts