Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ammar-faifi committed Jul 7, 2022
1 parent faa7479 commit cee6f7c
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions account/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
"""
This module is to test all possible use cases of `account` app.
# Run test
to run this file only:
`python manage.py test account/tests.py`
# Coverage:
- Tests all `account` model operations.
- Tests all relavent GraphQL API operations.
"""

import json
from django.conf import settings
from django.core import mail
Expand All @@ -9,9 +21,9 @@
import django.contrib.auth.views as auth_views


from .models import Profile
from . import views
from data import DepartmentEnum, years
from . import views
from .models import Profile


class UserTestCase(TransactionTestCase):
Expand Down Expand Up @@ -44,7 +56,8 @@ def test_auto_create_profile(self):
Profile._meta.get_field("profile_pic").get_default(),
)
self.assertEqual(
profile.language, Profile._meta.get_field("language").get_default()
profile.language,
Profile._meta.get_field("language").get_default(),
)
self.assertEqual(profile.theme, Profile._meta.get_field("theme").get_default())
self.assertEqual(profile.major, None)
Expand Down Expand Up @@ -101,7 +114,8 @@ def test_crud_prfile(self):
f"profile_pics/test/{self.user.username}",
)
self.assertEqual(
self.user.profile.profile_pic.metadata["original_filename"], "blank_profile"
self.user.profile.profile_pic.metadata["original_filename"],
"blank_profile",
)
self.assertEqual(self.user.profile.profile_pic.metadata["width"], 200)
self.assertEqual(self.user.profile.profile_pic.metadata["format"], "jpg")
Expand Down Expand Up @@ -138,7 +152,8 @@ def test_forbidden_user(self):
self.assertEqual(res.wsgi_request.path, "/account/login/")
self.assertTemplateUsed(res, "registration/login.html")
self.assertEqual(
res.resolver_match.func.__name__, auth_views.LoginView.as_view().__name__
res.resolver_match.func.__name__,
auth_views.LoginView.as_view().__name__,
)

# login with wrong credientials user
Expand All @@ -159,7 +174,8 @@ def test_forbidden_user(self):
self.assertEqual(res.status_code, 403) # not allowed
self.assertEqual(res.wsgi_request.path, "/account/")
self.assertEqual(
res.resolver_match.func.__name__, views.IndexView.as_view().__name__
res.resolver_match.func.__name__,
views.IndexView.as_view().__name__,
)

# logout this user
Expand All @@ -168,7 +184,8 @@ def test_forbidden_user(self):
self.assertEqual(res.status_code, 200)
self.assertEqual(res.wsgi_request.path, "/account/logout/")
self.assertEqual(
res.resolver_match.func.__name__, auth_views.LogoutView.as_view().__name__
res.resolver_match.func.__name__,
auth_views.LogoutView.as_view().__name__,
)

def test_staff_user(self):
Expand All @@ -182,7 +199,8 @@ def test_staff_user(self):
self.assertEqual(res.status_code, 200) # allowed
self.assertEqual(res.wsgi_request.path, "/account/")
self.assertEqual(
res.resolver_match.func.__name__, views.IndexView.as_view().__name__
res.resolver_match.func.__name__,
views.IndexView.as_view().__name__,
)

# logout this user
Expand All @@ -191,7 +209,8 @@ def test_staff_user(self):
self.assertEqual(res.status_code, 200)
self.assertEqual(res.wsgi_request.path, "/account/logout/")
self.assertEqual(
res.resolver_match.func.__name__, auth_views.LogoutView.as_view().__name__
res.resolver_match.func.__name__,
auth_views.LogoutView.as_view().__name__,
)


Expand Down Expand Up @@ -259,7 +278,9 @@ def test_user_cycle(self):
"""

res = self.client.post(
self.endpoint, data={"query": register}, content_type="application/json"
self.endpoint,
data={"query": register},
content_type="application/json",
)
self.assertEqual(res.status_code, 200)
data = json.loads(res.content)["data"]["register"]
Expand Down Expand Up @@ -402,9 +423,7 @@ def test_login(self):
r_token = data["obtainPayload"]["refreshToken"]
self.assertEqual(res.wsgi_request.content_type, "application/json")
self.assertTrue(data["success"])
self.assertTrue(
data["obtainPayload"]["payload"]["username"], self.user.username
)
self.assertTrue(data["obtainPayload"]["payload"]["username"], self.user.username)

# verify that token
res = self.client.post(
Expand All @@ -416,9 +435,7 @@ def test_login(self):
data = json.loads(res.content)["data"]["verifyToken"]
self.assertEqual(res.wsgi_request.content_type, "application/json")
self.assertTrue(data["success"])
self.assertEqual(
data["verifyPayload"]["payload"]["username"], self.user.username
)
self.assertEqual(data["verifyPayload"]["payload"]["username"], self.user.username)

# refresh that token
res = self.client.post(
Expand Down Expand Up @@ -565,7 +582,11 @@ def test_profile_update(self):
# update other user's profile
res = self.client.query(
profileUpdate,
{"pk": self.user2.profile.pk, "theme": "dark", "language": "ar-SA"},
{
"pk": self.user2.profile.pk,
"theme": "dark",
"language": "ar-SA",
},
)
self.assertIsNone(res.errors)
self.assertIsNotNone(res.data)
Expand All @@ -575,7 +596,11 @@ def test_profile_update(self):
# update the user profile
res = self.client.query(
profileUpdate,
{"pk": self.user.profile.pk, "theme": "dark", "language": "ar-SA"},
{
"pk": self.user.profile.pk,
"theme": "dark",
"language": "ar-SA",
},
)

self.assertIsNone(res.errors)
Expand Down

0 comments on commit cee6f7c

Please sign in to comment.