From c430aaff18b493a554639d4adbc046f0984c64a2 Mon Sep 17 00:00:00 2001
From: maxwell <maxwell@joindiaspora.com>
Date: Fri, 18 Jun 2010 14:35:51 -0700
Subject: [PATCH] DG MS; added database_cleaner for spec runner.  touched up
 common specs for webhooks; depirated old status message to xml

---
 Gemfile                                       |  2 +-
 app/controllers/status_messages_controller.rb |  3 +-
 app/helpers/status_messages_helper.rb         | 10 -----
 config/environments/test.rb                   | 14 ++++++
 lib/common.rb                                 | 19 +++-----
 spec/lib/common_spec.rb                       | 30 ++++++-------
 spec/models/bookmark_spec.rb                  | 18 ++++++++
 spec/models/status_message_spec.rb            | 44 +------------------
 spec/spec_helper.rb                           | 18 ++++++--
 9 files changed, 69 insertions(+), 89 deletions(-)

diff --git a/Gemfile b/Gemfile
index 00dd77f64a..274a0b8f57 100644
--- a/Gemfile
+++ b/Gemfile
@@ -11,7 +11,6 @@ gem "haml"
 gem "devise", :git => "git://github.com/plataformatec/devise.git"
 gem 'roxml', :git => "git://github.com/Empact/roxml.git"
 
-
 gem 'dm-core'
 
 group :test do
@@ -22,6 +21,7 @@ group :test do
   gem 'redgreen'
   gem 'autotest'
   gem 'factory_girl_rails'
+  gem 'database_cleaner'
 end
 
 group :development do
diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb
index 5b53d898e1..0d9e1b9d8c 100644
--- a/app/controllers/status_messages_controller.rb
+++ b/app/controllers/status_messages_controller.rb
@@ -1,6 +1,5 @@
 class StatusMessagesController < ApplicationController
   before_filter :authenticate_user!
-  include StatusMessagesHelper
 
   def index
     @status_messages = StatusMessage.criteria.all.order_by( [:created_at, :desc] )
@@ -8,7 +7,7 @@ class StatusMessagesController < ApplicationController
 
     respond_to do |format|
       format.html 
-      format.xml {render :xml => StatusMessages.new(@status_messages).to_xml }
+      format.xml {render :xml => Post.build_xml_for(@status_messages)}
       format.json { render :json => @status_messages }
     end
 
diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb
index 5d1bcdc930..a2b81e2e3d 100644
--- a/app/helpers/status_messages_helper.rb
+++ b/app/helpers/status_messages_helper.rb
@@ -9,14 +9,4 @@ module StatusMessagesHelper
     end
   end
 
-  class StatusMessages
-    include ROXML
-   
-   def initialize(messages=[])
-     @statusmessages = messages
-   end
-
-   xml_accessor :statusmessages, :as => [StatusMessage]
-   attr_accessor :statusmessages
-  end
 end
diff --git a/config/environments/test.rb b/config/environments/test.rb
index 40d2220323..1a91344077 100644
--- a/config/environments/test.rb
+++ b/config/environments/test.rb
@@ -29,4 +29,18 @@ Diaspora::Application.configure do
   # This is necessary if your schema can't be completely dumped by the schema dumper,
   # like if you have constraints or database-specific column types
   # config.active_record.schema_format = :sql
+  #
+  #
+  #
+
+
+
+begin
+ require 'database_cleaner'
+ DatabaseCleaner.strategy = :truncation
+ DatabaseCleaner.orm = "mongoid"
+rescue LoadError => ignore_if_database_cleaner_not_present
+ puts "Error on cleaner"
+end
+  
 end
diff --git a/lib/common.rb b/lib/common.rb
index 2c20d85ccf..14714245c4 100644
--- a/lib/common.rb
+++ b/lib/common.rb
@@ -4,23 +4,18 @@ module Diaspora
     def self.included(klass)
       klass.class_eval do
       after_save :notify_friends
-
         @@queue = MessageHandler.new
         
         def notify_friends
-         if self.owner == User.first.email
-          xml = Post.build_xml_for(self)
-          @@queue.add_post_request( friends_with_permissions, xml )
-          @@queue.process
+          if self.owner == User.first.email
+            xml = Post.build_xml_for(self)
+            @@queue.add_post_request( friends_with_permissions, xml )
+            @@queue.process
           end
         end
-        
-        def prep_webhook  
-          self.to_xml.to_s
-        end
  
-        def prep_many  
-          "<post>#{self.prep_webhook}</post>"
+        def prep_webhook
+          "<post>#{self.to_xml.to_s}</post>"
         end
 
         def friends_with_permissions
@@ -29,7 +24,7 @@ module Diaspora
 
         def self.build_xml_for(posts)
           xml = "<posts>"
-          posts.each {|x| xml << x.prep_many}
+          posts.each {|x| xml << x.prep_webhook}
           xml = xml + "</posts>"
         end
       end
diff --git a/spec/lib/common_spec.rb b/spec/lib/common_spec.rb
index 90cf185b24..3691431862 100644
--- a/spec/lib/common_spec.rb
+++ b/spec/lib/common_spec.rb
@@ -8,7 +8,7 @@ describe Diaspora do
   describe Webhooks do
     before do
       @user = Factory.create(:user)
-      @post = Factory.build(:post)
+      @post = Factory.create(:post)
     end
 
     it "should add the following methods to Post on inclusion" do
@@ -18,7 +18,7 @@ describe Diaspora do
     end
 
     it "should convert an object to a proper webhook" do
-      @post.prep_webhook.should == @post.to_xml.to_s
+      @post.prep_webhook.should == "<post>#{@post.to_xml.to_s}</post>"
     end
 
     it "should retrieve all valid friend endpoints" do
@@ -31,25 +31,21 @@ describe Diaspora do
       @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
+    it "should send an owners post to their friends" do
+      Post.stub(:build_xml_for).and_return(true) 
+      Post.should_receive(:build_xml_for).and_return true
+      @post.save
     end
   
-    it "should check that it only sends a user's posts to their friends" do
-      Factory.create(:friend, :url => "http://www.bob.com")
-      Factory.create(:friend, :url => "http://www.alice.com")
-      Factory.create(:status_message)
-      Factory.create(:bookmark)
-
-      # this is a messagequeue thing; out of scope for webhooks action
+    it "should check that it does not send a friends post to an owners friends" do
+      Post.stub(:build_xml_for).and_return(true) 
+      Post.should_not_receive(:build_xml_for)
+      Factory.create(:post, :owner => "nottheowner@post.com")
     end
 
-    it "should ensure no duplicate url posts" do
-      pending
-      # this is a messagequeue thing; out of scope for webhooks action
-      
+    it "should ensure one url is created for every friend" do
+      5.times {Factory.create(:friend)}
+      @post.friends_with_permissions.size.should == 5
     end
 
     it "should build an xml object containing multiple Post types" do
diff --git a/spec/models/bookmark_spec.rb b/spec/models/bookmark_spec.rb
index 99cbbd2a53..4cb8b5fc62 100644
--- a/spec/models/bookmark_spec.rb
+++ b/spec/models/bookmark_spec.rb
@@ -13,4 +13,22 @@ describe Bookmark do
     n = Factory.create(:bookmark)
     n.owner.should == "bob@aol.com" 
   end
+
+  describe "XML" do
+    it 'should serialize to XML' do
+      Factory.create(:user)
+      message = Factory.create(:bookmark, :title => "Reddit", :link => "http://reddit.com")
+      message.to_xml.to_s.should include "<title>Reddit</title>"
+      message.to_xml.to_s.should include "<link>http://reddit.com</link>"
+    end
+  
+    it 'should marshal serialized XML to object' do       
+      xml = "<bookmark><title>Reddit</message><link>http://reddit.com</link><owner>bob@aol.com</owner></bookmark>" 
+      parsed = Bookmark.from_xml(xml)
+      parsed.title.should == "Reddit"
+      parsed.link.should == "http://reddit.com"
+      parsed.owner.should == "bob@aol.com"
+      parsed.valid?.should be_true
+    end
+  end
 end 
diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb
index 67e9f4dbb8..7bfa0af008 100644
--- a/spec/models/status_message_spec.rb
+++ b/spec/models/status_message_spec.rb
@@ -1,5 +1,4 @@
 require File.dirname(__FILE__) + '/../spec_helper'
-include StatusMessagesHelper
 
 describe StatusMessage do
   before do
@@ -40,53 +39,12 @@ describe StatusMessage do
     end
   
     it 'should marshal serialized XML to object' do       
-      xml = "<statusmessage>\n  <message>I hate WALRUSES!</message><owner>Bob@rob.ert</owner></statusmessage>" 
+      xml = "<statusmessage><message>I hate WALRUSES!</message><owner>Bob@rob.ert</owner></statusmessage>" 
       parsed = StatusMessage.from_xml(xml)
       parsed.message.should == "I hate WALRUSES!"
       parsed.owner.should == "Bob@rob.ert"
       parsed.valid?.should be_true
     end
   end
-
-  describe "retrieving" do
-    before do
-      @remote = Factory.create(:friend, :url => "fakeurl")#http://localhost:1254/")
-      Curl.stub!(:get).and_return(@@remote_xml)
-    end
-    it "should marshal xml and serialize it without error" do
-      StatusMessages.from_xml(@@remote_xml).to_xml.to_s.should == @@remote_xml
-    end
-    it "marshal retrieved xml" do
-      remote_msgs = StatusMessage.retrieve_from_friend(@remote)
-      local_msgs = StatusMessages.from_xml(@@remote_xml)
-
-
-      remote_msgs.statusmessages.each{ |m| local_msgs.statusmessages.include?(m).should be_true}
-      local_msgs.statusmessages.each{ |m| remote_msgs.statusmessages.include?(m).should be_true}
-    end
-  end
 end
 
-@@remote_xml = 
-"<statusmessages>
-  <statusmessage>
-    <message>jimmy's 22 whales</message>
-    <owner>tester@yahoo.com</owner>
-  </statusmessage>
-  <statusmessage>
-    <message>jimmy's 23 whales</message>
-    <owner>tester@yahoo.com</owner>
-  </statusmessage>
-  <statusmessage>
-    <message>jimmy's 24 whales</message>
-    <owner>tester@yahoo.com</owner>
-  </statusmessage>
-  <statusmessage>
-    <message>jimmy's 25 whales</message>
-    <owner>tester@yahoo.com</owner>
-  </statusmessage>
-  <statusmessage>
-    <message>jimmy's 26 whales</message>
-    <owner>tester@yahoo.com</owner>
-  </statusmessage>
-</statusmessages>"
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index bc3f0762af..060511f9a5 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -3,6 +3,7 @@
 ENV["RAILS_ENV"] ||= 'test'
 require File.dirname(__FILE__) + "/../config/environment" unless defined?(Rails)
 require 'rspec/rails'
+require 'database_cleaner'
 #require File.dirname(__FILE__) + "/factories"
 include Devise::TestHelpers
 
@@ -20,12 +21,21 @@ Rspec.configure do |config|
   # config.mock_with :rr
   config.mock_with :rspec
 
-  config.fixture_path = "#{::Rails.root}/spec/fixtures"
-    config.before(:each) do
-      Mongoid.master.collections.select { |c| c.name != 'system.indexes' }.each(&:drop)  
+  DatabaseCleaner.strategy = :truncation
+  DatabaseCleaner.orm = "mongoid"
 
+  config.before(:suite) do
+    DatabaseCleaner.strategy = :transaction
+    DatabaseCleaner.clean_with(:truncation)
+  end
 
-    end
+  config.before(:each) do
+    DatabaseCleaner.start
+  end
+
+  config.after(:each) do
+    DatabaseCleaner.clean
+  end
   
   
   # If you're not using ActiveRecord, or you'd prefer not to run each of your
-- 
GitLab