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