]> git.openstreetmap.org Git - rails.git/blob - test/functional/node_controller_test.rb
added more tests
[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   fixtures :current_nodes, :nodes, :users, :current_segments, :segments
9   set_fixture_class :current_nodes => :Node
10   set_fixture_class :nodes => :OldNode
11   set_fixture_class :current_segments => :Segment
12   set_fixture_class :segments => :OldSegment
13
14   def setup
15     @controller = NodeController.new
16     @request    = ActionController::TestRequest.new
17     @response   = ActionController::TestResponse.new
18   end
19
20   def test_create
21     # cannot read password from fixture as it is stored as MD5 digest
22     basic_authorization("test@openstreetmap.org", "test");  
23     # create a node with random lat/lon
24     lat = rand(100)-50 + rand
25     lon = rand(100)-50 + rand
26     content("<osm><node lat='#{lat}' lon='#{lon}' /></osm>")
27     put :create
28     # hope for success
29     assert_response :success, "node upload did not return success status"
30     # read id of created node and search for it
31     nodeid = @response.body
32     checknode = Node.find(nodeid)
33     assert_not_nil checknode, "uploaded node not found in data base after upload"
34     # compare values
35     assert_in_delta lat, checknode.latitude, 1E-8, "saved node does not match requested latitude"
36     assert_in_delta lon, checknode.longitude, 1E-8, "saved node does not match requested longitude"
37     assert_equal users(:normal_user).id, checknode.user_id, "saved node does not belong to user that created it"
38     assert_equal true, checknode.visible, "saved node is not visible"
39   end
40
41   def test_read
42     # check that a visible node is returned properly
43     get :read, :id => current_nodes(:visible_node).id
44     assert_response :success
45
46     # check that an invisible node is not returned
47     get :read, :id => current_nodes(:invisible_node).id
48     assert_response :gone
49
50     # check chat a non-existent node is not returned
51     get :read, :id => 0
52     assert_response :not_found
53   end
54
55   # this tests deletion restrictions - basic deletion is tested in the unit
56   # tests for node!
57   def test_delete
58
59     # first try to delete node without auth
60     delete :delete, :id => current_nodes(:visible_node).id
61     assert_response :unauthorized
62
63     # now set auth
64     basic_authorization("test@openstreetmap.org", "test");  
65
66     # this should work
67     delete :delete, :id => current_nodes(:visible_node).id
68     assert_response :success
69
70     # this won't work since the node is already deleted
71     delete :delete, :id => current_nodes(:invisible_node).id
72     assert_response :gone
73
74     # this won't work since the node never existed
75     delete :delete, :id => 0
76     assert_response :not_found
77
78     # this won't work since the node is in use
79     delete :delete, :id => current_nodes(:used_node_1).id
80     assert_response :precondition_failed
81   end
82
83
84   def basic_authorization(user, pass)
85     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
86   end
87
88   def content(c)
89     @request.env["RAW_POST_DATA"] = c
90   end
91 end