This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
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.
Add unit testing for users model, forms and views (#28)
* add unit test for Login User * update unit testing and delete mash test * Address pull request review comments --------- Co-authored-by: Afrar Malakooth <mmafrar@gmail.com>
- Loading branch information
1 parent
f3c787c
commit 09c6c35
Showing
5 changed files
with
156 additions
and
38 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Empty file.
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,68 @@ | ||
from django.test import TestCase | ||
from django.contrib.auth.models import User | ||
from users.forms import RegisterForm, UserUpdateForm, UpdateProfileForm | ||
from users.models import Profile | ||
|
||
|
||
class TestUserForms(TestCase): | ||
|
||
def test_register_form_valid(self): | ||
# Prepare valid form data | ||
form_data = { | ||
'first_name': 'pravin', | ||
'last_name': 'kannappan', | ||
'username': 'irpravin', | ||
'email': 'irpravin@gmail.com', | ||
'password1': 'pg030399', | ||
'password2': 'pg030399', | ||
} | ||
|
||
# Create form instance with valid data | ||
form = RegisterForm(data=form_data) | ||
|
||
# Check if the form is valid | ||
self.assertTrue(form.is_valid()) | ||
|
||
def test_user_update_form_valid(self): | ||
# Create a User instance | ||
user = User.objects.create_user( | ||
username='irpravin', | ||
email='irpravin@gmail.com', | ||
password='pg030399' | ||
) | ||
|
||
# Prepare valid form data for user update | ||
form_data = { | ||
'first_name': 'Pravin Updated', | ||
'last_name': 'kannappan Updated', | ||
'email': 'irpravin_updated@gmail.com', | ||
} | ||
|
||
# Create form instance with valid data | ||
form = UserUpdateForm(data=form_data, instance=user) | ||
|
||
# Check if the form is valid | ||
self.assertTrue(form.is_valid()) | ||
|
||
def test_update_profile_form_valid(self): | ||
# Create a User instance | ||
user = User.objects.create_user( | ||
username='irpravin', | ||
email='irpravin_updated@gmail.com', | ||
password='pg030399' | ||
) | ||
|
||
# Prepare valid form data for profile update | ||
form_data = { | ||
'avatar': 'user/desktop/pravin.jpg', | ||
'address': 'Tamilnadu, Chennai', | ||
} | ||
|
||
# Retrieve the user's profile or create one if it doesn't exist | ||
profile, created = Profile.objects.get_or_create(user=user) | ||
|
||
# Create form instance with valid data | ||
form = UpdateProfileForm(data=form_data, instance=profile) | ||
|
||
# Check if the form is valid | ||
self.assertTrue(form.is_valid()) |
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,35 @@ | ||
from django.test import TestCase | ||
from django.contrib.auth.models import User | ||
from users.models import Profile | ||
|
||
|
||
class TestProfileModel(TestCase): | ||
|
||
def setUp(self): | ||
# Create a user for testing | ||
self.user = User.objects.create_user( | ||
username='pravin', email='irpravin@gmail.com', password='pg030399' | ||
) | ||
|
||
def test_profile_creation(self): | ||
# Check if a profile is created automatically when a user is created | ||
profile_count = Profile.objects.count() | ||
# Ensure one profile exists after user creation | ||
self.assertEqual(profile_count, 1) | ||
|
||
# Check if the created profile matches the user | ||
profile = Profile.objects.get(user=self.user) | ||
self.assertEqual(profile.user, self.user) | ||
# Default address should be empty | ||
self.assertEqual(profile.address, '') | ||
|
||
# Update profile address and check | ||
profile.address = 'Tamilnadu, Chennai.' | ||
profile.save() | ||
self.assertEqual(profile.address, 'Tamilnadu, Chennai.') | ||
|
||
def test_profile_str_method(self): | ||
# Test the __str__ method of the Profile model | ||
profile = Profile.objects.get(user=self.user) | ||
# Expecting the username as the string representation | ||
self.assertEqual(str(profile), 'pravin') |
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,53 @@ | ||
from django.test import TestCase, RequestFactory | ||
from django.urls import reverse | ||
from django.contrib.auth.models import User | ||
from users.views import RegisterView, CustomLoginView, UpdateUserView, ViewUserProfileView | ||
|
||
|
||
class TestViews(TestCase): | ||
def setUp(self): | ||
self.factory = RequestFactory() | ||
self.user = User.objects.create_user( | ||
username='mash', email='mash@gmail.com', password='Tinksg0303!kk') | ||
self.user.save() | ||
|
||
def test_register_view_get(self): | ||
# Test GET request to register view | ||
request = self.factory.get(reverse('users:users-register')) | ||
request.user = self.user | ||
response = RegisterView.as_view()(request) | ||
|
||
# Check if the response is a redirect (status code 302) | ||
self.assertEqual(response.status_code, 302) | ||
|
||
# Follow the redirect to ensure it goes to the login page | ||
if response.status_code == 302: | ||
# Use the client to follow the redirect | ||
response = self.client.get(response.url) | ||
|
||
# Check if the final response status code after redirect is 200 OK | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_custom_login_view_get(self): | ||
# Test GET request to custom login view | ||
request = self.factory.get(reverse('users:login')) | ||
request.user = self.user # Set the user attribute on the request | ||
response = CustomLoginView.as_view()(request) | ||
# Check if the view returns 200 OK | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_update_user_view_get(self): | ||
# Test GET request to update_user view | ||
request = self.factory.get(reverse('users:edit-profile')) | ||
request.user = self.user # Set the user attribute on the request | ||
response = UpdateUserView.as_view()(request) | ||
# Check if the view returns 200 OK | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_view_user_profile_view(self): | ||
# Test GET request to view_user_profile view | ||
request = self.factory.get(reverse('users:view-profile')) | ||
request.user = self.user # Set the user attribute on the request | ||
response = ViewUserProfileView.as_view()(request) | ||
# Check if the view returns 200 OK | ||
self.assertEqual(response.status_code, 200) |