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 content("<osm><node lat='#{lat}' lon='#{lon}' /></osm>")
27 assert_response :success, "node upload did not return success status"
28 # read id of created node and search for it
29 nodeid = @response.body
30 checknode = Node.find(nodeid)
31 assert_not_nil checknode, "uploaded node not found in data base after upload"
33 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
34 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
35 assert_equal users(:normal_user).id, checknode.user_id, "saved node does not belong to user that created it"
36 assert_equal true, checknode.visible, "saved node is not visible"
40 # check that a visible node is returned properly
41 get :read, :id => current_nodes(:visible_node).id
42 assert_response :success
44 # check that an invisible node is not returned
45 get :read, :id => current_nodes(:invisible_node).id
48 # check chat a non-existent node is not returned
50 assert_response :not_found
53 # this tests deletion restrictions - basic deletion is tested in the unit
57 # first try to delete node without auth
58 delete :delete, :id => current_nodes(:visible_node).id
59 assert_response :unauthorized
62 basic_authorization(users(:normal_user).email, "test");
65 delete :delete, :id => current_nodes(:visible_node).id
66 assert_response :success
68 # this won't work since the node is already deleted
69 delete :delete, :id => current_nodes(:invisible_node).id
72 # this won't work since the node never existed
73 delete :delete, :id => 0
74 assert_response :not_found
76 # this won't work since the node is in use
77 delete :delete, :id => current_nodes(:used_node_1).id
78 assert_response :precondition_failed
82 def basic_authorization(user, pass)
83 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
87 @request.env["RAW_POST_DATA"] = c