]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/messages/inboxes_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5457'
[rails.git] / test / controllers / api / messages / inboxes_controller_test.rb
1 require "test_helper"
2
3 module Api
4   module Messages
5     class InboxesControllerTest < 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/inbox", :method => :get },
11           { :controller => "api/messages/inboxes", :action => "show" }
12         )
13         assert_routing(
14           { :path => "/api/0.6/user/messages/inbox.xml", :method => :get },
15           { :controller => "api/messages/inboxes", :action => "show", :format => "xml" }
16         )
17         assert_routing(
18           { :path => "/api/0.6/user/messages/inbox.json", :method => :get },
19           { :controller => "api/messages/inboxes", :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_inbox_path
45         assert_response :unauthorized
46
47         # no messages in user1.inbox
48         get api_messages_inbox_path, :headers => user1_auth
49         assert_response :success
50         assert_equal "application/xml", response.media_type
51         assert_select "message", :count => 0
52
53         # 2 messages in user2.inbox
54         get api_messages_inbox_path, :headers => user2_auth
55         assert_response :success
56         assert_equal "application/xml", response.media_type
57         assert_select "message", :count => 2 do
58           assert_select "[from_user_id]"
59           assert_select "[from_display_name]"
60           assert_select "[to_user_id='#{user2.id}']"
61           assert_select "[to_display_name='#{user2.display_name}']"
62           assert_select "[sent_on]"
63           assert_select "[message_read='false']"
64           assert_select "[deleted='false']"
65           assert_select "[body_format]"
66           assert_select "body", false
67           assert_select "title"
68         end
69
70         # 2 messages in user3.inbox
71         get api_messages_inbox_path, :headers => user3_auth
72         assert_response :success
73         assert_equal "application/xml", response.media_type
74         assert_select "message", :count => 2 do
75           assert_select "[from_user_id]"
76           assert_select "[from_display_name]"
77           assert_select "[to_user_id='#{user3.id}']"
78           assert_select "[to_display_name='#{user3.display_name}']"
79           assert_select "[sent_on]"
80           assert_select "[message_read='false']"
81           assert_select "[deleted='false']"
82           assert_select "[body_format]"
83           assert_select "body", false
84           assert_select "title"
85         end
86       end
87
88       def test_show_paged_asc
89         recipient = create(:user)
90         recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
91
92         sender = create(:user)
93
94         create_list(:message, 100, :unread, :sender => sender, :recipient => recipient)
95
96         msgs_read = {}
97         params = { :order => "oldest", :limit => 20 }
98         10.times do
99           get api_messages_inbox_path(:format => "json"),
100               :params => params,
101               :headers => recipient_auth
102           assert_response :success
103           assert_equal "application/json", response.media_type
104           js = ActiveSupport::JSON.decode(@response.body)
105           jsm = js["messages"]
106           assert_operator jsm.count, :<=, 20
107
108           break if jsm.nil? || jsm.count.zero?
109
110           assert_operator(jsm[0]["id"], :>=, params[:from_id]) unless params[:from_id].nil?
111           # ensure ascending order
112           (0..jsm.count - 1).each do |i|
113             assert_operator(jsm[i]["id"], :<, jsm[i + 1]["id"]) unless i == jsm.count - 1
114             msgs_read[jsm[i]["id"]] = jsm[i]
115           end
116           params[:from_id] = jsm[jsm.count - 1]["id"]
117         end
118         assert_equal 100, msgs_read.count
119       end
120
121       def test_show_paged_desc
122         recipient = create(:user)
123         recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
124
125         sender = create(:user)
126
127         create_list(:message, 100, :unread, :sender => sender, :recipient => recipient)
128
129         real_max_id = -1
130         msgs_read = {}
131         params = { :order => "newest", :limit => 20 }
132         10.times do
133           get api_messages_inbox_path(:format => "json"),
134               :params => params,
135               :headers => recipient_auth
136           assert_response :success
137           assert_equal "application/json", response.media_type
138           js = ActiveSupport::JSON.decode(@response.body)
139           jsm = js["messages"]
140           assert_operator jsm.count, :<=, 20
141
142           break if jsm.nil? || jsm.count.zero?
143
144           if params[:from_id].nil?
145             real_max_id = jsm[0]["id"]
146           else
147             assert_operator jsm[0]["id"], :<=, params[:from_id]
148           end
149           # ensure descending order
150           (0..jsm.count - 1).each do |i|
151             assert_operator(jsm[i]["id"], :>, jsm[i + 1]["id"]) unless i == jsm.count - 1
152             msgs_read[jsm[i]["id"]] = jsm[i]
153           end
154           params[:from_id] = jsm[jsm.count - 1]["id"]
155         end
156         assert_equal 100, msgs_read.count
157         assert_not_equal(-1, real_max_id)
158
159         # invoke without min_id/max_id parameters, verify that we get the last batch
160         get api_messages_inbox_path(:format => "json"), :params => { :limit => 20 }, :headers => recipient_auth
161         assert_response :success
162         assert_equal "application/json", response.media_type
163         js = ActiveSupport::JSON.decode(@response.body)
164         jsm = js["messages"]
165         assert_not_nil jsm
166         assert_equal real_max_id, jsm[0]["id"]
167       end
168     end
169   end
170 end