]> git.openstreetmap.org Git - rails.git/blob - test/functional/node_controller_test.rb
a8ef700a3aba3f364c33e07b2541656297052ae1
[rails.git] / test / functional / node_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'node_controller'
3
4 # Re-raise errors caught by the controller.
5 class NodeController; def rescue_action(e) raise e end; end
6
7 class NodeControllerTest < Test::Unit::TestCase
8   api_fixtures
9
10   def setup
11     @controller = NodeController.new
12     @request    = ActionController::TestRequest.new
13     @response   = ActionController::TestResponse.new
14   end
15
16   def test_create
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
20     
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>")
28     put :create
29     # hope for success
30     assert_response :success, "node upload did not return success status"
31
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"
36     # compare values
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"
41   end
42
43   def test_read
44     # check that a visible node is returned properly
45     get :read, :id => current_nodes(:visible_node).id
46     assert_response :success
47
48     # check that an invisible node is not returned
49     get :read, :id => current_nodes(:invisible_node).id
50     assert_response :gone
51
52     # check chat a non-existent node is not returned
53     get :read, :id => 0
54     assert_response :not_found
55   end
56
57   # this tests deletion restrictions - basic deletion is tested in the unit
58   # tests for node!
59   def test_delete
60
61     # first try to delete node without auth
62     delete :delete, :id => current_nodes(:visible_node).id
63     assert_response :unauthorized
64
65     # now set auth
66     basic_authorization(users(:normal_user).email, "test");  
67
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
72
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
76     assert_response :gone
77
78     # this won't work since the node never existed
79     delete :delete, :id => 0
80     assert_response :not_found
81
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
86   end
87
88
89   def basic_authorization(user, pass)
90     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
91   end
92
93   def content(c)
94     @request.env["RAW_POST_DATA"] = c.to_s
95   end
96 end