From b69822444aeddb447de148d951aefde3ee551ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Mon, 23 Sep 2019 15:06:55 +0300 Subject: [PATCH 1/9] Refactor tests for authorization --- apps/account/tests.py | 3 --- apps/account/tests/__init__.py | 0 apps/account/tests/tests_common.py | 11 +++++++++++ apps/authorization/tests/tests.py | 29 +++++++++++++++++++++-------- 4 files changed, 32 insertions(+), 11 deletions(-) delete mode 100644 apps/account/tests.py create mode 100644 apps/account/tests/__init__.py create mode 100644 apps/account/tests/tests_common.py diff --git a/apps/account/tests.py b/apps/account/tests.py deleted file mode 100644 index 7ce503c2..00000000 --- a/apps/account/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/apps/account/tests/__init__.py b/apps/account/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/account/tests/tests_common.py b/apps/account/tests/tests_common.py new file mode 100644 index 00000000..fcabe946 --- /dev/null +++ b/apps/account/tests/tests_common.py @@ -0,0 +1,11 @@ +from rest_framework.test import APITestCase + + +# Create your tests here. + + +class AccountTests(APITestCase): + + def test_default(self): + print("account") + self.assertTrue(False) diff --git a/apps/authorization/tests/tests.py b/apps/authorization/tests/tests.py index 839af828..d9fd7b71 100644 --- a/apps/authorization/tests/tests.py +++ b/apps/authorization/tests/tests.py @@ -3,15 +3,28 @@ from account.models import User # Create your tests here. +def get_tokens_for_user( + username='sedragurda', password='sedragurdaredips19', email='sedragurda@desoz.com'): + + user = User.objects.create_user(username=username, email=email, password=password) + tokens = User.create_jwt_tokens(user) + + return { + "username": username, + "password": password, + "email": email, + "newsletter": True, + "user": user, + "tokens": tokens + } + + class AuthorizationTests(APITestCase): def setUp(self): - self.username = 'sedragurda' - self.password = 'sedragurdaredips19' - self.email = 'sedragurda@desoz.com' - self.newsletter = True - self.user = User.objects.create_user(username=self.username, email=self.email, password=self.password) - self.tokkens = User.create_jwt_tokens(self.user) + data = get_tokens_for_user() + self.username = data["username"] + self.password = data["password"] def LoginTests(self): data ={ @@ -20,6 +33,6 @@ class AuthorizationTests(APITestCase): 'remember': True } response = self.client.post('/api/auth/login/', data=data) - self.assertEqual(response.data['access_token'], self.tokkens.get('access_token')) - self.assertEqual(response.data['refresh_token'], self.tokkens.get('refresh_token')) + self.assertEqual(response.data['access_token'], self.tokens.get('access_token')) + self.assertEqual(response.data['refresh_token'], self.tokens.get('refresh_token')) From 23759974d9fbdddb7662200987e39fd70e05238e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Mon, 23 Sep 2019 15:34:40 +0300 Subject: [PATCH 2/9] Add account/user/ test for GET --- apps/account/tests/tests_common.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/apps/account/tests/tests_common.py b/apps/account/tests/tests_common.py index fcabe946..2e66b330 100644 --- a/apps/account/tests/tests_common.py +++ b/apps/account/tests/tests_common.py @@ -1,11 +1,25 @@ from rest_framework.test import APITestCase - - +from rest_framework import status +from authorization.tests.tests import get_tokens_for_user +from http.cookies import SimpleCookie # Create your tests here. class AccountTests(APITestCase): - def test_default(self): - print("account") - self.assertTrue(False) + url = '/api/web/account/user/' + + def setUp(self): + self.data = get_tokens_for_user() + + def test_user_url(self): + response = self.client.get(self.url) + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + + self.client.cookies = SimpleCookie( + {'access_token': self.data['tokens'].get('access_token'), + 'refresh_token': self.data['tokens'].get('access_token')}) + + response = self.client.get(self.url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + From 6e4d2f738fa29a74503fdbe6bee47ab04ccb3c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Mon, 23 Sep 2019 16:18:36 +0300 Subject: [PATCH 3/9] Test for /api/web/account/user/ --- apps/account/tests/tests_common.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/apps/account/tests/tests_common.py b/apps/account/tests/tests_common.py index 2e66b330..c92fe89e 100644 --- a/apps/account/tests/tests_common.py +++ b/apps/account/tests/tests_common.py @@ -5,7 +5,7 @@ from http.cookies import SimpleCookie # Create your tests here. -class AccountTests(APITestCase): +class AccountUserTests(APITestCase): url = '/api/web/account/user/' @@ -23,3 +23,23 @@ class AccountTests(APITestCase): response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_200_OK) + data = { + "username": self.data["username"], + "first_name": "Test first name", + "last_name": "Test last name", + "cropped_image_url": "http://localhost/image/cropped.png", + "image_url": "http://localhost/image/avatar.png", + "email": "sedragurdatest@desoz.com", + "newsletter": self.data["newsletter"] + } + response = self.client.patch(self.url, data=data, format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + data["email"] = "sedragurdatest2@desoz.com" + + response = self.client.put(self.url, data=data, format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + + + From 77b9d3f4a7b5a90fe730acf5e9c7feb6bdeadc2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Mon, 23 Sep 2019 16:38:57 +0300 Subject: [PATCH 4/9] Test account --- apps/account/tests/tests_common.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/apps/account/tests/tests_common.py b/apps/account/tests/tests_common.py index c92fe89e..6a9a1fdd 100644 --- a/apps/account/tests/tests_common.py +++ b/apps/account/tests/tests_common.py @@ -41,5 +41,19 @@ class AccountUserTests(APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) +class AccountChangePasswordTests(APITestCase): + url = '/api/web/account/change-password/' + + def setUp(self): + self.data = get_tokens_for_user() + + def test_change_password(self): + data = { + "password": "new password" + } + + response = self.client.patch(self.url, data=data, format='json') + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + From 70f9100813b02958b78e46688b5b6d6ea1390027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Mon, 23 Sep 2019 17:31:51 +0300 Subject: [PATCH 5/9] Add test for change-password --- apps/account/tests/tests_common.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/apps/account/tests/tests_common.py b/apps/account/tests/tests_common.py index 6a9a1fdd..fed80882 100644 --- a/apps/account/tests/tests_common.py +++ b/apps/account/tests/tests_common.py @@ -49,11 +49,19 @@ class AccountChangePasswordTests(APITestCase): def test_change_password(self): data = { + "old_password" : self.data["password"], "password": "new password" } response = self.client.patch(self.url, data=data, format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + self.client.cookies = SimpleCookie( + {'access_token': self.data['tokens'].get('access_token'), + 'refresh_token': self.data['tokens'].get('access_token')}) + + response = self.client.patch(self.url, data=data, format='json') + self.assertEqual(response.status_code, status.HTTP_200_OK) + From 22c237c3d74d4020b7250db97ac90d3780cbcad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Mon, 23 Sep 2019 18:01:06 +0300 Subject: [PATCH 6/9] add test confirm email --- apps/account/tests/tests_common.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/account/tests/tests_common.py b/apps/account/tests/tests_common.py index fed80882..bd8c13d5 100644 --- a/apps/account/tests/tests_common.py +++ b/apps/account/tests/tests_common.py @@ -2,6 +2,8 @@ from rest_framework.test import APITestCase from rest_framework import status from authorization.tests.tests import get_tokens_for_user from http.cookies import SimpleCookie +from account.models import User +from os import path # Create your tests here. @@ -49,7 +51,7 @@ class AccountChangePasswordTests(APITestCase): def test_change_password(self): data = { - "old_password" : self.data["password"], + "old_password": self.data["password"], "password": "new password" } @@ -64,4 +66,15 @@ class AccountChangePasswordTests(APITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) +class AccountChangePasswordTests(APITestCase): + # "/web/account/email/confirm/{uidb64}/{token}/" + url = "/web/account/email/confirm/" + def setUp(self): + self.data = get_tokens_for_user() + + def test_confirm_email(self): + user = User.objects.get(email=self.data["email"]) + token = user.confirm_email_token + uid64 = user.get_user_uidb64 + url = path.join(self.url, uid64, token, "") From 7f630e9e9449468ebb13e92c39e5c920ada0c402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Tue, 24 Sep 2019 09:23:57 +0300 Subject: [PATCH 7/9] Test web email confirm --- apps/account/tests/tests_common.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/account/tests/tests_common.py b/apps/account/tests/tests_common.py index bd8c13d5..963ba08d 100644 --- a/apps/account/tests/tests_common.py +++ b/apps/account/tests/tests_common.py @@ -68,7 +68,7 @@ class AccountChangePasswordTests(APITestCase): class AccountChangePasswordTests(APITestCase): # "/web/account/email/confirm/{uidb64}/{token}/" - url = "/web/account/email/confirm/" + url = "/api/web/account/email/confirm/" def setUp(self): self.data = get_tokens_for_user() @@ -78,3 +78,5 @@ class AccountChangePasswordTests(APITestCase): token = user.confirm_email_token uid64 = user.get_user_uidb64 url = path.join(self.url, uid64, token, "") + response = self.client.get(url) + self.assertEqual(response.status_code, status.HTTP_200_OK) \ No newline at end of file From 199ca1fa77935a8d4d059975b5a2a12e96588e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Tue, 24 Sep 2019 10:25:44 +0300 Subject: [PATCH 8/9] Refactor test code --- apps/account/tests/tests_common.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/apps/account/tests/tests_common.py b/apps/account/tests/tests_common.py index 963ba08d..5e3935b5 100644 --- a/apps/account/tests/tests_common.py +++ b/apps/account/tests/tests_common.py @@ -3,13 +3,12 @@ from rest_framework import status from authorization.tests.tests import get_tokens_for_user from http.cookies import SimpleCookie from account.models import User -from os import path -# Create your tests here. +from django.urls import reverse class AccountUserTests(APITestCase): - url = '/api/web/account/user/' + url = reverse('web:account:user-retrieve-update') def setUp(self): self.data = get_tokens_for_user() @@ -44,7 +43,8 @@ class AccountUserTests(APITestCase): class AccountChangePasswordTests(APITestCase): - url = '/api/web/account/change-password/' + + url = reverse('web:account:change-password') def setUp(self): self.data = get_tokens_for_user() @@ -67,16 +67,16 @@ class AccountChangePasswordTests(APITestCase): class AccountChangePasswordTests(APITestCase): - # "/web/account/email/confirm/{uidb64}/{token}/" - url = "/api/web/account/email/confirm/" - def setUp(self): self.data = get_tokens_for_user() def test_confirm_email(self): user = User.objects.get(email=self.data["email"]) token = user.confirm_email_token - uid64 = user.get_user_uidb64 - url = path.join(self.url, uid64, token, "") + uidb64 = user.get_user_uidb64 + url = reverse('web:account:confirm-email', kwargs={ + 'uidb64': uidb64, + 'token': token + }) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) \ No newline at end of file From b091da3f5647daee601ffa88ef5dbcce1ff80315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=93=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=BA=D0=B8=D1=85?= Date: Tue, 24 Sep 2019 11:07:07 +0300 Subject: [PATCH 9/9] Fix --- apps/account/tests/tests_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/account/tests/tests_common.py b/apps/account/tests/tests_common.py index 5e3935b5..0992b930 100644 --- a/apps/account/tests/tests_common.py +++ b/apps/account/tests/tests_common.py @@ -79,4 +79,4 @@ class AccountChangePasswordTests(APITestCase): 'token': token }) response = self.client.get(url) - self.assertEqual(response.status_code, status.HTTP_200_OK) \ No newline at end of file + self.assertEqual(response.status_code, status.HTTP_200_OK)