def test_error_message_with_no_min_length(self): field_length = 3 max_length = field_length + 1 field_name = "field" expected_result = ("The {field_name} field has to be shorter " "than {max_limit} characters.".format( field_name=field_name, max_limit=max_length + 1)) actual_result = get_length_validation_error_message( field_name, None, max_length) self.assertEqual(expected_result, actual_result)
def test_update_user_organization_request_data_validation(self): secure_random = SystemRandom() random_generated_organization = "".join( secure_random.choice(ascii_lowercase) for x in range(ORGANIZATION_MAX_LENGTH + 1)) field_name = "organization" request_body = dict(organization=random_generated_organization) expected_result = { "message": get_length_validation_error_message(field_name, None, ORGANIZATION_MAX_LENGTH) } actual_result = validate_update_profile_request_data(request_body) self.assertEqual(expected_result, actual_result)
def test_user_registration_request_data_name_with_just_spaces_good_length(self): request_body = dict( name=" " * (NAME_MIN_LENGTH + 1), username=user1["username"], password=user1["password"], email=user1["email"], terms_and_conditions_checked=user1["terms_and_conditions_checked"], ) expected_result = { "message": get_length_validation_error_message( "name", NAME_MIN_LENGTH, NAME_MAX_LENGTH ) } actual_result = validate_user_registration_request_data(request_body) self.assertEqual(expected_result, actual_result)
def test_error_message_with_min_length(self): field_length = 3 min_length = field_length - 1 max_length = field_length + 1 field_name = "field" expected_result = ( "The {field_name} field has to be longer than {min_limit} " "characters and shorter than {max_limit} characters.".format( field_name=field_name, min_limit=min_length - 1, max_limit=max_length + 1, )) actual_result = get_length_validation_error_message( field_name, min_length, max_length) self.assertEqual(expected_result, actual_result)
def test_update_user_username_request_data_validation(self): secure_random = SystemRandom() random_generated_username = "".join( secure_random.choice(ascii_lowercase) for x in range(USERNAME_MAX_LENGTH + 1)) field_name = "username" request_body = dict(username=random_generated_username) expected_result = { "message": get_length_validation_error_message(field_name, USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH) } actual_result = validate_update_profile_request_data(request_body) self.assertEqual(expected_result, actual_result)
def test_user_password_change_request_password_superior_to_limit(self): secure_random = SystemRandom() random_generated_password = "".join( secure_random.choice(ascii_lowercase) for x in range(PASSWORD_MAX_LENGTH + 1) ) data = dict( new_password=random_generated_password, current_password=user1["password"] ) expected_result = { "message": get_length_validation_error_message( "new_password", PASSWORD_MIN_LENGTH, PASSWORD_MAX_LENGTH ) } actual_result = validate_new_password(data) self.assertEqual(expected_result, actual_result)
def test_task_comment_creation_api_with_comment_too_long(self): auth_header = get_test_request_header(self.admin_user.id) expected_response = { "message": get_length_validation_error_message("comment", None, COMMENT_MAX_LENGTH) } actual_response = self.client.post( f"mentorship_relation/{self.relation_id}/task/{self.task_id}/comment", follow_redirects=True, headers=auth_header, content_type="application/json", data=json.dumps(dict(comment="a" * 500)), ) self.assertEqual(400, actual_response.status_code) self.assertDictEqual(expected_response, json.loads(actual_response.data))
def test_change_password_to_empty_one(self): with self.client: new_password = "" expected_response = { "message": get_length_validation_error_message("new_password", PASSWORD_MIN_LENGTH, PASSWORD_MAX_LENGTH) } response = self.client.put( "/user/change_password", json={ "current_password": self.current_password, "new_password": new_password, }, follow_redirects=True, headers=self.auth_header, ) self.assertEqual(400, response.status_code) self.assertEqual(expected_response, json.loads(response.data))
def test_user_registration_request_data_name_superior_to_limit(self): secure_random = SystemRandom() random_generated_name = "".join( secure_random.choice(ascii_lowercase) for x in range(NAME_MAX_LENGTH + 1) ) request_body = dict( name=random_generated_name, username=user1["username"], password=user1["password"], email=user1["email"], terms_and_conditions_checked=user1["terms_and_conditions_checked"], ) expected_result = { "message": get_length_validation_error_message( "name", NAME_MIN_LENGTH, NAME_MAX_LENGTH ) } actual_result = validate_user_registration_request_data(request_body) self.assertEqual(expected_result, actual_result)
def test_update_username_invalid_length(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() field_name = "username" secure_random = SystemRandom() random_generated_username = "".join( secure_random.choice(ascii_lowercase) for x in range(USERNAME_MAX_LENGTH + 1)) auth_header = get_test_request_header(self.first_user.id) expected_response = { "message": get_length_validation_error_message(field_name, USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH) } actual_response = self.client.put( "/user", follow_redirects=True, headers=auth_header, data=json.dumps(dict(username=random_generated_username)), content_type="application/json", ) self.assertEqual(400, actual_response.status_code) self.assertEqual(expected_response, json.loads(actual_response.data)) self.assertNotEqual(random_generated_username, self.first_user.username) self.assertEqual(user1["username"], self.first_user.username)