]> git.openstreetmap.org Git - rails.git/blobdiff - test/controllers/api/changeset_comments/visibilities_controller_test.rb
Refactor api changeset comment visibility json tests
[rails.git] / test / controllers / api / changeset_comments / visibilities_controller_test.rb
index 97ac9f3da81595805f69fcad1f637824de0fb32d..b842af56a90be0e72e2e6165341b1261ac6f55f6 100644 (file)
@@ -84,16 +84,7 @@ module Api
 
         post api_changeset_comment_visibility_path(comment), :headers => auth_header
 
-        assert_response :success
-        assert_equal "application/xml", response.media_type
-        assert_dom "osm", 1 do
-          assert_dom "> changeset", 1 do
-            assert_dom "> @id", comment.changeset_id.to_s
-            assert_dom "> @comments_count", "1"
-          end
-        end
-
-        assert comment.reload.visible
+        check_successful_response_xml(comment, :comment_visible => true)
       end
 
       def test_create_with_write_changeset_comments_scope_json
@@ -102,14 +93,7 @@ module Api
 
         post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
 
-        assert_response :success
-        assert_equal "application/json", response.media_type
-        js = ActiveSupport::JSON.decode(@response.body)
-        assert_not_nil js["changeset"]
-        assert_equal comment.changeset_id, js["changeset"]["id"]
-        assert_equal 1, js["changeset"]["comments_count"]
-
-        assert comment.reload.visible
+        check_successful_response_json(comment, :comment_visible => true)
       end
 
       def test_create_with_write_api_scope
@@ -118,16 +102,7 @@ module Api
 
         post api_changeset_comment_visibility_path(comment), :headers => auth_header
 
-        assert_response :success
-        assert_equal "application/xml", response.media_type
-        assert_dom "osm", 1 do
-          assert_dom "> changeset", 1 do
-            assert_dom "> @id", comment.changeset_id.to_s
-            assert_dom "> @comments_count", "1"
-          end
-        end
-
-        assert comment.reload.visible
+        check_successful_response_xml(comment, :comment_visible => true)
       end
 
       def test_create_with_write_api_scope_json
@@ -136,14 +111,7 @@ module Api
 
         post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
 
-        assert_response :success
-        js = ActiveSupport::JSON.decode(@response.body)
-        assert_equal "application/json", response.media_type
-        assert_not_nil js["changeset"]
-        assert_equal comment.changeset_id, js["changeset"]["id"]
-        assert_equal 1, js["changeset"]["comments_count"]
-
-        assert comment.reload.visible
+        check_successful_response_json(comment, :comment_visible => true)
       end
 
       def test_destroy_by_unauthorized
@@ -189,16 +157,7 @@ module Api
 
         delete api_changeset_comment_visibility_path(comment), :headers => auth_header
 
-        assert_response :success
-        assert_equal "application/xml", response.media_type
-        assert_dom "osm", 1 do
-          assert_dom "> changeset", 1 do
-            assert_dom "> @id", comment.changeset_id.to_s
-            assert_dom "> @comments_count", "0"
-          end
-        end
-
-        assert_not comment.reload.visible
+        check_successful_response_xml(comment, :comment_visible => false)
       end
 
       def test_destroy_with_write_changeset_comments_scope_json
@@ -207,14 +166,7 @@ module Api
 
         delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
 
-        assert_response :success
-        assert_equal "application/json", response.media_type
-        js = ActiveSupport::JSON.decode(@response.body)
-        assert_not_nil js["changeset"]
-        assert_equal comment.changeset_id, js["changeset"]["id"]
-        assert_equal 0, js["changeset"]["comments_count"]
-
-        assert_not comment.reload.visible
+        check_successful_response_json(comment, :comment_visible => false)
       end
 
       def test_destroy_with_write_api_scope
@@ -223,32 +175,42 @@ module Api
 
         delete api_changeset_comment_visibility_path(comment), :headers => auth_header
 
+        check_successful_response_xml(comment, :comment_visible => false)
+      end
+
+      def test_destroy_with_write_api_scope_json
+        comment = create(:changeset_comment)
+        auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
+
+        delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
+
+        check_successful_response_json(comment, :comment_visible => false)
+      end
+
+      private
+
+      def check_successful_response_xml(comment, comment_visible:)
         assert_response :success
         assert_equal "application/xml", response.media_type
         assert_dom "osm", 1 do
           assert_dom "> changeset", 1 do
             assert_dom "> @id", comment.changeset_id.to_s
-            assert_dom "> @comments_count", "0"
+            assert_dom "> @comments_count", comment_visible ? "1" : "0"
           end
         end
 
-        assert_not comment.reload.visible
+        assert_equal comment_visible, comment.reload.visible
       end
 
-      def test_destroy_with_write_api_scope_json
-        comment = create(:changeset_comment)
-        auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
-
-        delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
-
+      def check_successful_response_json(comment, comment_visible:)
         assert_response :success
         assert_equal "application/json", response.media_type
         js = ActiveSupport::JSON.decode(@response.body)
         assert_not_nil js["changeset"]
         assert_equal comment.changeset_id, js["changeset"]["id"]
-        assert_equal 0, js["changeset"]["comments_count"]
+        assert_equal comment_visible ? 1 : 0, js["changeset"]["comments_count"]
 
-        assert_not comment.reload.visible
+        assert_equal comment_visible, comment.reload.visible
       end
     end
   end