def setUp(self): super(TestListMentorshipRelationsApi, self).setUp() self.notes_example = "description of a good mentorship relation" self.now_datetime = datetime.now() self.past_end_date_example = self.now_datetime - timedelta(weeks=5) self.future_end_date_example = self.now_datetime + timedelta(weeks=5) # create new mentorship relation self.past_mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.past_end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) self.future_pending_mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.future_end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) self.future_accepted_mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.future_end_date_example.timestamp(), state=MentorshipRelationState.ACCEPTED, notes=self.notes_example, tasks_list=TasksListModel(), ) self.admin_only_mentorship_relation = MentorshipRelationModel( action_user_id=self.admin_user.id, mentor_user=self.admin_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.future_end_date_example.timestamp(), state=MentorshipRelationState.REJECTED, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(self.past_mentorship_relation) db.session.add(self.future_pending_mentorship_relation) db.session.add(self.future_accepted_mentorship_relation) db.session.commit()
def test_recepient_already_in_relation_accept_request(self): # create existing relation between admin_user and second_user mentorship_relation_current = MentorshipRelationModel( action_user_id=self.admin_user.id, mentor_user=self.admin_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.ACCEPTED, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(mentorship_relation_current) db.session.commit() self.assertEqual(MentorshipRelationState.ACCEPTED, mentorship_relation_current.state) # current self.assertEqual(MentorshipRelationState.PENDING, self.mentorship_relation.state) # new with self.client: response = self.client.put( f"/mentorship_relation/{self.mentorship_relation.id}/accept", headers=get_test_request_header(self.second_user.id), ) self.assertEqual(403, response.status_code) self.assertEqual(MentorshipRelationState.ACCEPTED, mentorship_relation_current.state) # current self.assertEqual(MentorshipRelationState.PENDING, self.mentorship_relation.state) # new self.assertDictEqual( messages.USER_IS_INVOLVED_IN_A_MENTORSHIP_RELATION, json.loads(response.data), )
def test_dao_create_mentorship_relation_with_mentor_already_in_relation(self): self.mentorship_relation = MentorshipRelationModel( action_user_id=self.admin_user.id, mentor_user=self.admin_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.ACCEPTED, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(self.mentorship_relation) db.session.commit() dao = MentorshipRelationDAO() data = dict( mentor_id=self.second_user.id, mentee_id=self.first_user.id, end_date=self.end_date_example.timestamp(), notes=self.notes_example, tasks_list=TasksListModel(), ) # Use DAO to create a mentorship relation result = dao.create_mentorship_relation(self.second_user.id, data) self.assertEqual((messages.MENTOR_ALREADY_IN_A_RELATION, 400), result) query_mentorship_relations = MentorshipRelationModel.query.all() self.assertTrue(1, len(query_mentorship_relations))
def test_accepted_requests_auth(self): start_date = datetime.now() end_date = start_date + timedelta(weeks=4) start_date = start_date.timestamp() end_date = end_date.timestamp() tasks_list = TasksListModel() mentorship_relation = MentorshipRelationModel( action_user_id=self.user2.id, mentor_user=self.user1, mentee_user=self.user2, creation_date=start_date, end_date=end_date, state=MentorshipRelationState.ACCEPTED, notes="", tasks_list=tasks_list, ) db.session.add(mentorship_relation) db.session.commit() expected_response = { "name": "User1", "pending_requests": 0, "accepted_requests": 1, "rejected_requests": 0, "completed_relations": 0, "cancelled_relations": 0, "achievements": [], } auth_header = get_test_request_header(self.user1.id) actual_response = self.client.get( "/home", follow_redirects=True, headers=auth_header ) self.assertEqual(200, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
def setUp(self): super(TestMentorshipRelationAcceptRequestDAO, self).setUp() self.notes_example = "description of a good mentorship relation" self.now_datetime = datetime.now() self.end_date_example = self.now_datetime + timedelta(weeks=5) # create new mentorship relation self.mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) self.mentorship_relation2 = MentorshipRelationModel( action_user_id=self.third_user.id, mentor_user=self.first_user, mentee_user=self.third_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) self.mentorship_relation3 = MentorshipRelationModel( action_user_id=self.second_user.id, mentor_user=self.third_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(self.mentorship_relation) db.session.add(self.mentorship_relation2) db.session.add(self.mentorship_relation3) db.session.commit()
def test_get_achievements(self): dao = UserDAO() mentor = UserModel("Test mentor", "test_mentor", "test_password", "*****@*****.**", True) mentee = UserModel("Test mentee", "test_mentee", "test_password", "*****@*****.**", True) mentor.is_email_verified = True mentor.available_to_mentor = True mentee.is_email_verified = True mentee.need_mentoring = True db.session.add(mentor) db.session.add(mentee) db.session.commit() start_date = datetime.datetime.now() end_date = start_date + datetime.timedelta(weeks=4) tasks_list = TasksListModel() tasks_list.add_task( description="Test Task", created_at=start_date.timestamp(), is_done=True, completed_at=end_date.timestamp(), ) tasks_list.add_task( description="Test Task 2", created_at=start_date.timestamp(), is_done=True, completed_at=end_date.timestamp(), ) relation = MentorshipRelationModel( action_user_id=mentee.id, mentor_user=mentor, mentee_user=mentee, creation_date=start_date.timestamp(), end_date=end_date.timestamp(), state=MentorshipRelationState.ACCEPTED, tasks_list=tasks_list, notes="Test Notes", ) db.session.add(tasks_list) db.session.add(relation) db.session.commit() achievements = dao.get_achievements(mentee.id) self.assertEqual(2, len(achievements)) for achievement in achievements: self.assertTrue(achievement.get("is_done"))
def setUp(self): super(TestMentorshipRelationDeleteDAO, self).setUp() self.first_user = UserModel( name=user1["name"], email=user1["email"], username=user1["username"], password=user1["password"], terms_and_conditions_checked=user1["terms_and_conditions_checked"], ) self.second_user = UserModel( name=user2["name"], email=user2["email"], username=user2["username"], password=user2["password"], terms_and_conditions_checked=user2["terms_and_conditions_checked"], ) # making sure both are available to be mentor or mentee self.first_user.need_mentoring = True self.first_user.available_to_mentor = True self.first_user.is_email_verified = True self.second_user.need_mentoring = True self.second_user.available_to_mentor = True self.second_user.is_email_verified = True self.notes_example = "description of a good mentorship relation" self.now_datetime = datetime.now() self.end_date_example = self.now_datetime + timedelta(weeks=5) db.session.add(self.first_user) db.session.add(self.second_user) db.session.commit() # create new mentorship relation self.mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(self.mentorship_relation) db.session.commit()
def create_relationship(self): """Creates relationship between verified and other user.""" relation = MentorshipRelationModel( self.other_user.id, self.other_user, self.verified_user, datetime.now().timestamp(), (datetime.now() + timedelta(weeks=5)).timestamp(), MentorshipRelationState.ACCEPTED, "notes", TasksListModel(), ) db.session.add(relation) db.session.commit()
def setUp(self): db.create_all() self.first_user = UserModel( name=user1["name"], email=user1["email"], username=user1["username"], password=user1["password"], terms_and_conditions_checked=user1["terms_and_conditions_checked"], ) self.second_user = UserModel( name=user2["name"], email=user2["email"], username=user2["username"], password=user2["password"], terms_and_conditions_checked=user2["terms_and_conditions_checked"], ) self.notes_example = "description of a good mentorship relation" now_datetime = datetime.now() self.start_date_example = datetime(year=now_datetime.year + 1, month=3, day=1).timestamp() self.end_date_example = datetime(year=now_datetime.year + 1, month=5, day=1).timestamp() self.now_datetime = datetime.now().timestamp() db.session.add(self.first_user) db.session.add(self.second_user) db.session.commit() self.mentorship_relation = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime, end_date=self.end_date_example, state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=TasksListModel(), ) db.session.add(self.mentorship_relation) db.session.commit()
def setUp(self): super(TestUserDao, self).setUp() self.first_user = UserModel( name=user1["name"], email=user1["email"], username=user1["username"], password=user1["password"], terms_and_conditions_checked=user1["terms_and_conditions_checked"], ) self.second_user = UserModel( name=user2["name"], email=user2["email"], username=user2["username"], password=user2["password"], terms_and_conditions_checked=user2["terms_and_conditions_checked"], ) # making sure both are available to be mentor or mentee self.first_user.need_mentoring = True self.first_user.available_to_mentor = True self.first_user.is_email_verified = True self.second_user.need_mentoring = True self.second_user.available_to_mentor = True self.second_user.is_email_verified = True self.notes_example = "description of a good mentorship relation" self.now_datetime = datetime.now() self.end_date_example = self.now_datetime + timedelta(weeks=5) self.tasks_list_1 = TasksListModel() self.tasks_list_2 = TasksListModel() self.tasks_list_3 = TasksListModel() db.session.add(self.tasks_list_1) db.session.add(self.tasks_list_2) db.session.add(self.tasks_list_3) db.session.add(self.first_user) db.session.add(self.second_user) db.session.commit() # create new mentorship relation self.mentorship_relation1 = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.ACCEPTED, notes=self.notes_example, tasks_list=self.tasks_list_1, ) self.mentorship_relation2 = MentorshipRelationModel( action_user_id=self.first_user.id, mentor_user=self.first_user, mentee_user=self.second_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.PENDING, notes=self.notes_example, tasks_list=self.tasks_list_2, ) self.mentorship_relation3 = MentorshipRelationModel( action_user_id=self.second_user.id, mentor_user=self.second_user, mentee_user=self.first_user, creation_date=self.now_datetime.timestamp(), end_date=self.end_date_example.timestamp(), state=MentorshipRelationState.CANCELLED, notes=self.notes_example, tasks_list=self.tasks_list_3, ) db.session.add(self.mentorship_relation1) db.session.add(self.mentorship_relation3) db.session.add(self.mentorship_relation2) db.session.commit() self.description_example = "This is an example of a description" self.tasks_list_1.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), ) self.tasks_list_1.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), is_done=True, completed_at=self.end_date_example.timestamp(), ) self.tasks_list_2.add_task( description=self.description_example, created_at=self.now_datetime.timestamp(), ) db.session.add(self.tasks_list_1) db.session.add(self.tasks_list_2) db.session.commit() self.test_description = "testing this description" self.test_is_done = False
def create_mentorship_relation(self, user_id: int, data: Dict[str, str]): """Creates a relationship between two users. Establishes the mentor-mentee relationship. Args: user_id: ID of the user initiating this request. Has to be either the mentor or the mentee. data: List containing the mentor_id, mentee_id, end_date_timestamp and notes. Returns: message: A message corresponding to the completed action; success if mentorship relationship is established, failure if otherwise. """ action_user_id = user_id mentor_id = data["mentor_id"] mentee_id = data["mentee_id"] end_date_timestamp = data["end_date"] notes = data["notes"] # user_id has to match either mentee_id or mentor_id is_valid_user_ids = action_user_id == mentor_id or action_user_id == mentee_id if not is_valid_user_ids: return messages.MATCH_EITHER_MENTOR_OR_MENTEE, HTTPStatus.BAD_REQUEST # mentor_id has to be different from mentee_id if mentor_id == mentee_id: return messages.MENTOR_ID_SAME_AS_MENTEE_ID, HTTPStatus.BAD_REQUEST try: end_date_datetime = datetime.fromtimestamp(end_date_timestamp) except ValueError: return messages.INVALID_END_DATE, HTTPStatus.BAD_REQUEST now_datetime = datetime.now() if end_date_datetime < now_datetime: return messages.END_TIME_BEFORE_PRESENT, HTTPStatus.BAD_REQUEST # business logic constraints max_relation_duration = end_date_datetime - now_datetime if max_relation_duration > self.MAXIMUM_MENTORSHIP_DURATION: return messages.MENTOR_TIME_GREATER_THAN_MAX_TIME, HTTPStatus.BAD_REQUEST if max_relation_duration < self.MINIMUM_MENTORSHIP_DURATION: return messages.MENTOR_TIME_LESS_THAN_MIN_TIME, HTTPStatus.BAD_REQUEST # validate if mentor user exists mentor_user = UserModel.find_by_id(mentor_id) if mentor_user is None: return messages.MENTOR_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND # validate if mentor is available to mentor if not mentor_user.available_to_mentor: return messages.MENTOR_NOT_AVAILABLE_TO_MENTOR, HTTPStatus.BAD_REQUEST # validate if mentee user exists mentee_user = UserModel.find_by_id(mentee_id) if mentee_user is None: return messages.MENTEE_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND # validate if mentee is wants to be mentored if not mentee_user.need_mentoring: return messages.MENTEE_NOT_AVAIL_TO_BE_MENTORED, HTTPStatus.BAD_REQUEST # TODO add tests for this portion all_mentor_relations = ( mentor_user.mentor_relations + mentor_user.mentee_relations ) for relation in all_mentor_relations: if relation.state == MentorshipRelationState.ACCEPTED: return messages.MENTOR_ALREADY_IN_A_RELATION, HTTPStatus.BAD_REQUEST all_mentee_relations = ( mentee_user.mentor_relations + mentee_user.mentee_relations ) for relation in all_mentee_relations: if relation.state == MentorshipRelationState.ACCEPTED: return messages.MENTEE_ALREADY_IN_A_RELATION, HTTPStatus.BAD_REQUEST # All validations were checked tasks_list = TasksListModel() tasks_list.save_to_db() mentorship_relation = MentorshipRelationModel( action_user_id=action_user_id, mentor_user=mentor_user, mentee_user=mentee_user, creation_date=datetime.now().timestamp(), end_date=end_date_timestamp, state=MentorshipRelationState.PENDING, notes=notes, tasks_list=tasks_list, ) mentorship_relation.save_to_db() return messages.MENTORSHIP_RELATION_WAS_SENT_SUCCESSFULLY, HTTPStatus.OK
def test_get_user_statistics(self): dao = UserDAO() mentor = UserModel("Test mentor", "test_mentor", "__test__", "*****@*****.**", True) mentee = UserModel("Test mentee", "test_mentee", "__test__", "*****@*****.**", True) mentor.is_email_verified = True mentor.available_to_mentor = True mentee.is_email_verified = True mentee.need_mentoring = True db.session.add(mentor) db.session.add(mentee) db.session.commit() start_date = datetime.datetime.now() end_date = start_date + datetime.timedelta(weeks=4) tasks_list = TasksListModel() pending_relation = MentorshipRelationModel( action_user_id=mentee.id, mentor_user=mentor, mentee_user=mentee, creation_date=start_date.timestamp(), end_date=end_date.timestamp(), state=MentorshipRelationState.PENDING, tasks_list=tasks_list, notes="Test Notes", ) db.session.add(tasks_list) db.session.add(pending_relation) db.session.commit() # We first test pending relations expected_response = { "name": mentor.name, "pending_requests": 1, "accepted_requests": 0, "rejected_requests": 0, "completed_relations": 0, "cancelled_relations": 0, "achievements": [], } actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response) # We now test accepted relations pending_relation.state = MentorshipRelationState.ACCEPTED expected_response["pending_requests"] = 0 expected_response["accepted_requests"] = 1 actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response) # We now test completed relations pending_relation.state = MentorshipRelationState.COMPLETED expected_response["accepted_requests"] = 0 expected_response["completed_relations"] = 1 actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response) # We now test rejected relations pending_relation.state = MentorshipRelationState.REJECTED expected_response["completed_relations"] = 0 expected_response["rejected_requests"] = 1 actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response) # We now test cancelled relations pending_relation.state = MentorshipRelationState.CANCELLED expected_response["rejected_requests"] = 0 expected_response["cancelled_relations"] = 1 actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response) # We now test achievements pending_relation.state = MentorshipRelationState.ACCEPTED tasks_list.add_task( description="Test Task", created_at=start_date.timestamp(), is_done=True, completed_at=end_date.timestamp(), ) tasks_list.add_task( description="Test Task 2", created_at=start_date.timestamp(), is_done=True, completed_at=end_date.timestamp(), ) expected_response["cancelled_relations"] = 0 expected_response["accepted_requests"] = 1 expected_response["achievements"] = [ { "completed_at": end_date.timestamp(), "created_at": start_date.timestamp(), "description": "Test Task", "id": 1, # This is the first task in the list "is_done": True, }, { "completed_at": end_date.timestamp(), "created_at": start_date.timestamp(), "description": "Test Task 2", "id": 2, # This is the second task in the list "is_done": True, }, ] actual_response = dao.get_user_statistics(mentor.id) self.assertEqual(expected_response, actual_response)
def test_achievements(self): start_date = datetime.now() end_date = start_date + timedelta(weeks=4) start_date = start_date.timestamp() end_date = end_date.timestamp() tasks_list = TasksListModel() task_created_time = datetime.now().timestamp() task_completed_time = task_created_time + 100 tasks_list.add_task( description="Test task", created_at=task_created_time, is_done=True, completed_at=task_completed_time, ) tasks_list.add_task( description="Incomplete task: Should not appear in achievements", created_at=task_created_time, is_done=False, completed_at=task_completed_time, ) db.session.add(tasks_list) db.session.commit() mentorship_relation = MentorshipRelationModel( action_user_id=self.user2.id, mentor_user=self.user1, mentee_user=self.user2, creation_date=start_date, end_date=end_date, state=MentorshipRelationState.ACCEPTED, notes="", tasks_list=tasks_list, ) db.session.add(mentorship_relation) db.session.commit() expected_response = { "name": "User1", "pending_requests": 0, "accepted_requests": 1, "rejected_requests": 0, "completed_relations": 0, "cancelled_relations": 0, "achievements": [ { "completed_at": task_completed_time, "created_at": task_created_time, "description": "Test task", "id": 1, # This is the only task in the list "is_done": True, } ], } auth_header = get_test_request_header(self.user1.id) actual_response = self.client.get( "/home", follow_redirects=True, headers=auth_header ) self.assertEqual(200, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))