1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'node_controller'
4 # Re-raise errors caught by the controller.
5 class NodeController; def rescue_action(e) raise e end; end
7 class NodeControllerTest < Test::Unit::TestCase
11 @controller = NodeController.new
12 @request = ActionController::TestRequest.new
13 @response = ActionController::TestResponse.new
17 # cannot read password from fixture as it is stored as MD5 digest
18 basic_authorization(users(:normal_user).email, "test");
19 # FIXME we need to create a changeset first argh
21 # create a node with random lat/lon
22 lat = rand(100)-50 + rand
23 lon = rand(100)-50 + rand
24 # normal user has a changeset open, so we'll use that.
25 changeset = changesets(:normal_user_first_change)
26 # create a minimal xml file
27 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
30 assert_response :success, "node upload did not return success status"
32 # read id of created node and search for it
33 nodeid = @response.body
34 checknode = Node.find(nodeid)
35 assert_not_nil checknode, "uploaded node not found in data base after upload"
37 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
38 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
39 assert_equal changesets(:normal_user_first_change).id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
40 assert_equal true, checknode.visible, "saved node is not visible"
44 # check that a visible node is returned properly
45 get :read, :id => current_nodes(:visible_node).id
46 assert_response :success
48 # check that an invisible node is not returned
49 get :read, :id => current_nodes(:invisible_node).id
52 # check chat a non-existent node is not returned
54 assert_response :not_found
57 # this tests deletion restrictions - basic deletion is tested in the unit
61 # first try to delete node without auth
62 delete :delete, :id => current_nodes(:visible_node).id
63 assert_response :unauthorized
66 basic_authorization(users(:normal_user).email, "test");
68 # delete now takes a payload
69 content(nodes(:visible_node).to_xml)
70 delete :delete, :id => current_nodes(:visible_node).id
71 assert_response :success
73 # this won't work since the node is already deleted
74 content(nodes(:invisible_node).to_xml)
75 delete :delete, :id => current_nodes(:invisible_node).id
78 # this won't work since the node never existed
79 delete :delete, :id => 0
80 assert_response :not_found
82 # this won't work since the node is in use
83 content(nodes(:used_node_1).to_xml)
84 delete :delete, :id => current_nodes(:used_node_1).id
85 assert_response :precondition_failed
89 def basic_authorization(user, pass)
90 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
94 @request.env["RAW_POST_DATA"] = c.to_s