5 class InboxesControllerTest < ActionDispatch::IntegrationTest
7 # test all routes which lead to this controller
10 { :path => "/api/0.6/user/messages/inbox", :method => :get },
11 { :controller => "api/messages/inboxes", :action => "show" }
14 { :path => "/api/0.6/user/messages/inbox.xml", :method => :get },
15 { :controller => "api/messages/inboxes", :action => "show", :format => "xml" }
18 { :path => "/api/0.6/user/messages/inbox.json", :method => :get },
19 { :controller => "api/messages/inboxes", :action => "show", :format => "json" }
25 user1_auth = bearer_authorization_header(user1, :scopes => %w[send_messages consume_messages])
28 user2_auth = bearer_authorization_header(user2, :scopes => %w[send_messages consume_messages])
31 user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
33 # create some messages between users
34 # user | inbox | outbox
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)
43 # only authorized users
44 get api_messages_inbox_path
45 assert_response :unauthorized
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
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
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
88 def test_show_paged_asc
89 recipient = create(:user)
90 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
92 sender = create(:user)
94 create_list(:message, 100, :unread, :sender => sender, :recipient => recipient)
97 params = { :order => "oldest", :limit => 20 }
99 get api_messages_inbox_path(:format => "json"),
101 :headers => recipient_auth
102 assert_response :success
103 assert_equal "application/json", response.media_type
104 js = ActiveSupport::JSON.decode(@response.body)
106 assert_operator jsm.count, :<=, 20
108 break if jsm.nil? || jsm.count.zero?
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]
116 params[:from_id] = jsm[jsm.count - 1]["id"]
118 assert_equal 100, msgs_read.count
121 def test_show_paged_desc
122 recipient = create(:user)
123 recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
125 sender = create(:user)
127 create_list(:message, 100, :unread, :sender => sender, :recipient => recipient)
131 params = { :order => "newest", :limit => 20 }
133 get api_messages_inbox_path(:format => "json"),
135 :headers => recipient_auth
136 assert_response :success
137 assert_equal "application/json", response.media_type
138 js = ActiveSupport::JSON.decode(@response.body)
140 assert_operator jsm.count, :<=, 20
142 break if jsm.nil? || jsm.count.zero?
144 if params[:from_id].nil?
145 real_max_id = jsm[0]["id"]
147 assert_operator jsm[0]["id"], :<=, params[:from_id]
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]
154 params[:from_id] = jsm[jsm.count - 1]["id"]
156 assert_equal 100, msgs_read.count
157 assert_not_equal(-1, real_max_id)
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)
166 assert_equal real_max_id, jsm[0]["id"]