From 55492535c6649a95bd3dc9426bcd379267f0fb9f Mon Sep 17 00:00:00 2001 From: Raphael Date: Wed, 22 Dec 2010 14:15:45 -0800 Subject: [PATCH] Fix some more specs, aspects controller is green in mysql --- app/models/aspect.rb | 2 + app/models/person.rb | 3 +- lib/diaspora/user/connecting.rb | 4 +- lib/diaspora/user/receiving.rb | 5 +- spec/controllers/aspects_controller_spec.rb | 3 +- spec/lib/diaspora/parser_spec.rb | 71 +++++++++++---------- 6 files changed, 48 insertions(+), 40 deletions(-) diff --git a/app/models/aspect.rb b/app/models/aspect.rb index c2e83a1ac..0b113701b 100644 --- a/app/models/aspect.rb +++ b/app/models/aspect.rb @@ -15,6 +15,8 @@ class Aspect < ActiveRecord::Base validates_length_of :name, :maximum => 20 validates_uniqueness_of :name, :scope => :user_id + attr_accessible :name + before_validation do name.strip! end diff --git a/app/models/person.rb b/app/models/person.rb index bd5cb2860..02fd76dc2 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -42,7 +42,8 @@ class Person < ActiveRecord::Base query_tokens = query.to_s.strip.split(" ") p = [] - query_tokens.each do |token| + query_tokens.each do |raw_token| + token = "%#{raw_token}%" p = Person.searchable.where('profiles.first_name LIKE :token', :token => token).limit(30) \ | Person.searchable.where('profiles.last_name LIKE :token', :token => token).limit(30) \ | Person.searchable.where('profiles.diaspora_handle LIKE :token', :token => token).limit(30) \ diff --git a/lib/diaspora/user/connecting.rb b/lib/diaspora/user/connecting.rb index 7784d452d..8fdbd6330 100644 --- a/lib/diaspora/user/connecting.rb +++ b/lib/diaspora/user/connecting.rb @@ -88,7 +88,9 @@ module Diaspora visibility_ids = visibilities.map{|v| v.id} PostVisibility.where(:id => visibility_ids).delete_all posts.each do |post| - post.decrement_user_refs + if post.user_refs == 0 + post.destroy + end end raise "Contact not deleted" unless contact.destroy end diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb index 73d8bd23a..a5b7a152d 100644 --- a/lib/diaspora/user/receiving.rb +++ b/lib/diaspora/user/receiving.rb @@ -23,10 +23,9 @@ module Diaspora def receive xml, salmon_author object = Diaspora::Parser.from_xml(xml) Rails.logger.info("event=receive status=start recipient=#{self.diaspora_handle} payload_type=#{object.class} sender=#{salmon_author.diaspora_handle}") - if object.is_a?(Request) - salmon_author.save - object.sender_handle = salmon_author.diaspora_handle + salmon_author.save! + object.sender = salmon_author end if object.is_a?(Comment) diff --git a/spec/controllers/aspects_controller_spec.rb b/spec/controllers/aspects_controller_spec.rb index 50357ba3a..5f3d90bd2 100644 --- a/spec/controllers/aspects_controller_spec.rb +++ b/spec/controllers/aspects_controller_spec.rb @@ -27,9 +27,8 @@ describe AspectsController do describe "#index" do it "assigns @contacts to all the user's contacts" do - Factory.create :person get :index - assigns[:contacts].should == @user.contacts + assigns[:contacts].map{|c| c.id}.should == @user.contacts.map{|c| c.id} end context 'performance' do before do diff --git a/spec/lib/diaspora/parser_spec.rb b/spec/lib/diaspora/parser_spec.rb index 53b5b56ba..c37f50115 100644 --- a/spec/lib/diaspora/parser_spec.rb +++ b/spec/lib/diaspora/parser_spec.rb @@ -5,83 +5,88 @@ require 'spec_helper' describe Diaspora::Parser do - let(:user) { Factory.create(:user) } - let(:aspect) { user.aspects.create(:name => 'spies') } - let(:user2) { Factory.create(:user) } - let(:aspect2) { user2.aspects.create(:name => "pandas") } - let(:person) { Factory.create(:person)} - + before do + @user = Factory.create(:user) + @aspect = @user.aspects.create(:name => 'spies') + @user2 = Factory.create(:user) + @aspect2 = @user2.aspects.create(:name => "pandas") + @person = Factory.create(:person) + end describe "parsing compliant XML object" do it 'should be able to correctly parse comment fields' do - post = user.post :status_message, :message => "hello", :to => aspect.id - comment = Factory.create(:comment, :post => post, :person => person, :diaspora_handle => person.diaspora_handle, :text => "Freedom!") + post = @user.post :status_message, :message => "hello", :to => @aspect.id + comment = Factory.create(:comment, :post => post, :person => @person, :diaspora_handle => @person.diaspora_handle, :text => "Freedom!") comment.delete xml = comment.to_diaspora_xml comment_from_xml = Diaspora::Parser.from_xml(xml) - comment_from_xml.diaspora_handle.should == person.diaspora_handle + comment_from_xml.diaspora_handle.should == @person.diaspora_handle comment_from_xml.post.should == post comment_from_xml.text.should == "Freedom!" comment_from_xml.should_not be comment end it 'should accept retractions' do - connect_users(user, aspect, user2, aspect2) - message = user2.post(:status_message, :message => "cats", :to => aspect2.id) + connect_users(@user, @aspect, @user2, @aspect2) + message = @user2.post(:status_message, :message => "cats", :to => @aspect2.id) retraction = Retraction.for(message) xml = retraction.to_diaspora_xml lambda { - user.receive xml, user2.person + @user.receive xml, @user2.person }.should change(StatusMessage, :count).by(-1) end context "connecting" do let(:good_request) { FakeHttpRequest.new(:success)} it "should create a new person upon getting a person request" do - remote_user = Factory.create(:user) - new_person = remote_user.person + new_person = @user2.person + + request = Request.new(:recipient =>@user.person, :sender => @user2.person) + xml = @user2.salmon(request).xml_for(@user.person) - request = Request.new(:recipient =>user.person, :sender => new_person) - xml = remote_user.salmon(request).xml_for(user.person) request.delete request.sender.delete - remote_user.delete + @user2.delete new_person.delete + new_person.profile.delete + new_person = new_person.dup + new_person.id = nil + new_person.owner_id = nil Person.should_receive(:by_account_identifier).twice.and_return(new_person) lambda { - user.receive_salmon xml + @user.receive_salmon xml }.should change(Person, :count).by(1) end end it "should activate the Person if I initiated a request to that url" do - user.send_contact_request_to(user2.person, aspect) - request = Request.where(:recipient_id => user2.person.id, :sender_id => user.id).first + @user.send_contact_request_to(@user2.person, @aspect) + request = @user2.request_from(@user.person) fantasy_resque do - user2.accept_and_respond(request.id, aspect2.id) + @user2.accept_and_respond(request.id, @aspect2.id) end - user.reload - aspect.reload - new_contact = user.contact_for(user2.person) - aspect.contacts.include?(new_contact).should be true - user.contacts.include?(new_contact).should be true + @user.reload + @aspect.reload + new_contact = @user.contact_for(@user2.person) + @aspect.contacts.include?(new_contact).should be true + @user.contacts.include?(new_contact).should be true end it 'should process retraction for a person' do - connect_users(user, aspect, user2, aspect2) - retraction = Retraction.for(user2) + connect_users(@user, @aspect, @user2, @aspect2) + retraction = Retraction.for(@user2) retraction_xml = retraction.to_diaspora_xml - lambda { user.receive retraction_xml, user2.person }.should change { - aspect.reload.contacts.size }.by(-1) + lambda { @user.receive retraction_xml, @user2.person }.should change { + @aspect.reload.contacts.size }.by(-1) end it 'should marshal a profile for a person' do - connect_users(user, aspect, user2, aspect2) + connect_users(@user, @aspect, @user2, @aspect2) #Create person - person = user2.person + person = @user2.person id = person.id person.profile = Profile.new(:first_name => 'bob', :last_name => 'billytown', :image_url => "http://clown.com") person.save @@ -101,7 +106,7 @@ describe Diaspora::Parser do old_profile.first_name.should == 'bob' #Marshal profile - user.receive xml, person + @user.receive xml, person #Check that marshaled profile is the same as old profile person = Person.find(person.id)