|
| 1 | +# Copyright (C) 2021 Intel Corporation |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +from django.urls import reverse |
| 6 | +from rest_framework import status |
| 7 | +from rest_framework.test import APITestCase |
| 8 | +from rest_framework.authtoken.models import Token |
| 9 | +from django.test import override_settings |
| 10 | +from cvat.apps.iam.urls import urlpatterns as iam_url_patterns |
| 11 | +from django.urls import path, re_path |
| 12 | +from allauth.account.views import ConfirmEmailView, EmailVerificationSentView |
| 13 | + |
| 14 | + |
| 15 | +urlpatterns = iam_url_patterns + [ |
| 16 | + re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$', ConfirmEmailView.as_view(), |
| 17 | + name='account_confirm_email'), |
| 18 | + path('register/account-email-verification-sent', EmailVerificationSentView.as_view(), |
| 19 | + name='account_email_verification_sent'), |
| 20 | +] |
| 21 | + |
| 22 | + |
| 23 | +class UserRegisterAPITestCase(APITestCase): |
| 24 | + |
| 25 | + user_data = {'first_name': 'test_first', 'last_name': 'test_last', 'username': 'test_username', |
| 26 | + 'email': '[email protected]', 'password1': '$Test357Test%', 'password2': '$Test357Test%', |
| 27 | + 'confirmations': []} |
| 28 | + |
| 29 | + def _run_api_v1_user_register(self, data): |
| 30 | + url = reverse('rest_register') |
| 31 | + response = self.client.post(url, data, format='json') |
| 32 | + return response |
| 33 | + |
| 34 | + def _check_response(self, response, data): |
| 35 | + self.assertEqual(response.status_code, status.HTTP_201_CREATED) |
| 36 | + self.assertEqual(response.data, data) |
| 37 | + |
| 38 | + @override_settings(ACCOUNT_EMAIL_VERIFICATION='none') |
| 39 | + def test_api_v1_user_register_with_email_verification_none(self): |
| 40 | + """ |
| 41 | + Ensure we can register a user and get auth token key when email verification is none |
| 42 | + """ |
| 43 | + response = self._run_api_v1_user_register(self.user_data) |
| 44 | + user_token = Token.objects.get(user__username=response.data['username']) |
| 45 | + self._check_response(response, {'first_name': 'test_first', 'last_name': 'test_last', |
| 46 | + 'username': 'test_username', 'email': '[email protected]', |
| 47 | + 'email_verification_required': False, 'key': user_token.key}) |
| 48 | + |
| 49 | + # Since URLConf is executed before running the tests, so we have to manually configure the url patterns for |
| 50 | + # the tests and pass it using ROOT_URLCONF in the override settings decorator |
| 51 | + |
| 52 | + @override_settings(ACCOUNT_EMAIL_VERIFICATION='optional', ROOT_URLCONF=__name__) |
| 53 | + def test_api_v1_user_register_with_email_verification_optional(self): |
| 54 | + """ |
| 55 | + Ensure we can register a user and get auth token key when email verification is optional |
| 56 | + """ |
| 57 | + response = self._run_api_v1_user_register(self.user_data) |
| 58 | + user_token = Token.objects.get(user__username=response.data['username']) |
| 59 | + self._check_response(response, {'first_name': 'test_first', 'last_name': 'test_last', |
| 60 | + 'username': 'test_username', 'email': '[email protected]', |
| 61 | + 'email_verification_required': False, 'key': user_token.key}) |
| 62 | + |
| 63 | + @override_settings(ACCOUNT_EMAIL_REQUIRED=True, ACCOUNT_EMAIL_VERIFICATION='mandatory', |
| 64 | + EMAIL_BACKEND='django.core.mail.backends.console.EmailBackend', ROOT_URLCONF=__name__) |
| 65 | + def test_register_account_with_email_verification_mandatory(self): |
| 66 | + """ |
| 67 | + Ensure we can register a user and it does not return auth token key when email verification is mandatory |
| 68 | + """ |
| 69 | + response = self._run_api_v1_user_register(self.user_data) |
| 70 | + self._check_response(response, {'first_name': 'test_first', 'last_name': 'test_last', |
| 71 | + 'username': 'test_username', 'email': '[email protected]', |
| 72 | + 'email_verification_required': True, 'key': None}) |
0 commit comments