forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversations_controller_spec.rb
More file actions
770 lines (646 loc) · 29.1 KB
/
Copy pathconversations_controller_spec.rb
File metadata and controls
770 lines (646 loc) · 29.1 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#
# 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')
describe ConversationsController do
def conversation(opts = {})
num_other_users = opts[:num_other_users] || 1
course = opts[:course] || @course
user_data = num_other_users.times.map{ {name: "User"} }
users = create_users_in_course(course, user_data, account_associations: true, return_type: :record)
@conversation = @user.initiate_conversation(users)
@conversation.add_message(opts[:message] || 'test')
@conversation.conversation.update_attribute(:context, course)
@conversation
end
# in Rails 5, `get`, `post`, etc. are changing to kwargs. we don't have to do that right away,
# but having a param named `body` triggers Rails 5 to treat it as a kwarg request, ignoring all
# of the params we want to send to the controller
def wrap_params(params)
CANVAS_RAILS4_2 ? params : { params: params }
end
describe "GET 'index'" do
before :once do
course_with_student(:active_all => true)
end
it "should require login" do
get 'index'
assert_require_login
end
it "should assign variables" do
user_session(@student)
conversation
term = @course.root_account.enrollment_terms.create! :name => "Fall"
@course.update_attributes! :enrollment_term => term
get 'index'
expect(response).to be_success
expect(assigns[:js_env]).not_to be_nil
end
it "should assign variables for json" do
user_session(@student)
conversation
get 'index', :format => 'json'
expect(response).to be_success
expect(assigns[:js_env]).to be_nil
expect(assigns[:conversations_json].map{|c|c[:id]}).to eq @user.conversations.map(&:conversation_id)
end
it "should work for an admin as well" do
account_admin_user
user_session(@user)
conversation
get 'index', :format => 'json'
expect(response).to be_success
expect(assigns[:conversations_json].map{|c|c[:id]}).to eq @user.conversations.map(&:conversation_id)
end
it "should return all sent conversations" do
user_session(@student)
@c1 = conversation
@c2 = conversation
@c3 = conversation
@c3.update_attribute :workflow_state, 'archived'
get 'index', :scope => 'sent', :format => 'json'
expect(response).to be_success
expect(assigns[:conversations_json].size).to eql 3
end
it "should return conversations matching the specified filter" do
user_session(@student)
@c1 = conversation
@other_course = course_factory(active_all: true)
enrollment = @other_course.enroll_student(@user)
enrollment.workflow_state = 'active'
enrollment.save!
@user.reload
@c2 = conversation(:num_other_users => 1, :course => @other_course)
get 'index', :filter => @other_course.asset_string, :format => 'json'
expect(response).to be_success
expect(assigns[:conversations_json].size).to eql 1
expect(assigns[:conversations_json][0][:id]).to eq @c2.conversation_id
end
it "should use the boolean operation in filter_mode when combining multiple filters" do
user_session(@student)
@course1 = @course
@c1 = conversation(:course => @course1)
@course2 = course_factory(active_all: true)
enrollment = @course2.enroll_student(@user)
enrollment.workflow_state = 'active'
enrollment.save!
@c2 = conversation(:course => @course2)
@c3 = conversation(:course => @course2)
get 'index', :filter => [@course1.asset_string, @course2.asset_string], :filter_mode => 'or', :format => 'json'
expect(response).to be_success
expect(assigns[:conversations_json].map{|c| c[:id]}.sort).to eql [@c1, @c2, @c3].map(&:conversation_id).sort
get 'index', :filter => [@course2.asset_string, @user.asset_string], :filter_mode => 'or', :format => 'json'
expect(response).to be_success
expect(assigns[:conversations_json].map{|c| c[:id]}.sort).to eql [@c1, @c2, @c3].map(&:conversation_id).sort
get 'index', :filter => [@course2.asset_string, @user.asset_string], :filter_mode => 'and', :format => 'json'
expect(response).to be_success
expect(assigns[:conversations_json].map{|c| c[:id]}.sort).to eql [@c2, @c3].map(&:conversation_id).sort
get 'index', :filter => [@course1.asset_string, @course2.asset_string], :filter_mode => 'and', :format => 'json'
expect(response).to be_success
expect(assigns[:conversations_json]).to eql []
end
it "should return conversations matching a user filter" do
user_session(@student)
@c1 = conversation
@other_course = course_factory(active_all: true)
enrollment = @other_course.enroll_student(@user)
enrollment.workflow_state = 'active'
enrollment.save!
@user.reload
@c2 = conversation(:num_other_users => 1, :course => @other_course)
get 'index', :filter => @user.asset_string, :format => 'json', :include_all_conversation_ids => 1
expect(response).to be_success
expect(assigns[:conversations_json].size).to eql 2
end
it "should not allow student view student to load inbox" do
course_with_teacher_logged_in(:active_all => true)
@fake_student = @course.student_view_student
session[:become_user_id] = @fake_student.id
get 'index'
assert_unauthorized
end
context "masquerading" do
before :once do
a = Account.default
@student = user_with_pseudonym(:active_all => true)
course_with_student(:active_all => true, :account => a, :user => @student)
@student.initiate_conversation([user_factory]).add_message('test1', :root_account_id => a.id)
@student.initiate_conversation([user_factory]).add_message('test2') # no root account, so teacher can't see it
course_with_teacher(:active_all => true, :account => a)
a.account_users.create!(user: @user)
end
before :each do
user_session(@teacher)
session[:become_user_id] = @student.id
end
it "should filter conversations" do
get 'index', :format => 'json'
expect(response).to be_success
expect(assigns[:conversations_json].size).to eql 1
end
it "should filter conversations when returning ids" do
get 'index', :format => 'json', :include_all_conversation_ids => true
expect(response).to be_success
expect(assigns[:conversations_json][:conversations].size).to eql 1
expect(assigns[:conversations_json][:conversation_ids].size).to eql 1
end
it "should recompute inbox count" do
# In an effort to make the data fix easy to do and self-healing,
# recompute the unread inbox count when the page is loaded.
course_with_student_logged_in(:active_all => true)
@user.update_attribute(:unread_conversations_count, -20) # create invalid starting value
@c1 = conversation
get 'index'
expect(response).to be_success
@user.reload
expect(@user.unread_conversations_count).to eq 0
end
end
end
describe "GET 'show'" do
before :once do
course_with_student(:active_all => true)
end
before :each do
user_session(@student)
conversation
end
it "should redirect if not xhr" do
get 'show', :id => @conversation.conversation_id
expect(response).to be_redirect
end
it "should assign variables" do
xhr :get, 'show', :id => @conversation.conversation_id
expect(response).to be_success
expect(assigns[:conversation]).to eq @conversation
end
end
describe "POST 'create'" do
before :once do
course_with_teacher(active_all: true)
student_in_course(active_all: true)
end
it "should create the conversation" do
user_session(@student)
new_user = User.create
enrollment = @course.enroll_student(new_user)
enrollment.workflow_state = 'active'
enrollment.save
post 'create', wrap_params(recipients: [new_user.id.to_s], body: "yo")
expect(response).to be_success
expect(assigns[:conversation]).not_to be_nil
end
it "should require permissions for sending to other students" do
user_session(@student)
new_user = User.create
enrollment = @course.enroll_student(new_user)
enrollment.workflow_state = 'active'
enrollment.save
@course.account.role_overrides.create!(:permission => :send_messages, :role => student_role, :enabled => false)
post 'create', wrap_params(recipients: [new_user.id.to_s], body: "yo", context_code: @course.asset_string)
expect(response).to_not be_success
end
it "should allow sending to instructors even if permissions are disabled" do
user_session(@student)
@course.account.role_overrides.create!(:permission => :send_messages, :role => student_role, :enabled => false)
post 'create', wrap_params(recipients: [@teacher.id.to_s], body: "yo", context_code: @course.asset_string)
expect(response).to be_success
expect(assigns[:conversation]).not_to be_nil
end
it "should not add the wrong tags in a certain terrible cached edge case" do
# tl;dr - not including the updated_at when we instantiate the users
# can cause us to grab stale conversation_context_codes
# which screws everything up
enable_cache do
course1 = course_factory(active_all: true)
student1 = user_factory(active_user: true)
student2 = user_factory(active_user: true)
Timecop.freeze(5.seconds.ago) do
course1.enroll_user(student1, "StudentEnrollment").accept!
course1.enroll_user(student2, "StudentEnrollment").accept!
user_session(student1)
post 'create', wrap_params(recipients: [student2.id.to_s], body: "yo", message: "you suck", group_conversation: true,
course: course1.asset_string, context_code: course1.asset_string)
expect(response).to be_success
end
course2 = course_factory(active_all: true)
course2.enroll_user(student2, "StudentEnrollment").accept!
course2.enroll_user(student1, "StudentEnrollment").accept!
user_session(User.find(student1.id)) # clear process local enrollment cache
# with the address book, there's another level of caching to bust. in
# non-test usage, this cache only lasts for the duration of the
# request, so it's not an issue
RequestStore.clear!
post 'create', wrap_params(recipients: [student2.id.to_s], body: "yo again", message: "you still suck", group_conversation: true,
course: course2.asset_string, context_code: course2.asset_string)
expect(response).to be_success
c = Conversation.where(:context_type => "Course", :context_id => course2).first
c.conversation_participants.each do |cp|
expect(cp.tags).to eq [course2.asset_string]
end
end
end
it "should allow messages to be forwarded from the conversation" do
user_session(@student)
conversation.update_attribute(:workflow_state, "unread")
new_user = User.create
enrollment = @course.enroll_student(new_user)
enrollment.workflow_state = 'active'
enrollment.save
post 'create', wrap_params(recipients: [new_user.id.to_s], body: "here's the info", forwarded_message_ids: @conversation.messages.map(&:id))
expect(response).to be_success
expect(assigns[:conversation]).not_to be_nil
expect(assigns[:conversation].messages.first.forwarded_message_ids).to eql(@conversation.messages.first.id.to_s)
end
context "group conversations" do
before :once do
@old_count = Conversation.count
@new_user1 = User.create
@course.enroll_student(@new_user1).accept!
@new_user2 = User.create
@course.enroll_student(@new_user2).accept!
@account_id = @course.account_id
end
before :each do
user_session(@teacher)
end
["1", "true", "yes", "on"].each do |truish|
it "should create a conversation shared by all recipients if group_conversation=#{truish.inspect}" do
post 'create', wrap_params(recipients: [@new_user1.id.to_s, @new_user2.id.to_s], body: "yo", group_conversation: truish)
expect(response).to be_success
expect(Conversation.count).to eql(@old_count + 1)
end
end
[nil, "", "0", "false", "no", "off", "wat"].each do |falsish|
it "should create one conversation per recipient if group_conversation=#{falsish.inspect}" do
post 'create', wrap_params(recipients: [@new_user1.id.to_s, @new_user2.id.to_s], body: "yo", group_conversation: falsish)
expect(response).to be_success
expect(Conversation.count).to eql(@old_count + 2)
end
end
it "should set the root account id to the participants for group conversations" do
post 'create', wrap_params(recipients: [@new_user1.id.to_s, @new_user2.id.to_s], body: "yo", group_conversation: "true")
expect(response).to be_success
json = json_parse(response.body)
json.each do |conv|
conversation = Conversation.find(conv['id'])
conversation.conversation_participants.each do |cp|
expect(cp.root_account_ids).to eq @account_id.to_s
end
end
end
it "should set the root account id to the participants for bulk private messages" do
post 'create', wrap_params(recipients: [@new_user1.id.to_s, @new_user2.id.to_s], body: "yo", mode: "sync")
expect(response).to be_success
json = json_parse(response.body)
json.each do |conv|
conversation = Conversation.find(conv['id'])
conversation.conversation_participants.each do |cp|
expect(cp.root_account_ids).to eq @account_id.to_s
end
end
end
end
it "should correctly infer context tags" do
course_with_teacher_logged_in(:active_all => true)
@course1 = @course
@course2 = course_factory(active_all: true)
@course2.enroll_teacher(@user).accept
@course3 = course_factory(active_all: true)
@course3.enroll_student(@user)
@group1 = @course1.groups.create!
@group2 = @course1.groups.create!
@group3 = @course3.groups.create!
@group1.users << @user
@group2.users << @user
@group3.users << @user
new_user1 = User.create
enrollment1 = @course1.enroll_student(new_user1)
enrollment1.workflow_state = 'active'
enrollment1.save
@group1.users << new_user1
@group2.users << new_user1
new_user2 = User.create
enrollment2 = @course1.enroll_student(new_user2)
enrollment2.workflow_state = 'active'
enrollment2.save
@group1.users << new_user2
@group2.users << new_user2
new_user3 = User.create
enrollment3 = @course2.enroll_student(new_user3)
enrollment3.workflow_state = 'active'
enrollment3.save
post 'create', wrap_params(recipients: [@course2.asset_string + "_students", @group1.asset_string],
body: "yo", group_conversation: true, context_code: @group3.asset_string)
expect(response).to be_success
c = Conversation.first
expect(c.tags.sort).to eql [@course1.asset_string, @course2.asset_string, @group1.asset_string, @course3.asset_string, @group3.asset_string].sort
# course1 inferred from group1, course2 inferred from synthetic context,
# group1 explicit, group2 not present (even though it's shared by everyone)
# group3 from context_code, course3 inferred from group3
end
it "should populate subject" do
user_session(@student)
new_user = User.create
enrollment = @course.enroll_student(new_user)
enrollment.workflow_state = 'active'
enrollment.save
post 'create', wrap_params(recipients: [new_user.id.to_s], body: "yo", subject: "greetings")
expect(response).to be_success
expect(assigns[:conversation].conversation.subject).not_to be_nil
end
it "should populate subject on batch conversations" do
user_session(@student)
new_user1 = User.create
enrollment1 = @course.enroll_student(new_user1)
enrollment1.workflow_state = 'active'
enrollment1.save
new_user2 = User.create
enrollment2 = @course.enroll_student(new_user2)
enrollment2.workflow_state = 'active'
enrollment2.save
post 'create', wrap_params(recipients: [new_user1.id.to_s, new_user2.id.to_s], body: "later", subject: "farewell")
expect(response).to be_success
json = json_parse(response.body)
expect(json.size).to eql 2
json.each { |c|
expect(c["subject"]).not_to be_nil
}
end
context "user_notes" do
before :each do
Account.default.update_attribute :enable_user_notes, true
user_session(@teacher)
@students = create_users_in_course(@course, 2, account_associations: true, return_type: :record)
end
it "should create user notes" do
post 'create', wrap_params(recipients: @students.map(&:id), body: "yo", subject: "greetings", user_note: '1')
@students.each{|x| expect(x.user_notes.size).to be(1)}
end
end
end
describe "POST 'update'" do
it "should update the conversation" do
course_with_student_logged_in(:active_all => true)
conversation(:num_other_users => 2).update_attribute(:workflow_state, "unread")
post 'update', :id => @conversation.conversation_id, :conversation => {:subscribed => "0", :workflow_state => "archived", :starred => "1"}
expect(response).to be_success
@conversation.reload
expect(@conversation.subscribed?).to be_falsey
expect(@conversation).to be_archived
expect(@conversation.starred).to be_truthy
end
end
describe "POST 'add_message'" do
it "should add a message" do
course_with_student_logged_in(:active_all => true)
conversation
expected_lma = Time.zone.parse('2012-12-21T12:42:00Z')
@conversation.last_message_at = expected_lma
@conversation.save!
post 'add_message', wrap_params(conversation_id: @conversation.conversation_id, body: "hello world")
expect(response).to be_success
expect(@conversation.messages.size).to eq 2
expect(@conversation.reload.last_message_at).to eql expected_lma
end
it "should require permissions" do
course_with_student_logged_in(:active_all => true)
conversation
@course.account.role_overrides.create!(:permission => :send_messages, :role => student_role, :enabled => false)
post 'add_message', wrap_params(conversation_id: @conversation.conversation_id, body: "hello world")
assert_unauthorized
end
it "should queue a job if needed" do
course_with_student_logged_in(:active_all => true)
conversation
expected_lma = Time.zone.parse('2012-12-21T12:42:00Z')
@conversation.last_message_at = expected_lma
@conversation.save!
ConversationParticipant.any_instance.stubs(:should_process_immediately?).returns(false)
post 'add_message', wrap_params(conversation_id: @conversation.conversation_id, body: "hello world")
expect(response).to be_success
expect(@conversation.reload.messages.count(:all)).to eq 1
run_jobs
expect(@conversation.reload.messages.count(:all)).to eq 2
expect(@conversation.reload.last_message_at).to eql expected_lma
end
it "should generate a user note when requested" do
Account.default.update_attribute :enable_user_notes, true
course_with_teacher_logged_in(:active_all => true)
conversation
post 'add_message', wrap_params(conversation_id: @conversation.conversation_id, body: "hello world")
expect(response).to be_success
message = @conversation.messages.first # newest message is first
student = message.recipients.first
expect(student.user_notes.size).to eq 0
post 'add_message', wrap_params(conversation_id: @conversation.conversation_id, body: "make a note", user_note: 1)
expect(response).to be_success
message = @conversation.messages.first
student = message.recipients.first
expect(student.user_notes.size).to eq 1
end
end
describe "POST 'add_recipients'" do
before :once do
course_with_student(:active_all => true)
conversation(:num_other_users => 2)
end
before(:each) { user_session(@student) }
it "should add recipients" do
new_user = User.create
enrollment = @course.enroll_student(new_user)
enrollment.workflow_state = 'active'
enrollment.save
post 'add_recipients', :conversation_id => @conversation.conversation_id, :recipients => [new_user.id.to_s]
expect(response).to be_success
expect(@conversation.reload.participants.size).to eq 4 # includes @user
end
it "should correctly infer context tags" do
@group = @course.groups.create!
@conversation.participants.each{ |user| @group.users << user }
2.times{ @group.users << User.create }
post 'add_recipients', :conversation_id => @conversation.conversation_id, :recipients => [@group.asset_string]
expect(response).to be_success
c = Conversation.first
expect(c.tags.sort).to eql [@course.asset_string, @group.asset_string]
# course inferred (when created), group explicit
end
end
describe "POST 'remove_messages'" do
before(:once) { course_with_student(active_all: true) }
before(:each) { user_session(@student) }
it "should remove messages" do
message = conversation.add_message('another')
post 'remove_messages', :conversation_id => @conversation.conversation_id, :remove => [message.id.to_s]
expect(response).to be_success
expect(@conversation.messages.size).to eq 1
end
it "should null a conversation_participant's last_message_at if all message_participants have been destroyed" do
message = conversation.conversation.conversation_messages.first
post 'remove_messages', conversation_id: @conversation.conversation_id, :remove => [message.id.to_s]
expect(@conversation.reload.last_message_at).to be_nil
end
end
describe "DELETE 'destroy'" do
it "should delete conversations" do
course_with_student_logged_in(:active_all => true)
conversation
delete 'destroy', :id => @conversation.conversation_id
expect(response).to be_success
expect(@user.conversations).to be_blank # the conversation_participant is no longer there
expect(@conversation.conversation).not_to be_nil # though the conversation is
end
end
describe "GET 'public_feed.atom'" do
before :once do
course_with_student(:course_name => "Message Course", :active_all => true)
end
it "should require authorization" do
conversation
get 'public_feed', :format => 'atom', :feed_code => @student.feed_code + "x"
expect(assigns[:problem]).to eql("The verification code is invalid.")
end
it "should return basic feed attributes" do
conversation
get 'public_feed', :format => 'atom', :feed_code => @student.feed_code
feed = Atom::Feed.load_feed(response.body) rescue nil
expect(feed).not_to be_nil
expect(feed.title).to eq "Conversations Feed"
expect(feed.links.first.href).to match(/conversations/)
end
it "should include message entries" do
conversation
get 'public_feed', :format => 'atom', :feed_code => @student.feed_code
expect(assigns[:entries].length).to eq 1
expect(response).to be_success
end
it "should not include messages the user is not a part of" do
conversation
student_in_course
get 'public_feed', :format => 'atom', :feed_code => @student.feed_code
expect(assigns[:entries]).to be_empty
end
it "should include part the message text in the title" do
message = "Sending a test message to some random users, in the hopes that it really works."
conversation(:message => message)
get 'public_feed', :format => 'atom', :feed_code => @student.feed_code
feed = Atom::Feed.load_feed(response.body) rescue nil
expect(feed).not_to be_nil
expect(feed.entries.first.title).to match(/Sending a test/)
expect(feed.entries.first.title).not_to match(message)
end
it "should include the message in the content" do
message = "Sending a test message to some random users, in the hopes that it really works."
conversation(:message => message)
get 'public_feed', :format => 'atom', :feed_code => @student.feed_code
feed = Atom::Feed.load_feed(response.body) rescue nil
expect(feed).not_to be_nil
expect(feed.entries.first.content).to match(message)
end
it "should include context about the conversation" do
message = "Sending a test message to some random users, in the hopes that it really works."
conversation(:num_other_users => 4, :message => message)
get 'public_feed', :format => 'atom', :feed_code => @student.feed_code
feed = Atom::Feed.load_feed(response.body) rescue nil
expect(feed).not_to be_nil
expect(feed.entries.first.content).to match(/Message Course/)
expect(feed.entries.first.content).to match(/User/)
expect(feed.entries.first.content).to match(/others/)
end
it "should include an attachment if one exists" do
conversation
attachment = @user.conversation_attachments_folder.attachments.create!(:filename => "somefile.doc", :context => @user, :uploaded_data => StringIO.new('test'))
@conversation.add_message('test attachment', :attachment_ids => [attachment.id])
HostUrl.stubs(:context_host).returns("test.host")
get 'public_feed', :format => 'atom', :feed_code => @student.feed_code
feed = Atom::Feed.load_feed(response.body) rescue nil
expect(feed).not_to be_nil
expect(feed.entries.first.content).to match(/somefile\.doc/)
end
end
describe "POST 'toggle_new_conversations'" do
before :each do
course_with_student_logged_in(:active_all => true)
end
it "should not disable new conversations for a user anymore" do
post 'toggle_new_conversations'
@user.reload
expect(@user.use_new_conversations?).to be_truthy
end
end
context "sharding" do
specs_require_sharding
describe 'index' do
it "should list conversation_ids across shards" do
users = []
# Create three users on different shards
users << user_factory(:name => 'a')
@shard1.activate { users << user_factory(:name => 'b') }
@shard2.activate { users << user_factory(:name => 'c') }
Shard.default.activate do
# Default shard conversation
conversation = Conversation.initiate(users, false)
users.each do |user|
conversation.add_message(user, "user '#{user.name}' says HI")
end
end
@shard2.activate do
# Create logged in user
@logged_in_user = users.last
course_with_student_logged_in(:user => @logged_in_user, :active_all => true)
# Shard 2 conversation
conversation = Conversation.initiate(users, false)
users.each do |user|
conversation.add_message(user, "user '#{user.name}' says HI")
end
end
get 'index', :include_all_conversation_ids => true, :format => 'json'
expect(response).to be_success
expect(assigns[:js_env]).to be_nil
# Should assign :conversations and :conversation_ids in json result
json = assigns[:conversations_json][:conversations]
ids = assigns[:conversations_json][:conversation_ids]
# IDs should match in returned lists
expect(ids.sort).to eq json.map{|c| c[:id]}.sort
# IDs returned should match IDs for user's conversations
expect(ids.sort).to eq @logged_in_user.conversations.map(&:conversation_id).sort
# Expect 2 elements in both groups
expect(json.length).to eq 2
expect(ids.length).to eq 2
end
end
describe "show" do
it "should find conversations across shards" do
users = []
users << user_factory(:name => 'a')
@shard1.activate { users << user_factory(:name => 'b') }
@shard1.activate do
@conversation = Conversation.initiate(users, false)
users.each do |user|
@conversation.add_message(user, "user '#{user.name}' says HI")
end
end
expect(@conversation.shard).to eq @shard1
users.each do |user|
user_session(user) # should work for both users
get 'show', :id => @conversation.global_id, :format => 'json'
expect(response).to be_success
end
end
end
end
end