5 class RedactionsControllerTest < ActionDispatch::IntegrationTest
7 # test all routes which lead to this controller
10 { :path => "/api/0.6/way/1/2/redaction", :method => :post },
11 { :controller => "api/old_ways/redactions", :action => "create", :way_id => "1", :version => "2" }
14 { :path => "/api/0.6/way/1/2/redaction", :method => :delete },
15 { :controller => "api/old_ways/redactions", :action => "destroy", :way_id => "1", :version => "2" }
19 { :controller => "api/old_ways/redactions", :action => "create", :way_id => "1", :version => "2", :allow_delete => true },
20 { :path => "/api/0.6/way/1/2/redact", :method => :post }
25 # test that, even as moderator, the current version of a way
27 def test_create_on_current_version
28 way = create(:way, :with_history, :version => 2)
29 old_way = way.old_ways.find_by(:version => 2)
30 redaction = create(:redaction)
31 auth_header = bearer_authorization_header create(:moderator_user)
33 post api_way_version_redaction_path(*old_way.id), :params => { :redaction => redaction.id }, :headers => auth_header
35 assert_response :bad_request, "shouldn't be OK to redact current version as moderator."
36 assert_nil old_way.reload.redaction
39 def test_create_without_redaction_id
40 way = create(:way, :with_history, :version => 2)
41 old_way = way.old_ways.find_by(:version => 1)
42 auth_header = bearer_authorization_header create(:moderator_user)
44 post api_way_version_redaction_path(*old_way.id), :headers => auth_header
46 assert_response :bad_request, "should need redaction ID to redact."
47 assert_nil old_way.reload.redaction
51 # test the redaction of an old version of a way, while not being
53 def test_create_by_unauthorised
54 way = create(:way, :with_history, :version => 2)
55 old_way = way.old_ways.find_by(:version => 1)
56 redaction = create(:redaction)
58 post api_way_version_redaction_path(*old_way.id), :params => { :redaction => redaction.id }
60 assert_response :unauthorized, "should need to be authenticated to redact."
61 assert_nil old_way.reload.redaction
64 def test_create_by_normal_user_without_write_redactions_scope
65 way = create(:way, :with_history, :version => 2)
66 old_way = way.old_ways.find_by(:version => 1)
67 redaction = create(:redaction)
68 auth_header = bearer_authorization_header create(:user), :scopes => %w[read_prefs write_api]
70 post api_way_version_redaction_path(*old_way.id), :params => { :redaction => redaction.id }, :headers => auth_header
72 assert_response :forbidden, "should need to be moderator to redact."
73 assert_nil old_way.reload.redaction
76 def test_create_by_normal_user_with_write_redactions_scope
77 way = create(:way, :with_history, :version => 2)
78 old_way = way.old_ways.find_by(:version => 1)
79 redaction = create(:redaction)
80 auth_header = bearer_authorization_header create(:user), :scopes => %w[write_redactions]
82 post api_way_version_redaction_path(*old_way.id), :params => { :redaction => redaction.id }, :headers => auth_header
84 assert_response :forbidden, "should need to be moderator to redact."
85 assert_nil old_way.reload.redaction
88 def test_create_by_moderator_without_write_redactions_scope
89 way = create(:way, :with_history, :version => 2)
90 old_way = way.old_ways.find_by(:version => 1)
91 redaction = create(:redaction)
92 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs write_api]
94 post api_way_version_redaction_path(*old_way.id), :params => { :redaction => redaction.id }, :headers => auth_header
96 assert_response :forbidden, "should need to have write_redactions scope to redact."
97 assert_nil old_way.reload.redaction
100 def test_create_by_moderator_with_write_redactions_scope
101 way = create(:way, :with_history, :version => 2)
102 old_way = way.old_ways.find_by(:version => 1)
103 redaction = create(:redaction)
104 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_redactions]
106 post api_way_version_redaction_path(*old_way.id), :params => { :redaction => redaction.id }, :headers => auth_header
108 assert_response :success, "should be OK to redact old version as moderator with write_redactions scope."
109 assert_equal redaction, old_way.reload.redaction
113 # test the unredaction of an old version of a way, while not being
115 def test_destroy_by_unauthorised
116 way = create(:way, :with_history, :version => 2)
117 old_way = way.old_ways.find_by(:version => 1)
118 redaction = create(:redaction)
119 old_way.redact!(redaction)
121 delete api_way_version_redaction_path(*old_way.id)
123 assert_response :unauthorized, "should need to be authenticated to unredact."
124 assert_equal redaction, old_way.reload.redaction
128 # test the unredaction of an old version of a way, while being
129 # authorised as a normal user.
130 def test_destroy_by_normal_user
131 way = create(:way, :with_history, :version => 2)
132 old_way = way.old_ways.find_by(:version => 1)
133 redaction = create(:redaction)
134 old_way.redact!(redaction)
135 auth_header = bearer_authorization_header
137 delete api_way_version_redaction_path(*old_way.id), :headers => auth_header
139 assert_response :forbidden, "should need to be moderator to unredact."
140 assert_equal redaction, old_way.reload.redaction
144 # test the unredaction of an old version of a way, while being
145 # authorised as a moderator.
146 def test_destroy_by_moderator
147 way = create(:way, :with_history, :version => 2)
148 old_way = way.old_ways.find_by(:version => 1)
149 old_way.redact!(create(:redaction))
150 auth_header = bearer_authorization_header create(:moderator_user)
152 delete api_way_version_redaction_path(*old_way.id), :headers => auth_header
154 assert_response :success, "should be OK to unredact old version as moderator."
155 assert_nil old_way.reload.redaction
158 def test_destroy_at_legacy_route
159 way = create(:way, :with_history, :version => 2)
160 old_way = way.old_ways.find_by(:version => 1)
161 old_way.redact!(create(:redaction))
162 auth_header = bearer_authorization_header create(:moderator_user)
164 post "/api/0.6/way/#{old_way.way_id}/#{old_way.version}/redact", :headers => auth_header
166 assert_response :success, "should be OK to unredact old version as moderator."
167 assert_nil old_way.reload.redaction