Merge branch 'stable' into develop

This commit is contained in:
Dennis Schubert 2015-07-16 03:10:06 +02:00
commit 201a3ec1cf
10 changed files with 350 additions and 402 deletions

View file

@ -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) * 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) * 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,91 +2,76 @@
# 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
it 'assigns the diaspora_handle from the person object' do let(:account_deletion_new) { AccountDeletion.new(person: alice.person) }
a = AccountDeletion.new(:person => alice.person) let(:account_deletion_create) { AccountDeletion.create(person: alice.person) }
expect(a.diaspora_handle).to eq(alice.person.diaspora_handle)
it "assigns the diaspora_handle from the person object" do
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
AccountDeletion.create(:person => alice.person)
end end
describe "#perform!" do describe "#perform!" do
before do it "creates a deleter" do
@ad = AccountDeletion.new(:person => alice.person) expect(AccountDeleter).to receive(:new).with(alice.person.diaspora_handle).and_return(double(perform!: true))
account_deletion_new.perform!
end end
it 'creates a deleter' do it "dispatches the account deletion if the user exists" do
expect(AccountDeleter).to receive(:new).with(alice.person.diaspora_handle).and_return(double(:perform! => true)) expect(account_deletion_new).to receive(:dispatch)
@ad.perform! account_deletion_new.perform!
end
it 'dispatches the account deletion if the user exists' do
expect(@ad).to receive(:dispatch)
@ad.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
ad = AccountDeletion.create(:person => alice.person) account_deletion_create.perform!
ad.perform! expect(account_deletion_create.reload.completed_at).not_to be_nil
expect(ad.reload.completed_at).not_to be_nil
end end
end end
describe '#dispatch' do describe "#dispatch" do
it "sends the account deletion xml" do it "creates a public postzord" do
@ad = AccountDeletion.new(:person => alice.person)
@ad.send(:dispatch)
end
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)
@ad = AccountDeletion.new(:person => alice.person) account_deletion_new.dispatch
@ad.send(:dispatch)
end end
end end
describe "#subscribers" do describe "#subscribers" do
it 'includes all remote contacts' do it "includes all remote contacts" do
@ad = AccountDeletion.new(:person => alice.person)
alice.share_with(remote_raphael, alice.aspects.first) 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 end
it 'includes remote resharers' do it "includes remote resharers" do
@ad = AccountDeletion.new(:person => alice.person) status_message = FactoryGirl.create(:status_message, public: true, author: alice.person)
sm = FactoryGirl.create( :status_message, :public => true, :author => alice.person) FactoryGirl.create(:reshare, author: remote_raphael, root: status_message)
r1 = FactoryGirl.create( :reshare, :author => remote_raphael, :root => sm) FactoryGirl.create(:reshare, author: local_luke.person, root: status_message)
r2 = FactoryGirl.create( :reshare, :author => local_luke.person, :root => sm)
expect(@ad.subscribers(alice)).to eq([remote_raphael]) expect(account_deletion_new.subscribers(alice)).to eq([remote_raphael])
end end
end end
describe 'serialization' do describe "serialization" do
before do let(:xml) { account_deletion_new.to_xml.to_s }
account_deletion = AccountDeletion.new(:person => alice.person)
@xml = account_deletion.to_xml.to_s it "should have a diaspora_handle" do
expect(xml.include?(alice.person.diaspora_handle)).to eq(true)
end end
it 'should have a diaspora_handle' do it "marshals the xml" do
expect(@xml.include?(alice.person.diaspora_handle)).to eq(true) expect(AccountDeletion.from_xml(xml)).to be_valid
end
it 'marshals the xml' do
expect(AccountDeletion.from_xml(@xml)).to be_valid
end end
end end
end end

View file

@ -1,45 +1,45 @@
require 'spec_helper' require "spec_helper"
describe ActsAsTaggableOn::Tag, :type => :model do describe ActsAsTaggableOn::Tag, :type => :model do
describe '.autocomplete' do subject(:tag) { ActsAsTaggableOn::Tag }
before do
@tag = ActsAsTaggableOn::Tag.create(:name => "cats")
end
it 'downcases the tag name' do
expect(ActsAsTaggableOn::Tag.autocomplete("CATS")).to eq([@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 end
it 'does an end where on tags' do it "does an end where on tags" do
expect(ActsAsTaggableOn::Tag.autocomplete("CAT")).to eq([@tag]) expect(tag.autocomplete("CAT")).to eq([tag_cats])
end end
end end
describe ".normalize" do describe ".normalize" do
it "removes leading hash symbols" do it "removes leading hash symbols" do
expect(ActsAsTaggableOn::Tag.normalize("#mytag")).to eq("mytag") expect(tag.normalize("#mytag")).to eq("mytag")
end end
it "removes punctuation and whitespace" do it "removes punctuation and whitespace" do
{ {
'node.js' => 'nodejs', "node.js" => "nodejs",
'.dotatstart' => 'dotatstart', ".dotatstart" => "dotatstart",
'you,inside' => 'youinside', "you,inside" => "youinside",
'iam(parenthetical)' => 'iamparenthetical', "iam(parenthetical)" => "iamparenthetical",
'imeanit?maybe' => 'imeanitmaybe', "imeanit?maybe" => "imeanitmaybe",
'imeanit!' => 'imeanit', "imeanit!" => "imeanit",
'how about spaces' => 'howaboutspaces', "how about spaces" => "howaboutspaces",
"other\twhitespace\n" => 'otherwhitespace', "other\twhitespace\n" => "otherwhitespace",
'hash#inside' => 'hashinside', "hash#inside" => "hashinside",
'f!u@n#k$y%-<c>^h&a*r(a)c{t}e[r]s' => 'funky-characters' "f!u@n#k$y%-<c>^h&a*r(a)c{t}e[r]s" => "funky-characters"
}.each do |invalid, normalized| }.each do |invalid, normalized|
expect(ActsAsTaggableOn::Tag.normalize(invalid)).to eq(normalized) expect(tag.normalize(invalid)).to eq(normalized)
end end
end end
it 'allows for love' do it "allows for love" do
expect(ActsAsTaggableOn::Tag.normalize("<3")).to eq("<3") expect(tag.normalize("<3")).to eq("<3")
expect(ActsAsTaggableOn::Tag.normalize("#<3")).to eq("<3") expect(tag.normalize("#<3")).to eq("<3")
end end
end end
end end

View file

@ -2,31 +2,29 @@
# 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
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 before do
@aspect = alice.aspects.create(:name => "two") allow(aspect_membership).to receive(:user).and_return(alice)
@contact = alice.contact_for(bob.person)
@am = alice.aspects.where(:name => "generic").first.aspect_memberships.first
allow(@am).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)
@am.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)
@am.destroy aspect_membership.destroy
end end
end end
end end

View file

@ -2,41 +2,35 @@
# 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
before do let(:name) { alice.aspects.first.name }
@name = alice.aspects.first.name
it "does not allow duplicate names" do
expect { alice.aspects.create(name: name) }.not_to change(Aspect, :count)
end end
it 'does not allow duplicate names' do it "validates case insensitiveness on names" do
expect { expect { alice.aspects.create(name: name.titleize) }.not_to change(Aspect, :count)
invalid_aspect = alice.aspects.create(:name => @name)
}.not_to change(Aspect, :count)
end end
it 'validates case insensitiveness on names' do it "has a 20 character limit on names" do
expect { aspect = Aspect.new(name: "this name is really too too too too too long")
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")
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]) expect(aspect.contacts.where(person_id: alice.person.id)).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.where(:person_id => eve.person.id)).not_to be_empty
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
@ -48,10 +42,10 @@ 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
end end
end end

View file

@ -1,10 +1,10 @@
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
end end
end end

View file

@ -2,161 +2,162 @@
# 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
before do let(:alices_aspect) { alice.aspects.first }
@alices_aspect = alice.aspects.first let(:status_bob) { bob.post(:status_message, text: "hello", to: bob.aspects.first.id) }
@status = bob.post(:status_message, :text => "hello", :to => bob.aspects.first.id) let(:comment_alice) { alice.comment!(status_bob, "why so formal?") }
end
describe "#destroy" do describe "#destroy" do
before do
@comment = alice.comment!(@status, "why so formal?")
end
it "should delete a participation" do 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 end
it "should decrease count participation" do it "should decrease count participation" do
alice.comment!(@status, "Are you there?") alice.comment!(status_bob, "Are you there?")
@comment.destroy comment_alice.destroy
participations = Participation.where(target_id: @comment.commentable_id, author_id: @comment.author_id) participations = Participation.where(target_id: comment_alice.commentable_id, author_id: comment_alice.author_id)
expect(participations.first.count).to eq(1) expect(participations.first.count).to eq(1)
end end
end end
describe 'comment#notification_type' do describe "comment#notification_type" do
let (:comment) { alice.comment!(@status, "why so formal?") }
it "returns 'comment_on_post' if the comment is on a post you own" 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 end
it "returns 'also_commented' if the comment is on a post you participate to" do it "returns 'also_commented' if the comment is on a post you participate to" do
eve.participate! @status eve.participate! status_bob
expect(comment.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
comment = alice.comment!(@status, "I simply felt like issuing a greeting. Do step off.") expect(comment_alice.notification_type(eve, alice.person)).to be false
expect(comment.notification_type(eve, alice.person)).to be false
end end
context "also commented" do context "also commented" do
let(:comment_eve) { eve.comment!(status_bob, "I also commented on the first user's post") }
before do before do
alice.comment!(@status, "a-commenta commenta") comment_alice
@comment = eve.comment!(@status, "I also commented on the first user's post")
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.notification_type(eve, alice.person)).to eq(false) expect(comment_eve.notification_type(eve, alice.person)).to eq(false)
end end
it "returns 'also_commented' if another person commented on a post you commented on" do 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 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
alice.comment!(@status, "Yeah, it was great") bob.comment!(status_bob, "sup dog")
expect(@status.reload.comments.first.text).to eq("Yeah, it was great") expect(status_bob.reload.comments.first.text).to eq("sup dog")
end end
it "should be able to comment on a contact's status" do it "should be able to comment on a contact's status" do
bob.comment!(@status, "sup dog") comment_alice
expect(@status.reload.comments.first.text).to eq("sup dog") 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 {
alice.comment!(@status, 'hello') comment_alice
}.to change { Comment.count }.by(1) }.to change { Comment.count }.by(1)
end end
it "should create a participation" do it "should create a participation" do
comment = bob.comment!(@status, "sup dog") comment_alice
participations = Participation.where(target_id: comment.commentable_id, author_id: comment.author_id) participations = Participation.where(target_id: comment_alice.commentable_id, author_id: comment_alice.author_id)
expect(participations.count).to eq(1) expect(participations.count).to eq(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 {
alice.comment!(@status, "oh yeah") comment_alice
}.to change{ }.to change{
@status.reload.comments_count status_bob.reload.comments_count
}.by(1) }.by(1)
end end
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 before do
@commenter = FactoryGirl.create(:user) connect_users(alice, alices_aspect, commenter, commenter_aspect)
@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
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
before do let(:marshalled_comment) { Comment.from_xml(xml) }
@marshalled_comment = Comment.from_xml(@xml)
it "marshals the author" do
expect(marshalled_comment.author).to eq(commenter.person)
end end
it 'marshals the author' do it "marshals the post" do
expect(@marshalled_comment.author).to eq(@commenter.person) expect(marshalled_comment.post).to eq(post)
end end
it 'marshals the post' do it "tries to fetch a missing parent" do
expect(@marshalled_comment.post).to eq(@post) guid = post.guid
end marshalled_comment
post.destroy
it 'tries to fetch a missing parent' do
guid = @post.guid
@post.destroy
expect_any_instance_of(Comment).to receive(:fetch_parent).with(guid).and_return(nil) 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 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 before do
@local_luke, @local_leia, @remote_raphael = set_up_friends # shared_behaviors/relayable.rb is still using instance variables, so we need to define them here.
@remote_parent = FactoryGirl.build(:status_message, :author => @remote_raphael) # Suggestion: refactor all specs using shared_behaviors/relayable.rb to use "let"
@local_parent = @local_luke.post :status_message, :text => "hi", :to => @local_luke.aspects.first @object_by_parent_author = object_by_parent_author
@object_by_recipient = object_by_recipient
@object_by_parent_author = @local_luke.comment!(@local_parent, "yo") @dup_object_by_parent_author = dup_object_by_parent_author
@object_by_recipient = @local_leia.build_comment(:text => "yo", :post => @local_parent) @object_on_remote_parent = object_on_remote_parent
@dup_object_by_parent_author = @object_by_parent_author.dup
@object_on_remote_parent = @local_luke.comment!(@remote_parent, "Yeah, it was great")
end end
let(:build_object) { alice.build_comment(:post => @status, :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) }
before do 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 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,10 +34,10 @@ 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)
expect(contact2).to be_valid expect(contact2).to be_valid
contact.user = alice contact.user = alice
@ -47,54 +47,53 @@ describe Contact, :type => :model do
it "validates that the person's account is not closed" do it "validates that the person's account is not closed" do
person = FactoryGirl.create(:person, :closed_account => true) 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).not_to be_valid
expect(contact.errors.full_messages).to include "Cannot be in contact with a closed account" expect(contact.errors.full_messages).to include "Cannot be in contact with a closed account"
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))
}.to change{ }.to change{
Contact.sharing.count Contact.sharing.count
}.by(1) }.by(1)
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))
}.to change{ }.to change{
Contact.receiving.count Contact.receiving.count
}.by(1) }.by(1)
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))
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: false, person: FactoryGirl.build(:person))
}.to change{ }.to change{
Contact.receiving.count Contact.receiving.count
}.by(2) }.by(2)
end end
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)
contacts = Contact.all_contacts_of_person(person) contacts = Contact.all_contacts_of_person(person)
expect(contacts).to eq([contact1]) expect(contacts).to eq([contact1])
@ -102,118 +101,107 @@ describe Contact, :type => :model do
end end
end end
describe '#contacts' do describe "#contacts" do
before do before do
@alice = alice bob.aspects.create(name: "next")
@bob = bob bob.aspects(true)
@eve = eve
@bob.aspects.create(:name => 'next')
@bob.aspects(true)
@original_aspect = @bob.aspects.where(:name => "generic").first @original_aspect = bob.aspects.where(name: "generic").first
@new_aspect = @bob.aspects.where(:name => "next").first @new_aspect = bob.aspects.where(name: "next").first
@people1 = [] @people1 = []
@people2 = [] @people2 = []
1.upto(5) do 1.upto(5) do
person = FactoryGirl.build(:person) person = FactoryGirl.build(:person)
@bob.contacts.create(:person => person, :aspects => [@original_aspect]) bob.contacts.create(person: person, aspects: [@original_aspect])
@people1 << person @people1 << person
end end
1.upto(5) do 1.upto(5) do
person = FactoryGirl.build(:person) person = FactoryGirl.build(:person)
@bob.contacts.create(:person => person, :aspects => [@new_aspect]) bob.contacts.create(person: person, aspects: [@new_aspect])
@people2 << person @people2 << person
end end
#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
@contact = @alice.contact_for(@bob.person) @contact = alice.contact_for(bob.person)
end end
it "returns the target local user's contacts that are in the same aspect" do 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 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{|p| p.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
before do let(:contact) { bob.contact_for @people1.first }
@contact = @bob.contact_for @people1.first
end 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
before do let(:contact) { Contact.new user: user, person: person }
@contact = Contact.new let(:user) { build(:user) }
@user = FactoryGirl.build(:user) let(:person) { build(:person) }
@person = FactoryGirl.build(:person)
@contact.user = @user describe "#generate_request" do
@contact.person = @person it "makes a request" do
end allow(contact).to receive(:user).and_return(user)
request = contact.generate_request
describe '#generate_request' do expect(request.sender).to eq(user.person)
it 'makes a request' do expect(request.recipient).to eq(person)
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)
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)
expect(Postzord::Dispatcher).to receive(:build).and_return(m) expect(Postzord::Dispatcher).to receive(:build).and_return(m)
@contact.dispatch_request contact.dispatch_request
end end
end end
end end
describe "#not_blocked_user" do describe "#not_blocked_user" do
before do let(:contact) { alice.contact_for(bob.person) }
@contact = alice.contact_for(bob.person)
end
it "is called on validate" do it "is called on validate" do
expect(@contact).to receive(:not_blocked_user) expect(contact).to receive(:not_blocked_user)
@contact.valid? contact.valid?
end end
it "adds to errors if potential contact is blocked by user" do it "adds to errors if potential contact is blocked by user" do
person = eve.person person = eve.person
block = alice.blocks.create(:person => person) alice.blocks.create(person: person)
bad_contact = alice.contacts.create(:person => person) bad_contact = alice.contacts.create(person: person)
expect(bad_contact.send(:not_blocked_user)).to be false expect(bad_contact.send(:not_blocked_user)).to be false
end end
it "does not add to errors" do 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 end
end end

View file

@ -2,172 +2,160 @@
# 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
before do let(:user1) { alice }
@user1 = alice let(:user2) { bob }
@user2 = bob let(:participant_ids) { [user1.contacts.first.person.id, user1.person.id] }
@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 = { it "creates a message on create" do
:author => @user1.person, expect { conversation }.to change(Message, :count).by(1)
:participant_ids => @participant_ids,
:subject => "cool stuff",
:messages_attributes => [ {:author => @user1.person, :text => 'hey'} ]
}
end end
it 'creates a message on create' do describe "#last_author" do
expect{ it "returns the last author to a conversation" do
Conversation.create(@create_hash) message_last
}.to change(Message, :count).by(1) expect(conversation.reload.last_author.id).to eq(user2.person.id)
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)
end end
end end
describe "#ordered_participants" do describe "#ordered_participants" do
it "returns the ordered participants" do it "returns the ordered participants" do
cnv = Conversation.create(@create_hash) message_last
Message.create(author: @user2.person, created_at: Time.now + 100, text: "last", conversation_id: cnv.id) expect(conversation.ordered_participants.first).to eq(user2.person)
expect(cnv.ordered_participants.first).to eq(@user2.person) expect(conversation.ordered_participants.last).to eq(user1.person)
expect(cnv.ordered_participants.last).to eq(@user1.person)
end end
end end
describe '#first_unread_message' do describe "#first_unread_message" do
before do before do
@cnv = Conversation.create(@create_hash) message_last.increase_unread(user1)
@message = Message.create(:author => @user2.person, :created_at => Time.now + 100, :text => "last", :conversation_id => @cnv.id)
@message.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
@cnv.first_unread_message(@user1) == @message 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
@cnv.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(@cnv.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
@cnv = Conversation.create(@create_hash) conversation
Message.create(:author => @user2.person, :created_at => Time.now + 100, :text => "first", :conversation_id => @cnv.id) message_first.increase_unread(user1)
.increase_unread(@user1) message_last.increase_unread(user1)
Message.create(:author => @user2.person, :created_at => Time.now + 200, :text => "last", :conversation_id => @cnv.id)
.increase_unread(@user1)
end end
it 'sets the unread counter to 0' do it "sets the unread counter to 0" do
expect(@cnv.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)
@cnv.set_read(@user1) conversation.set_read(user1)
expect(@cnv.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(:xml) { conversation.to_diaspora_xml }
before do before do
@cnv = Conversation.create(@create_hash) conversation
@message = @cnv.messages.first
@xml = @cnv.to_diaspora_xml
end end
describe 'serialization' do describe "serialization" do
it 'serializes the message' do it "serializes the message" do
expect(@xml.gsub(/\s/, '')).to include(@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{|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
it 'serializes the created_at time' do it "serializes the created_at time" do
expect(@xml).to include(@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(@cnv.subscribers(@user1)).to eq(@user1.contacts.map{|c| c.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
end end
end end
describe "#invalid parameters" do describe "#invalid parameters" do
context "local author" do context "local author" do
before do let(:invalid_hash) {
@invalid_hash = { {author: peter.person, participant_ids: [peter.person.id, user1.person.id],
author: peter.person, subject: "cool stuff", messages_attributes: [{author: peter.person, text: "hey"}]}
participant_ids: [peter.person.id, @user1.person.id], }
subject: "cool stuff",
messages_attributes: [{author: peter.person, text: "hey"}]
}
end
it "is invalid with invalid recipient" do it "is invalid with invalid recipient" do
conversation = Conversation.create(@invalid_hash) invalid_conversation = Conversation.create(invalid_hash)
expect(conversation).to be_invalid expect(invalid_conversation).to be_invalid
end end
end end
context "remote author" do context "remote author" do
before do let(:remote_person) { remote_raphael }
@remote_person = remote_raphael let(:local_user) { alice }
@local_user = alice let(:participant_ids) { [remote_person.id, local_user.person.id] }
@participant_ids = [@remote_person.id, @local_user.person.id] let(:invalid_hash_remote) {
{author: remote_person, participant_ids: participant_ids,
@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"}]
}
end
it "is invalid with invalid recipient" do it "is invalid with invalid recipient" do
conversation = Conversation.create(@invalid_hash_remote) invalid_conversation_remote = Conversation.create(invalid_hash_remote)
expect(conversation).to be_invalid expect(invalid_conversation_remote).to be_invalid
end end
end end
end end

View file

@ -2,27 +2,20 @@
# 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
before do let(:user1) { alice }
@user1 = alice let(:participant_ids) { [user1.contacts.first.person.id, user1.person.id] }
@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 = { it "destroy conversation when no participant" do
:author => @user1.person, conversation.conversation_visibilities.each(&:destroy)
: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 expect(Conversation).not_to exist(conversation.id)
@conversation.conversation_visibilities.each do |visibility|
visibility.destroy
end
expect(Conversation).not_to exist(@conversation.id)
end end
end end