]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/messages/outboxes_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5457'
[rails.git] / test / controllers / api / messages / outboxes_controller_test.rb
1 require "test_helper"
2
3 module Api
4   module Messages
5     class OutboxesControllerTest < ActionDispatch::IntegrationTest
6       ##
7       # test all routes which lead to this controller
8       def test_routes
9         assert_routing(
10           { :path => "/api/0.6/user/messages/outbox", :method => :get },
11           { :controller => "api/messages/outboxes", :action => "show" }
12         )
13         assert_routing(
14           { :path => "/api/0.6/user/messages/outbox.xml", :method => :get },
15           { :controller => "api/messages/outboxes", :action => "show", :format => "xml" }
16         )
17         assert_routing(
18           { :path => "/api/0.6/user/messages/outbox.json", :method => :get },
19           { :controller => "api/messages/outboxes", :action => "show", :format => "json" }
20         )
21       end
22
23       def test_show
24         user1 = create(:user)
25         user1_auth = bearer_authorization_header(user1, :scopes => %w[send_messages consume_messages])
26
27         user2 = create(:user)
28         user2_auth = bearer_authorization_header(user2, :scopes => %w[send_messages consume_messages])
29
30         user3 = create(:user)
31         user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
32
33         # create some messages between users
34         # user | inbox | outbox
35         #   1  |   0   |   3
36         #   2  |   2   |   1
37         #   3  |   2   |   0
38         create(:message, :unread, :sender => user1, :recipient => user2)
39         create(:message, :unread, :sender => user1, :recipient => user2)
40         create(:message, :unread, :sender => user1, :recipient => user3)
41         create(:message, :unread, :sender => user2, :recipient => user3)
42
43         # only authorized users
44         get api_messages_outbox_path
45         assert_response :unauthorized
46
47         # 3 messages in user1.outbox
48         get api_messages_outbox_path, :headers => user1_auth
49         assert_response :success
50         assert_equal "application/xml", response.media_type
51         assert_select "message", :count => 3 do
52           assert_select "[from_user_id='#{user1.id}']"
53           assert_select "[from_display_name='#{user1.display_name}']"
54           assert_select "[to_user_id]"
55           assert_select "[to_display_name]"
56           assert_select "[sent_on]"
57           assert_select "[message_read]", 0
58           assert_select "[deleted='false']"
59           assert_select "[body_format]"
60           assert_select "body", false
61           assert_select "title"
62         end
63
64         # 1 message in user2.outbox
65         get api_messages_outbox_path, :headers => user2_auth
66         assert_response :success
67         assert_equal "application/xml", response.media_type
68         assert_select "message", :count => 1 do
69           assert_select "[from_user_id='#{user2.id}']"
70           assert_select "[from_display_name='#{user2.display_name}']"
71           assert_select "[to_user_id]"
72           assert_select "[to_display_name]"
73           assert_select "[sent_on]"
74           assert_select "[deleted='false']"
75           assert_select "[message_read]", 0
76           assert_select "[body_format]"
77           assert_select "body", false
78           assert_select "title"
79         end
80
81         # 0 messages in user3.outbox
82         get api_messages_outbox_path, :headers => user3_auth
83         assert_response :success
84         assert_equal "application/xml", response.media_type
85         assert_select "message", :count => 0
86       end
87     end
88   end
89 end