Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for accounts app #4402

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/unit/accounts/test_apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest
from django.apps import AppConfig
from accounts.apps import AccountsConfig


class TestAccountsConfig(unittest.TestCase):
def test_is_subclass_of_AppConfig(self):
self.assertTrue(issubclass(AccountsConfig, AppConfig))


if __name__ == "__main__":
unittest.main()
13 changes: 12 additions & 1 deletion tests/unit/accounts/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib.auth.models import User
from django.test import TestCase

from accounts.models import Profile, UserStatus
from accounts.models import Profile, UserStatus, JwtToken


class BaseTestCase(TestCase):
Expand Down Expand Up @@ -31,3 +31,14 @@ def test__str__(self):
self.assertEqual(
"{}".format(self.profile.user), self.profile.__str__()
)


class JwtTokenTestCase(BaseTestCase):
def setUp(self):
super(JwtTokenTestCase, self).setUp()
self.jwt_token = JwtToken.objects.create(user=self.user)

def test__str__(self):
self.assertEqual(
"{}".format(self.jwt_token.user), self.jwt_token.__str__()
)
25 changes: 25 additions & 0 deletions tests/unit/accounts/test_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.test import TestCase
from allauth.account.models import EmailAddress
from accounts.models import User
from accounts.permissions import HasVerifiedEmail


class TestHasVerifiedEmailPermission(TestCase):
def setUp(self):
self.user = User.objects.create_user(
username='testuser',
email='test@example.com',
password='password123'
)
self.unverified_email = EmailAddress.objects.create(
user=self.user,
email='test@example.com',
verified=False
)

def test_has_permission_with_no_verified_email(self):
permission = HasVerifiedEmail()
request = self.client.get('/')
request.user = self.user
EmailAddress.objects.all().delete()
self.assertFalse(permission.has_permission(request, None))
69 changes: 69 additions & 0 deletions tests/unit/accounts/test_serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from django.test import TestCase
from django.contrib.auth import get_user_model
from rest_framework.exceptions import ValidationError
from accounts.serializers import CustomPasswordResetSerializer, ProfileSerializer


class TestCustomPasswordResetSerializer(TestCase):
def setUp(self):
self.user_model = get_user_model()
self.active_user = self.user_model.objects.create_user(username='active_user', email='active@example.com', password='password')
self.inactive_user = self.user_model.objects.create_user(username='inactive_user', email='inactive@example.com', password='password')
self.inactive_user.is_active = False
self.inactive_user.save()

def test_get_email_options_try_block(self):
serializer = CustomPasswordResetSerializer(data={'email': self.active_user.email})
self.assertEqual(serializer.is_valid(), True)
expected_email_options = super(CustomPasswordResetSerializer, serializer).get_email_options()
self.assertEqual(serializer.get_email_options(), expected_email_options)

def test_get_email_options_except_block(self):
serializer = CustomPasswordResetSerializer(data={'email': 'nonexistent@example.com'})
serializer.is_valid() # Ensure data is validated
with self.assertRaises(ValidationError) as e:
serializer.get_email_options()
self.assertEqual(e.exception.detail, {'details': "User with the given email does not exist."})

def test_get_email_options_inactive_user(self):
serializer = CustomPasswordResetSerializer(data={'email': self.inactive_user.email})
serializer.is_valid() # Ensure data is validated
with self.assertRaises(ValidationError) as e:
serializer.get_email_options()
self.assertEqual(e.exception.detail, {'details': "Account is not active. Please contact the administrator."})


class TestProfileSerializer(TestCase):
def setUp(self):
self.user_model = get_user_model()
self.user = self.user_model.objects.create_user(username='test_user', email='test@example.com', password='password')

def test_profile_serializer_fields(self):
serializer = ProfileSerializer(self.user)
expected_fields = [
"pk",
"email",
"username",
"first_name",
"last_name",
"affiliation",
"github_url",
"google_scholar_url",
"linkedin_url",
]
self.assertEqual(set(serializer.data.keys()), set(expected_fields))

def test_profile_serializer_update(self):
serializer = ProfileSerializer(self.user, data={
"affiliation": "Test Affiliation",
"github_url": "https://github.com/test",
"google_scholar_url": "https://scholar.google.com/citations?user=test",
"linkedin_url": "https://www.linkedin.com/in/test",
})
self.assertTrue(serializer.is_valid())
serializer.save()
self.user.refresh_from_db()
self.assertEqual(self.user.profile.affiliation, "Test Affiliation")
self.assertEqual(self.user.profile.github_url, "https://github.com/test")
self.assertEqual(self.user.profile.google_scholar_url, "https://scholar.google.com/citations?user=test")
self.assertEqual(self.user.profile.linkedin_url, "https://www.linkedin.com/in/test")
29 changes: 29 additions & 0 deletions tests/unit/accounts/test_throttles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from unittest.mock import MagicMock
from django.test import TestCase
from accounts.throttles import ResendEmailThrottle


class TestResendEmailThrottle(TestCase):
def test_get_cache_key_else_statement(self):
throttle = ResendEmailThrottle()
request = MagicMock()
request.user.is_authenticated = False
request.META = {'REMOTE_ADDR': '192.168.1.1'}

cache_key = throttle.get_cache_key(request, MagicMock())

self.assertIn('throttle_resend_email', cache_key)
self.assertIn('192.168.1.1', cache_key)

def test_get_cache_key_else_statement_with_auth_user(self):
throttle = ResendEmailThrottle()
user = MagicMock()
user.pk = 1
request = MagicMock()
request.user = user
request.user.is_authenticated = True

cache_key = throttle.get_cache_key(request, MagicMock())

self.assertIn('throttle_resend_email', cache_key)
self.assertIn('1', cache_key)