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