]> git.openstreetmap.org Git - rails.git/blobdiff - test/controllers/api/messages_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5391'
[rails.git] / test / controllers / api / messages_controller_test.rb
index 0b54be4dc2051fe33c1c8434d5ced5f823fb45f4..2e763939538706e166f0e693689ee3d97441e5cd 100644 (file)
@@ -46,9 +46,13 @@ module Api
         { :controller => "api/messages", :action => "create" }
       )
       assert_routing(
-        { :path => "/api/0.6/user/messages/1", :method => :post },
+        { :path => "/api/0.6/user/messages/1", :method => :put },
         { :controller => "api/messages", :action => "update", :id => "1" }
       )
+      assert_recognizes(
+        { :controller => "api/messages", :action => "update", :id => "1" },
+        { :path => "/api/0.6/user/messages/1", :method => :post }
+      )
       assert_routing(
         { :path => "/api/0.6/user/messages/1", :method => :delete },
         { :controller => "api/messages", :action => "destroy", :id => "1" }
@@ -59,10 +63,7 @@ module Api
       recipient = create(:user)
       sender = create(:user)
 
-      sender_token = create(:oauth_access_token,
-                            :resource_owner_id => sender.id,
-                            :scopes => %w[send_messages consume_messages])
-      sender_auth = bearer_authorization_header(sender_token.token)
+      sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
 
       msg = build(:message)
 
@@ -101,10 +102,7 @@ module Api
       recipient = create(:user)
 
       sender = create(:user)
-      sender_token = create(:oauth_access_token,
-                            :resource_owner_id => sender.id,
-                            :scopes => %w[send_messages consume_messages])
-      sender_auth = bearer_authorization_header(sender_token.token)
+      sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
 
       assert_no_difference "Message.count" do
         assert_no_difference "ActionMailer::Base.deliveries.size" do
@@ -160,20 +158,9 @@ module Api
       sender = create(:user)
       user3 = create(:user)
 
-      sender_token = create(:oauth_access_token,
-                            :resource_owner_id => sender.id,
-                            :scopes => %w[consume_messages])
-      sender_auth = bearer_authorization_header(sender_token.token)
-
-      recipient_token = create(:oauth_access_token,
-                               :resource_owner_id => recipient.id,
-                               :scopes => %w[consume_messages])
-      recipient_auth = bearer_authorization_header(recipient_token.token)
-
-      user3_token = create(:oauth_access_token,
-                           :resource_owner_id => user3.id,
-                           :scopes => %w[send_messages consume_messages])
-      user3_auth = bearer_authorization_header(user3_token.token)
+      sender_auth = bearer_authorization_header(sender, :scopes => %w[consume_messages])
+      recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
+      user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
 
       msg = create(:message, :unread, :sender => sender, :recipient => recipient)
 
@@ -264,40 +251,33 @@ module Api
       sender = create(:user)
       user3 = create(:user)
 
-      recipient_token = create(:oauth_access_token,
-                               :resource_owner_id => recipient.id,
-                               :scopes => %w[consume_messages])
-      recipient_auth = bearer_authorization_header(recipient_token.token)
-
-      user3_token = create(:oauth_access_token,
-                           :resource_owner_id => user3.id,
-                           :scopes => %w[send_messages consume_messages])
-      user3_auth = bearer_authorization_header(user3_token.token)
+      recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
+      user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
 
       msg = create(:message, :unread, :sender => sender, :recipient => recipient)
 
       # attempt to mark message as read by recipient, not authenticated
-      post api_message_path(:id => msg.id), :params => { :read_status => true }
+      put api_message_path(:id => msg.id), :params => { :read_status => true }
       assert_response :unauthorized
 
       # attempt to mark message as read by recipient, not allowed
-      post api_message_path(:id => msg.id), :params => { :read_status => true }, :headers => user3_auth
+      put api_message_path(:id => msg.id), :params => { :read_status => true }, :headers => user3_auth
       assert_response :forbidden
 
       # missing parameter
-      post api_message_path(:id => msg.id), :headers => recipient_auth
+      put api_message_path(:id => msg.id), :headers => recipient_auth
       assert_response :bad_request
 
       # wrong type of parameter
-      post api_message_path(:id => msg.id),
-           :params => { :read_status => "not a boolean" },
-           :headers => recipient_auth
+      put api_message_path(:id => msg.id),
+          :params => { :read_status => "not a boolean" },
+          :headers => recipient_auth
       assert_response :bad_request
 
       # mark message as read by recipient
-      post api_message_path(:id => msg.id, :format => "json"),
-           :params => { :read_status => true },
-           :headers => recipient_auth
+      put api_message_path(:id => msg.id, :format => "json"),
+          :params => { :read_status => true },
+          :headers => recipient_auth
       assert_response :success
       assert_equal "application/json", response.media_type
       js = ActiveSupport::JSON.decode(@response.body)
@@ -316,9 +296,9 @@ module Api
       assert_equal msg.body, jsm["body"]
 
       # mark message as unread by recipient
-      post api_message_path(:id => msg.id, :format => "json"),
-           :params => { :read_status => false },
-           :headers => recipient_auth
+      put api_message_path(:id => msg.id, :format => "json"),
+          :params => { :read_status => false },
+          :headers => recipient_auth
       assert_response :success
       assert_equal "application/json", response.media_type
       js = ActiveSupport::JSON.decode(@response.body)
@@ -339,22 +319,13 @@ module Api
 
     def test_delete
       recipient = create(:user)
-      recipient_token = create(:oauth_access_token,
-                               :resource_owner_id => recipient.id,
-                               :scopes => %w[consume_messages])
-      recipient_auth = bearer_authorization_header(recipient_token.token)
+      recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
 
       sender = create(:user)
-      sender_token = create(:oauth_access_token,
-                            :resource_owner_id => sender.id,
-                            :scopes => %w[send_messages consume_messages])
-      sender_auth = bearer_authorization_header(sender_token.token)
+      sender_auth = bearer_authorization_header(sender, :scopes => %w[send_messages consume_messages])
 
       user3 = create(:user)
-      user3_token = create(:oauth_access_token,
-                           :resource_owner_id => user3.id,
-                           :scopes => %w[send_messages consume_messages])
-      user3_auth = bearer_authorization_header(user3_token.token)
+      user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
 
       msg = create(:message, :read, :sender => sender, :recipient => recipient)
 
@@ -407,22 +378,13 @@ module Api
 
     def test_list_messages
       user1 = create(:user)
-      user1_token = create(:oauth_access_token,
-                           :resource_owner_id => user1.id,
-                           :scopes => %w[send_messages consume_messages])
-      user1_auth = bearer_authorization_header(user1_token.token)
+      user1_auth = bearer_authorization_header(user1, :scopes => %w[send_messages consume_messages])
 
       user2 = create(:user)
-      user2_token = create(:oauth_access_token,
-                           :resource_owner_id => user2.id,
-                           :scopes => %w[send_messages consume_messages])
-      user2_auth = bearer_authorization_header(user2_token.token)
+      user2_auth = bearer_authorization_header(user2, :scopes => %w[send_messages consume_messages])
 
       user3 = create(:user)
-      user3_token = create(:oauth_access_token,
-                           :resource_owner_id => user3.id,
-                           :scopes => %w[send_messages consume_messages])
-      user3_auth = bearer_authorization_header(user3_token.token)
+      user3_auth = bearer_authorization_header(user3, :scopes => %w[send_messages consume_messages])
 
       # create some messages between users
       # user | inbox | outbox
@@ -523,10 +485,7 @@ module Api
 
     def test_paged_list_messages_asc
       recipient = create(:user)
-      recipient_token = create(:oauth_access_token,
-                               :resource_owner_id => recipient.id,
-                               :scopes => %w[consume_messages])
-      recipient_auth = bearer_authorization_header(recipient_token.token)
+      recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
 
       sender = create(:user)
 
@@ -559,10 +518,7 @@ module Api
 
     def test_paged_list_messages_desc
       recipient = create(:user)
-      recipient_token = create(:oauth_access_token,
-                               :resource_owner_id => recipient.id,
-                               :scopes => %w[consume_messages])
-      recipient_auth = bearer_authorization_header(recipient_token.token)
+      recipient_auth = bearer_authorization_header(recipient, :scopes => %w[consume_messages])
 
       sender = create(:user)