Skip to content

Commit

Permalink
Merge pull request #50 from null-open-security-community/test_cases_WIP
Browse files Browse the repository at this point in the history
Initial Commit For Test Case of JobViewSet
  • Loading branch information
hims1911 authored Dec 30, 2023
2 parents c735a07 + b873f2c commit f4bc0f9
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
69 changes: 69 additions & 0 deletions apps/accounts/tests.py
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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": "[email protected]",
"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'])
66 changes: 66 additions & 0 deletions apps/jobs/tests.py
Original file line number Diff line number Diff line change
@@ -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


0 comments on commit f4bc0f9

Please sign in to comment.