From 991dd8a637a2568993c958fd2bd9ff4246ce04d9 Mon Sep 17 00:00:00 2001 From: Himanshu Sharma Date: Sun, 17 Dec 2023 19:58:11 +0530 Subject: [PATCH 1/2] Initial Commit For Test Case of JobViewSet --- apps/jobs/tests.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/apps/jobs/tests.py b/apps/jobs/tests.py index 7ce503c..e963591 100644 --- a/apps/jobs/tests.py +++ b/apps/jobs/tests.py @@ -1,3 +1,69 @@ +import uuid + from django.test import TestCase # Create your tests here. + + +from django.test import TestCase +from rest_framework.test import APIClient +from rest_framework import status +from rest_framework_simplejwt.tokens import AccessToken + +from .models import Job, Company +from apps.jobs.models import User + +class JobViewSetsTestCase(TestCase): + def setUp(self): + # Create sample data for testing + self.company_data = { + "name": "Test Company", + "location": "Test Location", + "about": "A test company for unit testing", + "company_id": uuid.uuid4(), + } + self.company = Company.objects.create(**self.company_data) + + # job_data struct + self.job_data = { + "job_role": "Software Engineer", + "company": self.company, + "description": "Sample job description", + "location": "Mumbai", + "post_date": "2023-01-01", + "posted": True, + "experience": 2, + "employer_id": uuid.uuid4(), + "job_type": "Full-Time", + "salary": 75000.00, + "qualifications": "Bachelor's degree in Computer Science", + "vacency_position": 3, + "industry": "Technology", + "job_responsibilities": "Sample responsibilities", + "skills_required": "Python, Django, REST API", + "education_or_certifications": "Bachelor's degree", + # Add other required fields for Job model + } + self.job = Job.objects.create(**self.job_data) + + # job url + self.job_url = '/jobs/' + + # Create an instance of the APIClient and set the Authorization header + self.client = APIClient() + + + + def test_list_jobs(self): + # Test the list action with no filters + response = self.client.get(self.job_url) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(len(response.data), 1) # Assuming there is one job in the database + + def test_filter_jobs(self): + # Test the list action with filters + response = self.client.get(self.job_url, {'location': 'Mumbai'}) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(len(response.data), 1) # Assuming one job matches the filter + + From b873f2c9932e08447b7f899feb6f2caf2ede6036 Mon Sep 17 00:00:00 2001 From: Himanshu Sharma Date: Wed, 20 Dec 2023 22:40:22 +0530 Subject: [PATCH 2/2] unit testing of registration view --- apps/accounts/tests.py | 69 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/apps/accounts/tests.py b/apps/accounts/tests.py index 7ce503c..1265277 100644 --- a/apps/accounts/tests.py +++ b/apps/accounts/tests.py @@ -1,3 +1,72 @@ from django.test import TestCase # Create your tests here. +from django.test import TestCase +from django.urls import reverse +from rest_framework import status +from rest_framework.test import APIClient +from .models import User +from apps.accounts.serializers import UserRegistrationSerializer # Replace with actual import path +# from apps.accounts.utils import generate_guest_token # Replace with actual import path +from apps.accounts.renderers import UserRenderer # Replace with actual import path + + + +from django.test import TestCase +from rest_framework.test import APIClient +from rest_framework import status +from unittest.mock import patch +from django.contrib.auth import get_user_model + +class UserRegistrationViewTest(TestCase): + def setUp(self): + self.client = APIClient() + self.url = '/register/' + + @patch('apps.accounts.views.generate_guest_token') + def test_user_registration_successful(self, mock_generate_guest_token): + + # Mocked Token + mock_generate_guest_token.return_value = "your-token" + + # Create a valid user registration payload + payload = { + "email": "Test@gmail.com", + "name": "Test User", + "password": "testpassword", + "password2": "testpassword", + "user_type": "Employer", + } + + # Make a POST request to the registration endpoint + response = self.client.post(self.url, payload, format='json') + print(response.data) + + # Check if the response is as expected (HTTP 200 OK) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + # Check if the response contains the expected message and token + self.assertIn("OTP Sent Successfully. Please Check your Email", response.data['msg']) + self.assertIn("token", response.data) + self.assertEqual(response.data['token'], "your-token") + + def test_user_registration_failure(self): + # Create an invalid user registration payload + invalid_payload = { + "email": "test@gmail.com", + "name": "Test User", + "password": "testpassword", + "password2": "differentpassword", + "user_type": "Employer", + } + + # Make a POST request to the registration endpoint with invalid payload + response = self.client.post(self.url, invalid_payload, format='json') + + # Check if the response is as expected (HTTP 400 Bad Request) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + print(response.data) + + # Check if the response contains the expected error message + self.assertIn("Password and Confirm Password doesn't match", response.data['non_field_errors'])