diff --git a/Changelog.md b/Changelog.md index 42a0b6e54..77d1513c9 100644 --- a/Changelog.md +++ b/Changelog.md @@ -58,6 +58,7 @@ With the port to Bootstrap 3, app/views/terms/default.haml has a new structure. * Update to Rails 4.2.3 [#6140](https://github.com/diaspora/diaspora/pull/6140) * Refactor person related URL generation [#6168](https://github.com/diaspora/diaspora/pull/6168) * Move webfinger and HCard generation out of the core and embed the `diaspora_federation-rails` gem [#6151](https://github.com/diaspora/diaspora/pull/6151/) +* Refactor rspec tests to to use `let` instead of before blocks [#6199](https://github.com/diaspora/diaspora/pull/6199) ## Bug fixes * Precompile facebox images [#6105](https://github.com/diaspora/diaspora/pull/6105) diff --git a/spec/models/account_deletion_spec.rb b/spec/models/account_deletion_spec.rb index 576d58c57..0126edb77 100644 --- a/spec/models/account_deletion_spec.rb +++ b/spec/models/account_deletion_spec.rb @@ -2,91 +2,76 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -require 'spec_helper' +require "spec_helper" describe AccountDeletion, :type => :model do - it 'assigns the diaspora_handle from the person object' do - a = AccountDeletion.new(:person => alice.person) - expect(a.diaspora_handle).to eq(alice.person.diaspora_handle) + let(:account_deletion_new) { AccountDeletion.new(person: alice.person) } + let(:account_deletion_create) { AccountDeletion.create(person: alice.person) } + + it "assigns the diaspora_handle from the person object" do + expect(account_deletion_new.diaspora_handle).to eq(alice.person.diaspora_handle) end - it 'fires a job after creation'do + it "fires a job after creation"do expect(Workers::DeleteAccount).to receive(:perform_async).with(anything) - - AccountDeletion.create(:person => alice.person) + account_deletion_create end describe "#perform!" do - before do - @ad = AccountDeletion.new(:person => alice.person) + it "creates a deleter" do + expect(AccountDeleter).to receive(:new).with(alice.person.diaspora_handle).and_return(double(perform!: true)) + account_deletion_new.perform! end - it 'creates a deleter' do - expect(AccountDeleter).to receive(:new).with(alice.person.diaspora_handle).and_return(double(:perform! => true)) - @ad.perform! - end - - it 'dispatches the account deletion if the user exists' do - expect(@ad).to receive(:dispatch) - @ad.perform! + it "dispatches the account deletion if the user exists" do + expect(account_deletion_new).to receive(:dispatch) + account_deletion_new.perform! end - it 'does not dispatch an account deletion for non-local people' do - deletion = AccountDeletion.new(:person => remote_raphael) + it "does not dispatch an account deletion for non-local people" do + deletion = AccountDeletion.new(person: remote_raphael) expect(deletion).not_to receive(:dispatch) deletion.perform! end - it 'marks an AccountDeletion as completed when successful' do - ad = AccountDeletion.create(:person => alice.person) - ad.perform! - expect(ad.reload.completed_at).not_to be_nil + it "marks an AccountDeletion as completed when successful" do + account_deletion_create.perform! + expect(account_deletion_create.reload.completed_at).not_to be_nil end end - describe '#dispatch' do - it "sends the account deletion xml" do - @ad = AccountDeletion.new(:person => alice.person) - @ad.send(:dispatch) - end - - it 'creates a public postzord' do + describe "#dispatch" do + it "creates a public postzord" do expect(Postzord::Dispatcher::Public).to receive(:new).and_return(double.as_null_object) - @ad = AccountDeletion.new(:person => alice.person) - @ad.send(:dispatch) + account_deletion_new.dispatch end end describe "#subscribers" do - it 'includes all remote contacts' do - @ad = AccountDeletion.new(:person => alice.person) + it "includes all remote contacts" do alice.share_with(remote_raphael, alice.aspects.first) - expect(@ad.subscribers(alice)).to eq([remote_raphael]) + expect(account_deletion_new.subscribers(alice)).to eq([remote_raphael]) end - it 'includes remote resharers' do - @ad = AccountDeletion.new(:person => alice.person) - sm = FactoryGirl.create( :status_message, :public => true, :author => alice.person) - r1 = FactoryGirl.create( :reshare, :author => remote_raphael, :root => sm) - r2 = FactoryGirl.create( :reshare, :author => local_luke.person, :root => sm) + it "includes remote resharers" do + status_message = FactoryGirl.create(:status_message, public: true, author: alice.person) + FactoryGirl.create(:reshare, author: remote_raphael, root: status_message) + FactoryGirl.create(:reshare, author: local_luke.person, root: status_message) - expect(@ad.subscribers(alice)).to eq([remote_raphael]) + expect(account_deletion_new.subscribers(alice)).to eq([remote_raphael]) end end - describe 'serialization' do - before do - account_deletion = AccountDeletion.new(:person => alice.person) - @xml = account_deletion.to_xml.to_s + describe "serialization" do + let(:xml) { account_deletion_new.to_xml.to_s } + + it "should have a diaspora_handle" do + expect(xml.include?(alice.person.diaspora_handle)).to eq(true) end - it 'should have a diaspora_handle' do - expect(@xml.include?(alice.person.diaspora_handle)).to eq(true) - end - - it 'marshals the xml' do - expect(AccountDeletion.from_xml(@xml)).to be_valid + it "marshals the xml" do + expect(AccountDeletion.from_xml(xml)).to be_valid end end end diff --git a/spec/models/acts_as_taggable_on_tag_spec.rb b/spec/models/acts_as_taggable_on_tag_spec.rb index 159c7c035..84f1fd982 100644 --- a/spec/models/acts_as_taggable_on_tag_spec.rb +++ b/spec/models/acts_as_taggable_on_tag_spec.rb @@ -1,45 +1,45 @@ -require 'spec_helper' +require "spec_helper" describe ActsAsTaggableOn::Tag, :type => :model do - describe '.autocomplete' do - before do - @tag = ActsAsTaggableOn::Tag.create(:name => "cats") - end - it 'downcases the tag name' do - expect(ActsAsTaggableOn::Tag.autocomplete("CATS")).to eq([@tag]) + subject(:tag) { ActsAsTaggableOn::Tag } + describe ".autocomplete" do + let!(:tag_cats) { tag.create(name: "cats") } + + it "downcases the tag name" do + expect(tag.autocomplete("CATS")).to eq([tag_cats]) end - it 'does an end where on tags' do - expect(ActsAsTaggableOn::Tag.autocomplete("CAT")).to eq([@tag]) + it "does an end where on tags" do + expect(tag.autocomplete("CAT")).to eq([tag_cats]) end end describe ".normalize" do it "removes leading hash symbols" do - expect(ActsAsTaggableOn::Tag.normalize("#mytag")).to eq("mytag") + expect(tag.normalize("#mytag")).to eq("mytag") end it "removes punctuation and whitespace" do { - 'node.js' => 'nodejs', - '.dotatstart' => 'dotatstart', - 'you,inside' => 'youinside', - 'iam(parenthetical)' => 'iamparenthetical', - 'imeanit?maybe' => 'imeanitmaybe', - 'imeanit!' => 'imeanit', - 'how about spaces' => 'howaboutspaces', - "other\twhitespace\n" => 'otherwhitespace', - 'hash#inside' => 'hashinside', - 'f!u@n#k$y%-^h&a*r(a)c{t}e[r]s' => 'funky-characters' + "node.js" => "nodejs", + ".dotatstart" => "dotatstart", + "you,inside" => "youinside", + "iam(parenthetical)" => "iamparenthetical", + "imeanit?maybe" => "imeanitmaybe", + "imeanit!" => "imeanit", + "how about spaces" => "howaboutspaces", + "other\twhitespace\n" => "otherwhitespace", + "hash#inside" => "hashinside", + "f!u@n#k$y%-^h&a*r(a)c{t}e[r]s" => "funky-characters" }.each do |invalid, normalized| - expect(ActsAsTaggableOn::Tag.normalize(invalid)).to eq(normalized) + expect(tag.normalize(invalid)).to eq(normalized) end end - it 'allows for love' do - expect(ActsAsTaggableOn::Tag.normalize("<3")).to eq("<3") - expect(ActsAsTaggableOn::Tag.normalize("#<3")).to eq("<3") + it "allows for love" do + expect(tag.normalize("<3")).to eq("<3") + expect(tag.normalize("#<3")).to eq("<3") end end end diff --git a/spec/models/aspect_membership_spec.rb b/spec/models/aspect_membership_spec.rb index 12b01fe24..f57950794 100644 --- a/spec/models/aspect_membership_spec.rb +++ b/spec/models/aspect_membership_spec.rb @@ -2,31 +2,29 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. # -require 'spec_helper' +require "spec_helper" describe AspectMembership, :type => :model do + describe "#before_destroy" do + let(:aspect) { alice.aspects.create(name: "two") } + let(:contact) { alice.contact_for(bob.person) } + let(:aspect_membership) { alice.aspects.where(name: "generic").first.aspect_memberships.first } - describe '#before_destroy' do before do - @aspect = alice.aspects.create(:name => "two") - @contact = alice.contact_for(bob.person) - - @am = alice.aspects.where(:name => "generic").first.aspect_memberships.first - allow(@am).to receive(:user).and_return(alice) + allow(aspect_membership).to receive(:user).and_return(alice) end - it 'calls disconnect if its the last aspect for the contact' do - expect(alice).to receive(:disconnect).with(@contact) + it "calls disconnect if its the last aspect for the contact" do + expect(alice).to receive(:disconnect).with(contact) - @am.destroy + aspect_membership.destroy end - it 'does not call disconnect if its not the last aspect for the contact' do + it "does not call disconnect if its not the last aspect for the contact" do expect(alice).not_to receive(:disconnect) - alice.add_contact_to_aspect(@contact, @aspect) - @am.destroy + alice.add_contact_to_aspect(contact, aspect) + aspect_membership.destroy end end - end diff --git a/spec/models/aspect_spec.rb b/spec/models/aspect_spec.rb index 4d85a2b1f..3ccae9515 100644 --- a/spec/models/aspect_spec.rb +++ b/spec/models/aspect_spec.rb @@ -2,41 +2,35 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -require 'spec_helper' +require "spec_helper" describe Aspect, :type => :model do - describe 'creation' do - before do - @name = alice.aspects.first.name + describe "creation" do + let(:name) { alice.aspects.first.name } + + it "does not allow duplicate names" do + expect { alice.aspects.create(name: name) }.not_to change(Aspect, :count) end - it 'does not allow duplicate names' do - expect { - invalid_aspect = alice.aspects.create(:name => @name) - }.not_to change(Aspect, :count) + it "validates case insensitiveness on names" do + expect { alice.aspects.create(name: name.titleize) }.not_to change(Aspect, :count) end - it 'validates case insensitiveness on names' do - expect { - invalid_aspect = alice.aspects.create(:name => @name.titleize) - }.not_to change(Aspect, :count) - end - - it 'has a 20 character limit on names' do - aspect = Aspect.new(:name => "this name is really too too too too too long") + it "has a 20 character limit on names" do + aspect = Aspect.new(name: "this name is really too too too too too long") expect(aspect.valid?).to eq(false) end - it 'is able to have other users as contacts' do - aspect = alice.aspects.create(:name => 'losers') + it "is able to have other users as contacts" do + aspect = alice.aspects.create(name: "losers") + Contact.create(user: alice, person: eve.person, aspects: [aspect]) - Contact.create(:user => alice, :person => eve.person, :aspects => [aspect]) - expect(aspect.contacts.where(:person_id => alice.person.id)).to be_empty - expect(aspect.contacts.where(:person_id => eve.person.id)).not_to be_empty + expect(aspect.contacts.where(person_id: alice.person.id)).to be_empty + expect(aspect.contacts.where(person_id: eve.person.id)).not_to be_empty expect(aspect.contacts.size).to eq(1) end - it 'has a contacts_visible? method' do + it "has a contacts_visible? method" do expect(alice.aspects.first.contacts_visible?).to be true end @@ -48,10 +42,10 @@ describe Aspect, :type => :model do end end - describe 'validation' do - it 'has no uniqueness of name between users' do - aspect = alice.aspects.create(:name => "New Aspect") - aspect2 = eve.aspects.create(:name => aspect.name) + describe "validation" do + it "has no uniqueness of name between users" do + aspect = alice.aspects.create(name: "New Aspect") + aspect2 = eve.aspects.create(name: aspect.name) expect(aspect2).to be_valid end end diff --git a/spec/models/block_spec.rb b/spec/models/block_spec.rb index e8b3683a6..54f7317fd 100644 --- a/spec/models/block_spec.rb +++ b/spec/models/block_spec.rb @@ -1,10 +1,10 @@ -require 'spec_helper' +require "spec_helper" describe Block, :type => :model do - describe 'validations' do - it 'doesnt allow you to block yourself' do - block = alice.blocks.create(:person => alice.person) + describe "validations" do + it "doesnt allow you to block yourself" do + block = alice.blocks.create(person: alice.person) expect(block.errors[:person_id].size).to eq(1) end end -end \ No newline at end of file +end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 33dc00e6f..9d62b3046 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -2,161 +2,162 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -require 'spec_helper' +require "spec_helper" require Rails.root.join("spec", "shared_behaviors", "relayable") describe Comment, :type => :model do - before do - @alices_aspect = alice.aspects.first - @status = bob.post(:status_message, :text => "hello", :to => bob.aspects.first.id) - end + let(:alices_aspect) { alice.aspects.first } + let(:status_bob) { bob.post(:status_message, text: "hello", to: bob.aspects.first.id) } + let(:comment_alice) { alice.comment!(status_bob, "why so formal?") } describe "#destroy" do - before do - @comment = alice.comment!(@status, "why so formal?") - end - it "should delete a participation" do - expect { @comment.destroy }.to change { Participation.count }.by(-1) + comment_alice + expect { comment_alice.destroy }.to change { Participation.count }.by(-1) end it "should decrease count participation" do - alice.comment!(@status, "Are you there?") - @comment.destroy - participations = Participation.where(target_id: @comment.commentable_id, author_id: @comment.author_id) + alice.comment!(status_bob, "Are you there?") + comment_alice.destroy + participations = Participation.where(target_id: comment_alice.commentable_id, author_id: comment_alice.author_id) expect(participations.first.count).to eq(1) end end - describe 'comment#notification_type' do - let (:comment) { alice.comment!(@status, "why so formal?") } - + describe "comment#notification_type" do it "returns 'comment_on_post' if the comment is on a post you own" do - expect(comment.notification_type(bob, alice.person)).to eq(Notifications::CommentOnPost) + expect(comment_alice.notification_type(bob, alice.person)).to eq(Notifications::CommentOnPost) end it "returns 'also_commented' if the comment is on a post you participate to" do - eve.participate! @status - expect(comment.notification_type(eve, alice.person)).to eq(Notifications::AlsoCommented) + eve.participate! status_bob + expect(comment_alice.notification_type(eve, alice.person)).to eq(Notifications::AlsoCommented) end - it 'returns false if the comment is not on a post you own and no one "also_commented"' do - comment = alice.comment!(@status, "I simply felt like issuing a greeting. Do step off.") - expect(comment.notification_type(eve, alice.person)).to be false + it "returns false if the comment is not on a post you own and no one 'also_commented'" do + expect(comment_alice.notification_type(eve, alice.person)).to be false end context "also commented" do + let(:comment_eve) { eve.comment!(status_bob, "I also commented on the first user's post") } + before do - alice.comment!(@status, "a-commenta commenta") - @comment = eve.comment!(@status, "I also commented on the first user's post") + comment_alice end - it 'does not return also commented if the user commented' do - expect(@comment.notification_type(eve, alice.person)).to eq(false) + it "does not return also commented if the user commented" do + expect(comment_eve.notification_type(eve, alice.person)).to eq(false) end it "returns 'also_commented' if another person commented on a post you commented on" do - expect(@comment.notification_type(alice, alice.person)).to eq(Notifications::AlsoCommented) + expect(comment_eve.notification_type(alice, alice.person)).to eq(Notifications::AlsoCommented) end end end - describe 'User#comment' do + describe "User#comment" do it "should be able to comment on one's own status" do - alice.comment!(@status, "Yeah, it was great") - expect(@status.reload.comments.first.text).to eq("Yeah, it was great") + bob.comment!(status_bob, "sup dog") + expect(status_bob.reload.comments.first.text).to eq("sup dog") end it "should be able to comment on a contact's status" do - bob.comment!(@status, "sup dog") - expect(@status.reload.comments.first.text).to eq("sup dog") + comment_alice + expect(status_bob.reload.comments.first.text).to eq("why so formal?") end - it 'does not multi-post a comment' do + it "does not multi-post a comment" do expect { - alice.comment!(@status, 'hello') + comment_alice }.to change { Comment.count }.by(1) end it "should create a participation" do - comment = bob.comment!(@status, "sup dog") - participations = Participation.where(target_id: comment.commentable_id, author_id: comment.author_id) + comment_alice + participations = Participation.where(target_id: comment_alice.commentable_id, author_id: comment_alice.author_id) expect(participations.count).to eq(1) end end - describe 'counter cache' do - it 'increments the counter cache on its post' do + describe "counter cache" do + it "increments the counter cache on its post" do expect { - alice.comment!(@status, "oh yeah") + comment_alice }.to change{ - @status.reload.comments_count + status_bob.reload.comments_count }.by(1) end end - describe 'xml' do + describe "xml" do + let(:commenter) { create(:user) } + let(:commenter_aspect) { commenter.aspects.create(name: "bruisers") } + let(:post) { alice.post :status_message, text: "hello", to: alices_aspect.id } + let(:comment) { commenter.comment!(post, "Fool!") } + let(:xml) { comment.to_xml.to_s } + before do - @commenter = FactoryGirl.create(:user) - @commenter_aspect = @commenter.aspects.create(:name => "bruisers") - connect_users(alice, @alices_aspect, @commenter, @commenter_aspect) - @post = alice.post :status_message, :text => "hello", :to => @alices_aspect.id - @comment = @commenter.comment!(@post, "Fool!") - @xml = @comment.to_xml.to_s + connect_users(alice, alices_aspect, commenter, commenter_aspect) end - it 'serializes the sender handle' do - expect(@xml.include?(@commenter.diaspora_handle)).to be true + it "serializes the sender handle" do + expect(xml.include?(commenter.diaspora_handle)).to be true end - it 'serializes the post_guid' do - expect(@xml).to include(@post.guid) + it "serializes the post_guid" do + expect(xml).to include(post.guid) end - describe 'marshalling' do - before do - @marshalled_comment = Comment.from_xml(@xml) + describe "marshalling" do + let(:marshalled_comment) { Comment.from_xml(xml) } + + it "marshals the author" do + expect(marshalled_comment.author).to eq(commenter.person) end - it 'marshals the author' do - expect(@marshalled_comment.author).to eq(@commenter.person) + it "marshals the post" do + expect(marshalled_comment.post).to eq(post) end - it 'marshals the post' do - expect(@marshalled_comment.post).to eq(@post) - end - - it 'tries to fetch a missing parent' do - guid = @post.guid - @post.destroy + it "tries to fetch a missing parent" do + guid = post.guid + marshalled_comment + post.destroy expect_any_instance_of(Comment).to receive(:fetch_parent).with(guid).and_return(nil) - Comment.from_xml(@xml) + Comment.from_xml(xml) end end end - describe 'it is relayable' do + describe "it is relayable" do + let(:remote_parent) { build(:status_message, author: remote_raphael) } + let(:local_parent) { local_luke.post :status_message, text: "hi", to: local_luke.aspects.first } + let(:object_by_parent_author) { local_luke.comment!(local_parent, "yo!") } + let(:object_by_recipient) { local_leia.build_comment(text: "yo", post: local_parent) } + let(:dup_object_by_parent_author) { object_by_parent_author.dup } + let(:object_on_remote_parent) { local_luke.comment!(remote_parent, "Yeah, it was great") } + before do - @local_luke, @local_leia, @remote_raphael = set_up_friends - @remote_parent = FactoryGirl.build(:status_message, :author => @remote_raphael) - @local_parent = @local_luke.post :status_message, :text => "hi", :to => @local_luke.aspects.first - - @object_by_parent_author = @local_luke.comment!(@local_parent, "yo") - @object_by_recipient = @local_leia.build_comment(:text => "yo", :post => @local_parent) - @dup_object_by_parent_author = @object_by_parent_author.dup - - @object_on_remote_parent = @local_luke.comment!(@remote_parent, "Yeah, it was great") + # shared_behaviors/relayable.rb is still using instance variables, so we need to define them here. + # Suggestion: refactor all specs using shared_behaviors/relayable.rb to use "let" + @object_by_parent_author = object_by_parent_author + @object_by_recipient = object_by_recipient + @dup_object_by_parent_author = dup_object_by_parent_author + @object_on_remote_parent = object_on_remote_parent end - let(:build_object) { alice.build_comment(:post => @status, :text => "why so formal?") } - it_should_behave_like 'it is relayable' + let(:build_object) { alice.build_comment(post: status_bob, text: "why so formal?") } + it_should_behave_like "it is relayable" end - describe 'tags' do + describe "tags" do + let(:object) { build(:comment) } + before do - @object = FactoryGirl.build(:comment) + # shared_behaviors/taggable.rb is still using instance variables, so we need to define them here. + # Suggestion: refactor all specs using shared_behaviors/taggable.rb to use "let" + @object = object end - it_should_behave_like 'it is taggable' + it_should_behave_like "it is taggable" end - end diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 9f8370952..b5efa37e1 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -2,31 +2,31 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -require 'spec_helper' +require "spec_helper" describe Contact, :type => :model do - describe 'aspect_memberships' do - it 'deletes dependent aspect memberships' do - expect{ + describe "aspect_memberships" do + it "deletes dependent aspect memberships" do + expect { alice.contact_for(bob.person).destroy }.to change(AspectMembership, :count).by(-1) end end - context 'validations' do - let(:contact){Contact.new} + context "validations" do + let(:contact) { Contact.new } - it 'requires a user' do + it "requires a user" do contact.valid? expect(contact.errors.full_messages).to include "User can't be blank" end - it 'requires a person' do + it "requires a person" do contact.valid? expect(contact.errors.full_messages).to include "Person can't be blank" end - it 'ensures user is not making a contact for himself' do + it "ensures user is not making a contact for himself" do contact.person = alice.person contact.user = alice @@ -34,10 +34,10 @@ describe Contact, :type => :model do expect(contact.errors.full_messages).to include "Cannot create self-contact" end - it 'validates uniqueness' do + it "validates uniqueness" do person = FactoryGirl.create(:person) - contact2 = alice.contacts.create(:person=>person) + contact2 = alice.contacts.create(person: person) expect(contact2).to be_valid contact.user = alice @@ -47,54 +47,53 @@ describe Contact, :type => :model do it "validates that the person's account is not closed" do person = FactoryGirl.create(:person, :closed_account => true) - - contact = alice.contacts.new(:person=>person) + contact = alice.contacts.new(person: person) expect(contact).not_to be_valid expect(contact.errors.full_messages).to include "Cannot be in contact with a closed account" end end - context 'scope' do - describe 'sharing' do - it 'returns contacts with sharing true' do + context "scope" do + describe "sharing" do + it "returns contacts with sharing true" do expect { - alice.contacts.create!(:sharing => true, :person => FactoryGirl.create(:person)) - alice.contacts.create!(:sharing => false, :person => FactoryGirl.create(:person)) + alice.contacts.create!(sharing: true, person: FactoryGirl.create(:person)) + alice.contacts.create!(sharing: false, person: FactoryGirl.create(:person)) }.to change{ Contact.sharing.count }.by(1) end end - describe 'receiving' do - it 'returns contacts with sharing true' do + describe "receiving" do + it "returns contacts with sharing true" do expect { - alice.contacts.create!(:receiving => true, :person => FactoryGirl.build(:person)) - alice.contacts.create!(:receiving => false, :person => FactoryGirl.build(:person)) + alice.contacts.create!(receiving: true, person: FactoryGirl.build(:person)) + alice.contacts.create!(receiving: false, person: FactoryGirl.build(:person)) }.to change{ Contact.receiving.count }.by(1) end end - describe 'only_sharing' do - it 'returns contacts with sharing true and receiving false' do + describe "only_sharing" do + it "returns contacts with sharing true and receiving false" do expect { - alice.contacts.create!(:receiving => true, :sharing => true, :person => FactoryGirl.build(:person)) - alice.contacts.create!(:receiving => false, :sharing => true, :person => FactoryGirl.build(:person)) - alice.contacts.create!(:receiving => false, :sharing => true, :person => FactoryGirl.build(:person)) - alice.contacts.create!(:receiving => true, :sharing => false, :person => FactoryGirl.build(:person)) + alice.contacts.create!(receiving: true, sharing: true, person: FactoryGirl.build(:person)) + alice.contacts.create!(receiving: false, sharing: true, person: FactoryGirl.build(:person)) + alice.contacts.create!(receiving: false, sharing: true, person: FactoryGirl.build(:person)) + alice.contacts.create!(receiving: true, sharing: false, person: FactoryGirl.build(:person)) }.to change{ Contact.receiving.count }.by(2) end end - + describe "all_contacts_of_person" do - it 'returns all contacts where the person is the passed in person' do + it "returns all contacts where the person is the passed in person" do person = FactoryGirl.create(:person) - contact1 = FactoryGirl.create(:contact, :person => person) + contact1 = FactoryGirl.create(:contact, person: person) contact2 = FactoryGirl.create(:contact) contacts = Contact.all_contacts_of_person(person) expect(contacts).to eq([contact1]) @@ -102,118 +101,107 @@ describe Contact, :type => :model do end end - describe '#contacts' do + describe "#contacts" do before do - @alice = alice - @bob = bob - @eve = eve - @bob.aspects.create(:name => 'next') - @bob.aspects(true) + bob.aspects.create(name: "next") + bob.aspects(true) - @original_aspect = @bob.aspects.where(:name => "generic").first - @new_aspect = @bob.aspects.where(:name => "next").first + @original_aspect = bob.aspects.where(name: "generic").first + @new_aspect = bob.aspects.where(name: "next").first @people1 = [] @people2 = [] 1.upto(5) do person = FactoryGirl.build(:person) - @bob.contacts.create(:person => person, :aspects => [@original_aspect]) + bob.contacts.create(person: person, aspects: [@original_aspect]) @people1 << person end 1.upto(5) do person = FactoryGirl.build(:person) - @bob.contacts.create(:person => person, :aspects => [@new_aspect]) + bob.contacts.create(person: person, aspects: [@new_aspect]) @people2 << person end #eve <-> bob <-> alice end - context 'on a contact for a local user' do + context "on a contact for a local user" do before do - @alice.reload - @alice.aspects.reload - @contact = @alice.contact_for(@bob.person) + alice.reload + alice.aspects.reload + @contact = alice.contact_for(bob.person) end it "returns the target local user's contacts that are in the same aspect" do - expect(@contact.contacts.map{|p| p.id}).to match_array([@eve.person].concat(@people1).map{|p| p.id}) + expect(@contact.contacts.map(&:id)).to match_array([eve.person].concat(@people1).map(&:id)) end - it 'returns nothing if contacts_visible is false in that aspect' do + it "returns nothing if contacts_visible is false in that aspect" do @original_aspect.contacts_visible = false @original_aspect.save expect(@contact.contacts).to eq([]) end - it 'returns no duplicate contacts' do - [@alice, @eve].each {|c| @bob.add_contact_to_aspect(@bob.contact_for(c.person), @bob.aspects.last)} - contact_ids = @contact.contacts.map{|p| p.id} + it "returns no duplicate contacts" do + [alice, eve].each {|c| bob.add_contact_to_aspect(bob.contact_for(c.person), bob.aspects.last) } + contact_ids = @contact.contacts.map(&:id) expect(contact_ids.uniq).to eq(contact_ids) end end - context 'on a contact for a remote user' do - before do - @contact = @bob.contact_for @people1.first - end - it 'returns an empty array' do - expect(@contact.contacts).to eq([]) + context "on a contact for a remote user" do + let(:contact) { bob.contact_for @people1.first } + + it "returns an empty array" do + expect(contact.contacts).to eq([]) end end end - context 'requesting' do - before do - @contact = Contact.new - @user = FactoryGirl.build(:user) - @person = FactoryGirl.build(:person) + context "requesting" do + let(:contact) { Contact.new user: user, person: person } + let(:user) { build(:user) } + let(:person) { build(:person) } - @contact.user = @user - @contact.person = @person - end + describe "#generate_request" do + it "makes a request" do + allow(contact).to receive(:user).and_return(user) + request = contact.generate_request - describe '#generate_request' do - it 'makes a request' do - allow(@contact).to receive(:user).and_return(@user) - request = @contact.generate_request - - expect(request.sender).to eq(@user.person) - expect(request.recipient).to eq(@person) + expect(request.sender).to eq(user.person) + expect(request.recipient).to eq(person) end end - describe '#dispatch_request' do - it 'pushes to people' do - allow(@contact).to receive(:user).and_return(@user) + describe "#dispatch_request" do + it "pushes to people" do + allow(contact).to receive(:user).and_return(user) m = double() expect(m).to receive(:post) expect(Postzord::Dispatcher).to receive(:build).and_return(m) - @contact.dispatch_request + contact.dispatch_request end end end describe "#not_blocked_user" do - before do - @contact = alice.contact_for(bob.person) - end + let(:contact) { alice.contact_for(bob.person) } it "is called on validate" do - expect(@contact).to receive(:not_blocked_user) - @contact.valid? + expect(contact).to receive(:not_blocked_user) + contact.valid? end it "adds to errors if potential contact is blocked by user" do person = eve.person - block = alice.blocks.create(:person => person) - bad_contact = alice.contacts.create(:person => person) + alice.blocks.create(person: person) + bad_contact = alice.contacts.create(person: person) expect(bad_contact.send(:not_blocked_user)).to be false end it "does not add to errors" do - expect(@contact.send(:not_blocked_user)).to be true + expect(contact.send(:not_blocked_user)).to be true end end end diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 690df1bba..fe44e463d 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -2,172 +2,160 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -require 'spec_helper' +require "spec_helper" describe Conversation, :type => :model do - before do - @user1 = alice - @user2 = bob - @participant_ids = [@user1.contacts.first.person.id, @user1.person.id] + let(:user1) { alice } + let(:user2) { bob } + let(:participant_ids) { [user1.contacts.first.person.id, user1.person.id] } + let(:create_hash) { + {author: user1.person, participant_ids: participant_ids, + subject: "cool stuff", messages_attributes: [{author: user1.person, text: "hey"}]} + } + let(:conversation) { Conversation.create(create_hash) } + let(:message_last) { + Message.create(author: user2.person, created_at: Time.now + 100, + text: "last", conversation_id: conversation.id) + } + let(:message_first) { + Message.create(author: user2.person, created_at: Time.now + 100, + text: "first", conversation_id: conversation.id) + } - @create_hash = { - :author => @user1.person, - :participant_ids => @participant_ids, - :subject => "cool stuff", - :messages_attributes => [ {:author => @user1.person, :text => 'hey'} ] - } + it "creates a message on create" do + expect { conversation }.to change(Message, :count).by(1) end - it 'creates a message on create' do - expect{ - Conversation.create(@create_hash) - }.to change(Message, :count).by(1) - end - - describe '#last_author' do - it 'returns the last author to a conversation' do - cnv = Conversation.create(@create_hash) - Message.create(:author => @user2.person, :created_at => Time.now + 100, :text => "last", :conversation_id => cnv.id) - expect(cnv.reload.last_author.id).to eq(@user2.person.id) + describe "#last_author" do + it "returns the last author to a conversation" do + message_last + expect(conversation.reload.last_author.id).to eq(user2.person.id) end end describe "#ordered_participants" do it "returns the ordered participants" do - cnv = Conversation.create(@create_hash) - Message.create(author: @user2.person, created_at: Time.now + 100, text: "last", conversation_id: cnv.id) - expect(cnv.ordered_participants.first).to eq(@user2.person) - expect(cnv.ordered_participants.last).to eq(@user1.person) + message_last + expect(conversation.ordered_participants.first).to eq(user2.person) + expect(conversation.ordered_participants.last).to eq(user1.person) end end - describe '#first_unread_message' do + describe "#first_unread_message" do before do - @cnv = Conversation.create(@create_hash) - @message = Message.create(:author => @user2.person, :created_at => Time.now + 100, :text => "last", :conversation_id => @cnv.id) - @message.increase_unread(@user1) + message_last.increase_unread(user1) end - it 'returns the first unread message if there are unread messages in a conversation' do - @cnv.first_unread_message(@user1) == @message + it "returns the first unread message if there are unread messages in a conversation" do + conversation.first_unread_message(user1) == message_last end - it 'returns nil if there are no unread messages in a conversation' do - @cnv.conversation_visibilities.where(:person_id => @user1.person.id).first.tap { |cv| cv.unread = 0 }.save - expect(@cnv.first_unread_message(@user1)).to be_nil + it "returns nil if there are no unread messages in a conversation" do + conversation.conversation_visibilities.where(person_id: user1.person.id).first.tap {|cv| cv.unread = 0 }.save + expect(conversation.first_unread_message(user1)).to be_nil end end - describe '#set_read' do + describe "#set_read" do before do - @cnv = Conversation.create(@create_hash) - Message.create(:author => @user2.person, :created_at => Time.now + 100, :text => "first", :conversation_id => @cnv.id) - .increase_unread(@user1) - Message.create(:author => @user2.person, :created_at => Time.now + 200, :text => "last", :conversation_id => @cnv.id) - .increase_unread(@user1) + conversation + message_first.increase_unread(user1) + message_last.increase_unread(user1) end - it 'sets the unread counter to 0' do - expect(@cnv.conversation_visibilities.where(:person_id => @user1.person.id).first.unread).to eq(2) - @cnv.set_read(@user1) - expect(@cnv.conversation_visibilities.where(:person_id => @user1.person.id).first.unread).to eq(0) + it "sets the unread counter to 0" do + expect(conversation.conversation_visibilities.where(person_id: user1.person.id).first.unread).to eq(2) + conversation.set_read(user1) + expect(conversation.conversation_visibilities.where(person_id: user1.person.id).first.unread).to eq(0) end end - context 'transport' do + context "transport" do + let(:conversation_message) { conversation.messages.first } + let(:xml) { conversation.to_diaspora_xml } + before do - @cnv = Conversation.create(@create_hash) - @message = @cnv.messages.first - @xml = @cnv.to_diaspora_xml + conversation end - describe 'serialization' do - it 'serializes the message' do - expect(@xml.gsub(/\s/, '')).to include(@message.to_xml.to_s.gsub(/\s/, '')) + describe "serialization" do + it "serializes the message" do + expect(xml.gsub(/\s/, "")).to include(conversation_message.to_xml.to_s.gsub(/\s/, "")) end - it 'serializes the participants' do - @create_hash[:participant_ids].each{|id| - expect(@xml).to include(Person.find(id).diaspora_handle) - } + it "serializes the participants" do + create_hash[:participant_ids].each do |id| + expect(xml).to include(Person.find(id).diaspora_handle) + end end - it 'serializes the created_at time' do - expect(@xml).to include(@message.created_at.to_s) + it "serializes the created_at time" do + expect(xml).to include(conversation_message.created_at.to_s) end end - describe '#subscribers' do - it 'returns the recipients for the post owner' do - expect(@cnv.subscribers(@user1)).to eq(@user1.contacts.map{|c| c.person}) + describe "#subscribers" do + it "returns the recipients for the post owner" do + expect(conversation.subscribers(user1)).to eq(user1.contacts.map(&:person)) end end - describe '#receive' do + describe "#receive" do before do Message.destroy_all Conversation.destroy_all end - it 'creates a message' do - expect{ - Diaspora::Parser.from_xml(@xml).receive(@user1, @user2.person) + it "creates a message" do + expect { + Diaspora::Parser.from_xml(xml).receive(user1, user2.person) }.to change(Message, :count).by(1) end - it 'creates a conversation' do - expect{ - Diaspora::Parser.from_xml(@xml).receive(@user1, @user2.person) + it "creates a conversation" do + expect { + Diaspora::Parser.from_xml(xml).receive(user1, user2.person) }.to change(Conversation, :count).by(1) end - it 'creates appropriate visibilities' do - expect{ - Diaspora::Parser.from_xml(@xml).receive(@user1, @user2.person) - }.to change(ConversationVisibility, :count).by(@participant_ids.size) + it "creates appropriate visibilities" do + expect { + Diaspora::Parser.from_xml(xml).receive(user1, user2.person) + }.to change(ConversationVisibility, :count).by(participant_ids.size) end - it 'does not save before receive' do - expect(Diaspora::Parser.from_xml(@xml).persisted?).to be false + it "does not save before receive" do + expect(Diaspora::Parser.from_xml(xml).persisted?).to be false end - it 'notifies for the message' do + it "notifies for the message" do expect(Notification).to receive(:notify).once - Diaspora::Parser.from_xml(@xml).receive(@user1, @user2.person) + Diaspora::Parser.from_xml(xml).receive(user1, user2.person) end end end describe "#invalid parameters" do context "local author" do - before do - @invalid_hash = { - author: peter.person, - participant_ids: [peter.person.id, @user1.person.id], - subject: "cool stuff", - messages_attributes: [{author: peter.person, text: "hey"}] - } - end + let(:invalid_hash) { + {author: peter.person, participant_ids: [peter.person.id, user1.person.id], + subject: "cool stuff", messages_attributes: [{author: peter.person, text: "hey"}]} + } it "is invalid with invalid recipient" do - conversation = Conversation.create(@invalid_hash) - expect(conversation).to be_invalid + invalid_conversation = Conversation.create(invalid_hash) + expect(invalid_conversation).to be_invalid end end context "remote author" do - before do - @remote_person = remote_raphael - @local_user = alice - @participant_ids = [@remote_person.id, @local_user.person.id] - - @invalid_hash_remote = { - author: @remote_person, - participant_ids: @participant_ids, - subject: "cool stuff", - messages_attributes: [{author: @remote_person, text: "hey"}] - } - end + let(:remote_person) { remote_raphael } + let(:local_user) { alice } + let(:participant_ids) { [remote_person.id, local_user.person.id] } + let(:invalid_hash_remote) { + {author: remote_person, participant_ids: participant_ids, + subject: "cool stuff", messages_attributes: [{author: remote_person, text: "hey"}]} + } it "is invalid with invalid recipient" do - conversation = Conversation.create(@invalid_hash_remote) - expect(conversation).to be_invalid + invalid_conversation_remote = Conversation.create(invalid_hash_remote) + expect(invalid_conversation_remote).to be_invalid end end end diff --git a/spec/models/conversation_visibilities_spec.rb b/spec/models/conversation_visibilities_spec.rb index da15b8743..68a77a2f9 100644 --- a/spec/models/conversation_visibilities_spec.rb +++ b/spec/models/conversation_visibilities_spec.rb @@ -2,27 +2,20 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -require 'spec_helper' +require "spec_helper" -describe ConversationVisibility, :type => :model do - before do - @user1 = alice - @participant_ids = [@user1.contacts.first.person.id, @user1.person.id] +describe ConversationVisibility, type: :model do + let(:user1) { alice } + let(:participant_ids) { [user1.contacts.first.person.id, user1.person.id] } + let(:create_hash) { + {author: user1.person, participant_ids: participant_ids, subject: "cool stuff", + messages_attributes: [{author: user1.person, text: "hey"}]} + } + let(:conversation) { Conversation.create(create_hash) } - @create_hash = { - :author => @user1.person, - :participant_ids => @participant_ids, - :subject => "cool stuff", - :messages_attributes => [ {:author => @user1.person, :text => 'hey'} ] - } - @conversation = Conversation.create(@create_hash) - end + it "destroy conversation when no participant" do + conversation.conversation_visibilities.each(&:destroy) - it 'destroy conversation when no participant' do - @conversation.conversation_visibilities.each do |visibility| - visibility.destroy - end - - expect(Conversation).not_to exist(@conversation.id) + expect(Conversation).not_to exist(conversation.id) end end