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( "/mentorship_relation/%s/accept" % self.mentorship_relation.id, headers=get_test_request_header(self.second_user.id), ) self.assertEqual(400, 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_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 test_list_filter_mentorship_relations_empty(self): with self.client: response = self.client.get( "/mentorship_relations?relation_state=%s" % "", headers=get_test_request_header(self.second_user.id), ) expected_response = [ marshal(self.past_mentorship_relation, mentorship_request_response_body), marshal( self.future_pending_mentorship_relation, mentorship_request_response_body, ), marshal( self.future_accepted_mentorship_relation, mentorship_request_response_body, ), marshal( self.admin_only_mentorship_relation, mentorship_request_response_body, ), ] self.assertEqual(200, response.status_code) self.assertEqual(expected_response, json.loads(response.data))
def test_change_password_to_current_password(self): 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.first_user.is_email_verified = True self.first_user.need_mentoring = True db.session.add(self.first_user) db.session.commit() auth_header = get_test_request_header(self.first_user.id) expected_response = messages.USER_ENTERED_CURRENT_PASSWORD actual_response = self.client.put( "/user/change_password", follow_redirects=True, headers=auth_header, data=json.dumps( dict(current_password=user1["password"], new_password=user1["password"])), content_type="application/json", ) self.assertEqual(400, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data))
def test_update_username_not_taken(self): 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.first_user.is_email_verified = True db.session.add(self.first_user) db.session.commit() user1_new_username = "******" auth_header = get_test_request_header(self.first_user.id) expected_response = messages.USER_SUCCESSFULLY_UPDATED actual_response = self.client.put( "/user", follow_redirects=True, headers=auth_header, data=json.dumps(dict(username=user1_new_username)), content_type="application/json", ) self.assertEqual(200, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data)) self.assertEqual(user1_new_username, self.first_user.username)
def test_update_username_already_taken(self): 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.first_user.is_email_verified = True db.session.add(self.first_user) db.session.commit() auth_header = get_test_request_header(self.first_user.id) expected_response = messages.USER_USES_A_USERNAME_THAT_ALREADY_EXISTS actual_response = self.client.put( "/user", follow_redirects=True, headers=auth_header, data=json.dumps(dict(username=self.admin_user.username)), content_type="application/json", ) self.assertEqual(400, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data))
def test_update_availability_to_be_mentee_to_false(self): 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.first_user.is_email_verified = True self.first_user.need_mentoring = True db.session.add(self.first_user) db.session.commit() expected_response = messages.USER_SUCCESSFULLY_UPDATED test_need_mentoring = False auth_header = get_test_request_header(self.first_user.id) self.assertEqual(True, self.first_user.need_mentoring) actual_response = self.client.put( "/user", follow_redirects=True, headers=auth_header, data=json.dumps(dict(need_mentoring=test_need_mentoring)), content_type="application/json", ) self.assertEqual(200, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data)) self.assertEqual(test_need_mentoring, self.first_user.need_mentoring)
def test_user_error_code_ok_for_other_user(self): auth_header = get_test_request_header(self.admin_user.id) expected_response = marshal(self.verified_user, public_user_api_model) actual_response = self.client.get(f"/users/{self.verified_user.id}", follow_redirects=True, headers=auth_header) self.assertEqual(HTTPStatus.OK, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
def test_relations_invalid_id(self): auth_header = get_test_request_header( None ) # Supply invalid user ID for the test actual_response = self.client.get( "/home", follow_redirects=True, headers=auth_header ) self.assertEqual(404, actual_response.status_code) self.assertEqual(messages.USER_NOT_FOUND, json.loads(actual_response.data))
def test_list_users_api_with_a_page_query_out_of_range_resource_auth(self): auth_header = get_test_request_header(self.admin_user.id) expected_response = [] actual_response = self.client.get("/users?page=2", follow_redirects=True, headers=auth_header) self.assertEqual(200, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
def test_user_profile_with_header_api(self): auth_header = get_test_request_header(self.first_user.id) expected_response = marshal(self.first_user, full_user_api_model) actual_response = self.client.get( "/user", follow_redirects=True, headers=auth_header ) self.assertEqual(200, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
def test_list_users_api_with_a_search_query_resource_auth(self): auth_header = get_test_request_header(self.admin_user.id) expected_response = [marshal(self.other_user, public_user_api_model)] actual_response = self.client.get("/users?search=b", follow_redirects=True, headers=auth_header) self.assertEqual(200, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
def test_list_users_api_with_a_page_and_empty_per_page_query_resource_verified_users( self): auth_header = get_test_request_header(self.admin_user.id) expected_response = [] actual_response = self.client.get("/users/verified?page=1&per_page=0", follow_redirects=True, headers=auth_header) self.assertEqual(200, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
def test_list_filter_mentorship_relations_invalid(self): with self.client: response = self.client.get( "/mentorship_relations?relation_state=invalid_param", headers=get_test_request_header(self.second_user.id), ) expected_response = [] self.assertEqual(400, response.status_code) self.assertEqual(expected_response, json.loads(response.data))
def test_list_admin_users_api_resource_auth_not_admin(self): auth_header = get_test_request_header(self.normal_user_1.id) expected_response = messages.USER_IS_NOT_AN_ADMIN actual_response = self.client.get("/admins", follow_redirects=True, headers=auth_header) # import pdb; pdb.set_trace() self.assertEqual(403, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
def test_list_users_api_resource_verified_users(self): auth_header = get_test_request_header(self.admin_user.id) expected_response = [ marshal(self.verified_user, public_user_api_model) ] actual_response = self.client.get("/users/verified", follow_redirects=True, headers=auth_header) self.assertEqual(200, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
def test_user_profile_with_token_expired_api(self): auth_header = get_test_request_header( self.first_user.id, token_expiration_delta=timedelta(minutes=-5) ) expected_response = messages.TOKEN_HAS_EXPIRED actual_response = self.client.get( "/user", follow_redirects=True, headers=auth_header ) self.assertEqual(401, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data))
def test_list_users_api_with_search_with_special_characters_resource_auth( self): auth_header = get_test_request_header(self.admin_user.id) expected_response = [marshal(self.second_user, public_user_api_model)] actual_response = self.client.get( f"/users?search=s_t-r%24a%2Fn'ge", follow_redirects=True, headers=auth_header, ) self.assertEqual(200, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
def test_task_comment_listing_api_with_relation_not_existing(self): auth_header = get_test_request_header(self.admin_user.id) expected_response = messages.MENTORSHIP_RELATION_DOES_NOT_EXIST actual_response = self.client.get( f"mentorship_relation/0/task/{self.task_id}/comments", follow_redirects=True, headers=auth_header, content_type="application/json", ) self.assertEqual(404, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data))
def test_task_comment_deletion_api_with_comment_not_existing(self): auth_header = get_test_request_header(self.admin_user.id) expected_response = messages.TASK_COMMENT_DOES_NOT_EXIST actual_response = self.client.delete( f"mentorship_relation/{self.relation_id}/task/{self.task_id}" f"/comment/0", follow_redirects=True, headers=auth_header, content_type="application/json", ) self.assertEqual(404, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data))
def test_delete_task_api_non_existing_task(self): auth_header = get_test_request_header(self.first_user.id) expected_response = messages.TASK_DOES_NOT_EXIST actual_response = self.client.delete( "/mentorship_relation/%s/task/%s" % (self.mentorship_relation_w_second_user.id, 0), follow_redirects=True, headers=auth_header, ) self.assertEqual(404, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data))
def test_accept_own_request(self): self.assertEqual(MentorshipRelationState.PENDING, self.mentorship_relation.state) with self.client: response = self.client.put( f"/mentorship_relation/{self.mentorship_relation.id}/accept", headers=get_test_request_header(self.first_user.id), ) self.assertEqual(403, response.status_code) self.assertEqual(MentorshipRelationState.PENDING, self.mentorship_relation.state) self.assertDictEqual(messages.CANT_ACCEPT_MENTOR_REQ_SENT_BY_USER, json.loads(response.data))
def test_task_comment_deletion_api_not_created_by_user(self): auth_header = get_test_request_header(self.first_user.id) expected_response = messages.TASK_COMMENT_WAS_NOT_CREATED_BY_YOU_DELETE actual_response = self.client.delete( f"mentorship_relation/{self.relation_id}/task/{self.task_id}" f"/comment/{self.comment_id}", follow_redirects=True, headers=auth_header, content_type="application/json", ) self.assertEqual(400, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data))
def test_list_past_mentorship_relations(self): with self.client: response = self.client.get( "/mentorship_relations/past", headers=get_test_request_header(self.second_user.id), ) expected_response = [ marshal(self.past_mentorship_relation, mentorship_request_response_body) ] self.assertEqual(200, response.status_code) self.assertEqual(expected_response, json.loads(response.data))
def test_cancel_mentorship_relation_notinvolvedinrelation(self): self.assertEqual( MentorshipRelationState.ACCEPTED, self.mentorship_relation.state ) with self.client: expected_response = messages.CANT_CANCEL_UNINVOLVED_REQUEST response = self.client.put( "/mentorship_relation/%s/cancel" % self.mentorship_relation.id, headers=get_test_request_header(self.admin_user.id), ) self.assertEqual(400, response.status_code) self.assertDictEqual(expected_response, json.loads(response.data))
def test_task_comment_deletion_api_user_not_involved(self): auth_header = get_test_request_header(self.second_user.id) expected_response = messages.USER_NOT_INVOLVED_IN_THIS_MENTOR_RELATION actual_response = self.client.delete( f"mentorship_relation/{self.relation_id}/task/{self.task_id}" f"/comment/{self.comment_id}", follow_redirects=True, headers=auth_header, content_type="application/json", ) self.assertEqual(401, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data))
def test_list_current_mentorship_relation(self): with self.client: response = self.client.get( "/mentorship_relations/current", headers=get_test_request_header(self.second_user.id), ) expected_response = marshal( self.future_accepted_mentorship_relation, mentorship_request_response_body, ) self.assertEqual(200, response.status_code) self.assertEqual(expected_response, json.loads(response.data))
def test_create_task_api_not_in_relation(self): auth_header = get_test_request_header(self.first_user.id) expected_response = messages.MENTORSHIP_RELATION_DOES_NOT_EXIST actual_response = self.client.post( "/mentorship_relation/%s/task" % 100, follow_redirects=True, headers=auth_header, content_type="application/json", data=json.dumps(dict(description=self.test_description)), ) self.assertEqual(404, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data))
def test_list_tasks_api_mentorship_relation_without_tasks(self): auth_header = get_test_request_header(self.second_user.id) expected_response = [] actual_response = self.client.get( "/mentorship_relation/%s/tasks" % self.mentorship_relation_without_first_user.id, follow_redirects=True, headers=auth_header, ) self.assertEqual(200, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data))
def test_task_comment_creation_api_with_task_not_existing(self): auth_header = get_test_request_header(self.admin_user.id) expected_response = messages.TASK_DOES_NOT_EXIST actual_response = self.client.post( f"mentorship_relation/{self.relation_id}/task/0/comment", follow_redirects=True, headers=auth_header, content_type="application/json", data=json.dumps(dict(comment="comment")), ) self.assertEqual(404, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data))