-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor old test cases and write some more test cases for views (#33)
- Loading branch information
Showing
5 changed files
with
133 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,20 @@ | ||
"""Tests for drf_user/views.py module""" | ||
import pytest | ||
from django.test import Client | ||
from django.test import TestCase | ||
from django.test.client import RequestFactory | ||
from django.urls import reverse | ||
from model_bakery import baker | ||
from rest_framework import status | ||
from rest_framework.test import force_authenticate | ||
from rest_framework.test import APITestCase | ||
|
||
from drf_user.models import AuthTransaction | ||
from drf_user.models import User | ||
from drf_user.views import RetrieveUpdateUserAccountView | ||
|
||
|
||
class TestLoginView(TestCase): | ||
class TestLoginView(APITestCase): | ||
"""LoginView Test""" | ||
|
||
def setUp(self) -> None: | ||
"""Create Client object to call the API""" | ||
self.client = Client() | ||
"""SetUp test data""" | ||
self.url = reverse("Login") | ||
|
||
"""Create User object using model_bakery""" | ||
self.user = baker.make( | ||
"drf_user.User", | ||
username="user", | ||
|
@@ -30,48 +24,48 @@ def setUp(self) -> None: | |
is_active=True, | ||
) | ||
|
||
"""Setting user's password""" | ||
self.user.set_password("pass123") | ||
self.user.save() | ||
|
||
@pytest.mark.django_db | ||
def test_object_created(self): | ||
"""Check if the User object is created or not""" | ||
self.assertEqual(User.objects.count(), 1) | ||
assert User.objects.count() == 1 | ||
|
||
@pytest.mark.django_db | ||
def test_successful_login_view(self): | ||
"""Check if the credentials are correct""" | ||
response = self.client.post( | ||
reverse("Login"), data={"username": "user", "password": "pass123"} | ||
self.url, data={"username": "user", "password": "pass123"} | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
assert response.status_code == 200 | ||
|
||
@pytest.mark.django_db | ||
def test_unsuccessful_login_view(self): | ||
"""Check if the credentials are incorrect""" | ||
response = self.client.post( | ||
reverse("Login"), data={"username": "user", "password": "pass1234"} | ||
self.url, data={"username": "user", "password": "pass1234"} | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_422_UNPROCESSABLE_ENTITY) | ||
|
||
assert response.status_code == 422 | ||
|
||
|
||
class TestRetrieveUpdateUserAccountView(TestCase): | ||
class TestRetrieveUpdateUserAccountView(APITestCase): | ||
"""RetrieveUpdateUserAccountView Test""" | ||
|
||
def setUp(self) -> None: | ||
"""Create RequestFactory object to call the API""" | ||
self.factory = RequestFactory() | ||
"""SetUp test data""" | ||
self.url = reverse("Retrieve Update Profile") | ||
|
||
"""Create User object using model_bakery""" | ||
self.user = baker.make( | ||
"drf_user.User", | ||
username="user", | ||
email="[email protected]", | ||
mobile=1234569877, | ||
password="old_password", | ||
) | ||
|
||
"""Create auth_transaction object using model_bakery""" | ||
self.auth_transaction = baker.make( | ||
"drf_user.AuthTransaction", | ||
created_by=self.user, | ||
|
@@ -80,69 +74,50 @@ def setUp(self) -> None: | |
@pytest.mark.django_db | ||
def test_object_created(self): | ||
"""Check if AuthTransaction object created or not""" | ||
assert User.objects.count() == 1 | ||
assert AuthTransaction.objects.count() == 1 | ||
|
||
@pytest.mark.django_db | ||
def test_get_object_method(self): | ||
"""Create request object using factory""" | ||
request = self.factory.get(reverse("Retrieve Update Profile")) | ||
|
||
"""Simulating that self.user has made the request""" | ||
request.user = self.user | ||
|
||
"""Creates and sets up the RetrieveUpdateUserAccountView""" | ||
view = RetrieveUpdateUserAccountView() | ||
view.setup(request) | ||
|
||
self.assertEqual(view.get_object(), self.user) | ||
|
||
@pytest.mark.django_db | ||
def test_get_user_account_view(self): | ||
"""Create request object using factory""" | ||
request = self.factory.get(reverse("Retrieve Update Profile")) | ||
"""Check Retrieve Update Profile View returns user""" | ||
self.client.force_authenticate(self.user) | ||
response = self.client.get(self.url) | ||
|
||
"""Authenticating the request""" | ||
force_authenticate( | ||
request=request, user=self.user, token=self.auth_transaction.token | ||
) | ||
assert response.status_code == 200 | ||
assert response.data["username"] == self.user.username | ||
|
||
"""Creates and sets up the RetrieveUpdateUserAccountView""" | ||
view = RetrieveUpdateUserAccountView.as_view() | ||
response = view(request) | ||
@pytest.mark.django_db | ||
def test_update_username(self): | ||
""" | ||
Check patch request to Retrieve Update Profile view updates user's username | ||
""" | ||
self.client.force_authenticate(self.user) | ||
response = self.client.patch(self.url, {"username": "updated_username"}) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data["username"], self.user.username) | ||
assert response.status_code == 200 | ||
assert self.user.username == "updated_username" | ||
|
||
@pytest.mark.django_db | ||
def test_update_user_account_view(self): | ||
"""Create request object using factory""" | ||
request = self.factory.patch( | ||
reverse("Retrieve Update Profile"), | ||
data={"username": "updated_username"}, | ||
content_type="application/json", | ||
) | ||
|
||
"""Authenticating the request""" | ||
force_authenticate( | ||
request=request, user=self.user, token=self.auth_transaction.token | ||
) | ||
def test_update_password(self): | ||
""" | ||
Check patch request to Retrieve Update Profile view updates user's password | ||
""" | ||
self.client.force_authenticate(self.user) | ||
assert self.user.password == "old_password" | ||
|
||
"""Creates and sets up the RetrieveUpdateUserAccountView""" | ||
view = RetrieveUpdateUserAccountView.as_view() | ||
response = view(request) | ||
response = self.client.patch(self.url, {"password": "my_unique_password"}) | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(self.user.username, "updated_username") | ||
assert response.status_code == 200 | ||
assert self.user.password == "my_unique_password" | ||
|
||
|
||
class TestCheckUniqueView(TestCase): | ||
class TestCheckUniqueView(APITestCase): | ||
"""CheckUniqueView Test""" | ||
|
||
def setUp(self) -> None: | ||
"""Create Client object to call the API""" | ||
self.client = Client() | ||
"""SetUp test data""" | ||
self.url = reverse("Check Unique") | ||
|
||
"""Create User object using model_bakery""" | ||
self.user = baker.make( | ||
"drf_user.User", | ||
username="user", | ||
|
@@ -153,24 +128,72 @@ def setUp(self) -> None: | |
@pytest.mark.django_db | ||
def test_user_object_created(self): | ||
"""Check if User object created or not""" | ||
self.assertEqual(User.objects.count(), 1) | ||
assert User.objects.count() == 1 | ||
|
||
@pytest.mark.django_db | ||
def test_is_unique(self): | ||
"""Check if the user is unique""" | ||
response = self.client.post( | ||
reverse("Check Unique"), data={"prop": "username", "value": "user7"} | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
response_data = response.json() | ||
self.assertTrue(response_data["data"][0]["unique"]) | ||
response = self.client.post(self.url, {"prop": "username", "value": "user7"}) | ||
assert response.status_code == 200 | ||
self.assertTrue(response.json()["data"][0]["unique"]) | ||
|
||
@pytest.mark.django_db | ||
def test_is_not_unique(self): | ||
"""Check if the user is non-unique""" | ||
response = self.client.post( | ||
reverse("Check Unique"), data={"prop": "username", "value": "user"} | ||
"""Check if the user is not unique""" | ||
response = self.client.post(self.url, {"prop": "username", "value": "user"}) | ||
assert response.status_code == 200 | ||
self.assertFalse(response.json()["data"][0]["unique"]) | ||
|
||
@pytest.mark.django_db | ||
def test_data_invalid(self): | ||
"""Check CheckUniqueView view raises 422 code when passed data is invalid""" | ||
response = self.client.post(self.url, {"prop": "invalid", "value": "user"}) | ||
assert response.status_code == 422 | ||
|
||
|
||
class TestRegisterView(APITestCase): | ||
"""RegisterView Test""" | ||
|
||
def setUp(self) -> None: | ||
"""SetUp test data""" | ||
# pre validate email | ||
self.validated_email = baker.make( | ||
"drf_user.OTPValidation", destination="[email protected]", is_validated=True | ||
) | ||
# pre validate mobile | ||
self.validated_mobile = baker.make( | ||
"drf_user.OTPValidation", destination="1234567890", is_validated=True | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
response_data = response.json() | ||
self.assertFalse(response_data["data"][0]["unique"]) | ||
self.url = reverse("Register") | ||
self.validated_data = { | ||
"username": "my_username", | ||
"password": "test_password", | ||
"name": "random_name", | ||
"email": "[email protected]", | ||
"mobile": 1234567890, | ||
} | ||
self.not_validated_data = { | ||
"username": "random", | ||
"password": "test_password", | ||
"name": "random_name", | ||
"email": "[email protected]", | ||
"mobile": 8800880080, | ||
} | ||
|
||
def test_register_with_validated_email_and_mobile(self): | ||
"""Check user creation when validated mobile and email is passed""" | ||
|
||
response = self.client.post(self.url, self.validated_data) | ||
|
||
assert response.status_code == 201 | ||
assert "my_username" in response.json()["username"] | ||
assert "random_name" in response.json()["name"] | ||
|
||
def test_raise_validation_error_when_email_mobile_not_validated(self): | ||
"""Check view raises Validation Error when mobile and email is not validated""" | ||
|
||
response = self.client.post(self.url, self.not_validated_data) | ||
|
||
assert response.status_code == 400 | ||
assert "The email must be pre-validated via OTP." in response.json()["email"] | ||
assert "The mobile must be pre-validated via OTP." in response.json()["mobile"] |