]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_nodes_controller_test.rb
Create way version redaction resource
[rails.git] / test / controllers / api / old_nodes_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class OldNodesControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/node/1/history", :method => :get },
10         { :controller => "api/old_nodes", :action => "index", :node_id => "1" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/node/1/history.json", :method => :get },
14         { :controller => "api/old_nodes", :action => "index", :node_id => "1", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/node/1/2", :method => :get },
18         { :controller => "api/old_nodes", :action => "show", :node_id => "1", :version => "2" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/node/1/2.json", :method => :get },
22         { :controller => "api/old_nodes", :action => "show", :node_id => "1", :version => "2", :format => "json" }
23       )
24     end
25
26     def test_index
27       node = create(:node, :version => 2)
28       create(:old_node, :node_id => node.id, :version => 1, :latitude => 60 * OldNode::SCALE, :longitude => 30 * OldNode::SCALE)
29       create(:old_node, :node_id => node.id, :version => 2, :latitude => 61 * OldNode::SCALE, :longitude => 31 * OldNode::SCALE)
30
31       get api_node_versions_path(node)
32
33       assert_response :success
34       assert_dom "osm:root", 1 do
35         assert_dom "> node", 2 do |dom_nodes|
36           assert_dom dom_nodes[0], "> @id", node.id.to_s
37           assert_dom dom_nodes[0], "> @version", "1"
38           assert_dom dom_nodes[0], "> @lat", "60.0000000"
39           assert_dom dom_nodes[0], "> @lon", "30.0000000"
40
41           assert_dom dom_nodes[1], "> @id", node.id.to_s
42           assert_dom dom_nodes[1], "> @version", "2"
43           assert_dom dom_nodes[1], "> @lat", "61.0000000"
44           assert_dom dom_nodes[1], "> @lon", "31.0000000"
45         end
46       end
47     end
48
49     ##
50     # test that redacted nodes aren't visible in the history
51     def test_index_redacted_unauthorised
52       node = create(:node, :with_history, :version => 2)
53       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
54
55       get api_node_versions_path(node)
56
57       assert_response :success, "Redaction shouldn't have stopped history working."
58       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
59                  "redacted node #{node.id} version 1 shouldn't be present in the history."
60
61       get api_node_versions_path(node, :show_redactions => "true")
62
63       assert_response :success, "Redaction shouldn't have stopped history working."
64       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
65                  "redacted node #{node.id} version 1 shouldn't be present in the history when passing flag."
66     end
67
68     def test_index_redacted_normal_user
69       node = create(:node, :with_history, :version => 2)
70       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
71
72       get api_node_versions_path(node), :headers => bearer_authorization_header
73
74       assert_response :success, "Redaction shouldn't have stopped history working."
75       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
76                  "redacted node #{node.id} version 1 shouldn't be present in the history, even when logged in."
77
78       get api_node_versions_path(node, :show_redactions => "true"), :headers => bearer_authorization_header
79
80       assert_response :success, "Redaction shouldn't have stopped history working."
81       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
82                  "redacted node #{node.id} version 1 shouldn't be present in the history, even when logged in and passing flag."
83     end
84
85     def test_index_redacted_moderator
86       node = create(:node, :with_history, :version => 2)
87       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
88       auth_header = bearer_authorization_header create(:moderator_user)
89
90       get api_node_versions_path(node), :headers => auth_header
91
92       assert_response :success, "Redaction shouldn't have stopped history working."
93       assert_dom "osm node[id='#{node.id}'][version='1']", 0,
94                  "node #{node.id} version 1 should not be present in the history for moderators when not passing flag."
95
96       get api_node_versions_path(node, :show_redactions => "true"), :headers => auth_header
97
98       assert_response :success, "Redaction shouldn't have stopped history working."
99       assert_dom "osm node[id='#{node.id}'][version='1']", 1,
100                  "node #{node.id} version 1 should still be present in the history for moderators when passing flag."
101     end
102
103     def test_show
104       node = create(:node, :version => 2)
105       create(:old_node, :node_id => node.id, :version => 1, :latitude => 60 * OldNode::SCALE, :longitude => 30 * OldNode::SCALE)
106       create(:old_node, :node_id => node.id, :version => 2, :latitude => 61 * OldNode::SCALE, :longitude => 31 * OldNode::SCALE)
107
108       get api_node_version_path(node, 1)
109
110       assert_response :success
111       assert_dom "osm:root", 1 do
112         assert_dom "> node", 1 do
113           assert_dom "> @id", node.id.to_s
114           assert_dom "> @version", "1"
115           assert_dom "> @lat", "60.0000000"
116           assert_dom "> @lon", "30.0000000"
117         end
118       end
119
120       get api_node_version_path(node, 2)
121
122       assert_response :success
123       assert_dom "osm:root", 1 do
124         assert_dom "> node", 1 do
125           assert_dom "> @id", node.id.to_s
126           assert_dom "> @version", "2"
127           assert_dom "> @lat", "61.0000000"
128           assert_dom "> @lon", "31.0000000"
129         end
130       end
131     end
132
133     def test_show_not_found
134       check_not_found_id_version(70000, 312344)
135       check_not_found_id_version(-1, -13)
136       check_not_found_id_version(create(:node).id, 24354)
137       check_not_found_id_version(24356, create(:node).version)
138     end
139
140     ##
141     # test that redacted nodes aren't visible, regardless of
142     # authorisation except as moderator...
143     def test_show_redacted_unauthorised
144       node = create(:node, :with_history, :version => 2)
145       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
146
147       get api_node_version_path(node, 1)
148
149       assert_response :forbidden, "Redacted node shouldn't be visible via the version API."
150
151       get api_node_version_path(node, 1, :show_redactions => "true")
152
153       assert_response :forbidden, "Redacted node shouldn't be visible via the version API when passing flag."
154     end
155
156     def test_show_redacted_normal_user
157       node = create(:node, :with_history, :version => 2)
158       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
159
160       get api_node_version_path(node, 1), :headers => bearer_authorization_header
161
162       assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in."
163
164       get api_node_version_path(node, 1, :show_redactions => "true"), :headers => bearer_authorization_header
165
166       assert_response :forbidden, "Redacted node shouldn't be visible via the version API, even when logged in and passing flag."
167     end
168
169     def test_show_redacted_moderator
170       node = create(:node, :with_history, :version => 2)
171       node.old_nodes.find_by(:version => 1).redact!(create(:redaction))
172       auth_header = bearer_authorization_header create(:moderator_user)
173
174       get api_node_version_path(node, 1), :headers => auth_header
175
176       assert_response :forbidden, "Redacted node should be gone for moderator, when flag not passed."
177
178       get api_node_version_path(node, 1, :show_redactions => "true"), :headers => auth_header
179
180       assert_response :success, "Redacted node should not be gone for moderator, when flag passed."
181     end
182
183     # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
184     def test_lat_lon_xml_format
185       old_node = create(:old_node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
186
187       get api_node_versions_path(old_node.node_id)
188       assert_match(/lat="0.0000400"/, response.body)
189       assert_match(/lon="0.0000800"/, response.body)
190     end
191
192     private
193
194     def check_not_found_id_version(id, version)
195       get api_node_version_path(id, version)
196       assert_response :not_found
197     rescue ActionController::UrlGenerationError => e
198       assert_match(/No route matches/, e.to_s)
199     end
200   end
201 end