|
| 1 | +import unittest |
| 2 | +from datetime import datetime, timedelta |
| 3 | +from http import HTTPStatus |
| 4 | + |
| 5 | +from flask import json |
| 6 | +from flask_restx import marshal |
| 7 | + |
| 8 | +from app import messages |
| 9 | +from app.api.models.user import public_user_api_model |
| 10 | +from app.database.models.mentorship_relation import MentorshipRelationModel |
| 11 | +from app.utils.enum_utils import MentorshipRelationState |
| 12 | +from app.database.models.tasks_list import TasksListModel |
| 13 | +from app.database.models.user import UserModel |
| 14 | +from app.database.sqlalchemy_extension import db |
| 15 | +from app.utils.enum_utils import MentorshipRelationState |
| 16 | +from tests.base_test_case import BaseTestCase |
| 17 | +from tests.test_utils import get_test_request_header |
| 18 | +from tests.test_data import user1, user2 |
| 19 | +from app.api.models.mentorship_relation import mentorship_request_response_body |
| 20 | + |
| 21 | + |
| 22 | +class TestRequestDeletionPath(BaseTestCase): |
| 23 | + """ |
| 24 | + Scenario: User A (mentor) and User B (mentee). The mentee sends |
| 25 | + a mentorship request to the mentor and then deletes the request. |
| 26 | + - User B (mentee) sends a request to User A (mentor) |
| 27 | + - User B (mentee) deletes the request |
| 28 | + """ |
| 29 | + |
| 30 | + def setUp(self): |
| 31 | + super().setUp() |
| 32 | + |
| 33 | + # Defining mentor for this test |
| 34 | + self.mentor = UserModel( |
| 35 | + name="Mentor A", |
| 36 | + email=user1["email"], |
| 37 | + username=user1["username"], |
| 38 | + password=user1["password"], |
| 39 | + terms_and_conditions_checked=user1["terms_and_conditions_checked"], |
| 40 | + ) |
| 41 | + |
| 42 | + # Defining mentee for this test |
| 43 | + self.mentee = UserModel( |
| 44 | + name="Mentee B", |
| 45 | + email=user2["email"], |
| 46 | + username=user2["username"], |
| 47 | + password=user2["password"], |
| 48 | + terms_and_conditions_checked=user2["terms_and_conditions_checked"], |
| 49 | + ) |
| 50 | + |
| 51 | + self.mentor.is_email_verified = True |
| 52 | + self.mentor.available_to_mentor = True |
| 53 | + self.mentee.is_email_verified = True |
| 54 | + self.mentee.need_mentoring = True |
| 55 | + |
| 56 | + db.session.add(self.mentor) |
| 57 | + db.session.add(self.mentee) |
| 58 | + db.session.commit() |
| 59 | + |
| 60 | + self.test_description = "A request for mentorship" |
| 61 | + |
| 62 | + def test_deletion_path(self): |
| 63 | + |
| 64 | + mentor_auth_header = get_test_request_header(self.mentor.id) |
| 65 | + mentee_auth_header = get_test_request_header(self.mentee.id) |
| 66 | + |
| 67 | + # The mentee sends a new mentorship request to the mentor |
| 68 | + request_body = { |
| 69 | + "mentor_id": self.mentor.id, |
| 70 | + "mentee_id": self.mentee.id, |
| 71 | + "end_date": int((datetime.now() + timedelta(weeks=5)).timestamp()), |
| 72 | + "notes": self.test_description, |
| 73 | + } |
| 74 | + send_request_response = self.client.post( |
| 75 | + "/mentorship_relation/send_request", |
| 76 | + headers=mentee_auth_header, |
| 77 | + content_type="application/json", |
| 78 | + data=json.dumps(request_body), |
| 79 | + ) |
| 80 | + |
| 81 | + self.assertEqual(HTTPStatus.CREATED, send_request_response.status_code) |
| 82 | + |
| 83 | + # Receiving the mentorship request on the mentor's side |
| 84 | + received_requests_response = self.client.get( |
| 85 | + "/mentorship_relations", |
| 86 | + headers=mentor_auth_header, |
| 87 | + content_type="application/json", |
| 88 | + ) |
| 89 | + requests = json.loads(received_requests_response.data) |
| 90 | + request_id = requests[0]["id"] |
| 91 | + |
| 92 | + self.assertIsNotNone(request_id) |
| 93 | + |
| 94 | + ## The mentee deletes the request by themself |
| 95 | + deletion_response = self.client.delete( |
| 96 | + f"/mentorship_relation/{request_id}", headers=mentee_auth_header |
| 97 | + ) |
| 98 | + deletion_request = json.loads(deletion_response.data) |
| 99 | + self.assertEqual( |
| 100 | + messages.MENTORSHIP_RELATION_WAS_DELETED_SUCCESSFULLY, deletion_request |
| 101 | + ) |
| 102 | + self.assertEqual(HTTPStatus.OK, deletion_response.status_code) |
| 103 | + |
| 104 | + # Checking the mentorship request on the mentor's side(if it's still visible) |
| 105 | + received_requests_response = self.client.get( |
| 106 | + "/mentorship_relations", |
| 107 | + headers=mentor_auth_header, |
| 108 | + content_type="application/json", |
| 109 | + ) |
| 110 | + requests = json.loads(received_requests_response.data) |
| 111 | + self.assertEqual(len(requests), 0) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + unittest.main() |
0 commit comments