forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversation_batch_spec.rb
More file actions
126 lines (112 loc) · 5.26 KB
/
Copy pathconversation_batch_spec.rb
File metadata and controls
126 lines (112 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../sharding_spec_helper.rb')
describe ConversationBatch do
before :once do
student_in_course(:active_all => true)
@user1 = @user
@message = Conversation.build_message @user1, "hi all"
student_in_course(:active_all => true)
@user2 = @user
student_in_course(:active_all => true)
@user3 = @user
end
context "generate" do
it "should create an async batch" do
batch = ConversationBatch.generate(@message, [@user2, @user3], :async)
expect(batch).to be_created
expect(batch.completion).to be < 1
end
it "should create a sync batch and run it" do
start_count = ConversationMessage.count
batch = ConversationBatch.generate(@message, [@user2, @user3], :sync)
expect(batch).to be_sent
expect(batch.completion).to eql 1
expect(batch.root_conversation_message.reload.conversation).to be_nil
expect(ConversationMessage.count - start_count).to eql 3 # the root message, plus the ones to each recipient
expect(@user1.reload.unread_conversations_count).to eql 0
expect(@user1.all_conversations.size).to eql 2
expect(@user2.reload.unread_conversations_count).to eql 1
expect(@user3.reload.unread_conversations_count).to eql 1
end
end
context "deliver" do
it "should be sent to all recipients" do
start_count = ConversationMessage.count
batch = ConversationBatch.generate(@message, [@user2, @user3], :async)
batch.deliver
expect(batch).to be_sent
expect(batch.completion).to eql 1
expect(batch.root_conversation_message.reload.conversation).to be_nil
expect(ConversationMessage.count - start_count).to eql 3 # the root message, plus the ones to each recipient
expect(@user1.reload.unread_conversations_count).to eql 0
expect(@user1.all_conversations.size).to eql 2
expect(@user2.reload.unread_conversations_count).to eql 1
expect(@user3.reload.unread_conversations_count).to eql 1
end
it "should apply the tags to each conversation" do
start_count = ConversationMessage.count
g = @course.groups.create
g.users << @user1 << @user2
batch = ConversationBatch.generate(@message, [@user2, @user3], :async, :tags => [g.asset_string])
batch.deliver
expect(ConversationMessage.count - start_count).to eql 3 # the root message, plus the ones to each recipient
expect(@user1.reload.unread_conversations_count).to eql 0
expect(@user1.all_conversations.size).to eql 2
expect(@user2.reload.unread_conversations_count).to eql 1
expect(@user2.conversations.first.tags).to eql [g.asset_string]
expect(@user3.reload.unread_conversations_count).to eql 1
expect(@user3.conversations.first.tags).to eql [@course.asset_string] # not in group, so it falls back to common contexts
end
it "should copy the attachment(s) to each conversation" do
start_count = ConversationMessage.count
attachment = attachment_model(:context => @user1, :folder => @user1.conversation_attachments_folder)
@message = Conversation.build_message @user1, "hi all", :attachment_ids => [attachment.id]
batch = ConversationBatch.generate(@message, [@user2, @user3], :async)
batch.deliver
expect(ConversationMessage.count - start_count).to eql 3
ConversationMessage.all.each do |message|
expect(message.attachments).to eq @message.attachments
end
end
it "should send group messages" do
start_count = Conversation.count
batch = ConversationBatch.generate(@message, [@user, @user3], :async, group: true)
batch.deliver
expect(Conversation.count - start_count).to eq 2
Conversation.all.each { |c| expect(c).not_to be_private }
end
context "sharding" do
specs_require_sharding
it "should reuse existing private conversations" do
@shard1.activate { @user4 = user_factory }
conversation = @user1.initiate_conversation([@user4]).conversation
conversation.add_message(@user1, "hello")
batch = ConversationBatch.generate(@message, [@user3, @user4], :sync)
expect(batch).to be_sent
expect(batch.completion).to eql 1
expect(batch.root_conversation_message.reload.conversation).to be_nil
expect(ConversationMessage.count).to eql 4 # the root message, plus the ones to each recipient
expect(@user1.reload.unread_conversations_count).to eql 0
expect(@user1.all_conversations.size).to eql 2
expect(@user3.reload.unread_conversations_count).to eql 1
expect(@user4.reload.unread_conversations_count).to eql 1
end
end
end
end