Skip to content

Commit

Permalink
Added test for users
Browse files Browse the repository at this point in the history
  • Loading branch information
sir-temi committed Dec 30, 2024
1 parent c47859f commit 9a6fd9b
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
Empty file.
16 changes: 16 additions & 0 deletions django_react_jollof/templates/backend/users/tests/test_models.py
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")
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))
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 django_react_jollof/templates/backend/users/tests/test_views.py
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)

0 comments on commit 9a6fd9b

Please sign in to comment.