diff --git a/core/tests/test_auth.py b/core/tests/test_auth.py index 4637c809a422aa68820a53b70fc6ef446f984d51..2920c63617b9e0ba2859e66d545cc18ced2ff3de 100644 --- a/core/tests/test_auth.py +++ b/core/tests/test_auth.py @@ -128,3 +128,42 @@ class AuthTests(APITestCase): } response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + def test_validate_token_default(self): + username = "xjhmlcy" + email = "xjhmlcy123@gmail.com" + password = "abcdefg123" + token, token_key = self.add_test_person(username, email, password) + self.client.credentials(HTTP_AUTHORIZATION='Token ' + token_key) + + url = "/api/auth/validate-token" + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data['valid'], 'true') + + def test_validate_token_invalid(self): + username = "xjhmlcy" + email = "xjhmlcy123@gmail.com" + password = "abcdefg123" + token, token_key = self.add_test_person(username, email, password) + self.client.credentials(HTTP_AUTHORIZATION='Token ' + token_key[:-1] + "1") + token.user.is_active = True + token.user.save() + + url = "/api/auth/validate-token" + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data['valid'], 'false') + + def test_varify_user_and_activate_default(self): + username = "xjhmlcy" + email = "xjhmlcy123@gmail.com" + password = "abcdefg123" + token, token_key = self.add_test_person(username, email, password) + token.user.is_active = False + token.user.save() + + url = f"/api/auth/activate/{token.digest}" + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertTrue(User.objects.get().is_active) \ No newline at end of file diff --git a/core/tests/test_password.py b/core/tests/test_password.py new file mode 100644 index 0000000000000000000000000000000000000000..64683d05a2ba0a27f30c6c704b45ec28c18cbb1b --- /dev/null +++ b/core/tests/test_password.py @@ -0,0 +1,57 @@ +from django.contrib.auth.models import User +from rest_framework.test import APITestCase +from rest_framework import status +from knox.models import AuthToken + +from core.models.profile import Profile + + +class PasswordTests(APITestCase): + + """ + Test suite for Password + """ + def add_test_person(self, username, email, password): + user = User.objects.create_user(username=username, email=email, password=password) + return AuthToken.objects.create(user) + + def test_password_change_default(self): + username = "xjhmlcy" + email = "xjhmlcy123@gmail.com" + old_password = "abcdefg123" + new_password = "abcdefg123456" + token, token_key = self.add_test_person(username, email, old_password) + self.client.credentials(HTTP_AUTHORIZATION='Token ' + token_key) + + url = "/api/change-password" + data = { + "old_password": old_password, + "new_password": new_password + } + response = self.client.patch(url, data, format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + url = "/api/auth/login" + data = { + "username": email, + "password": new_password + } + response = self.client.post(url, data, format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + def test_password_change_wrong_old_password(self): + username = "xjhmlcy" + email = "xjhmlcy123@gmail.com" + old_password = "abcdefg123" + new_password = "abcdefg123456" + token, token_key = self.add_test_person(username, email, old_password) + self.client.credentials(HTTP_AUTHORIZATION='Token ' + token_key) + + url = "/api/change-password" + data = { + "old_password": old_password + "123", + "new_password": new_password + } + response = self.client.patch(url, data, format='json') + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + diff --git a/requirements.txt b/requirements.txt index faa06226a9dd56141939c347810a56363c9912be..369acab3c1d0e0574b2ef6d32d52e5e872077177 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +coverage==6.3.2 Django==4.1 django_countries==7.5 django_extensions==3.2.1