From ba6d136bc751810ee214f8898536589cf6174ccb Mon Sep 17 00:00:00 2001 From: danielvincent Date: Thu, 17 Jun 2010 15:22:02 -0700 Subject: [PATCH] DG MS; added specs for common module --- app/models/post.rb | 2 +- lib/common.rb | 25 +++---------------------- spec/lib/common_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 spec/lib/common_spec.rb diff --git a/app/models/post.rb b/app/models/post.rb index 926545c1c..be9ff1591 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -7,7 +7,7 @@ class Post include Mongoid::Document include Mongoid::Timestamps include ROXML - include Diaspora::Hookey + include Diaspora::Webhooks xml_accessor :owner xml_accessor :snippet diff --git a/lib/common.rb b/lib/common.rb index 942f476fb..218d34ca5 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -1,26 +1,7 @@ module Diaspora - module CommonFields + + module Webhooks def self.included(klass) - klass.class_eval do - include Mongoid::Document - include ROXML - include Mongoid::Timestamps - - xml_accessor :owner - xml_accessor :snippet - xml_accessor :source - - field :owner - field :source - field :snippet - end - end - end - - module Hookey - - def self.included(klass) - klass.class_eval do before_save :notify_friends @@ -33,7 +14,7 @@ module Diaspora end def prep_webhook - self.to_xml.to_s.chomp + self.to_xml.to_s end def friends_with_permissions diff --git a/spec/lib/common_spec.rb b/spec/lib/common_spec.rb new file mode 100644 index 000000000..0e9a3ebf9 --- /dev/null +++ b/spec/lib/common_spec.rb @@ -0,0 +1,40 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +include Diaspora + +describe Diaspora do + + describe Webhooks do + before do + @post = Factory.build(:post) + end + + it "should add the following methods to Post on inclusion" do + @post.respond_to?(:notify_friends).should be true + @post.respond_to?(:prep_webhook).should be true + @post.respond_to?(:friends_with_permissions).should be true + end + + it "should convert an object to a proper webhook" do + @post.prep_webhook.should == @post.to_xml.to_s + end + + it "should retrieve all valid friend endpoints" do + Factory.create(:friend, :url => "http://www.bob.com") + Factory.create(:friend, :url => "http://www.alice.com") + Factory.create(:friend, :url => "http://www.jane.com") + + @post.friends_with_permissions.should include("http://www.bob.com/receive/") + @post.friends_with_permissions.should include("http://www.alice.com/receive/") + @post.friends_with_permissions.should include("http://www.jane.com/receive/") + end + + it "should send all prepped webhooks to be processed" do + MessageHandler.any_instance.stubs(:add_post_request).returns(true) + MessageHandler.any_instance.stubs(:process).returns(true) + @post.notify_friends.should be true + end + + end + +end