Handle not found domain.

This commit is contained in:
Santiago Lamora 2019-12-17 10:25:10 +01:00
parent c69668a9eb
commit c2b3485485
3 changed files with 22 additions and 9 deletions

View File

@ -2,7 +2,9 @@ import requests
import urllib.parse import urllib.parse
from django.conf import settings from django.conf import settings
from django.http import Http404
from django.urls.exceptions import NoReverseMatch from django.urls.exceptions import NoReverseMatch
from django.utils.translation import gettext_lazy as _
from .models import Domain, DatabaseService, MailService, SaasService, UserAccount 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}) path = API_PATHS.get('domain-detail').format_map({'pk': pk})
url = urllib.parse.urljoin(self.base_url, path) 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) return Domain.new_from_json(domain_json)
def retrieve_domain_list(self): def retrieve_domain_list(self):

View File

@ -1,3 +1,14 @@
from django.test import TestCase 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)

View File

@ -1,4 +1,3 @@
from itertools import groupby from itertools import groupby
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
@ -190,18 +189,17 @@ class DomainDetailView(CustomContextMixin, UserTokenRequiredMixin, DetailView):
template_name = "musician/domain_detail.html" template_name = "musician/domain_detail.html"
def get_queryset(self): 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): def get_object(self, queryset=None):
if queryset is None: if queryset is None:
queryset = self.get_queryset() queryset = self.get_queryset()
pk = self.kwargs.get(self.pk_url_kwarg) pk = self.kwargs.get(self.pk_url_kwarg)
# TODO try to retrieve object capturing exception
try:
domain = self.orchestra.retrieve_domain(pk) domain = self.orchestra.retrieve_domain(pk)
except:
raise
return domain return domain