-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
16 changes: 16 additions & 0 deletions
16
django_react_jollof/templates/backend/users/tests/test_models.py
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.test import TestCase | ||
from django.contrib.auth.models import User | ||
from users.models import Profile | ||
|
||
|
||
class ProfileModelTest(TestCase): | ||
def setUp(self): | ||
self.user = User.objects.create_user(username="testuser", password="password") | ||
self.profile = Profile.objects.create(user=self.user, role="user") | ||
|
||
def test_profile_creation(self): | ||
self.assertEqual(self.profile.user.username, "testuser") | ||
self.assertEqual(self.profile.role, "user") | ||
|
||
def test_profile_str(self): | ||
self.assertEqual(str(self.profile), "testuser - user") |
33 changes: 33 additions & 0 deletions
33
django_react_jollof/templates/backend/users/tests/test_permissions.py
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from django.test import TestCase | ||
from django.contrib.auth.models import User | ||
from users.models import Profile | ||
from users.permissions import IsAdmin, IsUser | ||
|
||
|
||
class PermissionTest(TestCase): | ||
def setUp(self): | ||
self.admin_user = User.objects.create_user( | ||
username="admin", password="password" | ||
) | ||
Profile.objects.create(user=self.admin_user, role="admin") | ||
|
||
self.regular_user = User.objects.create_user( | ||
username="user", password="password" | ||
) | ||
Profile.objects.create(user=self.regular_user, role="user") | ||
|
||
def test_is_admin_permission(self): | ||
request = self.client.get("/admin/") | ||
request.user = self.admin_user | ||
self.assertTrue(IsAdmin().has_permission(request, None)) | ||
|
||
request.user = self.regular_user | ||
self.assertFalse(IsAdmin().has_permission(request, None)) | ||
|
||
def test_is_user_permission(self): | ||
request = self.client.get("/user/") | ||
request.user = self.regular_user | ||
self.assertTrue(IsUser().has_permission(request, None)) | ||
|
||
request.user = self.admin_user | ||
self.assertFalse(IsUser().has_permission(request, None)) |
24 changes: 24 additions & 0 deletions
24
django_react_jollof/templates/backend/users/tests/test_serializers.py
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from django.test import TestCase | ||
from django.contrib.auth.models import User | ||
from users.serializers import RegisterSerializer, UserSerializer | ||
|
||
|
||
class SerializerTest(TestCase): | ||
def test_register_serializer(self): | ||
data = { | ||
"username": "newuser", | ||
"email": "[email protected]", | ||
"password": "password", | ||
} | ||
serializer = RegisterSerializer(data=data) | ||
self.assertTrue(serializer.is_valid()) | ||
user = serializer.save() | ||
self.assertEqual(user.username, "newuser") | ||
|
||
def test_user_serializer(self): | ||
user = User.objects.create_user( | ||
username="testuser", email="[email protected]", password="password" | ||
) | ||
serializer = UserSerializer(user) | ||
self.assertEqual(serializer.data["username"], "testuser") | ||
self.assertEqual(serializer.data["email"], "[email protected]") |
69 changes: 69 additions & 0 deletions
69
django_react_jollof/templates/backend/users/tests/test_views.py
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from django.test import TestCase | ||
from django.contrib.auth.models import User | ||
from rest_framework.test import APIClient | ||
from rest_framework import status | ||
from users.models import Profile | ||
|
||
|
||
class ViewTest(TestCase): | ||
def setUp(self): | ||
self.client = APIClient() | ||
self.admin_user = User.objects.create_user( | ||
username="admin", password="password" | ||
) | ||
self.admin_profile = Profile.objects.create(user=self.admin_user, role="admin") | ||
|
||
self.user = User.objects.create_user(username="user", password="password") | ||
self.user_profile = Profile.objects.create(user=self.user, role="user") | ||
|
||
def test_register_view(self): | ||
response = self.client.post( | ||
"/register/", | ||
{ | ||
"username": "newuser", | ||
"email": "[email protected]", | ||
"password": "password", | ||
}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertIn("message", response.data) | ||
|
||
def test_login_view(self): | ||
response = self.client.post( | ||
"/login/", | ||
{"username": "user", "password": "password"}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertIn("access", response.data) | ||
|
||
response = self.client.post( | ||
"/login/", | ||
{"username": "user", "password": "wrongpassword"}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_profile_view(self): | ||
self.client.force_authenticate(user=self.user) | ||
response = self.client.get("/profile/") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data["username"], "user") | ||
|
||
def test_admin_only_view(self): | ||
self.client.force_authenticate(user=self.admin_user) | ||
response = self.client.get("/admin/") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data["message"], "Welcome, admin!") | ||
|
||
self.client.force_authenticate(user=self.user) | ||
response = self.client.get("/admin/") | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
||
def test_user_only_view(self): | ||
self.client.force_authenticate(user=self.user) | ||
response = self.client.get("/user/") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data["message"], "Welcome, regular user!") | ||
|
||
self.client.force_authenticate(user=self.admin_user) | ||
response = self.client.get("/user/") | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |