1 # frozen_string_literal: true
7 class RedactionsControllerTest < ActionDispatch::IntegrationTest
9 # test all routes which lead to this controller
12 { :path => "/api/0.6/relation/1/2/redaction", :method => :post },
13 { :controller => "api/old_relations/redactions", :action => "create", :relation_id => "1", :version => "2" }
16 { :path => "/api/0.6/relation/1/2/redaction", :method => :delete },
17 { :controller => "api/old_relations/redactions", :action => "destroy", :relation_id => "1", :version => "2" }
21 { :controller => "api/old_relations/redactions", :action => "create", :relation_id => "1", :version => "2", :allow_delete => true },
22 { :path => "/api/0.6/relation/1/2/redact", :method => :post }
27 # test that, even as moderator, the current version of a relation
29 def test_create_on_current_version
30 relation = create(:relation, :with_history, :version => 2)
31 old_relation = relation.old_relations.find_by(:version => 2)
32 redaction = create(:redaction)
33 auth_header = bearer_authorization_header create(:moderator_user)
35 post api_relation_version_redaction_path(*old_relation.id), :params => { :redaction => redaction.id }, :headers => auth_header
37 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
38 assert_nil old_relation.reload.redaction
41 def test_create_without_redaction_id
42 relation = create(:relation, :with_history, :version => 2)
43 old_relation = relation.old_relations.find_by(:version => 1)
44 auth_header = bearer_authorization_header create(:moderator_user)
46 post api_relation_version_redaction_path(*old_relation.id), :headers => auth_header
48 assert_response :bad_request, "should need redaction ID to redact."
49 assert_nil old_relation.reload.redaction
53 # test the redaction of an old version of a relation, while not being
55 def test_create_by_unauthorised
56 relation = create(:relation, :with_history, :version => 2)
57 old_relation = relation.old_relations.find_by(:version => 1)
58 redaction = create(:redaction)
60 post api_relation_version_redaction_path(*old_relation.id), :params => { :redaction => redaction.id }
62 assert_response :unauthorized, "should need to be authenticated to redact."
63 assert_nil old_relation.reload.redaction
66 def test_create_by_normal_user_without_write_redactions_scope
67 relation = create(:relation, :with_history, :version => 2)
68 old_relation = relation.old_relations.find_by(:version => 1)
69 redaction = create(:redaction)
70 auth_header = bearer_authorization_header create(:user), :scopes => %w[read_prefs write_api]
72 post api_relation_version_redaction_path(*old_relation.id), :params => { :redaction => redaction.id }, :headers => auth_header
74 assert_response :forbidden, "should need to be moderator to redact."
75 assert_nil old_relation.reload.redaction
78 def test_create_by_normal_user_with_write_redactions_scope
79 relation = create(:relation, :with_history, :version => 2)
80 old_relation = relation.old_relations.find_by(:version => 1)
81 redaction = create(:redaction)
82 auth_header = bearer_authorization_header create(:user), :scopes => %w[write_redactions]
84 post api_relation_version_redaction_path(*old_relation.id), :params => { :redaction => redaction.id }, :headers => auth_header
86 assert_response :forbidden, "should need to be moderator to redact."
87 assert_nil old_relation.reload.redaction
90 def test_create_by_moderator_without_write_redactions_scope
91 relation = create(:relation, :with_history, :version => 2)
92 old_relation = relation.old_relations.find_by(:version => 1)
93 redaction = create(:redaction)
94 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs write_api]
96 post api_relation_version_redaction_path(*old_relation.id), :params => { :redaction => redaction.id }, :headers => auth_header
98 assert_response :forbidden, "should need to have write_redactions scope to redact."
99 assert_nil old_relation.reload.redaction
102 def test_create_by_moderator_with_write_redactions_scope
103 relation = create(:relation, :with_history, :version => 2)
104 old_relation = relation.old_relations.find_by(:version => 1)
105 redaction = create(:redaction)
106 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_redactions]
108 post api_relation_version_redaction_path(*old_relation.id), :params => { :redaction => redaction.id }, :headers => auth_header
110 assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
111 assert_equal redaction, old_relation.reload.redaction
115 # test the unredaction of an old version of a relation, while not being
117 def test_destroy_by_unauthorised
118 relation = create(:relation, :with_history, :version => 2)
119 old_relation = relation.old_relations.find_by(:version => 1)
120 redaction = create(:redaction)
121 old_relation.redact!(redaction)
123 delete api_relation_version_redaction_path(*old_relation.id)
125 assert_response :unauthorized, "should need to be authenticated to unredact."
126 assert_equal redaction, old_relation.reload.redaction
130 # test the unredaction of an old version of a relation, while being
131 # authorised as a normal user.
132 def test_destroy_by_normal_user
133 relation = create(:relation, :with_history, :version => 2)
134 old_relation = relation.old_relations.find_by(:version => 1)
135 redaction = create(:redaction)
136 old_relation.redact!(redaction)
137 auth_header = bearer_authorization_header
139 delete api_relation_version_redaction_path(*old_relation.id), :headers => auth_header
141 assert_response :forbidden, "should need to be moderator to unredact."
142 assert_equal redaction, old_relation.reload.redaction
146 # test the unredaction of an old version of a relation, while being
147 # authorised as a moderator.
148 def test_destroy_by_moderator
149 relation = create(:relation, :with_history, :version => 2)
150 old_relation = relation.old_relations.find_by(:version => 1)
151 old_relation.redact!(create(:redaction))
152 auth_header = bearer_authorization_header create(:moderator_user)
154 delete api_relation_version_redaction_path(*old_relation.id), :headers => auth_header
156 assert_response :success, "should be OK to unredact old version as moderator."
157 assert_nil old_relation.reload.redaction
160 def test_destroy_at_legacy_route
161 relation = create(:relation, :with_history, :version => 2)
162 old_relation = relation.old_relations.find_by(:version => 1)
163 old_relation.redact!(create(:redaction))
164 auth_header = bearer_authorization_header create(:moderator_user)
166 post "/api/0.6/relation/#{old_relation.relation_id}/#{old_relation.version}/redact", :headers => auth_header
168 assert_response :success, "should be OK to unredact old version as moderator."
169 assert_nil old_relation.reload.redaction