4 class MessagesControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/user/messages/1", :method => :get },
10 { :controller => "api/messages", :action => "show", :id => "1" }
13 { :path => "/api/0.6/user/messages/1.xml", :method => :get },
14 { :controller => "api/messages", :action => "show", :id => "1", :format => "xml" }
17 { :path => "/api/0.6/user/messages/1.json", :method => :get },
18 { :controller => "api/messages", :action => "show", :id => "1", :format => "json" }
21 { :path => "/api/0.6/user/messages", :method => :post },
22 { :controller => "api/messages", :action => "create" }
25 { :path => "/api/0.6/user/messages/1", :method => :put },
26 { :controller => "api/messages", :action => "update", :id => "1" }
29 { :controller => "api/messages", :action => "update", :id => "1" },
30 { :path => "/api/0.6/user/messages/1", :method => :post }
33 { :path => "/api/0.6/user/messages/1", :method => :delete },
34 { :controller => "api/messages", :action => "destroy", :id => "1" }
38 def test_create_success
39 recipient = create(:user)
40 sender = create(:user)
42 sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
46 assert_difference "Message.count", 1 do
47 assert_difference "ActionMailer::Base.deliveries.size", 1 do
48 perform_enqueued_jobs do
49 post api_messages_path,
50 :params => { :title => msg.title,
51 :recipient_id => recipient.id,
54 :headers => sender_auth
55 assert_response :success
60 assert_equal "application/json", response.media_type
61 js = ActiveSupport::JSON.decode(@response.body)
64 assert_not_nil jsm["id"]
65 assert_equal sender.id, jsm["from_user_id"]
66 assert_equal sender.display_name, jsm["from_display_name"]
67 assert_equal recipient.id, jsm["to_user_id"]
68 assert_equal recipient.display_name, jsm["to_display_name"]
69 assert_equal msg.title, jsm["title"]
70 assert_not_nil jsm["sent_on"]
71 assert_equal !msg.from_user_visible, jsm["deleted"]
72 assert_not jsm.key?("message_read")
73 assert_equal "markdown", jsm["body_format"]
74 assert_equal msg.body, jsm["body"]
78 recipient = create(:user)
80 sender = create(:user)
81 sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
83 assert_no_difference "Message.count" do
84 assert_no_difference "ActionMailer::Base.deliveries.size" do
85 perform_enqueued_jobs do
86 post api_messages_path,
87 :params => { :title => "Title",
88 :recipient_id => recipient.id,
93 assert_response :unauthorized
95 assert_no_difference "Message.count" do
96 assert_no_difference "ActionMailer::Base.deliveries.size" do
97 perform_enqueued_jobs do
98 post api_messages_path,
99 :params => { :recipient_id => recipient.id,
101 :headers => sender_auth
105 assert_response :bad_request
107 assert_no_difference "Message.count" do
108 assert_no_difference "ActionMailer::Base.deliveries.size" do
109 perform_enqueued_jobs do
110 post api_messages_path,
111 :params => { :title => "Title",
113 :headers => sender_auth
117 assert_response :bad_request
119 assert_no_difference "Message.count" do
120 assert_no_difference "ActionMailer::Base.deliveries.size" do
121 perform_enqueued_jobs do
122 post api_messages_path,
123 :params => { :title => "Title",
124 :recipient_id => recipient.id },
125 :headers => sender_auth
129 assert_response :bad_request
133 recipient = create(:user)
134 sender = create(:user)
135 user3 = create(:user)
137 sender_auth = bearer_authorization_header(sender, :scopes => %w[consume_messages])
138 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
139 user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
141 msg = create(:message, :unread, :sender => sender, :recipient => recipient)
143 # fail if not authorized
144 get api_message_path(msg)
145 assert_response :unauthorized
147 # only recipient and sender can read the message
148 get api_message_path(msg), :headers => user3_auth
149 assert_response :forbidden
151 # message does not exist
152 get api_message_path(99999), :headers => user3_auth
153 assert_response :not_found
156 get api_message_path(msg), :headers => recipient_auth
157 assert_equal "application/xml", response.media_type
158 assert_select "message", :count => 1 do
159 assert_select "[id='#{msg.id}']"
160 assert_select "[from_user_id='#{sender.id}']"
161 assert_select "[from_display_name='#{sender.display_name}']"
162 assert_select "[to_user_id='#{recipient.id}']"
163 assert_select "[to_display_name='#{recipient.display_name}']"
164 assert_select "[sent_on]"
165 assert_select "[deleted='#{!msg.to_user_visible}']"
166 assert_select "[message_read='#{msg.message_read}']"
167 assert_select "[body_format='markdown']"
168 assert_select "title", msg.title
169 assert_select "body", msg.body
173 get api_message_path(msg, :format => "json"), :headers => recipient_auth
174 assert_equal "application/json", response.media_type
175 js = ActiveSupport::JSON.decode(@response.body)
178 assert_equal msg.id, jsm["id"]
179 assert_equal sender.id, jsm["from_user_id"]
180 assert_equal sender.display_name, jsm["from_display_name"]
181 assert_equal recipient.id, jsm["to_user_id"]
182 assert_equal recipient.display_name, jsm["to_display_name"]
183 assert_equal msg.title, jsm["title"]
184 assert_not_nil jsm["sent_on"]
185 assert_equal msg.message_read, jsm["message_read"]
186 assert_equal !msg.to_user_visible, jsm["deleted"]
187 assert_equal "markdown", jsm["body_format"]
188 assert_equal msg.body, jsm["body"]
190 get api_message_path(msg), :headers => sender_auth
191 assert_equal "application/xml", response.media_type
192 assert_select "message", :count => 1 do
193 assert_select "[id='#{msg.id}']"
194 assert_select "[from_user_id='#{sender.id}']"
195 assert_select "[from_display_name='#{sender.display_name}']"
196 assert_select "[to_user_id='#{recipient.id}']"
197 assert_select "[to_display_name='#{recipient.display_name}']"
198 assert_select "[sent_on]"
199 assert_select "[deleted='#{!msg.from_user_visible}']"
200 assert_select "[message_read='#{msg.message_read}']", 0
201 assert_select "[body_format='markdown']"
202 assert_select "title", msg.title
203 assert_select "body", msg.body
207 get api_message_path(msg, :format => "json"), :headers => sender_auth
208 assert_equal "application/json", response.media_type
209 js = ActiveSupport::JSON.decode(@response.body)
212 assert_equal msg.id, jsm["id"]
213 assert_equal sender.id, jsm["from_user_id"]
214 assert_equal sender.display_name, jsm["from_display_name"]
215 assert_equal recipient.id, jsm["to_user_id"]
216 assert_equal recipient.display_name, jsm["to_display_name"]
217 assert_equal msg.title, jsm["title"]
218 assert_not_nil jsm["sent_on"]
219 assert_equal !msg.from_user_visible, jsm["deleted"]
220 assert_not jsm.key?("message_read")
221 assert_equal "markdown", jsm["body_format"]
222 assert_equal msg.body, jsm["body"]
225 def test_show_message_to_self_read
227 message = create(:message, :sender => user, :recipient => user)
228 auth_header = bearer_authorization_header user
230 get api_message_path(message), :headers => auth_header
231 assert_response :success
232 assert_equal "application/xml", response.media_type
233 assert_dom "message", :count => 1 do
234 assert_dom "> @message_read", "false"
238 def test_show_message_to_self_read_json
240 message = create(:message, :sender => user, :recipient => user)
241 auth_header = bearer_authorization_header user
243 get api_message_path(message, :format => "json"), :headers => auth_header
244 assert_response :success
245 assert_equal "application/json", response.media_type
246 js = ActiveSupport::JSON.decode(@response.body)
249 assert jsm.key?("message_read")
250 assert_not jsm["message_read"]
253 def test_update_status
254 recipient = create(:user)
255 sender = create(:user)
256 user3 = create(:user)
258 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
259 user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
261 msg = create(:message, :unread, :sender => sender, :recipient => recipient)
263 # attempt to mark message as read by recipient, not authenticated
264 put api_message_path(msg), :params => { :read_status => true }
265 assert_response :unauthorized
267 # attempt to mark message as read by recipient, not allowed
268 put api_message_path(msg), :params => { :read_status => true }, :headers => user3_auth
269 assert_response :forbidden
272 put api_message_path(msg), :headers => recipient_auth
273 assert_response :bad_request
275 # wrong type of parameter
276 put api_message_path(msg),
277 :params => { :read_status => "not a boolean" },
278 :headers => recipient_auth
279 assert_response :bad_request
281 # mark message as read by recipient
282 put api_message_path(msg, :format => "json"),
283 :params => { :read_status => true },
284 :headers => recipient_auth
285 assert_response :success
286 assert_equal "application/json", response.media_type
287 js = ActiveSupport::JSON.decode(@response.body)
290 assert_equal msg.id, jsm["id"]
291 assert_equal sender.id, jsm["from_user_id"]
292 assert_equal sender.display_name, jsm["from_display_name"]
293 assert_equal recipient.id, jsm["to_user_id"]
294 assert_equal recipient.display_name, jsm["to_display_name"]
295 assert_equal msg.title, jsm["title"]
296 assert_not_nil jsm["sent_on"]
297 assert jsm["message_read"]
298 assert_equal !msg.to_user_visible, jsm["deleted"]
299 assert_equal "markdown", jsm["body_format"]
300 assert_equal msg.body, jsm["body"]
302 # mark message as unread by recipient
303 put api_message_path(msg, :format => "json"),
304 :params => { :read_status => false },
305 :headers => recipient_auth
306 assert_response :success
307 assert_equal "application/json", response.media_type
308 js = ActiveSupport::JSON.decode(@response.body)
311 assert_equal msg.id, jsm["id"]
312 assert_equal sender.id, jsm["from_user_id"]
313 assert_equal sender.display_name, jsm["from_display_name"]
314 assert_equal recipient.id, jsm["to_user_id"]
315 assert_equal recipient.display_name, jsm["to_display_name"]
316 assert_equal msg.title, jsm["title"]
317 assert_not_nil jsm["sent_on"]
318 assert_not jsm["message_read"]
319 assert_equal !msg.to_user_visible, jsm["deleted"]
320 assert_equal "markdown", jsm["body_format"]
321 assert_equal msg.body, jsm["body"]
325 recipient = create(:user)
326 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
328 sender = create(:user)
329 sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
331 user3 = create(:user)
332 user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
334 msg = create(:message, :read, :sender => sender, :recipient => recipient)
336 # attempt to delete message, not authenticated
337 delete api_message_path(msg)
338 assert_response :unauthorized
340 # attempt to delete message, by user3
341 delete api_message_path(msg), :headers => user3_auth
342 assert_response :forbidden
344 # delete message by recipient
345 delete api_message_path(msg, :format => "json"), :headers => recipient_auth
346 assert_response :success
347 assert_equal "application/json", response.media_type
348 js = ActiveSupport::JSON.decode(@response.body)
351 assert_equal msg.id, jsm["id"]
352 assert_equal sender.id, jsm["from_user_id"]
353 assert_equal sender.display_name, jsm["from_display_name"]
354 assert_equal recipient.id, jsm["to_user_id"]
355 assert_equal recipient.display_name, jsm["to_display_name"]
356 assert_equal msg.title, jsm["title"]
357 assert_not_nil jsm["sent_on"]
358 assert_equal msg.message_read, jsm["message_read"]
359 assert jsm["deleted"]
360 assert_equal "markdown", jsm["body_format"]
361 assert_equal msg.body, jsm["body"]
363 # delete message by sender
364 delete api_message_path(msg, :format => "json"), :headers => sender_auth
365 assert_response :success
366 assert_equal "application/json", response.media_type
367 js = ActiveSupport::JSON.decode(@response.body)
370 assert_equal msg.id, jsm["id"]
371 assert_equal sender.id, jsm["from_user_id"]
372 assert_equal sender.display_name, jsm["from_display_name"]
373 assert_equal recipient.id, jsm["to_user_id"]
374 assert_equal recipient.display_name, jsm["to_display_name"]
375 assert_equal msg.title, jsm["title"]
376 assert_not_nil jsm["sent_on"]
377 assert jsm["deleted"]
378 assert_not jsm.key?("message_read")
379 assert_equal "markdown", jsm["body_format"]
380 assert_equal msg.body, jsm["body"]