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_update_status
226 recipient = create(:user)
227 sender = create(:user)
228 user3 = create(:user)
230 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
231 user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
233 msg = create(:message, :unread, :sender => sender, :recipient => recipient)
235 # attempt to mark message as read by recipient, not authenticated
236 put api_message_path(msg), :params => { :read_status => true }
237 assert_response :unauthorized
239 # attempt to mark message as read by recipient, not allowed
240 put api_message_path(msg), :params => { :read_status => true }, :headers => user3_auth
241 assert_response :forbidden
244 put api_message_path(msg), :headers => recipient_auth
245 assert_response :bad_request
247 # wrong type of parameter
248 put api_message_path(msg),
249 :params => { :read_status => "not a boolean" },
250 :headers => recipient_auth
251 assert_response :bad_request
253 # mark message as read by recipient
254 put api_message_path(msg, :format => "json"),
255 :params => { :read_status => true },
256 :headers => recipient_auth
257 assert_response :success
258 assert_equal "application/json", response.media_type
259 js = ActiveSupport::JSON.decode(@response.body)
262 assert_equal msg.id, jsm["id"]
263 assert_equal sender.id, jsm["from_user_id"]
264 assert_equal sender.display_name, jsm["from_display_name"]
265 assert_equal recipient.id, jsm["to_user_id"]
266 assert_equal recipient.display_name, jsm["to_display_name"]
267 assert_equal msg.title, jsm["title"]
268 assert_not_nil jsm["sent_on"]
269 assert jsm["message_read"]
270 assert_equal !msg.to_user_visible, jsm["deleted"]
271 assert_equal "markdown", jsm["body_format"]
272 assert_equal msg.body, jsm["body"]
274 # mark message as unread by recipient
275 put api_message_path(msg, :format => "json"),
276 :params => { :read_status => false },
277 :headers => recipient_auth
278 assert_response :success
279 assert_equal "application/json", response.media_type
280 js = ActiveSupport::JSON.decode(@response.body)
283 assert_equal msg.id, jsm["id"]
284 assert_equal sender.id, jsm["from_user_id"]
285 assert_equal sender.display_name, jsm["from_display_name"]
286 assert_equal recipient.id, jsm["to_user_id"]
287 assert_equal recipient.display_name, jsm["to_display_name"]
288 assert_equal msg.title, jsm["title"]
289 assert_not_nil jsm["sent_on"]
290 assert_not jsm["message_read"]
291 assert_equal !msg.to_user_visible, jsm["deleted"]
292 assert_equal "markdown", jsm["body_format"]
293 assert_equal msg.body, jsm["body"]
297 recipient = create(:user)
298 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
300 sender = create(:user)
301 sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
303 user3 = create(:user)
304 user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
306 msg = create(:message, :read, :sender => sender, :recipient => recipient)
308 # attempt to delete message, not authenticated
309 delete api_message_path(msg)
310 assert_response :unauthorized
312 # attempt to delete message, by user3
313 delete api_message_path(msg), :headers => user3_auth
314 assert_response :forbidden
316 # delete message by recipient
317 delete api_message_path(msg, :format => "json"), :headers => recipient_auth
318 assert_response :success
319 assert_equal "application/json", response.media_type
320 js = ActiveSupport::JSON.decode(@response.body)
323 assert_equal msg.id, jsm["id"]
324 assert_equal sender.id, jsm["from_user_id"]
325 assert_equal sender.display_name, jsm["from_display_name"]
326 assert_equal recipient.id, jsm["to_user_id"]
327 assert_equal recipient.display_name, jsm["to_display_name"]
328 assert_equal msg.title, jsm["title"]
329 assert_not_nil jsm["sent_on"]
330 assert_equal msg.message_read, jsm["message_read"]
331 assert jsm["deleted"]
332 assert_equal "markdown", jsm["body_format"]
333 assert_equal msg.body, jsm["body"]
335 # delete message by sender
336 delete api_message_path(msg, :format => "json"), :headers => sender_auth
337 assert_response :success
338 assert_equal "application/json", response.media_type
339 js = ActiveSupport::JSON.decode(@response.body)
342 assert_equal msg.id, jsm["id"]
343 assert_equal sender.id, jsm["from_user_id"]
344 assert_equal sender.display_name, jsm["from_display_name"]
345 assert_equal recipient.id, jsm["to_user_id"]
346 assert_equal recipient.display_name, jsm["to_display_name"]
347 assert_equal msg.title, jsm["title"]
348 assert_not_nil jsm["sent_on"]
349 assert jsm["deleted"]
350 assert_not jsm.key?("message_read")
351 assert_equal "markdown", jsm["body_format"]
352 assert_equal msg.body, jsm["body"]