source: subversion/sites/rails_port/test/functional/node_controller_test.rb @ 3799

Last change on this file since 3799 was 3799, checked in by tomhughes, 12 years ago

Factor out loading of the various fixtures needed to test API calls
into a shared method.

File size: 2.8 KB
Line 
1require File.dirname(__FILE__) + '/../test_helper'
2require 'node_controller'
3
4# Re-raise errors caught by the controller.
5class NodeController; def rescue_action(e) raise e end; end
6
7class 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("test@openstreetmap.org", "test"); 
19    # create a node with random lat/lon
20    lat = rand(100)-50 + rand
21    lon = rand(100)-50 + rand
22    content("<osm><node lat='#{lat}' lon='#{lon}' /></osm>")
23    put :create
24    # hope for success
25    assert_response :success, "node upload did not return success status"
26    # read id of created node and search for it
27    nodeid = @response.body
28    checknode = Node.find(nodeid)
29    assert_not_nil checknode, "uploaded node not found in data base after upload"
30    # compare values
31    assert_in_delta lat, checknode.latitude, 1E-8, "saved node does not match requested latitude"
32    assert_in_delta lon, checknode.longitude, 1E-8, "saved node does not match requested longitude"
33    assert_equal users(:normal_user).id, checknode.user_id, "saved node does not belong to user that created it"
34    assert_equal true, checknode.visible, "saved node is not visible"
35  end
36
37  def test_read
38    # check that a visible node is returned properly
39    get :read, :id => current_nodes(:visible_node).id
40    assert_response :success
41
42    # check that an invisible node is not returned
43    get :read, :id => current_nodes(:invisible_node).id
44    assert_response :gone
45
46    # check chat a non-existent node is not returned
47    get :read, :id => 0
48    assert_response :not_found
49  end
50
51  # this tests deletion restrictions - basic deletion is tested in the unit
52  # tests for node!
53  def test_delete
54
55    # first try to delete node without auth
56    delete :delete, :id => current_nodes(:visible_node).id
57    assert_response :unauthorized
58
59    # now set auth
60    basic_authorization("test@openstreetmap.org", "test"); 
61
62    # this should work
63    delete :delete, :id => current_nodes(:visible_node).id
64    assert_response :success
65
66    # this won't work since the node is already deleted
67    delete :delete, :id => current_nodes(:invisible_node).id
68    assert_response :gone
69
70    # this won't work since the node never existed
71    delete :delete, :id => 0
72    assert_response :not_found
73
74    # this won't work since the node is in use
75    delete :delete, :id => current_nodes(:used_node_1).id
76    assert_response :precondition_failed
77  end
78
79
80  def basic_authorization(user, pass)
81    @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
82  end
83
84  def content(c)
85    @request.env["RAW_POST_DATA"] = c
86  end
87end
Note: See TracBrowser for help on using the repository browser.