4 class MessagesControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/user/messages/inbox", :method => :get },
10 { :controller => "api/messages", :action => "inbox" }
13 { :path => "/api/0.6/user/messages/inbox.xml", :method => :get },
14 { :controller => "api/messages", :action => "inbox", :format => "xml" }
17 { :path => "/api/0.6/user/messages/inbox.json", :method => :get },
18 { :controller => "api/messages", :action => "inbox", :format => "json" }
21 { :path => "/api/0.6/user/messages/outbox", :method => :get },
22 { :controller => "api/messages", :action => "outbox" }
25 { :path => "/api/0.6/user/messages/outbox.xml", :method => :get },
26 { :controller => "api/messages", :action => "outbox", :format => "xml" }
29 { :path => "/api/0.6/user/messages/outbox.json", :method => :get },
30 { :controller => "api/messages", :action => "outbox", :format => "json" }
33 { :path => "/api/0.6/user/messages/1", :method => :get },
34 { :controller => "api/messages", :action => "show", :id => "1" }
37 { :path => "/api/0.6/user/messages/1.xml", :method => :get },
38 { :controller => "api/messages", :action => "show", :id => "1", :format => "xml" }
41 { :path => "/api/0.6/user/messages/1.json", :method => :get },
42 { :controller => "api/messages", :action => "show", :id => "1", :format => "json" }
45 { :path => "/api/0.6/user/messages", :method => :post },
46 { :controller => "api/messages", :action => "create" }
49 { :path => "/api/0.6/user/messages/1", :method => :post },
50 { :controller => "api/messages", :action => "update", :id => "1" }
53 { :path => "/api/0.6/user/messages/1", :method => :delete },
54 { :controller => "api/messages", :action => "destroy", :id => "1" }
58 def test_create_success
59 recipient = create(:user)
60 sender = create(:user)
62 sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
66 assert_difference "Message.count", 1 do
67 assert_difference "ActionMailer::Base.deliveries.size", 1 do
68 perform_enqueued_jobs do
69 post api_messages_path,
70 :params => { :title => msg.title,
71 :recipient_id => recipient.id,
74 :headers => sender_auth
75 assert_response :success
80 assert_equal "application/json", response.media_type
81 js = ActiveSupport::JSON.decode(@response.body)
84 assert_not_nil jsm["id"]
85 assert_equal sender.id, jsm["from_user_id"]
86 assert_equal sender.display_name, jsm["from_display_name"]
87 assert_equal recipient.id, jsm["to_user_id"]
88 assert_equal recipient.display_name, jsm["to_display_name"]
89 assert_equal msg.title, jsm["title"]
90 assert_not_nil jsm["sent_on"]
91 assert_equal !msg.from_user_visible, jsm["deleted"]
92 assert_not jsm.key?("message_read")
93 assert_equal "markdown", jsm["body_format"]
94 assert_equal msg.body, jsm["body"]
98 recipient = create(:user)
100 sender = create(:user)
101 sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
103 assert_no_difference "Message.count" do
104 assert_no_difference "ActionMailer::Base.deliveries.size" do
105 perform_enqueued_jobs do
106 post api_messages_path,
107 :params => { :title => "Title",
108 :recipient_id => recipient.id,
113 assert_response :unauthorized
115 assert_no_difference "Message.count" do
116 assert_no_difference "ActionMailer::Base.deliveries.size" do
117 perform_enqueued_jobs do
118 post api_messages_path,
119 :params => { :recipient_id => recipient.id,
121 :headers => sender_auth
125 assert_response :bad_request
127 assert_no_difference "Message.count" do
128 assert_no_difference "ActionMailer::Base.deliveries.size" do
129 perform_enqueued_jobs do
130 post api_messages_path,
131 :params => { :title => "Title",
133 :headers => sender_auth
137 assert_response :bad_request
139 assert_no_difference "Message.count" do
140 assert_no_difference "ActionMailer::Base.deliveries.size" do
141 perform_enqueued_jobs do
142 post api_messages_path,
143 :params => { :title => "Title",
144 :recipient_id => recipient.id },
145 :headers => sender_auth
149 assert_response :bad_request
153 recipient = create(:user)
154 sender = create(:user)
155 user3 = create(:user)
157 sender_auth = bearer_authorization_header(sender, :scopes => %w[consume_messages])
158 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
159 user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
161 msg = create(:message, :unread, :sender => sender, :recipient => recipient)
163 # fail if not authorized
164 get api_message_path(:id => msg.id)
165 assert_response :unauthorized
167 # only recipient and sender can read the message
168 get api_message_path(:id => msg.id), :headers => user3_auth
169 assert_response :forbidden
171 # message does not exist
172 get api_message_path(:id => 99999), :headers => user3_auth
173 assert_response :not_found
176 get api_message_path(:id => msg.id), :headers => recipient_auth
177 assert_equal "application/xml", response.media_type
178 assert_select "message", :count => 1 do
179 assert_select "[id='#{msg.id}']"
180 assert_select "[from_user_id='#{sender.id}']"
181 assert_select "[from_display_name='#{sender.display_name}']"
182 assert_select "[to_user_id='#{recipient.id}']"
183 assert_select "[to_display_name='#{recipient.display_name}']"
184 assert_select "[sent_on]"
185 assert_select "[deleted='#{!msg.to_user_visible}']"
186 assert_select "[message_read='#{msg.message_read}']"
187 assert_select "[body_format='markdown']"
188 assert_select "title", msg.title
189 assert_select "body", msg.body
193 get api_message_path(:id => msg.id, :format => "json"), :headers => recipient_auth
194 assert_equal "application/json", response.media_type
195 js = ActiveSupport::JSON.decode(@response.body)
198 assert_equal msg.id, jsm["id"]
199 assert_equal sender.id, jsm["from_user_id"]
200 assert_equal sender.display_name, jsm["from_display_name"]
201 assert_equal recipient.id, jsm["to_user_id"]
202 assert_equal recipient.display_name, jsm["to_display_name"]
203 assert_equal msg.title, jsm["title"]
204 assert_not_nil jsm["sent_on"]
205 assert_equal msg.message_read, jsm["message_read"]
206 assert_equal !msg.to_user_visible, jsm["deleted"]
207 assert_equal "markdown", jsm["body_format"]
208 assert_equal msg.body, jsm["body"]
210 get api_message_path(:id => msg.id), :headers => sender_auth
211 assert_equal "application/xml", response.media_type
212 assert_select "message", :count => 1 do
213 assert_select "[id='#{msg.id}']"
214 assert_select "[from_user_id='#{sender.id}']"
215 assert_select "[from_display_name='#{sender.display_name}']"
216 assert_select "[to_user_id='#{recipient.id}']"
217 assert_select "[to_display_name='#{recipient.display_name}']"
218 assert_select "[sent_on]"
219 assert_select "[deleted='#{!msg.from_user_visible}']"
220 assert_select "[message_read='#{msg.message_read}']", 0
221 assert_select "[body_format='markdown']"
222 assert_select "title", msg.title
223 assert_select "body", msg.body
227 get api_message_path(:id => msg.id, :format => "json"), :headers => sender_auth
228 assert_equal "application/json", response.media_type
229 js = ActiveSupport::JSON.decode(@response.body)
232 assert_equal msg.id, jsm["id"]
233 assert_equal sender.id, jsm["from_user_id"]
234 assert_equal sender.display_name, jsm["from_display_name"]
235 assert_equal recipient.id, jsm["to_user_id"]
236 assert_equal recipient.display_name, jsm["to_display_name"]
237 assert_equal msg.title, jsm["title"]
238 assert_not_nil jsm["sent_on"]
239 assert_equal !msg.from_user_visible, jsm["deleted"]
240 assert_not jsm.key?("message_read")
241 assert_equal "markdown", jsm["body_format"]
242 assert_equal msg.body, jsm["body"]
245 def test_update_status
246 recipient = create(:user)
247 sender = create(:user)
248 user3 = create(:user)
250 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
251 user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
253 msg = create(:message, :unread, :sender => sender, :recipient => recipient)
255 # attempt to mark message as read by recipient, not authenticated
256 post api_message_path(:id => msg.id), :params => { :read_status => true }
257 assert_response :unauthorized
259 # attempt to mark message as read by recipient, not allowed
260 post api_message_path(:id => msg.id), :params => { :read_status => true }, :headers => user3_auth
261 assert_response :forbidden
264 post api_message_path(:id => msg.id), :headers => recipient_auth
265 assert_response :bad_request
267 # wrong type of parameter
268 post api_message_path(:id => msg.id),
269 :params => { :read_status => "not a boolean" },
270 :headers => recipient_auth
271 assert_response :bad_request
273 # mark message as read by recipient
274 post api_message_path(:id => msg.id, :format => "json"),
275 :params => { :read_status => true },
276 :headers => recipient_auth
277 assert_response :success
278 assert_equal "application/json", response.media_type
279 js = ActiveSupport::JSON.decode(@response.body)
282 assert_equal msg.id, jsm["id"]
283 assert_equal sender.id, jsm["from_user_id"]
284 assert_equal sender.display_name, jsm["from_display_name"]
285 assert_equal recipient.id, jsm["to_user_id"]
286 assert_equal recipient.display_name, jsm["to_display_name"]
287 assert_equal msg.title, jsm["title"]
288 assert_not_nil jsm["sent_on"]
289 assert jsm["message_read"]
290 assert_equal !msg.to_user_visible, jsm["deleted"]
291 assert_equal "markdown", jsm["body_format"]
292 assert_equal msg.body, jsm["body"]
294 # mark message as unread by recipient
295 post api_message_path(:id => msg.id, :format => "json"),
296 :params => { :read_status => false },
297 :headers => recipient_auth
298 assert_response :success
299 assert_equal "application/json", response.media_type
300 js = ActiveSupport::JSON.decode(@response.body)
303 assert_equal msg.id, jsm["id"]
304 assert_equal sender.id, jsm["from_user_id"]
305 assert_equal sender.display_name, jsm["from_display_name"]
306 assert_equal recipient.id, jsm["to_user_id"]
307 assert_equal recipient.display_name, jsm["to_display_name"]
308 assert_equal msg.title, jsm["title"]
309 assert_not_nil jsm["sent_on"]
310 assert_not jsm["message_read"]
311 assert_equal !msg.to_user_visible, jsm["deleted"]
312 assert_equal "markdown", jsm["body_format"]
313 assert_equal msg.body, jsm["body"]
317 recipient = create(:user)
318 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
320 sender = create(:user)
321 sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
323 user3 = create(:user)
324 user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
326 msg = create(:message, :read, :sender => sender, :recipient => recipient)
328 # attempt to delete message, not authenticated
329 delete api_message_path(:id => msg.id)
330 assert_response :unauthorized
332 # attempt to delete message, by user3
333 delete api_message_path(:id => msg.id), :headers => user3_auth
334 assert_response :forbidden
336 # delete message by recipient
337 delete api_message_path(:id => msg.id, :format => "json"), :headers => recipient_auth
338 assert_response :success
339 assert_equal "application/json", response.media_type
340 js = ActiveSupport::JSON.decode(@response.body)
343 assert_equal msg.id, jsm["id"]
344 assert_equal sender.id, jsm["from_user_id"]
345 assert_equal sender.display_name, jsm["from_display_name"]
346 assert_equal recipient.id, jsm["to_user_id"]
347 assert_equal recipient.display_name, jsm["to_display_name"]
348 assert_equal msg.title, jsm["title"]
349 assert_not_nil jsm["sent_on"]
350 assert_equal msg.message_read, jsm["message_read"]
351 assert jsm["deleted"]
352 assert_equal "markdown", jsm["body_format"]
353 assert_equal msg.body, jsm["body"]
355 # delete message by sender
356 delete api_message_path(:id => msg.id, :format => "json"), :headers => sender_auth
357 assert_response :success
358 assert_equal "application/json", response.media_type
359 js = ActiveSupport::JSON.decode(@response.body)
362 assert_equal msg.id, jsm["id"]
363 assert_equal sender.id, jsm["from_user_id"]
364 assert_equal sender.display_name, jsm["from_display_name"]
365 assert_equal recipient.id, jsm["to_user_id"]
366 assert_equal recipient.display_name, jsm["to_display_name"]
367 assert_equal msg.title, jsm["title"]
368 assert_not_nil jsm["sent_on"]
369 assert jsm["deleted"]
370 assert_not jsm.key?("message_read")
371 assert_equal "markdown", jsm["body_format"]
372 assert_equal msg.body, jsm["body"]
375 def test_list_messages
376 user1 = create(:user)
377 user1_auth = bearer_authorization_header(user1, :scopes => %w[send_messages consume_messages])
379 user2 = create(:user)
380 user2_auth = bearer_authorization_header(user2, :scopes => %w[send_messages consume_messages])
382 user3 = create(:user)
383 user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
385 # create some messages between users
386 # user | inbox | outbox
390 create(:message, :unread, :sender => user1, :recipient => user2)
391 create(:message, :unread, :sender => user1, :recipient => user2)
392 create(:message, :unread, :sender => user1, :recipient => user3)
393 create(:message, :unread, :sender => user2, :recipient => user3)
395 # only authorized users
396 get inbox_api_messages_path
397 assert_response :unauthorized
398 get outbox_api_messages_path
399 assert_response :unauthorized
401 # no messages in user1.inbox
402 get inbox_api_messages_path, :headers => user1_auth
403 assert_response :success
404 assert_equal "application/xml", response.media_type
405 assert_select "message", :count => 0
407 # 3 messages in user1.outbox
408 get outbox_api_messages_path, :headers => user1_auth
409 assert_response :success
410 assert_equal "application/xml", response.media_type
411 assert_select "message", :count => 3 do
412 assert_select "[from_user_id='#{user1.id}']"
413 assert_select "[from_display_name='#{user1.display_name}']"
414 assert_select "[to_user_id]"
415 assert_select "[to_display_name]"
416 assert_select "[sent_on]"
417 assert_select "[message_read]", 0
418 assert_select "[deleted='false']"
419 assert_select "[body_format]"
420 assert_select "body", false
421 assert_select "title"
424 # 2 messages in user2.inbox
425 get inbox_api_messages_path, :headers => user2_auth
426 assert_response :success
427 assert_equal "application/xml", response.media_type
428 assert_select "message", :count => 2 do
429 assert_select "[from_user_id]"
430 assert_select "[from_display_name]"
431 assert_select "[to_user_id='#{user2.id}']"
432 assert_select "[to_display_name='#{user2.display_name}']"
433 assert_select "[sent_on]"
434 assert_select "[message_read='false']"
435 assert_select "[deleted='false']"
436 assert_select "[body_format]"
437 assert_select "body", false
438 assert_select "title"
441 # 1 message in user2.outbox
442 get outbox_api_messages_path, :headers => user2_auth
443 assert_response :success
444 assert_equal "application/xml", response.media_type
445 assert_select "message", :count => 1 do
446 assert_select "[from_user_id='#{user2.id}']"
447 assert_select "[from_display_name='#{user2.display_name}']"
448 assert_select "[to_user_id]"
449 assert_select "[to_display_name]"
450 assert_select "[sent_on]"
451 assert_select "[deleted='false']"
452 assert_select "[message_read]", 0
453 assert_select "[body_format]"
454 assert_select "body", false
455 assert_select "title"
458 # 2 messages in user3.inbox
459 get inbox_api_messages_path, :headers => user3_auth
460 assert_response :success
461 assert_equal "application/xml", response.media_type
462 assert_select "message", :count => 2 do
463 assert_select "[from_user_id]"
464 assert_select "[from_display_name]"
465 assert_select "[to_user_id='#{user3.id}']"
466 assert_select "[to_display_name='#{user3.display_name}']"
467 assert_select "[sent_on]"
468 assert_select "[message_read='false']"
469 assert_select "[deleted='false']"
470 assert_select "[body_format]"
471 assert_select "body", false
472 assert_select "title"
475 # 0 messages in user3.outbox
476 get outbox_api_messages_path, :headers => user3_auth
477 assert_response :success
478 assert_equal "application/xml", response.media_type
479 assert_select "message", :count => 0
482 def test_paged_list_messages_asc
483 recipient = create(:user)
484 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
486 sender = create(:user)
488 create_list(:message, 100, :unread, :sender => sender, :recipient => recipient)
491 params = { :order => "oldest", :limit => 20 }
493 get inbox_api_messages_path(:format => "json"),
495 :headers => recipient_auth
496 assert_response :success
497 assert_equal "application/json", response.media_type
498 js = ActiveSupport::JSON.decode(@response.body)
500 assert_operator jsm.count, :<=, 20
502 break if jsm.nil? || jsm.count.zero?
504 assert_operator(jsm[0]["id"], :>=, params[:from_id]) unless params[:from_id].nil?
505 # ensure ascending order
506 (0..jsm.count - 1).each do |i|
507 assert_operator(jsm[i]["id"], :<, jsm[i + 1]["id"]) unless i == jsm.count - 1
508 msgs_read[jsm[i]["id"]] = jsm[i]
510 params[:from_id] = jsm[jsm.count - 1]["id"]
512 assert_equal 100, msgs_read.count
515 def test_paged_list_messages_desc
516 recipient = create(:user)
517 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
519 sender = create(:user)
521 create_list(:message, 100, :unread, :sender => sender, :recipient => recipient)
525 params = { :order => "newest", :limit => 20 }
527 get inbox_api_messages_path(:format => "json"),
529 :headers => recipient_auth
530 assert_response :success
531 assert_equal "application/json", response.media_type
532 js = ActiveSupport::JSON.decode(@response.body)
534 assert_operator jsm.count, :<=, 20
536 break if jsm.nil? || jsm.count.zero?
538 if params[:from_id].nil?
539 real_max_id = jsm[0]["id"]
541 assert_operator jsm[0]["id"], :<=, params[:from_id]
543 # ensure descending order
544 (0..jsm.count - 1).each do |i|
545 assert_operator(jsm[i]["id"], :>, jsm[i + 1]["id"]) unless i == jsm.count - 1
546 msgs_read[jsm[i]["id"]] = jsm[i]
548 params[:from_id] = jsm[jsm.count - 1]["id"]
550 assert_equal 100, msgs_read.count
551 assert_not_equal(-1, real_max_id)
553 # invoke without min_id/max_id parameters, verify that we get the last batch
554 get inbox_api_messages_path(:format => "json"), :params => { :limit => 20 }, :headers => recipient_auth
555 assert_response :success
556 assert_equal "application/json", response.media_type
557 js = ActiveSupport::JSON.decode(@response.body)
560 assert_equal real_max_id, jsm[0]["id"]