Skip to content

Commit

Permalink
Nicer handling of job not found on the job details view (#295)
Browse files Browse the repository at this point in the history
* Nicer handling of job not found on the job details view

* Update server/src/views.py

Co-authored-by: Nancy Chen <nancy.chen@canonical.com>

---------

Co-authored-by: Nancy Chen <nancy.chen@canonical.com>
  • Loading branch information
plars and nancyc12 authored Jul 12, 2024
1 parent d6fd583 commit 30ab937
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
11 changes: 11 additions & 0 deletions server/src/templates/job_not_found.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.html' %}
{% set active_page = 'jobs' %}
{% block content %}
<div class="p-strip is-shallow">
<div class="row">
<h1 class="p-heading--3">
Job not found: {{ job_id }}
</h1>
</div>
</div>
{% endblock %}
5 changes: 5 additions & 0 deletions server/src/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def jobs():
def job_detail(job_id):
"""Job detail view"""
job_data = mongo.db.jobs.find_one({"job_id": job_id})
if not job_data:
response = make_response(
render_template("job_not_found.html", job_id=job_id), 404
)
return response
return render_template("job_detail.html", job=job_data)


Expand Down
16 changes: 15 additions & 1 deletion server/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import mongomock
from mock import patch
from src.views import queues_data, agent_detail
from src.views import job_detail, queues_data, agent_detail


def test_queues():
Expand Down Expand Up @@ -96,3 +96,17 @@ def test_agent_not_found(testapp):

assert "Agent not found: agent1" in str(response.data)
assert response.status_code == 404


def test_job_not_found(testapp):
"""
Test that the job_detail fails gracefully when a job is not found
"""

mongo = mongomock.MongoClient()
with patch("src.views.mongo", mongo):
with testapp.test_request_context():
response = job_detail("job1")

assert "Job not found: job1" in str(response.data)
assert response.status_code == 404

0 comments on commit 30ab937

Please sign in to comment.