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

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

Add some basic tests for nodes.

File size: 2.1 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  fixtures :current_nodes, :nodes, :users
9  set_fixture_class :current_nodes => :Node
10  set_fixture_class :nodes => :OldNode
11
12  def setup
13    @controller = NodeController.new
14    @request    = ActionController::TestRequest.new
15    @response   = ActionController::TestResponse.new
16  end
17
18  def test_create
19    # cannot read password from fixture as it is stored as MD5 digest
20    basic_authorization("test@openstreetmap.org", "test"); 
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>")
25    put :create
26    # hope for success
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"
32    # compare values
33    assert_in_delta lat, checknode.latitude, 1E-8, "saved node does not match requested latitude"
34    assert_in_delta lon, checknode.longitude, 1E-8, "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"
37  end
38
39  def test_read
40    # check that a visible node is returned properly
41    get :read, :id => current_nodes(:visible_node).id
42    assert_response :success
43
44    # check that an invisible node is not returned
45    get :read, :id => current_nodes(:invisible_node).id
46    assert_response :gone
47
48    # check chat a non-existent node is not returned
49    get :read, :id => 0
50    assert_response :not_found
51  end
52
53  def basic_authorization(user, pass)
54    @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
55  end
56
57  def content(c)
58    @request.env["RAW_POST_DATA"] = c
59  end
60end
Note: See TracBrowser for help on using the repository browser.