Skip to content

Commit

Permalink
support list of ids
Browse files Browse the repository at this point in the history
  • Loading branch information
sastels committed Oct 4, 2024
1 parent 3182f04 commit e15f37c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 25 deletions.
41 changes: 27 additions & 14 deletions app/support/rest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from uuid import UUID

from flask import Blueprint, Response, jsonify
from flask import Blueprint, Response, jsonify, request
from sqlalchemy.orm.exc import NoResultFound

from app.dao.jobs_dao import dao_get_job_by_id
Expand All @@ -14,7 +14,7 @@
register_errors(support_blueprint)


def notification_query(id: UUID) -> dict | None:
def notification_query(id: str) -> dict | None:
try:
notification = get_notification_by_id(id)
if notification:
Expand All @@ -37,7 +37,7 @@ def notification_query(id: UUID) -> dict | None:
return None


def template_query(id: UUID) -> dict | None:
def template_query(id: str) -> dict | None:
try:
template = dao_get_template_by_id(id)
if template:
Expand All @@ -53,7 +53,7 @@ def template_query(id: UUID) -> dict | None:
return None


def service_query(id: UUID) -> dict | None:
def service_query(id: str) -> dict | None:
try:
service = dao_fetch_service_by_id(id)
if service:
Expand All @@ -63,7 +63,7 @@ def service_query(id: UUID) -> dict | None:
return None


def job_query(id: UUID) -> dict | None:
def job_query(id: str) -> dict | None:
try:
job = dao_get_job_by_id(id)
if job:
Expand All @@ -86,7 +86,7 @@ def job_query(id: UUID) -> dict | None:
return None


def user_query(id: UUID) -> dict | None:
def user_query(id: str) -> dict | None:
try:
user = get_user_by_id(id)
if user:
Expand All @@ -100,11 +100,24 @@ def user_query(id: UUID) -> dict | None:
return None


@support_blueprint.route("/<uuid:id>", methods=["GET"])
def query_id(id: UUID) -> Response:
for query_func in [user_query, service_query, template_query, job_query, notification_query]:
results = query_func(id)
if results:
return jsonify(results)

return jsonify({"type": "no result found"})
@support_blueprint.route("/search", methods=["GET"])
def query_id() -> Response:
ids = request.args.get("ids")
if not ids:
return jsonify({"error": "no ids provided"})

info = []
for id in ids.split(","):
try:
UUID(id)
except ValueError:
info.append({id: {"type": "not a uuid"}})
continue
for query_func in [user_query, service_query, template_query, job_query, notification_query]:
results = query_func(id)
if results:
info.append({id: results})
break
if not results:
info.append({id: {"type": "no result found"}})
return jsonify(info)
42 changes: 31 additions & 11 deletions tests/app/support/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@


def test_query_id_unknown_uuid(admin_request, sample_user):
json_resp = admin_request.get("support.query_id", id=uuid.uuid4())
id = str(uuid.uuid4())
json_resp = admin_request.get("support.query_id", ids=id)[0][id]
assert json_resp["type"] == "no result found"


def test_query_id_user(admin_request, sample_user):
json_resp = admin_request.get("support.query_id", id=sample_user.id)
json_resp = admin_request.get("support.query_id", ids=sample_user.id)[0][str(sample_user.id)]
assert json_resp["type"] == "user"
assert json_resp["id"] == str(sample_user.id)
assert json_resp["name"] == sample_user.name


def test_query_id_service(admin_request, sample_service):
json_resp = admin_request.get("support.query_id", id=sample_service.id)
json_resp = admin_request.get("support.query_id", ids=sample_service.id)[0][str(sample_service.id)]
assert json_resp["type"] == "service"
assert json_resp["id"] == str(sample_service.id)
assert json_resp["name"] == sample_service.name


def test_query_id_template(admin_request, sample_template):
json_resp = admin_request.get("support.query_id", id=sample_template.id)
json_resp = admin_request.get("support.query_id", ids=sample_template.id)[0][str(sample_template.id)]
assert json_resp["type"] == "template"
assert json_resp["id"] == str(sample_template.id)
assert json_resp["name"] == sample_template.name
Expand All @@ -30,32 +31,51 @@ def test_query_id_template(admin_request, sample_template):


def test_query_id_job(admin_request, sample_job):
json_resp = admin_request.get("support.query_id", id=sample_job.id)
json_resp = admin_request.get("support.query_id", ids=sample_job.id)[0][str(sample_job.id)]
assert json_resp["type"] == "job"
assert json_resp["id"] == str(sample_job.id)
assert json_resp["original_file_name"] == sample_job.original_file_name
# assert json_resp["created_at"] == str(sample_job.created_at)
assert json_resp["created_by_id"] == str(sample_job.created_by_id)
assert json_resp["created_by_name"] == sample_job.created_by.name
# assert json_resp["processing_started"] == str(sample_job.processing_started)
# assert json_resp["processing_finished"] == str(sample_job.processing_finished)
assert json_resp["notification_count"] == sample_job.notification_count
assert json_resp["job_status"] == sample_job.job_status
assert json_resp["service_id"] == str(sample_job.service_id)
assert json_resp["service_name"] == sample_job.service.name


def test_query_id_notification(admin_request, sample_notification_with_job):
json_resp = admin_request.get("support.query_id", id=sample_notification_with_job.id)
json_resp = admin_request.get("support.query_id", ids=sample_notification_with_job.id)[0][
str(sample_notification_with_job.id)
]
assert json_resp["type"] == "notification"
assert json_resp["id"] == str(sample_notification_with_job.id)
assert json_resp["notification_type"] == sample_notification_with_job.notification_type
assert json_resp["status"] == sample_notification_with_job.status
# assert json_resp["created_at"] == sample_notification_with_job.created_at
# assert json_resp["sent_at"] == sample_notification_with_job.sent_at
assert json_resp["to"] == sample_notification_with_job.to
assert json_resp["service_id"] == str(sample_notification_with_job.service_id)
assert json_resp["service_name"] == sample_notification_with_job.service.name
assert json_resp["job_id"] == str(sample_notification_with_job.job_id)
assert json_resp["job_row_number"] == sample_notification_with_job.job_row_number
assert json_resp["api_key_id"] is None


def test_no_ids(admin_request):
json_resp = admin_request.get("support.query_id", ids=None)
assert json_resp == {"error": "no ids provided"}


def test_empty_ids(admin_request):
json_resp = admin_request.get("support.query_id", ids=[])
assert json_resp == {"error": "no ids provided"}


def test_id_not_uuid(admin_request):
id = "hello"
json_resp = admin_request.get("support.query_id", ids=id)[0][id]
assert json_resp["type"] == "not a uuid"


def test_two_ids(admin_request, sample_user, sample_service):
json_resp = admin_request.get("support.query_id", ids=f"{sample_user.id},{sample_service.id}")
assert json_resp[0][str(sample_user.id)]["type"] == "user"
assert json_resp[1][str(sample_service.id)]["type"] == "service"

0 comments on commit e15f37c

Please sign in to comment.