From 37570ffd3fe52d8b7ea4bc514a7bb90ab71f12b2 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Fri, 4 Jul 2025 21:24:23 +0300 Subject: [PATCH] Split create node tests --- test/controllers/api/nodes_controller_test.rb | 70 ++++++++----------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/test/controllers/api/nodes_controller_test.rb b/test/controllers/api/nodes_controller_test.rb index ad6f2e7f0..2c5237195 100644 --- a/test/controllers/api/nodes_controller_test.rb +++ b/test/controllers/api/nodes_controller_test.rb @@ -90,54 +90,46 @@ module Api assert_response :not_found end - def test_create - private_user = create(:user, :data_public => false) - private_changeset = create(:changeset, :user => private_user) - user = create(:user) - changeset = create(:changeset, :user => user) + def test_create_when_unauthorized + with_unchanging_request do |_headers, changeset| + osm = "" - # create a node with random lat/lon - lat = rand(-50..50) + rand - lon = rand(-50..50) + rand + post api_nodes_path, :params => osm - ## First try with no auth - # create a minimal xml file - xml = "" - assert_difference("OldNode.count", 0) do - post api_nodes_path, :params => xml + assert_response :unauthorized end - # hope for unauthorized - assert_response :unauthorized, "node upload did not return unauthorized status" + end - ## Now try with the user which doesn't have their data public - auth_header = bearer_authorization_header private_user + def test_create_by_private_user + with_unchanging_request([:data_public => false]) do |headers, changeset| + osm = "" - # create a minimal xml file - xml = "" - assert_difference("Node.count", 0) do - post api_nodes_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => osm, :headers => headers + + assert_require_public_data "node create did not return forbidden status" end - # hope for success - assert_require_public_data "node create did not return forbidden status" + end - ## Now try with the user that has the public data - auth_header = bearer_authorization_header user + def test_create + with_request do |headers, changeset| + lat = rand(-50..50) + rand + lon = rand(-50..50) + rand - # create a minimal xml file - xml = "" - post api_nodes_path, :params => xml, :headers => auth_header - # hope for success - assert_response :success, "node upload did not return success status" + assert_difference "Node.count", 1 do + osm = "" - # read id of created node and search for it - nodeid = @response.body - checknode = Node.find(nodeid) - assert_not_nil checknode, "uploaded node not found in data base after upload" - # compare values - assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude" - assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude" - assert_equal changeset.id, checknode.changeset_id, "saved node does not belong to changeset that it was created in" - assert checknode.visible, "saved node is not visible" + post api_nodes_path, :params => osm, :headers => headers + + assert_response :success, "node upload did not return success status" + end + + created_node_id = @response.body + node = Node.find(created_node_id) + assert_in_delta lat * 10000000, node.latitude, 1, "saved node does not match requested latitude" + assert_in_delta lon * 10000000, node.longitude, 1, "saved node does not match requested longitude" + assert_equal changeset.id, node.changeset_id, "saved node does not belong to changeset that it was created in" + assert node.visible, "saved node is not visible" + end end def test_create_invalid_xml -- 2.39.5