def test_create_case(create_suite, host, auth_header, delete_suite, delete_case): """ Check that test case can be created. :param create_suite: setup fixture to create a test suite :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 :param auth_header: authorisation token fixture :param delete_suite: fixture to delete test suite :param delete_case: fixture to delete test case """ suite_id = create_suite LOGGER.info('Testing test case creation.') resp = PostRequest(host, f'{BASE_PATH}/test_cases')\ .call(request_body=case_body(suite_id, CASE_TITLE, CASE_DESCRIPTION), headers={**APPLICATION_JSON, **auth_header}) validator = ResponseHandler(resp) msg = validator.get_value_from_json( resp=resp, json_path=get_from_json_path_config('message')) case_id = validator.get_value_from_json( resp=resp, json_path=get_from_json_path_config('id')) # deleting just created test suite and test case delete_case(case_id) delete_suite(suite_id) assert msg == 'Test case successfully added', \ f'Unexpected message in response body: {msg}'
def test_create_suite(host, auth_header, delete_suite): """ Check that test suite can be created. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 :param auth_header: authorisation token fixture :param delete_suite: teardown fixture """ LOGGER.info('Testing test suite creation.') resp = PostRequest(host, f'{BASE_PATH}/test_suites').call( request_body=TEST_SUITE, headers={ **APPLICATION_JSON, **auth_header }) validator = ResponseHandler(resp) msg = validator.get_value_from_json( resp=resp, json_path=get_from_json_path_config('message')) suite_id = validator.get_value_from_json( resp=resp, json_path=get_from_json_path_config('id')) delete_suite(suite_id) assert msg == 'Test suite successfully added', \ f'Unexpected message in response body: {msg}.'
def test_index(host): """ Check response message for server index. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 """ LOGGER.info('Testing server index response message.') resp = GetRequest(host, BASE_PATH).call() validator = ResponseHandler(resp) msg = validator.get_value_from_json(resp=resp, json_path=get_from_json_path_config('message')) assert msg == 'Simple Test Management System API',\ f'Unexpected message in response: {msg}'
def delete(suite_id): resp = DeleteRequest(host, f'{BASE_PATH}/test_suites/{suite_id}').\ call(headers={**APPLICATION_JSON, **auth_header}) msg = ResponseHandler(resp).\ get_value_from_json(resp=resp, json_path=get_from_json_path_config('message')) if not re.search(r'Test suite successfully deleted', msg): LOGGER.warning(msg)
def test_login_invalid_creds_message(host): """ Check response message when log in with invalid password. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 """ LOGGER.info('Testing response message when log in with invalid password.') resp = PostRequest(host, f'{BASE_PATH}/login').call(request_body=INVALID_CREDS, headers=APPLICATION_JSON) validator = ResponseHandler(resp, status_code_check=False) msg = validator.get_value_from_json( resp=resp, json_path=get_from_json_path_config('message')) assert msg == 'No such username or password', \ f'Unexpected message in response body: {msg}'
def test_login_invalid_creds_status_code(host): """ Check that 401 status code is returned when log in with invalid password. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 """ LOGGER.info( 'Testing 401 status code returned when log in with invalid password.') resp = PostRequest(host, f'{BASE_PATH}/login').call(request_body=INVALID_CREDS, headers=APPLICATION_JSON) validator = ResponseHandler(resp, status_code_check=False) status_code = validator.get_status_code(resp=resp) assert status_code == 401, \ f'Status code is not 401. Actual status code: {status_code}'
def test_create_case_without_suite_id_code(host, auth_header): """ Check status code when trying to create test case without suite_id. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 :param auth_header: authorisation token fixture """ LOGGER.info( 'Testing status code when trying to create test case without suite_id.' ) resp = PostRequest(host, f'{BASE_PATH}/test_cases')\ .call(request_body=CASE_NO_SUITE_ID, headers={**APPLICATION_JSON, **auth_header}) validator = ResponseHandler(resp, status_code_check=False) status_code = validator.get_status_code(resp=resp) assert status_code == 400, \ f'Status code is {status_code}, but 404 is expected.'
def test_create_case_for_nonexisting_suite_code(host, auth_header): """ Check response message when trying to create test case for non-existing test suite. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 :param auth_header: authorisation token fixture """ LOGGER.info( 'Testing status codee when trying to create test case for non-existing test suite' ) resp = PostRequest(host, f'{BASE_PATH}/test_cases')\ .call(request_body=case_body(NON_EXISTING_SUITE_ID, CASE_TITLE, CASE_DESCRIPTION), headers={**APPLICATION_JSON, **auth_header}) validator = ResponseHandler(resp, status_code_check=False) status_code = validator.get_status_code(resp=resp) assert status_code == 404, \ f'Status code is {status_code}, but 404 is expected.'
def test_create_case_for_nonexisting_suite_message(host, auth_header): """ Check response message when trying to create test case for non-existing test suite. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 :param auth_header: authorisation token fixture """ LOGGER.info( 'Testing response message when trying to create test case for non-existing test suite' ) resp = PostRequest(host, f'{BASE_PATH}/test_cases')\ .call(request_body=case_body(NON_EXISTING_SUITE_ID, CASE_TITLE, CASE_DESCRIPTION), headers={**APPLICATION_JSON, **auth_header}) validator = ResponseHandler(resp, status_code_check=False) msg = validator.get_value_from_json( resp=resp, json_path=get_from_json_path_config('message')) assert msg == 'Test suite does not exist', \ f'Unexpected message in response body: {msg}'
def test_create_case_without_suite_id_message(host, auth_header): """ Check response message when trying to create test case without suite_id. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 :param auth_header: authorisation token fixture """ LOGGER.info( 'Testing response message when trying to create test case without suite_id.' ) resp = PostRequest(host, f'{BASE_PATH}/test_cases')\ .call(request_body=CASE_NO_SUITE_ID, headers={**APPLICATION_JSON, **auth_header}) validator = ResponseHandler(resp, status_code_check=False) msg = validator.get_value_from_json( resp=resp, json_path=get_from_json_path_config('message')) assert msg == 'Bad request body', \ f'Unexpected message in response body: {msg}'
def test_create_suite_invalid_content_type_status_code(host, auth_header): """ Check 415 status code returned with 'Content-Type': 'application/xml' request header for suite creation. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 :param auth_header: authorisation token fixture """ LOGGER.info( 'Testing 415 status code returned with invalid request header for suite creation' ) resp = PostRequest(host, f'{BASE_PATH}/test_suites').call( request_body=TEST_SUITE, headers={ **APPLICATION_XML, **auth_header }) validator = ResponseHandler(resp, status_code_check=False) status_code = validator.get_status_code(resp) assert status_code == 415, \ f'Expected 415 status code, but {status_code} returned.'
def test_create_suite_invalid_content_type_message(host, auth_header): """ Check response message with 'Content-Type': 'application/xml' request header. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 :param auth_header: authorisation token fixture """ LOGGER.info( 'Testing response messafe for suite creation with invalid request header.' ) resp = PostRequest(host, f'{BASE_PATH}/test_suites').call( request_body=TEST_SUITE, headers={ **APPLICATION_XML, **auth_header }) validator = ResponseHandler(resp, status_code_check=False) msg = validator.get_value_from_json( resp=resp, json_path=get_from_json_path_config('message')) assert msg == 'Content-type must be application/json', \ f'Unexpected message in response body: {msg}'
def test_login(host): """ Check that access_token can be obtained with valid credentials. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 """ LOGGER.info( 'Testing that access_token can be obtained with valid credentials.') resp = PostRequest(host, f'{BASE_PATH}/login').call(request_body=VALID_CREDS, headers=APPLICATION_JSON) validator = ResponseHandler(resp) access_token = validator.get_value_from_json( resp=resp, json_path=get_from_json_path_config('token')) # regexp is split into 3 groups: # 1. 36 character-long alpha-numeric string # 2. 196 character-long alpha-numeric string # 3. 43 character-long alpha-numeric string with -, _ characters allowed access_token_matches = re.match(TOKEN_REGEXP, access_token) assert access_token_matches is not None, \ f'Access token does not match pattern: {TOKEN_REGEXP}'
def create_suite(host, auth_header): """ Used as setup fixture to create test suite. By calling POST api/v1/test_suites endpoint. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 :param auth_header: authorisation token fixture :return: suite_id<str> """ resp = PostRequest(host, f'{BASE_PATH}/test_suites').\ call(request_body=TEST_SUITE, headers={**APPLICATION_JSON, **auth_header}) suite_id = ResponseHandler(resp).\ get_value_from_json(resp=resp, json_path=get_from_json_path_config('id')) assert suite_id, \ f'Cannot get suite_id when creating test suite: {suite_id}' return suite_id
def auth_header(host): """ Returns auth header (used for logging in) as dict by calling api/v1/login endpoint. :param host: setup fixture to set request host+port, e.g. http://127.0.0.1:5000 :return: Authorization header <dict> """ resp = PostRequest(host, f'{BASE_PATH}/login').\ call(request_body=VALID_CREDS, headers=APPLICATION_JSON) validator = ResponseHandler(resp) access_token = validator.\ get_value_from_json(resp=resp, json_path=get_from_json_path_config('token')) access_token_matches = re.match(TOKEN_REGEXP, access_token) assert access_token_matches is not None, \ f'Access token does not match pattern: {TOKEN_REGEXP}' return {'Authorization': f'Bearer {access_token}'}