diff --git a/backend/tests/test_tc004.py b/backend/tests/test_tc004.py new file mode 100644 index 0000000..ec60360 --- /dev/null +++ b/backend/tests/test_tc004.py @@ -0,0 +1,71 @@ +from rest_framework.test import APITestCase +from django.contrib.auth import get_user_model + +class TestTC004(APITestCase): + """ + TestTC004 is a test case class designed to validate the behavior of the API when creating offers between users. + + This test case includes: + - Validation of error handling when the recipient user does not exist (`test_non_existing_user`). + - Validation of error handling when the recipient is not a coach (`test_non_coach_user`). + - Validation of successfully creating a valid offer to a coach (`test_valid_offer`). + + The tests ensure that the API correctly handles offer creation, including proper validation of recipient users and their roles. + """ + def setUp(self): + self.athlete = get_user_model().objects.create_user( + username="athlete", + email='athlete@example.com', + password="athletetest" + ) + + self.coach = get_user_model().objects.create_user( + username="coach", + email='coach@example.com', + password="coachtest", + isCoach=True, + specialism="specialism" + ) + + self.client.force_authenticate(self.athlete) + + + def test_non_existing_user(self): + response = self.client.post( + "/api/offers/", + { + "recipient": "/api/users/999999/", + "status": "p", + }, + format="json" + ) + print(f"Response data: {response.data}") + print(f"Response status code: {response.status_code}") + assert response.status_code == 400 + + def test_non_coach_user(self): + response = self.client.post( + "/api/offers/", + { + "recipient": f"/api/users/{self.athlete.id}/", + "status": "p", + }, + format="json" + ) + print(f"Response data: {response.data}") + print(f"Response status code: {response.status_code}") + assert response.status_code == 400 + + def test_valid_offer(self): + # Add the coach as coach and store offer url to accept the offer + response = self.client.post( + "/api/offers/", + { + "recipient": f"/api/users/{self.coach.id}/", + "status": "p", + }, + format="json" + ) + print(f"Response data: {response.data}") + print(f"Response status code: {response.status_code}") + assert response.status_code == 201 \ No newline at end of file