Skip to content

Commit

Permalink
Create tests for recruitment views (#693)
Browse files Browse the repository at this point in the history
* Create tests for recruitment views
  • Loading branch information
Mathias-a authored Sep 22, 2023
1 parent 01ed447 commit 5480078
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 54 deletions.
18 changes: 17 additions & 1 deletion backend/samfundet/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from samfundet.contants import DEV_PASSWORD
from samfundet.models.billig import BilligEvent
from samfundet.models.event import Event, EventAgeRestriction, EventTicketType
from samfundet.models.recruitment import Recruitment, RecruitmentPosition
from samfundet.models.recruitment import Recruitment, RecruitmentPosition, RecruitmentAdmission
from samfundet.models.general import User, Image, InformationPage, Organization, Gang, BlogPost

import root.management.commands.seed_scripts.billig as billig_seed
Expand Down Expand Up @@ -251,3 +251,19 @@ def fixture_blogpost(fixture_image: Image) -> Iterator[BlogPost]:
)
yield blogpost
blogpost.delete()


@pytest.fixture
def fixture_recruitment_admission(fixture_user: User, fixture_recruitment_position: RecruitmentPosition,
fixture_recruitment: Recruitment) -> Iterator[RecruitmentAdmission]:
admission = RecruitmentAdmission.objects.create(
admission_text='Test admission text',
recruitment_position=fixture_recruitment_position,
recruitment=fixture_recruitment,
user=fixture_user,
applicant_priority=1,
recruiter_priority=RecruitmentAdmission.PRIORITY_CHOICES[0][0],
recruiter_status=RecruitmentAdmission.STATUS_CHOICES[0][0],
)
yield admission
admission.delete()
162 changes: 113 additions & 49 deletions backend/samfundet/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

from django.contrib.auth.models import Permission, Group
from django.urls import reverse
from django.utils import timezone
from rest_framework import status
from guardian.shortcuts import assign_perm

from root.utils import routes, permissions
from samfundet.models.recruitment import Recruitment, RecruitmentPosition
from samfundet.models.recruitment import Recruitment, RecruitmentPosition, RecruitmentAdmission
from samfundet.models.general import User, KeyValue, TextItem, InformationPage, BlogPost, Image
from samfundet.serializers import UserSerializer

Expand Down Expand Up @@ -98,54 +99,6 @@ def test_get_groups(fixture_rest_client: APIClient, fixture_user: User):
assert status.is_success(code=response.status_code)


def test_get_recruitments(fixture_rest_client: APIClient, fixture_superuser: User, fixture_recruitment: Recruitment):
### Arrange ###
fixture_rest_client.force_authenticate(user=fixture_superuser)
url = reverse(routes.samfundet__recruitment_list)

### Act ###
response = fixture_rest_client.get(url)

### Assert ###
assert response.status_code == status.HTTP_200_OK
assert len(response.data) == 1
assert response.data[0]['name_nb'] == fixture_recruitment.name_nb


def test_get_recruitment_positions(fixture_rest_client: APIClient, fixture_superuser: User, fixture_recruitment_position: RecruitmentPosition):
### Arrange ###
fixture_rest_client.force_authenticate(user=fixture_superuser)
url = reverse(routes.samfundet__recruitment_position_list)

### Act ###
response = fixture_rest_client.get(url)

### Assert ###
assert response.status_code == status.HTTP_200_OK
assert len(response.data) == 1
assert response.data[0]['name_nb'] == fixture_recruitment_position.name_nb


def test_recruitment_positions_per_recruitment(
fixture_rest_client: APIClient,
fixture_superuser: User,
fixture_recruitment: Recruitment,
fixture_recruitment_position: RecruitmentPosition,
):
### Arrange ###
fixture_rest_client.force_authenticate(user=fixture_superuser)
url = reverse(routes.samfundet__recruitment_positions)
recruitment = fixture_recruitment

### Act ###
response: Response = fixture_rest_client.get(path=f'{url}?recruitment={recruitment.id}')

### Assert ###
assert response.status_code == status.HTTP_200_OK
assert len(response.data) == 1
assert response.data[0]['name_nb'] == fixture_recruitment_position.name_nb


class TestInformationPagesView:

def test_get_informationpage(self, fixture_rest_client: APIClient, fixture_user: User, fixture_informationpage: InformationPage):
Expand Down Expand Up @@ -465,3 +418,114 @@ def test_assign_group_not_found(self, fixture_rest_client: APIClient, fixture_su

### Assert ###
assert response.status_code == status.HTTP_404_NOT_FOUND


# =============================== #
# Recruitment #
# =============================== #


def test_get_recruitments(fixture_rest_client: APIClient, fixture_superuser: User, fixture_recruitment: Recruitment):
### Arrange ###
fixture_rest_client.force_authenticate(user=fixture_superuser)
url = reverse(routes.samfundet__recruitment_list)

### Act ###
response = fixture_rest_client.get(url)

### Assert ###
assert response.status_code == status.HTTP_200_OK
assert len(response.data) == 1
assert response.data[0]['name_nb'] == fixture_recruitment.name_nb


def test_get_recruitment_positions(fixture_rest_client: APIClient, fixture_superuser: User, fixture_recruitment_position: RecruitmentPosition):
### Arrange ###
fixture_rest_client.force_authenticate(user=fixture_superuser)
url = reverse(routes.samfundet__recruitment_position_list)

### Act ###
response = fixture_rest_client.get(url)

### Assert ###
assert response.status_code == status.HTTP_200_OK
assert len(response.data) == 1
assert response.data[0]['name_nb'] == fixture_recruitment_position.name_nb


def test_recruitment_positions_per_recruitment(
fixture_rest_client: APIClient,
fixture_superuser: User,
fixture_recruitment: Recruitment,
fixture_recruitment_position: RecruitmentPosition,
):
### Arrange ###
fixture_rest_client.force_authenticate(user=fixture_superuser)
url = reverse(routes.samfundet__recruitment_positions)

### Act ###
response: Response = fixture_rest_client.get(path=url, data={'recruitment': fixture_recruitment.id})

### Assert ###
assert response.status_code == status.HTTP_200_OK
assert len(response.data) == 1
assert response.data[0]['name_nb'] == fixture_recruitment_position.name_nb


def test_get_applicants_without_interviews(
fixture_rest_client: APIClient, fixture_superuser: User, fixture_recruitment: Recruitment, fixture_user: User,
fixture_recruitment_admission: RecruitmentAdmission
):
### Arrange ###
fixture_rest_client.force_authenticate(user=fixture_superuser)
url = reverse(routes.samfundet__applicants_without_interviews)

### Act ###
response: Response = fixture_rest_client.get(path=url, data={'recruitment': fixture_recruitment.id})

### Assert ###
assert response.status_code == status.HTTP_200_OK
assert len(response.data) == 1
assert response.data[0]['id'] == fixture_user.id
assert response.data[0]['first_name'] == fixture_user.first_name
assert response.data[0]['last_name'] == fixture_user.last_name
assert response.data[0]['email'] == fixture_user.email


def test_get_applicants_without_interviews_when_interview_is_set(
fixture_rest_client: APIClient, fixture_superuser: User, fixture_recruitment: Recruitment, fixture_user: User,
fixture_recruitment_admission: RecruitmentAdmission
):
### Arrange ###
fixture_rest_client.force_authenticate(user=fixture_superuser)
url = reverse(routes.samfundet__applicants_without_interviews)

# Setting the interview time for the user's admission
fixture_recruitment_admission.interview_time = timezone.now() + timezone.timedelta(hours=1)
fixture_recruitment_admission.save()

### Act ###
response: Response = fixture_rest_client.get(path=url, data={'recruitment': fixture_recruitment.id})

### Assert ###
assert response.status_code == status.HTTP_200_OK
assert len(response.data) == 0


def test_recruitment_admission_for_applicant(
fixture_rest_client: APIClient, fixture_user: User, fixture_recruitment_admission: RecruitmentAdmission, fixture_recruitment: Recruitment
):
### Arrange ###
fixture_rest_client.force_authenticate(user=fixture_user)
url = reverse(routes.samfundet__recruitment_admissions_for_applicant_list)

### Act ###
response: Response = fixture_rest_client.get(path=url, data={'recruitment': fixture_recruitment.id})

### Assert ###
assert response.status_code == status.HTTP_200_OK
# Assert the returned data based on the logic in the view
assert len(response.data) == 1
assert response.data[0]['admission_text'] == fixture_recruitment_admission.admission_text
assert response.data[0]['recruitment_position'] == fixture_recruitment_admission.recruitment_position.id
assert response.data[0]['interview_location'] == fixture_recruitment_admission.interview_location
8 changes: 6 additions & 2 deletions backend/samfundet/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ def list(self, request: Request) -> Response:
Returns a list of all the recruitments for the specified gang.
"""
recruitment_id = request.query_params.get('recruitment')
user_id = request.query_params.get('user_id')

if not recruitment_id:
return Response({'error': 'A recruitment parameter is required'}, status=status.HTTP_400_BAD_REQUEST)
Expand All @@ -540,8 +541,11 @@ def list(self, request: Request) -> Response:
user=request.user,
)

# check permissions for each admission
admissions = get_objects_for_user(user=request.user, perms=['view_recruitmentadmission'], klass=admissions)
if user_id:
# TODO: Add permissions
admissions = RecruitmentAdmission.objects.filter(recruitment=recruitment, user_id=user_id)
else:
admissions = RecruitmentAdmission.objects.filter(recruitment=recruitment, user=request.user)

serializer = self.get_serializer(admissions, many=True)
return Response(serializer.data)
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/routes/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ THIS FILE IS AUTOGENERATED.
DO NOT WRITE IN THIS FILE, AS IT WILL BE OVERWRITTEN ON NEXT UPDATE.
THIS FILE WAS GENERATED BY: root.management.commands.generate_routes
LAST UPDATE: 2023-09-14 19:45:16.057665+00:00
LAST UPDATE: 2023-09-22 09:21:59.070385+00:00
"""
*/
// ############################################################
Expand Down Expand Up @@ -372,7 +372,7 @@ export const ROUTES_BACKEND = {
samfundet__eventgroups_list: '/api/eventgroups/',
samfundet__eventgroups_detail: '/api/eventgroups/:pk/',
samfundet__venues_list: '/api/venues/',
samfundet__venues_detail: '/api/venues/:pk/',
samfundet__venues_detail: '/api/venues/:slug/',
samfundet__closedperiods_list: '/api/closed/',
samfundet__closedperiods_detail: '/api/closed/:pk/',
samfundet__gangs_list: '/api/gangs/',
Expand Down

0 comments on commit 5480078

Please sign in to comment.