Skip to content

Commit

Permalink
Return HTTP 404 if view resource not found (#287) (#288)
Browse files Browse the repository at this point in the history
Added 404 responses to storage service view, storage service edit view,
storage service delete view, fetch job delete view, AIP view, and file
view.
  • Loading branch information
mcantelon authored Feb 1, 2024
1 parent 79fb5a6 commit 2f57c4c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
40 changes: 40 additions & 0 deletions AIPscan/Aggregator/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,43 @@ def test_new_fetch_job_bad_connection(app_with_populated_files, mocker):
response = test_client.post("/aggregator/new_fetch_job/1")
task.assert_not_called()
assert response.status_code == 400


def test_view_storage_service(app_with_populated_files):
with current_app.test_client() as test_client:
response = test_client.get("/aggregator/storage_service/1")
assert response.status_code == 200

response = test_client.get("/aggregator/storage_service/0")
assert response.status_code == 404


def test_edit_storage_service(app_with_populated_files):
with current_app.test_client() as test_client:
response = test_client.post("/aggregator/edit_storage_service/1")
assert response.status_code == 200

response = test_client.post("/aggregator/edit_storage_service/0")
assert response.status_code == 404


def test_delete_storage_service(app_with_populated_files, mocker):
with current_app.test_client() as test_client:
response = test_client.get("/aggregator/delete_storage_service/0")
assert response.status_code == 404

mocker.patch("AIPscan.Aggregator.tasks.delete_storage_service.delay")

response = test_client.get("/aggregator/delete_storage_service/1")
assert response.status_code == 302


def test_delete_fetch_job(app_with_populated_files, mocker):
with current_app.test_client() as test_client:
response = test_client.get("/aggregator/delete_fetch_job/0")
assert response.status_code == 404

mocker.patch("AIPscan.Aggregator.tasks.delete_fetch_job.delay")

response = test_client.get("/aggregator/delete_fetch_job/1")
assert response.status_code == 302
33 changes: 30 additions & 3 deletions AIPscan/Aggregator/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
from datetime import datetime

from celery.result import AsyncResult
from flask import Blueprint, flash, jsonify, redirect, render_template, request, url_for
from flask import (
Blueprint,
abort,
flash,
jsonify,
redirect,
render_template,
request,
url_for,
)

from AIPscan import db
from AIPscan.Aggregator import database_helpers, tasks
Expand Down Expand Up @@ -77,6 +86,10 @@ def ss_default():
@aggregator.route("/storage_service/<storage_service_id>", methods=["GET"])
def storage_service(storage_service_id):
storage_service = StorageService.query.get(storage_service_id)

if storage_service is None:
abort(404)

mets_fetch_jobs = FetchJob.query.filter_by(
storage_service_id=storage_service_id
).all()
Expand All @@ -97,6 +110,10 @@ def storage_services():
def edit_storage_service(storage_service_id):
form = StorageServiceForm()
storage_service = StorageService.query.get(storage_service_id)

if storage_service is None:
abort(404)

if request.method == "GET":
form.name.data = storage_service.name
form.url.data = storage_service.url
Expand Down Expand Up @@ -149,8 +166,13 @@ def new_storage_service():

@aggregator.route("/delete_storage_service/<storage_service_id>", methods=["GET"])
def delete_storage_service(storage_service_id):
tasks.delete_storage_service.delay(storage_service_id)
storage_service = StorageService.query.get(storage_service_id)

if storage_service is None:
abort(404)

tasks.delete_storage_service.delay(storage_service_id)

flash("Storage service '{}' is being deleted".format(storage_service.name))
return redirect(url_for("aggregator.storage_services"))

Expand Down Expand Up @@ -214,9 +236,14 @@ def new_fetch_job(fetch_job_id):

@aggregator.route("/delete_fetch_job/<fetch_job_id>", methods=["GET"])
def delete_fetch_job(fetch_job_id):
tasks.delete_fetch_job.delay(fetch_job_id)
fetch_job = FetchJob.query.get(fetch_job_id)

if fetch_job is None:
abort(404)

storage_service = StorageService.query.get(fetch_job.storage_service_id)
tasks.delete_fetch_job.delay(fetch_job_id)

flash("Fetch job {} is being deleted".format(fetch_job.download_start))
return redirect(
url_for("aggregator.storage_service", storage_service_id=storage_service.id)
Expand Down
18 changes: 18 additions & 0 deletions AIPscan/Reporter/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,21 @@ def test_get_file_pager(app_instance, mocker, page, pager_page):
assert pager.per_page == 2
assert pager.prev_num is None
assert pager.next_num is None


def test_view_aip(app_with_populated_files):
with current_app.test_client() as test_client:
response = test_client.get("/reporter/aip/1")
assert response.status_code == 200

response = test_client.get("/reporter/aip/0")
assert response.status_code == 404


def test_view_file(app_with_populated_files):
with current_app.test_client() as test_client:
response = test_client.get("/reporter/file/1")
assert response.status_code == 200

response = test_client.get("/reporter/file/0")
assert response.status_code == 404
18 changes: 17 additions & 1 deletion AIPscan/Reporter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
from datetime import datetime

import requests
from flask import Response, jsonify, make_response, render_template, request, session
from flask import (
Response,
abort,
jsonify,
make_response,
render_template,
request,
session,
)

from AIPscan.Aggregator.task_helpers import get_mets_url
from AIPscan.models import (
Expand Down Expand Up @@ -153,6 +161,10 @@ def view_aips():
def view_aip(aip_id):
"""Detailed view of specific AIP."""
aip = AIP.query.get(aip_id)

if aip is None:
abort(404)

fetch_job = FetchJob.query.get(aip.fetch_job_id)
storage_service = StorageService.query.get(fetch_job.storage_service_id)
storage_location = StorageLocation.query.get(aip.storage_location_id)
Expand Down Expand Up @@ -205,6 +217,10 @@ def view_aip(aip_id):
def view_file(file_id):
"""File page displays Object and Event metadata for file"""
file_ = File.query.get(file_id)

if file_ is None:
abort(404)

aip = AIP.query.get(file_.aip_id)
events = Event.query.filter_by(file_id=file_id).all()
preservation_file = File.query.filter_by(
Expand Down

0 comments on commit 2f57c4c

Please sign in to comment.