]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/old_nodes/redactions_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / api / old_nodes / redactions_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Api
6   module OldNodes
7     class RedactionsControllerTest < ActionDispatch::IntegrationTest
8       ##
9       # test all routes which lead to this controller
10       def test_routes
11         assert_routing(
12           { :path => "/api/0.6/node/1/2/redaction", :method => :post },
13           { :controller => "api/old_nodes/redactions", :action => "create", :node_id => "1", :version => "2" }
14         )
15         assert_routing(
16           { :path => "/api/0.6/node/1/2/redaction", :method => :delete },
17           { :controller => "api/old_nodes/redactions", :action => "destroy", :node_id => "1", :version => "2" }
18         )
19
20         assert_recognizes(
21           { :controller => "api/old_nodes/redactions", :action => "create", :node_id => "1", :version => "2", :allow_delete => true },
22           { :path => "/api/0.6/node/1/2/redact", :method => :post }
23         )
24       end
25
26       ##
27       # test that, even as moderator, the current version of a node
28       # can't be redacted.
29       def test_create_on_current_version
30         node = create(:node, :with_history, :version => 2)
31         old_node = node.old_nodes.find_by(:version => 2)
32         redaction = create(:redaction)
33         auth_header = bearer_authorization_header create(:moderator_user)
34
35         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header
36
37         assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
38         assert_nil old_node.reload.redaction
39       end
40
41       def test_create_without_redaction_id
42         node = create(:node, :with_history, :version => 2)
43         old_node = node.old_nodes.find_by(:version => 1)
44         auth_header = bearer_authorization_header create(:moderator_user)
45
46         post api_node_version_redaction_path(*old_node.id), :headers => auth_header
47
48         assert_response :bad_request, "should need redaction ID to redact."
49         assert_nil old_node.reload.redaction
50       end
51
52       ##
53       # test the redaction of an old version of a node, while not being
54       # authorised.
55       def test_create_by_unauthorised
56         node = create(:node, :with_history, :version => 2)
57         old_node = node.old_nodes.find_by(:version => 1)
58         redaction = create(:redaction)
59
60         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }
61
62         assert_response :unauthorized, "should need to be authenticated to redact."
63         assert_nil old_node.reload.redaction
64       end
65
66       def test_create_by_normal_user_without_write_redactions_scope
67         node = create(:node, :with_history, :version => 2)
68         old_node = node.old_nodes.find_by(:version => 1)
69         redaction = create(:redaction)
70         auth_header = bearer_authorization_header create(:user), :scopes => %w[read_prefs write_api]
71
72         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header
73
74         assert_response :forbidden, "should need to be moderator to redact."
75         assert_nil old_node.reload.redaction
76       end
77
78       def test_create_by_normal_user_with_write_redactions_scope
79         node = create(:node, :with_history, :version => 2)
80         old_node = node.old_nodes.find_by(:version => 1)
81         redaction = create(:redaction)
82         auth_header = bearer_authorization_header create(:user), :scopes => %w[write_redactions]
83
84         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header
85
86         assert_response :forbidden, "should need to be moderator to redact."
87         assert_nil old_node.reload.redaction
88       end
89
90       def test_create_by_moderator_without_write_redactions_scope
91         node = create(:node, :with_history, :version => 2)
92         old_node = node.old_nodes.find_by(:version => 1)
93         redaction = create(:redaction)
94         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs write_api]
95
96         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header
97
98         assert_response :forbidden, "should need to have write_redactions scope to redact."
99         assert_nil old_node.reload.redaction
100       end
101
102       def test_create_by_moderator_with_write_redactions_scope
103         node = create(:node, :with_history, :version => 2)
104         old_node = node.old_nodes.find_by(:version => 1)
105         redaction = create(:redaction)
106         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_redactions]
107
108         post api_node_version_redaction_path(*old_node.id), :params => { :redaction => redaction.id }, :headers => auth_header
109
110         assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
111         assert_equal redaction, old_node.reload.redaction
112       end
113
114       ##
115       # test the unredaction of an old version of a node, while not being
116       # authorised.
117       def test_destroy_by_unauthorised
118         node = create(:node, :with_history, :version => 2)
119         old_node = node.old_nodes.find_by(:version => 1)
120         redaction = create(:redaction)
121         old_node.redact!(redaction)
122
123         delete api_node_version_redaction_path(*old_node.id)
124
125         assert_response :unauthorized, "should need to be authenticated to unredact."
126         assert_equal redaction, old_node.reload.redaction
127       end
128
129       ##
130       # test the unredaction of an old version of a node, while being
131       # authorised as a normal user.
132       def test_destroy_by_normal_user
133         node = create(:node, :with_history, :version => 2)
134         old_node = node.old_nodes.find_by(:version => 1)
135         redaction = create(:redaction)
136         old_node.redact!(redaction)
137         auth_header = bearer_authorization_header
138
139         delete api_node_version_redaction_path(*old_node.id), :headers => auth_header
140
141         assert_response :forbidden, "should need to be moderator to unredact."
142         assert_equal redaction, old_node.reload.redaction
143       end
144
145       ##
146       # test the unredaction of an old version of a node, while being
147       # authorised as a moderator.
148       def test_destroy_by_moderator
149         node = create(:node, :with_history, :version => 2)
150         old_node = node.old_nodes.find_by(:version => 1)
151         old_node.redact!(create(:redaction))
152         auth_header = bearer_authorization_header create(:moderator_user)
153
154         delete api_node_version_redaction_path(*old_node.id), :headers => auth_header
155
156         assert_response :success, "should be OK to unredact old version as moderator."
157         assert_nil old_node.reload.redaction
158       end
159
160       def test_destroy_at_legacy_route
161         node = create(:node, :with_history, :version => 2)
162         old_node = node.old_nodes.find_by(:version => 1)
163         old_node.redact!(create(:redaction))
164         auth_header = bearer_authorization_header create(:moderator_user)
165
166         post "/api/0.6/node/#{old_node.node_id}/#{old_node.version}/redact", :headers => auth_header
167
168         assert_response :success, "should be OK to unredact old version as moderator."
169         assert_nil old_node.reload.redaction
170       end
171     end
172   end
173 end