|
41 | 41 |
|
42 | 42 | context "logged in as moderator user" do |
43 | 43 | let(:user) { FactoryBot.create(:moderator_user) } |
44 | | - before { login_as(user) } |
| 44 | + let(:token) { SecureRandom.base64(32) } |
| 45 | + let(:verifier) { ActiveSupport::MessageVerifier.new(token, serializer: JSON) } |
| 46 | + let(:signature_ids) { verifier.generate([signature.id]) } |
| 47 | + |
| 48 | + before do |
| 49 | + login_as(user) |
| 50 | + session[:_bulk_verification_token] = token |
| 51 | + end |
45 | 52 |
|
46 | 53 | describe "GET /admin/signatures" do |
47 | 54 | before { get :index, q: "Alice" } |
|
168 | 175 | before do |
169 | 176 | expect(Signature).to receive(:find).with([signature.id]).and_return([signature]) |
170 | 177 | expect(signature).to receive(:validate!).and_return(true) |
171 | | - post :bulk_validate, ids: signature.id, q: "user@example.com" |
| 178 | + post :bulk_validate, selected_ids: signature.id, all_ids: signature_ids, q: "user@example.com" |
172 | 179 | end |
173 | 180 |
|
174 | 181 | it "redirects to the search page" do |
|
187 | 194 | expect(Signature).to receive(:find).with([signature.id]).and_return([signature]) |
188 | 195 | expect(signature).to receive(:validate!).and_raise(exception) |
189 | 196 | expect(Appsignal).to receive(:send_exception).with(exception) |
190 | | - post :bulk_validate, ids: signature.id, q: "user@example.com" |
| 197 | + post :bulk_validate, selected_ids: signature.id, all_ids: signature_ids, q: "user@example.com" |
191 | 198 | end |
192 | 199 |
|
193 | 200 | it "redirects to the search page" do |
|
198 | 205 | expect(flash[:alert]).to eq("Signatures could not be validated - please contact support") |
199 | 206 | end |
200 | 207 | end |
| 208 | + |
| 209 | + context "when the signature ids hmac is missing" do |
| 210 | + before do |
| 211 | + expect(Signature).not_to receive(:find) |
| 212 | + end |
| 213 | + |
| 214 | + it "returns a 400 Bad Request" do |
| 215 | + expect { |
| 216 | + delete :bulk_validate, selected_ids: signature.id, all_ids: "", q: "user@example.com" |
| 217 | + }.to raise_error(BulkVerification::InvalidBulkRequest, /Invalid bulk request for \[\d+\]/) |
| 218 | + end |
| 219 | + end |
| 220 | + |
| 221 | + context "when the selected_ids param contains an invalid id" do |
| 222 | + before do |
| 223 | + expect(Signature).not_to receive(:find) |
| 224 | + end |
| 225 | + |
| 226 | + it "returns a 400 Bad Request" do |
| 227 | + expect { |
| 228 | + delete :bulk_validate, selected_ids: "1,2", all_ids: signature_ids, q: "user@example.com" |
| 229 | + }.to raise_error(BulkVerification::InvalidBulkRequest, /Invalid bulk request - \d+ not present in \[\d+\]/) |
| 230 | + end |
| 231 | + end |
201 | 232 | end |
202 | 233 |
|
203 | 234 | describe "POST /admin/signatures/invalidate" do |
204 | 235 | context "when the signature is invalidated" do |
205 | 236 | before do |
206 | 237 | expect(Signature).to receive(:find).with([signature.id]).and_return([signature]) |
207 | 238 | expect(signature).to receive(:invalidate!).and_return(true) |
208 | | - post :bulk_invalidate, ids: signature.id, q: "user@example.com" |
| 239 | + post :bulk_invalidate, selected_ids: signature.id, all_ids: signature_ids, q: "user@example.com" |
209 | 240 | end |
210 | 241 |
|
211 | 242 | it "redirects to the search page" do |
|
224 | 255 | expect(Signature).to receive(:find).with([signature.id]).and_return([signature]) |
225 | 256 | expect(signature).to receive(:invalidate!).and_raise(exception) |
226 | 257 | expect(Appsignal).to receive(:send_exception).with(exception) |
227 | | - post :bulk_invalidate, ids: signature.id, q: "user@example.com" |
| 258 | + post :bulk_invalidate, selected_ids: signature.id, all_ids: signature_ids, q: "user@example.com" |
228 | 259 | end |
229 | 260 |
|
230 | 261 | it "redirects to the search page" do |
|
235 | 266 | expect(flash[:alert]).to eq("Signatures could not be invalidated - please contact support") |
236 | 267 | end |
237 | 268 | end |
| 269 | + |
| 270 | + context "when the signature ids hmac is missing" do |
| 271 | + before do |
| 272 | + expect(Signature).not_to receive(:find) |
| 273 | + end |
| 274 | + |
| 275 | + it "returns a 400 Bad Request" do |
| 276 | + expect { |
| 277 | + delete :bulk_invalidate, selected_ids: signature.id, all_ids: "", q: "user@example.com" |
| 278 | + }.to raise_error(BulkVerification::InvalidBulkRequest, /Invalid bulk request for \[\d+\]/) |
| 279 | + end |
| 280 | + end |
| 281 | + |
| 282 | + context "when the selected_ids param contains an invalid id" do |
| 283 | + before do |
| 284 | + expect(Signature).not_to receive(:find) |
| 285 | + end |
| 286 | + |
| 287 | + it "returns a 400 Bad Request" do |
| 288 | + expect { |
| 289 | + delete :bulk_invalidate, selected_ids: "1,2", all_ids: signature_ids, q: "user@example.com" |
| 290 | + }.to raise_error(BulkVerification::InvalidBulkRequest, /Invalid bulk request - \d+ not present in \[\d+\]/) |
| 291 | + end |
| 292 | + end |
238 | 293 | end |
239 | 294 |
|
240 | 295 | describe "DELETE /admin/signatures" do |
241 | 296 | context "when the signature is destroyed" do |
242 | 297 | before do |
243 | 298 | expect(Signature).to receive(:find).with([signature.id]).and_return([signature]) |
244 | 299 | expect(signature).to receive(:destroy!).and_return(true) |
245 | | - delete :bulk_destroy, ids: signature.id, q: "user@example.com" |
| 300 | + delete :bulk_destroy, selected_ids: signature.id, all_ids: signature_ids, q: "user@example.com" |
246 | 301 | end |
247 | 302 |
|
248 | 303 | it "redirects to the search page" do |
|
261 | 316 | expect(Signature).to receive(:find).with([signature.id]).and_return([signature]) |
262 | 317 | expect(signature).to receive(:destroy!).and_raise(exception) |
263 | 318 | expect(Appsignal).to receive(:send_exception).with(exception) |
264 | | - delete :bulk_destroy, ids: signature.id, q: "user@example.com" |
| 319 | + delete :bulk_destroy, selected_ids: signature.id, all_ids: signature_ids, q: "user@example.com" |
265 | 320 | end |
266 | 321 |
|
267 | 322 | it "redirects to the search page" do |
|
272 | 327 | expect(flash[:alert]).to eq("Signatures could not be removed - please contact support") |
273 | 328 | end |
274 | 329 | end |
| 330 | + |
| 331 | + context "when the signature ids hmac is missing" do |
| 332 | + before do |
| 333 | + expect(Signature).not_to receive(:find) |
| 334 | + end |
| 335 | + |
| 336 | + it "returns a 400 Bad Request" do |
| 337 | + expect { |
| 338 | + delete :bulk_destroy, selected_ids: signature.id, all_ids: "", q: "user@example.com" |
| 339 | + }.to raise_error(BulkVerification::InvalidBulkRequest, /Invalid bulk request for \[\d+\]/) |
| 340 | + end |
| 341 | + end |
| 342 | + |
| 343 | + context "when the selected_ids param contains an invalid id" do |
| 344 | + before do |
| 345 | + expect(Signature).not_to receive(:find) |
| 346 | + end |
| 347 | + |
| 348 | + it "returns a 400 Bad Request" do |
| 349 | + expect { |
| 350 | + delete :bulk_destroy, selected_ids: "1,2", all_ids: signature_ids, q: "user@example.com" |
| 351 | + }.to raise_error(BulkVerification::InvalidBulkRequest, /Invalid bulk request - \d+ not present in \[\d+\]/) |
| 352 | + end |
| 353 | + end |
275 | 354 | end |
276 | 355 | end |
277 | 356 | end |
0 commit comments