]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_relations_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / api / old_relations_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Api
6   class OldRelationsControllerTest < ActionDispatch::IntegrationTest
7     ##
8     # test all routes which lead to this controller
9     def test_routes
10       assert_routing(
11         { :path => "/api/0.6/relation/1/history", :method => :get },
12         { :controller => "api/old_relations", :action => "index", :relation_id => "1" }
13       )
14       assert_routing(
15         { :path => "/api/0.6/relation/1/history.json", :method => :get },
16         { :controller => "api/old_relations", :action => "index", :relation_id => "1", :format => "json" }
17       )
18       assert_routing(
19         { :path => "/api/0.6/relation/1/2", :method => :get },
20         { :controller => "api/old_relations", :action => "show", :relation_id => "1", :version => "2" }
21       )
22       assert_routing(
23         { :path => "/api/0.6/relation/1/2.json", :method => :get },
24         { :controller => "api/old_relations", :action => "show", :relation_id => "1", :version => "2", :format => "json" }
25       )
26     end
27
28     ##
29     # check that a visible relations is returned properly
30     def test_index
31       relation = create(:relation, :with_history, :version => 2)
32
33       get api_relation_versions_path(relation)
34
35       assert_response :success
36       assert_dom "osm:root", 1 do
37         assert_dom "> relation", 2 do |dom_relations|
38           assert_dom dom_relations[0], "> @id", relation.id.to_s
39           assert_dom dom_relations[0], "> @version", "1"
40
41           assert_dom dom_relations[1], "> @id", relation.id.to_s
42           assert_dom dom_relations[1], "> @version", "2"
43         end
44       end
45     end
46
47     ##
48     # check that a non-existent relations is not returned
49     def test_index_invalid
50       get api_relation_versions_path(0)
51       assert_response :not_found
52     end
53
54     ##
55     # test that redacted relations aren't visible in the history
56     def test_index_redacted_unauthorised
57       relation = create(:relation, :with_history, :version => 2)
58       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
59
60       get api_relation_versions_path(relation)
61
62       assert_response :success, "Redaction shouldn't have stopped history working."
63       assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
64                  "redacted relation #{relation.id} version 1 shouldn't be present in the history."
65
66       get api_relation_versions_path(relation, :show_redactions => "true")
67
68       assert_response :success, "Redaction shouldn't have stopped history working."
69       assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
70                  "redacted relation #{relation.id} version 1 shouldn't be present in the history when passing flag."
71     end
72
73     def test_index_redacted_normal_user
74       relation = create(:relation, :with_history, :version => 2)
75       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
76
77       get api_relation_versions_path(relation), :headers => bearer_authorization_header
78
79       assert_response :success, "Redaction shouldn't have stopped history working."
80       assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
81                  "redacted relation #{relation.id} version 1 shouldn't be present in the history, even when logged in."
82
83       get api_relation_versions_path(relation, :show_redactions => "true"), :headers => bearer_authorization_header
84
85       assert_response :success, "Redaction shouldn't have stopped history working."
86       assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
87                  "redacted relation #{relation.id} version 1 shouldn't be present in the history, even when logged in and passing flag."
88     end
89
90     def test_index_redacted_moderator
91       relation = create(:relation, :with_history, :version => 2)
92       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
93       auth_header = bearer_authorization_header create(:moderator_user)
94
95       get api_relation_versions_path(relation), :headers => auth_header
96
97       assert_response :success, "Redaction shouldn't have stopped history working."
98       assert_dom "osm relation[id='#{relation.id}'][version='1']", 0,
99                  "relation #{relation.id} version 1 should not be present in the history for moderators when not passing flag."
100
101       get api_relation_versions_path(relation, :show_redactions => "true"), :headers => auth_header
102
103       assert_response :success, "Redaction shouldn't have stopped history working."
104       assert_dom "osm relation[id='#{relation.id}'][version='1']", 1,
105                  "relation #{relation.id} version 1 should still be present in the history for moderators when passing flag."
106     end
107
108     def test_show
109       relation = create(:relation, :with_history, :version => 2)
110       create(:old_relation_tag, :old_relation => relation.old_relations[0], :k => "k1", :v => "v1")
111       create(:old_relation_tag, :old_relation => relation.old_relations[1], :k => "k2", :v => "v2")
112
113       get api_relation_version_path(relation, 1)
114
115       assert_response :success
116       assert_dom "osm:root", 1 do
117         assert_dom "> relation", 1 do
118           assert_dom "> @id", relation.id.to_s
119           assert_dom "> @version", "1"
120           assert_dom "> tag", 1 do
121             assert_dom "> @k", "k1"
122             assert_dom "> @v", "v1"
123           end
124         end
125       end
126
127       get api_relation_version_path(relation, 2)
128
129       assert_response :success
130       assert_dom "osm:root", 1 do
131         assert_dom "> relation", 1 do
132           assert_dom "> @id", relation.id.to_s
133           assert_dom "> @version", "2"
134           assert_dom "> tag", 1 do
135             assert_dom "> @k", "k2"
136             assert_dom "> @v", "v2"
137           end
138         end
139       end
140     end
141
142     ##
143     # test that redacted relations aren't visible, regardless of
144     # authorisation except as moderator...
145     def test_show_redacted_unauthorised
146       relation = create(:relation, :with_history, :version => 2)
147       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
148
149       get api_relation_version_path(relation, 1)
150
151       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API."
152
153       get api_relation_version_path(relation, 1, :show_redactions => "true")
154
155       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API when passing flag."
156     end
157
158     def test_show_redacted_normal_user
159       relation = create(:relation, :with_history, :version => 2)
160       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
161
162       get api_relation_version_path(relation, 1), :headers => bearer_authorization_header
163
164       assert_response :forbidden, "Redacted relation shouldn't be visible via the version API, even when logged in."
165
166       get api_relation_version_path(relation, 1, :show_redactions => "true"), :headers => bearer_authorization_header
167
168       assert_response :forbidden, "Redacted relation 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       relation = create(:relation, :with_history, :version => 2)
173       relation.old_relations.find_by(:version => 1).redact!(create(:redaction))
174       auth_header = bearer_authorization_header create(:moderator_user)
175
176       get api_relation_version_path(relation, 1), :headers => auth_header
177
178       assert_response :forbidden, "Redacted relation should be gone for moderator, when flag not passed."
179
180       get api_relation_version_path(relation, 1, :show_redactions => "true"), :headers => auth_header
181
182       assert_response :success, "Redacted relation should not be gone for moderator, when flag passed."
183     end
184   end
185 end