From 5d1afe21be813c851e8b4ae8289a3d2472d76d2e Mon Sep 17 00:00:00 2001 From: Sarah Mei Date: Sun, 26 Sep 2010 13:08:30 -0700 Subject: [PATCH] Refactor user specs to speed up spec runs. --- spec/factories.rb | 16 +++- spec/models/user/posting_spec.rb | 111 +++++++++++++------------ spec/models/user/visible_posts_spec.rb | 68 ++++++--------- 3 files changed, 94 insertions(+), 101 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 834304e33..aecbf74cf 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -37,10 +37,18 @@ Factory.define :user do |u| u.sequence(:email) {|n| "bob#{n}@pivotallabs.com"} u.password "bluepin7" u.password_confirmation "bluepin7" - u.person { |a| Factory.create(:person_with_user, - :owner_id => a._id, - :diaspora_handle => "#{a.username}@#{APP_CONFIG[:pod_url].gsub(/(https?:|www\.)\/\//, '').chop!}") - } + u.after_build do |user| + user.person = Factory(:person_with_private_key, :owner_id => user._id, + :diaspora_handle => "#{user.username}@#{APP_CONFIG[:pod_url].gsub(/(https?:|www\.)\/\//, '').chop!}") + end +end + +Factory.define :user_with_aspect, :parent => :user do |u| + u.after_build { |user| user.aspects << Factory(:aspect) } +end + +Factory.define :aspect do |aspect| + aspect.name "generic" end Factory.define :status_message do |m| diff --git a/spec/models/user/posting_spec.rb b/spec/models/user/posting_spec.rb index 2f86ac549..76f4243f7 100644 --- a/spec/models/user/posting_spec.rb +++ b/spec/models/user/posting_spec.rb @@ -6,77 +6,78 @@ require 'spec_helper' describe User do - let(:user) { Factory(:user) } - let(:user2) { Factory(:user) } - let(:user3) { Factory(:user) } - let(:user4) { Factory(:user) } + let!(:user) { Factory(:user) } + let!(:aspect) { user.aspect(:name => 'heroes') } + let!(:aspect1) { user.aspect(:name => 'other') } - let(:aspect) {user.aspect(:name => 'heroes')} - let!(:aspect1) {user.aspect(:name => 'heroes')} - let!(:aspect2) {user2.aspect(:name => 'losers')} - let!(:aspect3) {user3.aspect(:name => 'heroes')} - let!(:aspect4) {user4.aspect(:name => 'heroes')} + let!(:user2) { Factory(:user) } + let!(:aspect2) { user2.aspect(:name => 'losers') } - before do - friend_users(user, aspect, user2, aspect2) - friend_users(user, aspect, user3, aspect3) - friend_users(user, aspect1, user4, aspect4) + describe '#validate_aspect_permissions' do + it 'requires an aspect' do + proc { + user.validate_aspect_permissions([]) + }.should raise_error /You must post to someone/ + end + + it "won't let you post to someone else's aspect" do + proc { + user.validate_aspect_permissions(aspect2.id) + }.should raise_error /Cannot post to an aspect you do not own./ + end + + it "lets you post to your own aspects" do + user.validate_aspect_permissions(aspect.id).should be_true + user.validate_aspect_permissions(aspect1.id).should be_true + end end - context 'posting' do - - describe '#validate_aspect_permissions' do - it 'should not be able to post without a aspect' do - proc { - user.validate_aspect_permissions([]) - }.should raise_error /You must post to someone/ - end - - it 'should not be able to post to someone elses aspect' do - proc { - user.validate_aspect_permissions(aspect2.id) - }.should raise_error /Cannot post to an aspect you do not own./ - end + describe '#post' do + it 'should put the post in the aspect post array' do + post = user.post(:status_message, :message => "hey", :to => aspect.id) + aspect.reload + aspect.posts.should include post end - describe '#post' do - it 'should put the post in the aspect post array' do - post = user.post(:status_message, :message => "hey", :to => aspect.id) - aspect.reload - aspect.posts.should include post - end - - it 'should put an album in the aspect post array' do - album = user.post :album, :name => "Georges", :to => aspect.id - aspect.reload - aspect.posts.should include album - end + it 'should put an album in the aspect post array' do + album = user.post :album, :name => "Georges", :to => aspect.id + aspect.reload + aspect.posts.should include album end + end - describe '#repost' do - let!(:status_message) { user.post(:status_message, :message => "hello", :to => aspect.id) } - - it 'should make the post visible in another aspect' do - user.repost( status_message, :to => aspect1.id ) - aspect1.reload - aspect1.posts.count.should be 1 - end + describe '#repost' do + it 'should make the post visible in another aspect' do + status_message = user.post(:status_message, :message => "hello", :to => aspect.id) + user.repost(status_message, :to => aspect1.id) + aspect1.reload + aspect1.posts.count.should be 1 end + end - describe '#update_post' do - let!(:album) { user.post(:album, :name => "Profile Photos", :to => aspect.id) } - - it 'should update fields' do - update_hash = { :name => "Other Photos" } - user.update_post( album, update_hash ) - album.name.should == "Other Photos" - end + describe '#update_post' do + it 'should update fields' do + album = user.post(:album, :name => "Profile Photos", :to => aspect.id) + update_hash = {:name => "Other Photos"} + user.update_post(album, update_hash) + album.name.should == "Other Photos" end end context 'dispatching' do + let!(:user3) { Factory(:user) } + let!(:aspect3) { user3.aspect(:name => 'heroes') } + let!(:user4) { Factory(:user) } + let!(:aspect4) { user4.aspect(:name => 'heroes') } + let!(:post) { user.build_post :status_message, :message => "hey" } + before do + friend_users(user, aspect, user2, aspect2) + friend_users(user, aspect, user3, aspect3) + friend_users(user, aspect1, user4, aspect4) + end + describe '#push_to_aspects' do it 'should push a post to a aspect' do user.should_receive(:salmon).twice diff --git a/spec/models/user/visible_posts_spec.rb b/spec/models/user/visible_posts_spec.rb index 8d20bcb96..e0a3d5215 100644 --- a/spec/models/user/visible_posts_spec.rb +++ b/spec/models/user/visible_posts_spec.rb @@ -5,41 +5,35 @@ require 'spec_helper' describe User do - let(:user) { Factory(:user) } + let!(:user) { Factory(:user_with_aspect) } + let!(:first_aspect) { user.aspects.first } + let!(:second_aspect) { user.aspect(:name => 'losers') } - let(:user2) { Factory(:user) } - let(:user3) { Factory(:user) } - let(:user4) { Factory(:user) } + let!(:user2) { Factory(:user_with_aspect) } - let!(:aspect) { user.aspect(:name => 'heroes') } - let!(:aspect2) { user.aspect(:name => 'losers') } - - let!(:user2_aspect) { user2.aspect(:name => 'dudes') } - let!(:user3_aspect) { user3.aspect(:name => 'dudes') } - let!(:user4_aspect) { user4.aspect(:name => 'dudes') } - - let(:status_message1) { user2.post :status_message, :message => "hi", :to => user2_aspect.id } - let(:status_message2) { user3.post :status_message, :message => "heyyyy", :to => user3_aspect.id } - let(:status_message3) { user4.post :status_message, :message => "yooo", :to => user4_aspect.id } + let!(:status_message1) { user2.post :status_message, :message => "hi", :to => user2.aspects.first.id } before do - friend_users(user, aspect, user2, user2_aspect) - friend_users(user, aspect2, user3, user3_aspect) - friend_users(user, aspect2, user4, user4_aspect) + friend_users(user, first_aspect, user2, user2.aspects.first) end - it 'should generate a valid stream for a aspect of people' do - (1..3).each{ |n| - eval("user.receive status_message#{n}.to_diaspora_xml") - } + describe "#visible_posts" do + it "generates a stream for each aspect that includes only that aspect's posts" do + user3 = Factory(:user_with_aspect) + status_message2 = user3.post :status_message, :message => "heyyyy", :to => user3.aspects.first.id + user4 = Factory(:user_with_aspect) + status_message3 = user4.post :status_message, :message => "yooo", :to => user4.aspects.first.id - user.visible_posts(:by_members_of => aspect).should include status_message1 - user.visible_posts(:by_members_of => aspect).should_not include status_message2 - user.visible_posts(:by_members_of => aspect).should_not include status_message3 + friend_users(user, second_aspect, user3, user3.aspects.first) + friend_users(user, second_aspect, user4, user4.aspects.first) - user.visible_posts(:by_members_of => aspect2).should_not include status_message1 - user.visible_posts(:by_members_of => aspect2).should include status_message2 - user.visible_posts(:by_members_of => aspect2).should include status_message3 + user.receive status_message1.to_diaspora_xml + user.receive status_message2.to_diaspora_xml + user.receive status_message3.to_diaspora_xml + + user.visible_posts(:by_members_of => first_aspect).should =~ [status_message1] + user.visible_posts(:by_members_of => second_aspect).should =~ [status_message2, status_message3] + end end context 'querying' do @@ -54,19 +48,9 @@ describe User do context 'albums' do before do - @album = user.post :album, :name => "Georges", :to => aspect.id - aspect.reload - aspect2.reload - user.reload - - @album2 = user.post :album, :name => "Borges", :to => aspect.id - aspect.reload - aspect2.reload - user.reload - - user.post :album, :name => "Luises", :to => aspect2.id - aspect.reload - aspect2.reload + user.post :album, :name => "Georges", :to => first_aspect.id + user.post :album, :name => "Borges", :to => first_aspect.id + user.post :album, :name => "Luises", :to => second_aspect.id user.reload end @@ -75,8 +59,8 @@ describe User do end it 'should return the right number of albums' do - user.albums_by_aspect(aspect).should have(2).albums - user.albums_by_aspect(aspect2).should have(1).album + user.albums_by_aspect(first_aspect.reload).should have(2).albums + user.albums_by_aspect(second_aspect.reload).should have(1).album end end end