From 23b73e7deb5d2795cc6764daec7e2d60a635debf Mon Sep 17 00:00:00 2001 From: Ngure Nyaga Date: Mon, 22 Nov 2021 08:21:02 +0300 Subject: [PATCH] fix: set up different URL for wagtail content API --- config/urls.py | 2 +- mycarehub/templates/404.html | 8 ++++++++ mycarehub/users/models.py | 2 +- mycarehub/users/signals.py | 7 +++++++ mycarehub/users/tests/test_signals.py | 24 ++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/config/urls.py b/config/urls.py index 7c83548..da1f45e 100644 --- a/config/urls.py +++ b/config/urls.py @@ -118,7 +118,7 @@ wagtail_api_router.register_endpoint("documents", DocumentsAPIViewSet) urlpatterns += [ - path("api/cms/", wagtail_api_router.urls), + path("contentapi/", wagtail_api_router.urls), path("admin/", include(wagtailadmin_urls)), path("documents/", include(wagtaildocs_urls)), re_path( diff --git a/mycarehub/templates/404.html b/mycarehub/templates/404.html index 784709f..560f565 100644 --- a/mycarehub/templates/404.html +++ b/mycarehub/templates/404.html @@ -3,6 +3,14 @@ {% block content %}

Page not found

+{% block sidebar %} + {#% do not show the sidebar %#} +{% endblock sidebar %} + +{% block topbar %} + {#% do not show the top bar %#} +{% endblock topbar %} +
404
diff --git a/mycarehub/users/models.py b/mycarehub/users/models.py index e791b9b..129315b 100644 --- a/mycarehub/users/models.py +++ b/mycarehub/users/models.py @@ -55,7 +55,7 @@ def default_organisation(): return org.pk except (ProgrammingError, Exception): # pragma: nocover # this will occur during initial migrations on a clean db - return settings.DEFAULT_ORG_ID + return uuid.UUID(settings.DEFAULT_ORG_ID) class TermsOfService(Model): diff --git a/mycarehub/users/signals.py b/mycarehub/users/signals.py index 20af6ee..e005ef8 100644 --- a/mycarehub/users/signals.py +++ b/mycarehub/users/signals.py @@ -10,6 +10,7 @@ from django.dispatch import receiver from django.http import HttpRequest from django.template.loader import get_template +from rest_framework.authtoken.models import Token LOGGER = logging.getLogger(__name__) BASIC_PERMISSIONS = [ @@ -148,3 +149,9 @@ def send_user_account_approved_email(user): # record the notification so that we do not re-send it user.approval_notified = True user.save() + + +@receiver(post_save, sender=settings.AUTH_USER_MODEL) +def create_auth_token(sender, instance=None, created=False, **kwargs): + if created: + Token.objects.create(user=instance) diff --git a/mycarehub/users/tests/test_signals.py b/mycarehub/users/tests/test_signals.py index a7a02ae..ef8c91b 100644 --- a/mycarehub/users/tests/test_signals.py +++ b/mycarehub/users/tests/test_signals.py @@ -5,6 +5,7 @@ from django.contrib.auth import get_user_model from faker import Faker from model_bakery import baker +from rest_framework.authtoken.models import Token from mycarehub.users.signals import ( BASIC_PERMISSIONS, @@ -153,3 +154,26 @@ def test_account_confirmed_handler_newly_created_whitelist_user(): email="noreply@savannahghi.org", ) assert account_confirmed_handler(User, user, created=True) is None + + +def test_create_auth_token(): + # create a user + user = baker.make( + User, + email="token@savannahghi.org", + ) + + # confirm that a token was created + token = Token.objects.get(user=user) + assert token is not None + + # update the user + user.email = "token2@savannahghi.org" + user.save() + + # re-fetch the token + token2 = Token.objects.get(user__email="token2@savannahghi.org") + assert token2 is not None + + # confirm that the token was not regenerated + assert token.pk == token2.pk