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