source: subversion/sites/rails_port/test/functional/old_node_controller_test.rb @ 15832

Last change on this file since 15832 was 14642, checked in by Shaun McDonald, 10 years ago

Hopefully fix the old_node_controller tests.

File size: 6.3 KB
Line 
1require File.dirname(__FILE__) + '/../test_helper'
2require 'old_node_controller'
3
4class OldNodeControllerTest < ActionController::TestCase
5  api_fixtures
6
7  #
8  # TODO: test history
9  #
10
11  ##
12  # test the version call by submitting several revisions of a new node
13  # to the API and ensuring that later calls to version return the
14  # matching versions of the object.
15  #
16  ##
17  # FIXME Move this test to being an integration test since it spans multiple controllers
18  def test_version
19    ## First try this with a non-public user
20    basic_authorization(users(:normal_user).email, "test")
21    changeset_id = changesets(:normal_user_first_change).id
22   
23    # setup a simple XML node
24    xml_doc = current_nodes(:visible_node).to_xml
25    xml_node = xml_doc.find("//osm/node").first
26    nodeid = current_nodes(:visible_node).id
27
28    # keep a hash of the versions => string, as we'll need something
29    # to test against later
30    versions = Hash.new
31
32    # save a version for later checking
33    versions[xml_node['version']] = xml_doc.to_s
34
35    # randomly move the node about
36    20.times do 
37      # move the node somewhere else
38      xml_node['lat'] = precision(rand * 180 -  90).to_s
39      xml_node['lon'] = precision(rand * 360 - 180).to_s
40      with_controller(NodeController.new) do
41        content xml_doc
42        put :update, :id => nodeid
43        assert_response :forbidden, "Should have rejected node update"
44        xml_node['version'] = @response.body.to_s
45      end
46      # save a version for later checking
47      versions[xml_node['version']] = xml_doc.to_s
48    end
49
50    # add a bunch of random tags
51    30.times do 
52      xml_tag = XML::Node.new("tag")
53      xml_tag['k'] = random_string
54      xml_tag['v'] = random_string
55      xml_node << xml_tag
56      with_controller(NodeController.new) do
57        content xml_doc
58        put :update, :id => nodeid
59        assert_response :forbidden,
60        "should have rejected node #{nodeid} (#{@response.body}) with forbidden"
61        xml_node['version'] = @response.body.to_s
62      end
63      # save a version for later checking
64      versions[xml_node['version']] = xml_doc.to_s
65    end
66
67    # probably should check that they didn't get written to the database
68
69   
70    ## Now do it with the public user
71    basic_authorization(users(:public_user).email, "test")
72    changeset_id = changesets(:public_user_first_change).id
73
74    # setup a simple XML node
75    xml_doc = current_nodes(:node_with_versions).to_xml
76    xml_node = xml_doc.find("//osm/node").first
77    nodeid = current_nodes(:node_with_versions).id
78
79    # keep a hash of the versions => string, as we'll need something
80    # to test against later
81    versions = Hash.new
82
83    # save a version for later checking
84    versions[xml_node['version']] = xml_doc.to_s
85
86    # randomly move the node about
87    20.times do 
88      # move the node somewhere else
89      xml_node['lat'] = precision(rand * 180 -  90).to_s
90      xml_node['lon'] = precision(rand * 360 - 180).to_s
91      with_controller(NodeController.new) do
92        content xml_doc
93        put :update, :id => nodeid
94        assert_response :success
95        xml_node['version'] = @response.body.to_s
96      end
97      # save a version for later checking
98      versions[xml_node['version']] = xml_doc.to_s
99    end
100
101    # add a bunch of random tags
102    30.times do 
103      xml_tag = XML::Node.new("tag")
104      xml_tag['k'] = random_string
105      xml_tag['v'] = random_string
106      xml_node << xml_tag
107      with_controller(NodeController.new) do
108        content xml_doc
109        put :update, :id => nodeid
110        assert_response :success,
111        "couldn't update node #{nodeid} (#{@response.body})"
112        xml_node['version'] = @response.body.to_s
113      end
114      # save a version for later checking
115      versions[xml_node['version']] = xml_doc.to_s
116    end
117
118    # check all the versions
119    versions.keys.each do |key|
120      get :version, :id => nodeid, :version => key.to_i
121
122      assert_response :success,
123         "couldn't get version #{key.to_i} of node #{nodeid}"
124
125      check_node = Node.from_xml(versions[key])
126      api_node = Node.from_xml(@response.body.to_s)
127
128      assert_nodes_are_equal check_node, api_node
129    end
130  end
131 
132  def test_not_found_version
133    check_not_found_id_version(70000,312344)
134    check_not_found_id_version(-1, -13)
135    check_not_found_id_version(nodes(:visible_node).id, 24354)
136    check_not_found_id_version(24356,   nodes(:visible_node).version)
137  end
138 
139  def check_not_found_id_version(id, version)
140    get :version, :id => id, :version => version
141    assert_response :not_found
142  end
143 
144  ##
145  # Test that getting the current version is identical to picking
146  # that version with the version URI call.
147  def test_current_version
148    check_current_version(current_nodes(:visible_node))
149    check_current_version(current_nodes(:used_node_1))
150    check_current_version(current_nodes(:used_node_2))
151    check_current_version(current_nodes(:node_used_by_relationship))
152    check_current_version(current_nodes(:node_with_versions))
153  end
154 
155  def check_current_version(node_id)
156    # get the current version of the node
157    current_node = with_controller(NodeController.new) do
158      get :read, :id => node_id
159      assert_response :success, "cant get current node #{node_id}" 
160      Node.from_xml(@response.body)
161    end
162    assert_not_nil current_node, "getting node #{node_id} returned nil"
163
164    # get the "old" version of the node from the old_node interface
165    get :version, :id => node_id, :version => current_node.version
166    assert_response :success, "cant get old node #{node_id}, v#{current_node.version}" 
167    old_node = Node.from_xml(@response.body)
168
169    # check the nodes are the same
170    assert_nodes_are_equal current_node, old_node
171  end
172
173  ##
174  # returns a 16 character long string with some nasty characters in it.
175  # this ought to stress-test the tag handling as well as the versioning.
176  def random_string
177    letters = [['!','"','$','&',';','@'],
178               ('a'..'z').to_a,
179               ('A'..'Z').to_a,
180               ('0'..'9').to_a].flatten
181    (1..16).map { |i| letters[ rand(letters.length) ] }.join
182  end
183
184  ##
185  # truncate a floating point number to the scale that it is stored in
186  # the database. otherwise rounding errors can produce failing unit
187  # tests when they shouldn't.
188  def precision(f)
189    return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
190  end
191end
Note: See TracBrowser for help on using the repository browser.