]> git.openstreetmap.org Git - rails.git/blobdiff - test/controllers/amf_controller_test.rb
More work on test coverage
[rails.git] / test / controllers / amf_controller_test.rb
index def849f2aa3a157f9f69c218f74165f4547b899b..371319c23d65a6e46919262c7f70691ccd7fbe5c 100644 (file)
@@ -1,5 +1,5 @@
-require 'test_helper'
-require 'stringio'
+require "test_helper"
+require "stringio"
 include Potlatch
 
 class AmfControllerTest < ActionController::TestCase
@@ -18,6 +18,28 @@ class AmfControllerTest < ActionController::TestCase
     )
   end
 
+  def test_getpresets
+    amf_content "getpresets", "/1", ["test@example.com:test", ""]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    presets = amf_result("/1")
+
+    assert_equal 15, presets.length
+    assert_equal POTLATCH_PRESETS[0], presets[0]
+    assert_equal POTLATCH_PRESETS[1], presets[1]
+    assert_equal POTLATCH_PRESETS[2], presets[2]
+    assert_equal POTLATCH_PRESETS[3], presets[3]
+    assert_equal POTLATCH_PRESETS[4], presets[4]
+    assert_equal POTLATCH_PRESETS[5], presets[5]
+    assert_equal POTLATCH_PRESETS[6], presets[6]
+    assert_equal POTLATCH_PRESETS[7], presets[7]
+    assert_equal POTLATCH_PRESETS[8], presets[8]
+    assert_equal POTLATCH_PRESETS[9], presets[9]
+    assert_equal POTLATCH_PRESETS[10], presets[10]
+    assert_equal POTLATCH_PRESETS[12], presets[12]
+  end
+
   def test_getway
     # check a visible way
     id = current_ways(:visible_way).id
@@ -129,35 +151,35 @@ class AmfControllerTest < ActionController::TestCase
 
     # check contents of message
     map = amf_result "/1"
-    assert_equal 0, map[0], 'map error code should be 0'
-    assert_equal "", map[1], 'map error text should be empty'
+    assert_equal 0, map[0], "map error code should be 0"
+    assert_equal "", map[1], "map error text should be empty"
 
     # check the formatting of the message
-    assert_equal 5, map.length, 'map should have length 5'
+    assert_equal 5, map.length, "map should have length 5"
     assert_equal Array, map[2].class, 'map "ways" element should be an array'
     assert_equal Array, map[3].class, 'map "nodes" element should be an array'
     assert_equal Array, map[4].class, 'map "relations" element should be an array'
     map[2].each do |w|
-      assert_equal 2, w.length, 'way should be (id, version) pair'
-      assert w[0] == w[0].floor, 'way ID should be an integer'
-      assert w[1] == w[1].floor, 'way version should be an integer'
+      assert_equal 2, w.length, "way should be (id, version) pair"
+      assert w[0] == w[0].floor, "way ID should be an integer"
+      assert w[1] == w[1].floor, "way version should be an integer"
     end
 
     map[3].each do |n|
-      assert_equal 5, w.length, 'node should be (id, lat, lon, [tags], version) tuple'
-      assert n[0] == n[0].floor, 'node ID should be an integer'
-      assert n[1] >= minlat - 0.01, 'node lat should be greater than min'
-      assert n[1] <= maxlat - 0.01, 'node lat should be less than max'
-      assert n[2] >= minlon - 0.01, 'node lon should be greater than min'
-      assert n[2] <= maxlon - 0.01, 'node lon should be less than max'
-      assert_equal Array, a[3].class, 'node tags should be array'
-      assert n[4] == n[4].floor, 'node version should be an integer'
+      assert_equal 5, w.length, "node should be (id, lat, lon, [tags], version) tuple"
+      assert n[0] == n[0].floor, "node ID should be an integer"
+      assert n[1] >= minlat - 0.01, "node lat should be greater than min"
+      assert n[1] <= maxlat - 0.01, "node lat should be less than max"
+      assert n[2] >= minlon - 0.01, "node lon should be greater than min"
+      assert n[2] <= maxlon - 0.01, "node lon should be less than max"
+      assert_equal Array, a[3].class, "node tags should be array"
+      assert n[4] == n[4].floor, "node version should be an integer"
     end
 
     map[4].each do |r|
-      assert_equal 2, r.length, 'relation should be (id, version) pair'
-      assert r[0] == r[0].floor, 'relation ID should be an integer'
-      assert r[1] == r[1].floor, 'relation version should be an integer'
+      assert_equal 2, r.length, "relation should be (id, version) pair"
+      assert r[0] == r[0].floor, "relation ID should be an integer"
+      assert r[1] == r[1].floor, "relation version should be an integer"
     end
 
     # TODO: looks like amf_controller changed since this test was written
@@ -166,7 +188,7 @@ class AmfControllerTest < ActionController::TestCase
     assert ways.include?(current_ways(:used_way).id),
            "map should include used way"
     assert !ways.include?(current_ways(:invisible_way).id),
-           'map should not include deleted way'
+           "map should not include deleted way"
   end
 
   ##
@@ -214,15 +236,15 @@ class AmfControllerTest < ActionController::TestCase
 
     # check contents of message
     map = amf_result "/1"
-    assert_equal 0, map[0], 'first map element should be 0'
-    assert_equal "", map[1], 'second map element should be an empty string'
-    assert_equal Array, map[2].class, 'third map element should be an array'
+    assert_equal 0, map[0], "first map element should be 0"
+    assert_equal "", map[1], "second map element should be an empty string"
+    assert_equal Array, map[2].class, "third map element should be an array"
     # TODO: looks like amf_controller changed since this test was written
     # so someone who knows what they're doing should check this!
     assert !map[2].include?(current_ways(:used_way).id),
            "map should not include used way"
     assert map[2].include?(current_ways(:invisible_way).id),
-           'map should include deleted way'
+           "map should include deleted way"
   end
 
   def test_whichways_deleted_toobig
@@ -280,7 +302,7 @@ class AmfControllerTest < ActionController::TestCase
     # instead of a version number...
     # try to get version 1
     v1 = ways(:way_with_versions_v1)
-    { latest.id => '',
+    { latest.id => "",
       v1.way_id => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
     }.each do |id, t|
       amf_content "getway_old", "/1", [id, t]
@@ -323,8 +345,8 @@ class AmfControllerTest < ActionController::TestCase
     v1 = ways(:way_with_versions_v1)
     # try to get last visible version of non-existent way
     # try to get specific version of non-existent way
-    [[0, ''],
-     [0, '1 Jan 1970, 00:00:00'],
+    [[0, ""],
+     [0, "1 Jan 1970, 00:00:00"],
      [v1.way_id, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
     ].each do |id, t|
       amf_content "getway_old", "/1", [id, t]
@@ -350,7 +372,7 @@ class AmfControllerTest < ActionController::TestCase
     history = amf_result("/1")
 
     # ['way',wayid,history]
-    assert_equal 'way', history[0]
+    assert_equal "way", history[0]
     assert_equal latest.id, history[1]
     # We use dates rather than version numbers here, because you might
     # have moved a node within a way (i.e. way version not incremented).
@@ -368,7 +390,7 @@ class AmfControllerTest < ActionController::TestCase
     history = amf_result("/1")
 
     # ['way',wayid,history]
-    assert_equal history[0], 'way'
+    assert_equal history[0], "way"
     assert_equal history[1], 0
     assert history[2].empty?
   end
@@ -384,16 +406,16 @@ class AmfControllerTest < ActionController::TestCase
     # ['node',nodeid,history]
     # note that (as per getway_history) we actually round up
     # to the next second
-    assert_equal history[0], 'node',
+    assert_equal history[0], "node",
                  'first element should be "node"'
     assert_equal history[1], latest.id,
-                 'second element should be the input node ID'
+                 "second element should be the input node ID"
     assert_equal history[2].first[0],
                  (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
-                 'first element in third element (array) should be the latest version'
+                 "first element in third element (array) should be the latest version"
     assert_equal history[2].last[0],
                  (nodes(:node_with_versions_v1).timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
-                 'last element in third element (array) should be the initial version'
+                 "last element in third element (array) should be the initial version"
   end
 
   def test_getnode_history_nonexistent
@@ -404,13 +426,197 @@ class AmfControllerTest < ActionController::TestCase
     history = amf_result("/1")
 
     # ['node',nodeid,history]
-    assert_equal history[0], 'node'
+    assert_equal history[0], "node"
     assert_equal history[1], 0
     assert history[2].empty?
   end
 
+  def test_findgpx_bad_user
+    amf_content "findgpx", "/1", [1, "test@example.com:wrong"]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 2, result.length
+    assert_equal -1, result[0]
+    assert_match /must be logged in/, result[1]
+
+    amf_content "findgpx", "/1", [1, "blocked@openstreetmap.org:test"]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 2, result.length
+    assert_equal -1, result[0]
+    assert_match /access to the API has been blocked/, result[1]
+  end
+
+  def test_findgpx_by_id
+    trace = gpx_files(:anon_trace_file)
+
+    amf_content "findgpx", "/1", [trace.id, "test@example.com:test"]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 3, result.length
+    assert_equal 0, result[0]
+    assert_equal "", result[1]
+    traces = result[2]
+    assert_equal 1, traces.length
+    assert_equal 3, traces[0].length
+    assert_equal trace.id, traces[0][0]
+    assert_equal trace.name, traces[0][1]
+    assert_equal trace.description, traces[0][2]
+  end
+
+  def test_findgpx_by_name
+    amf_content "findgpx", "/1", ["Trace", "test@example.com:test"]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    # find by name fails as it uses mysql text search syntax...
+    assert_equal 2, result.length
+    assert_equal -2, result[0]
+  end
+
+  def test_findrelations_by_id
+    relation = current_relations(:relation_with_versions)
+
+    amf_content "findrelations", "/1", [relation.id]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 1, result.length
+    assert_equal 4, result[0].length
+    assert_equal relation.id, result[0][0]
+    assert_equal relation.tags, result[0][1]
+    assert_equal relation.members, result[0][2]
+    assert_equal relation.version, result[0][3]
+
+    amf_content "findrelations", "/1", [999999]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 0, result.length
+  end
+
+  def test_findrelations_by_tags
+    visible_relation = current_relations(:visible_relation)
+    used_relation = current_relations(:used_relation)
+
+    amf_content "findrelations", "/1", ["yes"]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1").sort
+
+    assert_equal 2, result.length
+    assert_equal 4, result[0].length
+    assert_equal visible_relation.id, result[0][0]
+    assert_equal visible_relation.tags, result[0][1]
+    assert_equal visible_relation.members, result[0][2]
+    assert_equal visible_relation.version, result[0][3]
+    assert_equal 4, result[1].length
+    assert_equal used_relation.id, result[1][0]
+    assert_equal used_relation.tags, result[1][1]
+    assert_equal used_relation.members, result[1][2]
+    assert_equal used_relation.version, result[1][3]
+
+    amf_content "findrelations", "/1", ["no"]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1").sort
+
+    assert_equal 0, result.length
+  end
+
+  def test_getpoi_without_timestamp
+    node = current_nodes(:node_with_versions)
+
+    amf_content "getpoi", "/1", [node.id, ""]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 7, result.length
+    assert_equal 0, result[0]
+    assert_equal "", result[1]
+    assert_equal node.id, result[2]
+    assert_equal node.lon, result[3]
+    assert_equal node.lat, result[4]
+    assert_equal node.tags, result[5]
+    assert_equal node.version, result[6]
+
+    amf_content "getpoi", "/1", [999999, ""]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 3, result.length
+    assert_equal -4, result[0]
+    assert_equal "node", result[1]
+    assert_equal 999999, result[2]
+  end
+
+  def test_getpoi_with_timestamp
+    node = nodes(:node_with_versions_v2)
+    current_node = current_nodes(:node_with_versions)
+
+    amf_content "getpoi", "/1", [node.node_id, node.timestamp.xmlschema]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 7, result.length
+    assert_equal 0, result[0]
+    assert_equal "", result[1]
+    assert_equal node.node_id, result[2]
+    assert_equal node.lon, result[3]
+    assert_equal node.lat, result[4]
+    assert_equal node.tags, result[5]
+    assert_equal current_node.version, result[6]
+
+    amf_content "getpoi", "/1", [node.node_id, "2000-01-01T00:00:00Z"]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 3, result.length
+    assert_equal -4, result[0]
+    assert_equal "node", result[1]
+    assert_equal node.node_id, result[2]
+
+    amf_content "getpoi", "/1", [999999, Time.now.xmlschema]
+    post :amf_read
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 3, result.length
+    assert_equal -4, result[0]
+    assert_equal "node", result[1]
+    assert_equal 999999, result[2]
+  end
+
   # ************************************************************
   # AMF Write tests
+
+  # check that we can update a poi
   def test_putpoi_update_valid
     nd = current_nodes(:visible_node)
     cs_id = changesets(:public_user_first_change).id
@@ -420,6 +626,7 @@ class AmfControllerTest < ActionController::TestCase
     amf_parse_response
     result = amf_result("/1")
 
+    assert_equal 5, result.size
     assert_equal 0, result[0]
     assert_equal "", result[1]
     assert_equal nd.id, result[2]
@@ -435,6 +642,7 @@ class AmfControllerTest < ActionController::TestCase
     amf_parse_response
     result = amf_result("/2")
 
+    assert_equal 5, result.size
     assert_equal 0, result[0]
     assert_equal "", result[1]
     assert_equal nd.id, result[2]
@@ -584,16 +792,118 @@ class AmfControllerTest < ActionController::TestCase
     assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
   end
 
+  # try deleting a node
   def test_putpoi_delete_valid
+    nd = current_nodes(:visible_node)
+    cs_id = changesets(:public_user_first_change).id
+    amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
+    post :amf_write
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 5, result.size
+    assert_equal 0, result[0]
+    assert_equal "", result[1]
+    assert_equal nd.id, result[2]
+    assert_equal nd.id, result[3]
+    assert_equal nd.version + 1, result[4]
+
+    current_node = Node.find(result[3].to_i)
+    assert_equal false, current_node.visible
   end
 
+  # try deleting a node that is already deleted
   def test_putpoi_delete_already_deleted
+    nd = current_nodes(:invisible_node)
+    cs_id = changesets(:public_user_first_change).id
+    amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, false]
+    post :amf_write
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 3, result.size
+    assert_equal -4, result[0]
+    assert_equal "node", result[1]
+    assert_equal nd.id, result[2]
   end
 
+  # try deleting a node that has never existed
   def test_putpoi_delete_not_found
+    cs_id = changesets(:public_user_first_change).id
+    amf_content "putpoi", "/1", ["test@example.com:test", cs_id, 1, 999999, 0, 0, {}, false]
+    post :amf_write
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 3, result.size
+    assert_equal -4, result[0]
+    assert_equal "node", result[1]
+    assert_equal 999999, result[2]
   end
 
+  # try setting an invalid location on a node
   def test_putpoi_invalid_latlon
+    nd = current_nodes(:visible_node)
+    cs_id = changesets(:public_user_first_change).id
+    amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, 200, 100, nd.tags, true]
+    post :amf_write
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 2, result.size
+    assert_equal -2, result[0]
+    assert_match /Node is not in the world/, result[1]
+  end
+
+  # check that we can update way
+  def test_putway_update_valid
+    way = current_ways(:way_with_multiple_nodes)
+    cs_id = changesets(:public_user_first_change).id
+    amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version, way.id, way.nds, { "test" => "ok" }, [], {}]
+    post :amf_write
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 8, result.size
+    assert_equal 0, result[0]
+    assert_equal "", result[1]
+    assert_equal way.id, result[2]
+    assert_equal way.id, result[3]
+    assert_equal({}, result[4])
+    assert_equal way.version + 1, result[5]
+    assert_equal({}, result[6])
+    assert_equal({}, result[7])
+
+    new_way = Way.find(way.id)
+    assert_equal way.version + 1, new_way.version
+    assert_equal way.nds, new_way.nds
+    assert_equal({ "test" => "ok" }, new_way.tags)
+
+    amf_content "putway", "/1", ["test@example.com:test", cs_id, way.version + 1, way.id, [4, 6, 15, 1], way.tags, [], {}]
+    post :amf_write
+    assert_response :success
+    amf_parse_response
+    result = amf_result("/1")
+
+    assert_equal 8, result.size
+    assert_equal 0, result[0]
+    assert_equal "", result[1]
+    assert_equal way.id, result[2]
+    assert_equal way.id, result[3]
+    assert_equal({}, result[4])
+    assert_equal way.version + 2, result[5]
+    assert_equal({}, result[6])
+    assert_equal({}, result[7])
+
+    new_way = Way.find(way.id)
+    assert_equal way.version + 2, new_way.version
+    assert_equal [4, 6, 15, 1], new_way.nds
+    assert_equal way.tags, new_way.tags
   end
 
   def test_startchangeset_invalid_xmlchar_comment
@@ -614,6 +924,8 @@ class AmfControllerTest < ActionController::TestCase
     assert_equal "foobar", cs.tags["comment"]
   end
 
+  private
+
   # ************************************************************
   # AMF Helper functions