From c2b3485485a9eb88c77c23d66bfa6958a3188c91 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Tue, 17 Dec 2019 10:25:10 +0100 Subject: [PATCH] Handle not found domain. --- musician/api.py | 6 +++++- musician/tests.py | 13 ++++++++++++- musician/views.py | 12 +++++------- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/musician/api.py b/musician/api.py index dcfe7e2..4df4943 100644 --- a/musician/api.py +++ b/musician/api.py @@ -2,7 +2,9 @@ import requests import urllib.parse from django.conf import settings +from django.http import Http404 from django.urls.exceptions import NoReverseMatch +from django.utils.translation import gettext_lazy as _ from .models import Domain, DatabaseService, MailService, SaasService, UserAccount @@ -95,7 +97,9 @@ class Orchestra(object): path = API_PATHS.get('domain-detail').format_map({'pk': pk}) url = urllib.parse.urljoin(self.base_url, path) - status, domain_json = self.request("GET", url=url) + status, domain_json = self.request("GET", url=url, raise_exception=False) + if status == 404: + raise Http404(_("No domain found matching the query")) return Domain.new_from_json(domain_json) def retrieve_domain_list(self): diff --git a/musician/tests.py b/musician/tests.py index 7ce503c..b25e23a 100644 --- a/musician/tests.py +++ b/musician/tests.py @@ -1,3 +1,14 @@ from django.test import TestCase -# Create your tests here. + +class DomainsTestCase(TestCase): + def test_domain_not_found(self): + response = self.client.post( + '/auth/login/', + {'username': 'admin', 'password': 'admin'}, + follow=True + ) + self.assertEqual(200, response.status_code) + + response = self.client.get('/domains/3/') + self.assertEqual(404, response.status_code) diff --git a/musician/views.py b/musician/views.py index 7d93509..ae3b019 100644 --- a/musician/views.py +++ b/musician/views.py @@ -1,4 +1,3 @@ - from itertools import groupby from django.core.exceptions import ImproperlyConfigured @@ -190,18 +189,17 @@ class DomainDetailView(CustomContextMixin, UserTokenRequiredMixin, DetailView): template_name = "musician/domain_detail.html" def get_queryset(self): - return [] # self.orchestra.retrieve_domain_list() + # Return an empty list to avoid a request to retrieve all the + # user domains. We will get a 404 if the domain doesn't exists + # while invoking `get_object` + return [] def get_object(self, queryset=None): if queryset is None: queryset = self.get_queryset() pk = self.kwargs.get(self.pk_url_kwarg) - # TODO try to retrieve object capturing exception - try: - domain = self.orchestra.retrieve_domain(pk) - except: - raise + domain = self.orchestra.retrieve_domain(pk) return domain