refactor even more tests
This commit is contained in:
parent
3ff33d355f
commit
5fba53a105
1 changed files with 51 additions and 74 deletions
|
|
@ -5,101 +5,91 @@
|
||||||
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"} ]} }
|
||||||
@create_hash = {
|
let(:conversation) { Conversation.create(create_hash) }
|
||||||
:author => @user1.person,
|
let(:message_last) { Message.create(author: user2.person, created_at: Time.now + 100, text: "last", conversation_id: conversation.id) }
|
||||||
:participant_ids => @participant_ids,
|
let(:message_first) { Message.create(author: user2.person, created_at: Time.now + 100, text: "first", conversation_id: conversation.id) }
|
||||||
:subject => "cool stuff",
|
|
||||||
:messages_attributes => [ {:author => @user1.person, :text => 'hey'} ]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'creates a message on create' do
|
it 'creates a message on create' do
|
||||||
expect{
|
expect{ conversation }.to change(Message, :count).by(1)
|
||||||
Conversation.create(@create_hash)
|
|
||||||
}.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
|
||||||
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.reload.last_author.id).to eq(user2.person.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{|id|
|
||||||
expect(@xml).to include(Person.find(id).diaspora_handle)
|
expect(xml).to include(Person.find(id).diaspora_handle)
|
||||||
}
|
}
|
||||||
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{|c| c.person})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -111,63 +101,50 @@ describe Conversation, :type => :model do
|
||||||
|
|
||||||
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) { {author: peter.person, participant_ids: [peter.person.id, user1.person.id],
|
||||||
@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"}]
|
|
||||||
}
|
|
||||||
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,
|
||||||
|
subject: "cool stuff", messages_attributes: [{author: remote_person, text: "hey"}]} }
|
||||||
@invalid_hash_remote = {
|
|
||||||
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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue