]> git.openstreetmap.org Git - rails.git/blob - test/functional/old_node_controller_test.rb
DateTime => Time for consistency in changeset code, ok by shaun.
[rails.git] / test / functional / old_node_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'old_node_controller'
3
4 class 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   def test_version
16     basic_authorization(users(:normal_user).email, "test")
17     changeset_id = changesets(:normal_user_first_change).id
18
19     # setup a simple XML node
20     xml_doc = current_nodes(:visible_node).to_xml
21     xml_node = xml_doc.find("//osm/node").first
22     nodeid = current_nodes(:visible_node).id
23
24     # keep a hash of the versions => string, as we'll need something
25     # to test against later
26     versions = Hash.new
27
28     # save a version for later checking
29     versions[xml_node['version']] = xml_doc.to_s
30
31     # randomly move the node about
32     20.times do 
33       # move the node somewhere else
34       xml_node['lat'] = precision(rand * 180 -  90).to_s
35       xml_node['lon'] = precision(rand * 360 - 180).to_s
36       with_controller(NodeController.new) do
37         content xml_doc
38         put :update, :id => nodeid
39         assert_response :success
40         xml_node['version'] = @response.body.to_s
41       end
42       # save a version for later checking
43       versions[xml_node['version']] = xml_doc.to_s
44     end
45
46     # add a bunch of random tags
47     30.times do 
48       xml_tag = XML::Node.new("tag")
49       xml_tag['k'] = random_string
50       xml_tag['v'] = random_string
51       xml_node << xml_tag
52       with_controller(NodeController.new) do
53         content xml_doc
54         put :update, :id => nodeid
55         assert_response :success,
56         "couldn't update node #{nodeid} (#{@response.body})"
57         xml_node['version'] = @response.body.to_s
58       end
59       # save a version for later checking
60       versions[xml_node['version']] = xml_doc.to_s
61     end
62
63     # check all the versions
64     versions.keys.each do |key|
65       get :version, :id => nodeid, :version => key.to_i
66
67       assert_response :success,
68          "couldn't get version #{key.to_i} of node #{nodeid}"
69
70       check_node = Node.from_xml(versions[key])
71       api_node = Node.from_xml(@response.body.to_s)
72
73       assert_nodes_are_equal check_node, api_node
74     end
75   end
76
77   ##
78   # Test that getting the current version is identical to picking
79   # that version with the version URI call.
80   def test_current_version
81     check_current_version(current_nodes(:visible_node))
82     check_current_version(current_nodes(:used_node_1))
83     check_current_version(current_nodes(:used_node_2))
84     check_current_version(current_nodes(:node_used_by_relationship))
85     check_current_version(current_nodes(:node_with_versions))
86   end
87   
88   def check_current_version(node_id)
89     # get the current version of the node
90     current_node = with_controller(NodeController.new) do
91       get :read, :id => node_id
92       assert_response :success, "cant get current node #{node_id}" 
93       Node.from_xml(@response.body)
94     end
95     assert_not_nil current_node, "getting node #{node_id} returned nil"
96
97     # get the "old" version of the node from the old_node interface
98     get :version, :id => node_id, :version => current_node.version
99     assert_response :success, "cant get old node #{node_id}, v#{current_node.version}" 
100     old_node = Node.from_xml(@response.body)
101
102     # check the nodes are the same
103     assert_nodes_are_equal current_node, old_node
104   end
105
106   ##
107   # returns a 16 character long string with some nasty characters in it.
108   # this ought to stress-test the tag handling as well as the versioning.
109   def random_string
110     letters = [['!','"','$','&',';','@'],
111                ('a'..'z').to_a,
112                ('A'..'Z').to_a,
113                ('0'..'9').to_a].flatten
114     (1..16).map { |i| letters[ rand(letters.length) ] }.join
115   end
116
117   ##
118   # truncate a floating point number to the scale that it is stored in
119   # the database. otherwise rounding errors can produce failing unit
120   # tests when they shouldn't.
121   def precision(f)
122     return (f * GeoRecord::SCALE).round.to_f / GeoRecord::SCALE
123   end
124
125   def basic_authorization(user, pass)
126     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
127   end
128
129   def content(c)
130     @request.env["RAW_POST_DATA"] = c.to_s
131   end
132
133 end