refactor hash indentation and add double quotes

this is the suggested styling according to hound/pronto/rubocop
(#6192)
This commit is contained in:
realtin 2015-07-15 15:19:16 +02:00 committed by Dennis Schubert
parent c5a2334fbe
commit d0b290ea4d
10 changed files with 131 additions and 118 deletions

View file

@ -6,6 +6,7 @@
* Add link to pod statistics in right navigation [#6117](https://github.com/diaspora/diaspora/pull/6117) * Add link to pod statistics in right navigation [#6117](https://github.com/diaspora/diaspora/pull/6117)
* Refactor person related URL generation [#6168](https://github.com/diaspora/diaspora/pull/6168) * 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/) * 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 ## Bug fixes
* Precompile facebox images [#6105](https://github.com/diaspora/diaspora/pull/6105) * Precompile facebox images [#6105](https://github.com/diaspora/diaspora/pull/6105)

View file

@ -2,59 +2,59 @@
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
require 'spec_helper' require "spec_helper"
describe AccountDeletion, :type => :model do describe AccountDeletion, :type => :model do
let(:account_deletion_new) { AccountDeletion.new(person: alice.person) } let(:account_deletion_new) { AccountDeletion.new(person: alice.person) }
let(:account_deletion_create) { AccountDeletion.create(person: alice.person) } let(:account_deletion_create) { AccountDeletion.create(person: alice.person) }
it 'assigns the diaspora_handle from the person object' do it "assigns the diaspora_handle from the person object" do
expect(account_deletion_new.diaspora_handle).to eq(alice.person.diaspora_handle) expect(account_deletion_new.diaspora_handle).to eq(alice.person.diaspora_handle)
end end
it 'fires a job after creation'do it "fires a job after creation"do
expect(Workers::DeleteAccount).to receive(:perform_async).with(anything) expect(Workers::DeleteAccount).to receive(:perform_async).with(anything)
account_deletion_create account_deletion_create
end end
describe "#perform!" do describe "#perform!" do
it 'creates a deleter' do it "creates a deleter" do
expect(AccountDeleter).to receive(:new).with(alice.person.diaspora_handle).and_return(double(perform!: true)) expect(AccountDeleter).to receive(:new).with(alice.person.diaspora_handle).and_return(double(perform!: true))
account_deletion_new.perform! account_deletion_new.perform!
end end
it 'dispatches the account deletion if the user exists' do it "dispatches the account deletion if the user exists" do
expect(account_deletion_new).to receive(:dispatch) expect(account_deletion_new).to receive(:dispatch)
account_deletion_new.perform! account_deletion_new.perform!
end end
it 'does not dispatch an account deletion for non-local people' do it "does not dispatch an account deletion for non-local people" do
deletion = AccountDeletion.new(person: remote_raphael) deletion = AccountDeletion.new(person: remote_raphael)
expect(deletion).not_to receive(:dispatch) expect(deletion).not_to receive(:dispatch)
deletion.perform! deletion.perform!
end end
it 'marks an AccountDeletion as completed when successful' do it "marks an AccountDeletion as completed when successful" do
account_deletion_create.perform! account_deletion_create.perform!
expect(account_deletion_create.reload.completed_at).not_to be_nil expect(account_deletion_create.reload.completed_at).not_to be_nil
end end
end end
describe '#dispatch' do describe "#dispatch" do
it 'creates a public postzord' do it "creates a public postzord" do
expect(Postzord::Dispatcher::Public).to receive(:new).and_return(double.as_null_object) expect(Postzord::Dispatcher::Public).to receive(:new).and_return(double.as_null_object)
account_deletion_new.dispatch account_deletion_new.dispatch
end end
end end
describe "#subscribers" do describe "#subscribers" do
it 'includes all remote contacts' do it "includes all remote contacts" do
alice.share_with(remote_raphael, alice.aspects.first) alice.share_with(remote_raphael, alice.aspects.first)
expect(account_deletion_new.subscribers(alice)).to eq([remote_raphael]) expect(account_deletion_new.subscribers(alice)).to eq([remote_raphael])
end end
it 'includes remote resharers' do it "includes remote resharers" do
status_message = FactoryGirl.create(:status_message, public: true, author: alice.person) status_message = FactoryGirl.create(:status_message, public: true, author: alice.person)
FactoryGirl.create(:reshare, author: remote_raphael, root: status_message) FactoryGirl.create(:reshare, author: remote_raphael, root: status_message)
FactoryGirl.create(:reshare, author: local_luke.person, root: status_message) FactoryGirl.create(:reshare, author: local_luke.person, root: status_message)
@ -63,14 +63,14 @@ describe AccountDeletion, :type => :model do
end end
end end
describe 'serialization' do describe "serialization" do
let(:xml) { account_deletion_new.to_xml.to_s } let(:xml) { account_deletion_new.to_xml.to_s }
it 'should have a diaspora_handle' do it "should have a diaspora_handle" do
expect(xml.include?(alice.person.diaspora_handle)).to eq(true) expect(xml.include?(alice.person.diaspora_handle)).to eq(true)
end end
it 'marshals the xml' do it "marshals the xml" do
expect(AccountDeletion.from_xml(xml)).to be_valid expect(AccountDeletion.from_xml(xml)).to be_valid
end end
end end

View file

@ -1,16 +1,16 @@
require 'spec_helper' require "spec_helper"
describe ActsAsTaggableOn::Tag, :type => :model do describe ActsAsTaggableOn::Tag, :type => :model do
subject(:tag) { ActsAsTaggableOn::Tag } subject(:tag) { ActsAsTaggableOn::Tag }
describe '.autocomplete' do describe ".autocomplete" do
let!(:tag_cats) { tag.create(name: "cats") } let!(:tag_cats) { tag.create(name: "cats") }
it 'downcases the tag name' do it "downcases the tag name" do
expect(tag.autocomplete("CATS")).to eq([tag_cats]) expect(tag.autocomplete("CATS")).to eq([tag_cats])
end end
it 'does an end where on tags' do it "does an end where on tags" do
expect(tag.autocomplete("CAT")).to eq([tag_cats]) expect(tag.autocomplete("CAT")).to eq([tag_cats])
end end
end end
@ -37,7 +37,7 @@ describe ActsAsTaggableOn::Tag, :type => :model do
end end
end end
it 'allows for love' do it "allows for love" do
expect(tag.normalize("<3")).to eq("<3") expect(tag.normalize("<3")).to eq("<3")
expect(tag.normalize("#<3")).to eq("<3") expect(tag.normalize("#<3")).to eq("<3")
end end

View file

@ -2,10 +2,10 @@
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
# #
require 'spec_helper' require "spec_helper"
describe AspectMembership, :type => :model do describe AspectMembership, :type => :model do
describe '#before_destroy' do describe "#before_destroy" do
let(:aspect) { alice.aspects.create(name: "two") } let(:aspect) { alice.aspects.create(name: "two") }
let(:contact) { alice.contact_for(bob.person) } let(:contact) { alice.contact_for(bob.person) }
let(:aspect_membership) { alice.aspects.where(name: "generic").first.aspect_memberships.first } let(:aspect_membership) { alice.aspects.where(name: "generic").first.aspect_memberships.first }
@ -14,13 +14,13 @@ describe AspectMembership, :type => :model do
allow(aspect_membership).to receive(:user).and_return(alice) allow(aspect_membership).to receive(:user).and_return(alice)
end end
it 'calls disconnect if its the last aspect for the contact' do it "calls disconnect if its the last aspect for the contact" do
expect(alice).to receive(:disconnect).with(contact) expect(alice).to receive(:disconnect).with(contact)
aspect_membership.destroy aspect_membership.destroy
end 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) expect(alice).not_to receive(:disconnect)
alice.add_contact_to_aspect(contact, aspect) alice.add_contact_to_aspect(contact, aspect)

View file

@ -2,26 +2,26 @@
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
require 'spec_helper' require "spec_helper"
describe Aspect, :type => :model do describe Aspect, :type => :model do
describe 'creation' do describe "creation" do
let(:name) { alice.aspects.first.name } let(:name) { alice.aspects.first.name }
it 'does not allow duplicate names' do it "does not allow duplicate names" do
expect { alice.aspects.create(name: name) }.not_to change(Aspect, :count) expect { alice.aspects.create(name: name) }.not_to change(Aspect, :count)
end end
it 'validates case insensitiveness on names' do it "validates case insensitiveness on names" do
expect { alice.aspects.create(name: name.titleize) }.not_to change(Aspect, :count) expect { alice.aspects.create(name: name.titleize) }.not_to change(Aspect, :count)
end end
it 'has a 20 character limit on names' do it "has a 20 character limit on names" do
aspect = Aspect.new(name: "this name is really too too too too too long") aspect = Aspect.new(name: "this name is really too too too too too long")
expect(aspect.valid?).to eq(false) expect(aspect.valid?).to eq(false)
end end
it 'is able to have other users as contacts' do it "is able to have other users as contacts" do
aspect = alice.aspects.create(name: "losers") aspect = alice.aspects.create(name: "losers")
Contact.create(user: alice, person: eve.person, aspects: [aspect]) Contact.create(user: alice, person: eve.person, aspects: [aspect])
@ -30,7 +30,7 @@ describe Aspect, :type => :model do
expect(aspect.contacts.size).to eq(1) expect(aspect.contacts.size).to eq(1)
end end
it 'has a contacts_visible? method' do it "has a contacts_visible? method" do
expect(alice.aspects.first.contacts_visible?).to be true expect(alice.aspects.first.contacts_visible?).to be true
end end
@ -42,8 +42,8 @@ describe Aspect, :type => :model do
end end
end end
describe 'validation' do describe "validation" do
it 'has no uniqueness of name between users' do it "has no uniqueness of name between users" do
aspect = alice.aspects.create(name: "New Aspect") aspect = alice.aspects.create(name: "New Aspect")
aspect2 = eve.aspects.create(name: aspect.name) aspect2 = eve.aspects.create(name: aspect.name)
expect(aspect2).to be_valid expect(aspect2).to be_valid

View file

@ -1,8 +1,8 @@
require 'spec_helper' require "spec_helper"
describe Block, :type => :model do describe Block, :type => :model do
describe 'validations' do describe "validations" do
it 'doesnt allow you to block yourself' do it "doesnt allow you to block yourself" do
block = alice.blocks.create(person: alice.person) block = alice.blocks.create(person: alice.person)
expect(block.errors[:person_id].size).to eq(1) expect(block.errors[:person_id].size).to eq(1)
end end

View file

@ -2,7 +2,7 @@
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
require 'spec_helper' require "spec_helper"
require Rails.root.join("spec", "shared_behaviors", "relayable") require Rails.root.join("spec", "shared_behaviors", "relayable")
describe Comment, :type => :model do describe Comment, :type => :model do
@ -20,7 +20,7 @@ describe Comment, :type => :model do
expect(comment_alice.notification_type(eve, alice.person)).to eq(Notifications::AlsoCommented) expect(comment_alice.notification_type(eve, alice.person)).to eq(Notifications::AlsoCommented)
end end
it 'returns false if the comment is not on a post you own and no one "also_commented"' do 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 expect(comment_alice.notification_type(eve, alice.person)).to be false
end end
@ -31,7 +31,7 @@ describe Comment, :type => :model do
comment_alice comment_alice
end end
it 'does not return also commented if the user commented' do it "does not return also commented if the user commented" do
expect(comment_eve.notification_type(eve, alice.person)).to eq(false) expect(comment_eve.notification_type(eve, alice.person)).to eq(false)
end end
@ -41,7 +41,7 @@ describe Comment, :type => :model do
end end
end end
describe 'User#comment' do describe "User#comment" do
it "should be able to comment on one's own status" do it "should be able to comment on one's own status" do
bob.comment!(status_bob, "sup dog") bob.comment!(status_bob, "sup dog")
expect(status_bob.reload.comments.first.text).to eq("sup dog") expect(status_bob.reload.comments.first.text).to eq("sup dog")
@ -52,15 +52,15 @@ describe Comment, :type => :model do
expect(status_bob.reload.comments.first.text).to eq("why so formal?") expect(status_bob.reload.comments.first.text).to eq("why so formal?")
end end
it 'does not multi-post a comment' do it "does not multi-post a comment" do
expect { expect {
comment_alice comment_alice
}.to change { Comment.count }.by(1) }.to change { Comment.count }.by(1)
end end
end end
describe 'counter cache' do describe "counter cache" do
it 'increments the counter cache on its post' do it "increments the counter cache on its post" do
expect { expect {
comment_alice comment_alice
}.to change{ }.to change{
@ -69,7 +69,7 @@ describe Comment, :type => :model do
end end
end end
describe 'xml' do describe "xml" do
let(:commenter) { create(:user) } let(:commenter) { create(:user) }
let(:commenter_aspect) { commenter.aspects.create(name: "bruisers") } let(:commenter_aspect) { commenter.aspects.create(name: "bruisers") }
let(:post) { alice.post :status_message, text: "hello", to: alices_aspect.id } let(:post) { alice.post :status_message, text: "hello", to: alices_aspect.id }
@ -80,26 +80,26 @@ describe Comment, :type => :model do
connect_users(alice, alices_aspect, commenter, commenter_aspect) connect_users(alice, alices_aspect, commenter, commenter_aspect)
end end
it 'serializes the sender handle' do it "serializes the sender handle" do
expect(xml.include?(commenter.diaspora_handle)).to be true expect(xml.include?(commenter.diaspora_handle)).to be true
end end
it 'serializes the post_guid' do it "serializes the post_guid" do
expect(xml).to include(post.guid) expect(xml).to include(post.guid)
end end
describe 'marshalling' do describe "marshalling" do
let(:marshalled_comment) { Comment.from_xml(xml) } let(:marshalled_comment) { Comment.from_xml(xml) }
it 'marshals the author' do it "marshals the author" do
expect(marshalled_comment.author).to eq(commenter.person) expect(marshalled_comment.author).to eq(commenter.person)
end end
it 'marshals the post' do it "marshals the post" do
expect(marshalled_comment.post).to eq(post) expect(marshalled_comment.post).to eq(post)
end end
it 'tries to fetch a missing parent' do it "tries to fetch a missing parent" do
guid = post.guid guid = post.guid
marshalled_comment marshalled_comment
post.destroy post.destroy
@ -109,7 +109,7 @@ describe Comment, :type => :model do
end end
end end
describe 'it is relayable' do describe "it is relayable" do
let(:remote_parent) { build(:status_message, author: remote_raphael) } 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(: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_parent_author) { local_luke.comment!(local_parent, "yo!") }
@ -119,7 +119,7 @@ describe Comment, :type => :model do
before do before do
# shared_behaviors/relayable.rb is still using instance variables, so we need to define them here. # 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' # Suggestion: refactor all specs using shared_behaviors/relayable.rb to use "let"
@object_by_parent_author = object_by_parent_author @object_by_parent_author = object_by_parent_author
@object_by_recipient = object_by_recipient @object_by_recipient = object_by_recipient
@dup_object_by_parent_author = dup_object_by_parent_author @dup_object_by_parent_author = dup_object_by_parent_author
@ -127,17 +127,17 @@ describe Comment, :type => :model do
end end
let(:build_object) { alice.build_comment(post: status_bob, text: "why so formal?") } let(:build_object) { alice.build_comment(post: status_bob, text: "why so formal?") }
it_should_behave_like 'it is relayable' it_should_behave_like "it is relayable"
end end
describe 'tags' do describe "tags" do
let(:object) { build(:comment) } let(:object) { build(:comment) }
before do before do
# shared_behaviors/taggable.rb is still using instance variables, so we need to define them here. # 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' # Suggestion: refactor all specs using shared_behaviors/taggable.rb to use "let"
@object = object @object = object
end end
it_should_behave_like 'it is taggable' it_should_behave_like "it is taggable"
end end
end end

View file

@ -2,31 +2,31 @@
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
require 'spec_helper' require "spec_helper"
describe Contact, :type => :model do describe Contact, :type => :model do
describe 'aspect_memberships' do describe "aspect_memberships" do
it 'deletes dependent aspect memberships' do it "deletes dependent aspect memberships" do
expect { expect {
alice.contact_for(bob.person).destroy alice.contact_for(bob.person).destroy
}.to change(AspectMembership, :count).by(-1) }.to change(AspectMembership, :count).by(-1)
end end
end end
context 'validations' do context "validations" do
let(:contact) { Contact.new } let(:contact) { Contact.new }
it 'requires a user' do it "requires a user" do
contact.valid? contact.valid?
expect(contact.errors.full_messages).to include "User can't be blank" expect(contact.errors.full_messages).to include "User can't be blank"
end end
it 'requires a person' do it "requires a person" do
contact.valid? contact.valid?
expect(contact.errors.full_messages).to include "Person can't be blank" expect(contact.errors.full_messages).to include "Person can't be blank"
end 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.person = alice.person
contact.user = alice contact.user = alice
@ -34,7 +34,7 @@ describe Contact, :type => :model do
expect(contact.errors.full_messages).to include "Cannot create self-contact" expect(contact.errors.full_messages).to include "Cannot create self-contact"
end end
it 'validates uniqueness' do it "validates uniqueness" do
person = FactoryGirl.create(:person) person = FactoryGirl.create(:person)
contact2 = alice.contacts.create(person: person) contact2 = alice.contacts.create(person: person)
@ -54,9 +54,9 @@ describe Contact, :type => :model do
end end
end end
context 'scope' do context "scope" do
describe 'sharing' do describe "sharing" do
it 'returns contacts with sharing true' do it "returns contacts with sharing true" do
expect { expect {
alice.contacts.create!(sharing: true, person: FactoryGirl.create(:person)) alice.contacts.create!(sharing: true, person: FactoryGirl.create(:person))
alice.contacts.create!(sharing: false, person: FactoryGirl.create(:person)) alice.contacts.create!(sharing: false, person: FactoryGirl.create(:person))
@ -66,8 +66,8 @@ describe Contact, :type => :model do
end end
end end
describe 'receiving' do describe "receiving" do
it 'returns contacts with sharing true' do it "returns contacts with sharing true" do
expect { expect {
alice.contacts.create!(receiving: true, person: FactoryGirl.build(:person)) alice.contacts.create!(receiving: true, person: FactoryGirl.build(:person))
alice.contacts.create!(receiving: false, person: FactoryGirl.build(:person)) alice.contacts.create!(receiving: false, person: FactoryGirl.build(:person))
@ -77,8 +77,8 @@ describe Contact, :type => :model do
end end
end end
describe 'only_sharing' do describe "only_sharing" do
it 'returns contacts with sharing true and receiving false' do it "returns contacts with sharing true and receiving false" do
expect { expect {
alice.contacts.create!(receiving: true, sharing: true, 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))
@ -91,7 +91,7 @@ describe Contact, :type => :model do
end end
describe "all_contacts_of_person" do 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) person = FactoryGirl.create(:person)
contact1 = FactoryGirl.create(:contact, person: person) contact1 = FactoryGirl.create(:contact, person: person)
contact2 = FactoryGirl.create(:contact) contact2 = FactoryGirl.create(:contact)
@ -101,7 +101,7 @@ describe Contact, :type => :model do
end end
end end
describe '#contacts' do describe "#contacts" do
before do before do
bob.aspects.create(name: "next") bob.aspects.create(name: "next")
bob.aspects(true) bob.aspects(true)
@ -125,7 +125,7 @@ describe Contact, :type => :model do
#eve <-> bob <-> alice #eve <-> bob <-> alice
end end
context 'on a contact for a local user' do context "on a contact for a local user" do
before do before do
alice.reload alice.reload
alice.aspects.reload alice.aspects.reload
@ -136,35 +136,35 @@ describe Contact, :type => :model do
expect(@contact.contacts.map(&:id)).to match_array([eve.person].concat(@people1).map(&:id)) expect(@contact.contacts.map(&:id)).to match_array([eve.person].concat(@people1).map(&:id))
end 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.contacts_visible = false
@original_aspect.save @original_aspect.save
expect(@contact.contacts).to eq([]) expect(@contact.contacts).to eq([])
end end
it 'returns no duplicate contacts' do it "returns no duplicate contacts" do
[alice, eve].each {|c| bob.add_contact_to_aspect(bob.contact_for(c.person), bob.aspects.last) } [alice, eve].each {|c| bob.add_contact_to_aspect(bob.contact_for(c.person), bob.aspects.last) }
contact_ids = @contact.contacts.map(&:id) contact_ids = @contact.contacts.map(&:id)
expect(contact_ids.uniq).to eq(contact_ids) expect(contact_ids.uniq).to eq(contact_ids)
end end
end end
context 'on a contact for a remote user' do context "on a contact for a remote user" do
let(:contact) { bob.contact_for @people1.first } let(:contact) { bob.contact_for @people1.first }
it 'returns an empty array' do it "returns an empty array" do
expect(contact.contacts).to eq([]) expect(contact.contacts).to eq([])
end end
end end
end end
context 'requesting' do context "requesting" do
let(:contact) { Contact.new user: user, person: person } let(:contact) { Contact.new user: user, person: person }
let(:user) { build(:user) } let(:user) { build(:user) }
let(:person) { build(:person) } let(:person) { build(:person) }
describe '#generate_request' do describe "#generate_request" do
it 'makes a request' do it "makes a request" do
allow(contact).to receive(:user).and_return(user) allow(contact).to receive(:user).and_return(user)
request = contact.generate_request request = contact.generate_request
@ -173,8 +173,8 @@ describe Contact, :type => :model do
end end
end end
describe '#dispatch_request' do describe "#dispatch_request" do
it 'pushes to people' do it "pushes to people" do
allow(contact).to receive(:user).and_return(user) allow(contact).to receive(:user).and_return(user)
m = double() m = double()
expect(m).to receive(:post) expect(m).to receive(:post)

View file

@ -2,26 +2,32 @@
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
require 'spec_helper' require "spec_helper"
describe Conversation, :type => :model do describe Conversation, :type => :model do
let(:user1) { alice } let(:user1) { alice }
let(:user2) { bob } let(:user2) { bob }
let(:participant_ids) { [user1.contacts.first.person.id, user1.person.id] } let(:participant_ids) { [user1.contacts.first.person.id, user1.person.id] }
let(:create_hash) {{author: user1.person, participant_ids: participant_ids, let(:create_hash) {
subject: "cool stuff", messages_attributes: [{author: user1.person, text: "hey"}]}} {author: user1.person, participant_ids: participant_ids,
subject: "cool stuff", messages_attributes: [{author: user1.person, text: "hey"}]}
}
let(:conversation) { Conversation.create(create_hash) } let(:conversation) { Conversation.create(create_hash) }
let(:message_last) { Message.create(author: user2.person, created_at: Time.now + 100, let(:message_last) {
text: "last", conversation_id: conversation.id) } Message.create(author: user2.person, created_at: Time.now + 100,
let(:message_first) { Message.create(author: user2.person, created_at: Time.now + 100, text: "last", conversation_id: conversation.id)
text: "first", conversation_id: conversation.id) } }
let(:message_first) {
Message.create(author: user2.person, created_at: Time.now + 100,
text: "first", conversation_id: conversation.id)
}
it 'creates a message on create' do it "creates a message on create" do
expect { conversation }.to change(Message, :count).by(1) expect { conversation }.to change(Message, :count).by(1)
end end
describe '#last_author' do describe "#last_author" do
it 'returns the last author to a conversation' do it "returns the last author to a conversation" do
message_last message_last
expect(conversation.reload.last_author.id).to eq(user2.person.id) expect(conversation.reload.last_author.id).to eq(user2.person.id)
end end
@ -35,36 +41,36 @@ describe Conversation, :type => :model do
end end
end end
describe '#first_unread_message' do describe "#first_unread_message" do
before do before do
message_last.increase_unread(user1) message_last.increase_unread(user1)
end end
it 'returns the first unread message if there are unread messages in a conversation' do it "returns the first unread message if there are unread messages in a conversation" do
conversation.first_unread_message(user1) == message_last conversation.first_unread_message(user1) == message_last
end end
it 'returns nil if there are no unread messages in a conversation' do 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 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 expect(conversation.first_unread_message(user1)).to be_nil
end end
end end
describe '#set_read' do describe "#set_read" do
before do before do
conversation conversation
message_first.increase_unread(user1) message_first.increase_unread(user1)
message_last.increase_unread(user1) message_last.increase_unread(user1)
end end
it 'sets the unread counter to 0' do it "sets the unread counter to 0" do
expect(conversation.conversation_visibilities.where(person_id: user1.person.id).first.unread).to eq(2) expect(conversation.conversation_visibilities.where(person_id: user1.person.id).first.unread).to eq(2)
conversation.set_read(user1) conversation.set_read(user1)
expect(conversation.conversation_visibilities.where(person_id: user1.person.id).first.unread).to eq(0) expect(conversation.conversation_visibilities.where(person_id: user1.person.id).first.unread).to eq(0)
end end
end end
context 'transport' do context "transport" do
let(:conversation_message) { conversation.messages.first } let(:conversation_message) { conversation.messages.first }
let(:xml) { conversation.to_diaspora_xml } let(:xml) { conversation.to_diaspora_xml }
@ -72,53 +78,53 @@ describe Conversation, :type => :model do
conversation conversation
end end
describe 'serialization' do describe "serialization" do
it 'serializes the message' do it "serializes the message" do
expect(xml.gsub(/\s/, "")).to include(conversation_message.to_xml.to_s.gsub(/\s/, "")) expect(xml.gsub(/\s/, "")).to include(conversation_message.to_xml.to_s.gsub(/\s/, ""))
end end
it 'serializes the participants' do it "serializes the participants" do
create_hash[:participant_ids].each do |id| create_hash[:participant_ids].each do |id|
expect(xml).to include(Person.find(id).diaspora_handle) expect(xml).to include(Person.find(id).diaspora_handle)
end end
end end
it 'serializes the created_at time' do it "serializes the created_at time" do
expect(xml).to include(conversation_message.created_at.to_s) expect(xml).to include(conversation_message.created_at.to_s)
end end
end end
describe '#subscribers' do describe "#subscribers" do
it 'returns the recipients for the post owner' do it "returns the recipients for the post owner" do
expect(conversation.subscribers(user1)).to eq(user1.contacts.map(&:person)) expect(conversation.subscribers(user1)).to eq(user1.contacts.map(&:person))
end end
end end
describe '#receive' do describe "#receive" do
before do before do
Message.destroy_all Message.destroy_all
Conversation.destroy_all Conversation.destroy_all
end end
it 'creates a message' do it "creates a message" do
expect { expect {
Diaspora::Parser.from_xml(xml).receive(user1, user2.person) Diaspora::Parser.from_xml(xml).receive(user1, user2.person)
}.to change(Message, :count).by(1) }.to change(Message, :count).by(1)
end end
it 'creates a conversation' do it "creates a conversation" do
expect { expect {
Diaspora::Parser.from_xml(xml).receive(user1, user2.person) Diaspora::Parser.from_xml(xml).receive(user1, user2.person)
}.to change(Conversation, :count).by(1) }.to change(Conversation, :count).by(1)
end end
it 'creates appropriate visibilities' do it "creates appropriate visibilities" do
expect { expect {
Diaspora::Parser.from_xml(xml).receive(user1, user2.person) Diaspora::Parser.from_xml(xml).receive(user1, user2.person)
}.to change(ConversationVisibility, :count).by(participant_ids.size) }.to change(ConversationVisibility, :count).by(participant_ids.size)
end end
it 'does not save before receive' do it "does not save before receive" do
expect(Diaspora::Parser.from_xml(xml).persisted?).to be false expect(Diaspora::Parser.from_xml(xml).persisted?).to be false
end end
it 'notifies for the message' do it "notifies for the message" do
expect(Notification).to receive(:notify).once 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
@ -127,8 +133,10 @@ describe Conversation, :type => :model do
describe "#invalid parameters" do describe "#invalid parameters" do
context "local author" do context "local author" do
let(:invalid_hash) {{author: peter.person, participant_ids: [peter.person.id, user1.person.id], let(:invalid_hash) {
subject: "cool stuff", messages_attributes: [{author: peter.person, text: "hey"}]}} {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 it "is invalid with invalid recipient" do
invalid_conversation = Conversation.create(invalid_hash) invalid_conversation = Conversation.create(invalid_hash)
@ -140,8 +148,10 @@ describe Conversation, :type => :model do
let(:remote_person) { remote_raphael } let(:remote_person) { remote_raphael }
let(:local_user) { alice } let(:local_user) { alice }
let(:participant_ids) { [remote_person.id, local_user.person.id] } let(:participant_ids) { [remote_person.id, local_user.person.id] }
let(:invalid_hash_remote) {{author: remote_person, participant_ids: participant_ids, let(:invalid_hash_remote) {
subject: "cool stuff", messages_attributes: [{author: remote_person, text: "hey"}]}} {author: remote_person, participant_ids: participant_ids,
subject: "cool stuff", messages_attributes: [{author: remote_person, text: "hey"}]}
}
it "is invalid with invalid recipient" do it "is invalid with invalid recipient" do
invalid_conversation_remote = Conversation.create(invalid_hash_remote) invalid_conversation_remote = Conversation.create(invalid_hash_remote)

View file

@ -2,16 +2,18 @@
# licensed under the Affero General Public License version 3 or later. See # licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file. # the COPYRIGHT file.
require 'spec_helper' require "spec_helper"
describe ConversationVisibility, type: :model do describe ConversationVisibility, type: :model do
let(:user1) { alice } let(:user1) { alice }
let(:participant_ids) { [user1.contacts.first.person.id, user1.person.id] } let(:participant_ids) { [user1.contacts.first.person.id, user1.person.id] }
let(:create_hash) {{author: user1.person, participant_ids: participant_ids, subject: "cool stuff", let(:create_hash) {
messages_attributes: [{author: user1.person, text: "hey"}]}} {author: user1.person, participant_ids: participant_ids, subject: "cool stuff",
messages_attributes: [{author: user1.person, text: "hey"}]}
}
let(:conversation) { Conversation.create(create_hash) } let(:conversation) { Conversation.create(create_hash) }
it 'destroy conversation when no participant' do it "destroy conversation when no participant" do
conversation.conversation_visibilities.each(&:destroy) conversation.conversation_visibilities.each(&:destroy)
expect(Conversation).not_to exist(conversation.id) expect(Conversation).not_to exist(conversation.id)