def test_get_user_results_endpoint_with_advanced_level_authorization_returns_list_of_results_of_user( self): res = self.client().get('/profile', headers=basic_auth_header()) data = json.loads(res.data) message = self.check_if_operation_was_successful_and_get_payload(data) user_id = message['id'] post_data = { 'test_id': self.create_test(), 'time': datetime.datetime.utcnow(), 'value': 3 } self.client().post('/results', json=post_data, headers=basic_auth_header(True)) self.client().post('/results', json=post_data, headers=basic_auth_header(True)) self.client().post('/results', json=post_data, headers=basic_auth_header(True)) res = self.client().get('/users/' + str(user_id) + '/results', headers=advanced_auth_header()) data = json.loads(res.data) message = self.check_if_operation_was_successful_and_get_payload(data) self.assertTrue(len(message['data']) == 3)
def test_patch_results_endpoint_with_basic_level_authorization_updates_result_data( self): post_data = { 'test_id': self.create_test(), 'time': datetime.datetime.utcnow(), 'value': 3 } res = self.client().post('/results', json=post_data, headers=basic_auth_header(True)) data = json.loads(res.data) post_result = self.check_if_operation_was_successful_and_get_payload( data) patch_data = {'value': 4} res = self.client().patch('/results/' + str(post_result['id']), json=patch_data, headers=basic_auth_header(True)) data = json.loads(res.data) self.check_if_operation_was_successful_and_get_payload(data) res = self.client().get('/results', headers=basic_auth_header()) data = json.loads(res.data) message = self.check_if_operation_was_successful_and_get_payload(data) self.assertTrue(len(message['data']) == 1) self.assertTrue(message['data'][0]['value'] == patch_data['value'])
def test_get_profile_endpoint_with_basic_level_authorization_returns_user_info( self): res = self.client().get('/profile', headers=basic_auth_header()) data = json.loads(res.data) message = self.check_if_operation_was_successful_and_get_payload(data) self.assertTrue(message['name'] == 'Unknown') self.assertTrue(len(message['tests']) == 0)
def test_post_results_endpoint_with_no_value_throws_422(self): post_data = {'test_id': 1, 'time': datetime.datetime.utcnow()} res = self.client().post('/results', json=post_data, headers=basic_auth_header(True)) data = json.loads(res.data) self.check_if_operation_failed_with_error_code(data, 422)
def test_patch_profile_endpoint_with_basic_level_authorization_without_name_in_payload_returns_422( self): patch_data = {} res = self.client().patch('/profile', json=patch_data, headers=basic_auth_header(True)) data = json.loads(res.data) self.check_if_operation_failed_with_error_code(data, 422)
def test_post_tests_endpoint_with_basic_level_authorization_returns_403( self): post_data = {'name': 'someName'} res = self.client().post('/tests', json=post_data, headers=basic_auth_header(True)) data = json.loads(res.data) self.check_if_operation_failed_with_error_code(data, 403)
def test_patch_results_endpoint_with_non_existing_result_id_throws_404( self): patch_data = {'value': 4} res = self.client().patch('/results/1', json=patch_data, headers=basic_auth_header(True)) data = json.loads(res.data) self.check_if_operation_failed_with_error_code(data, 404)
def test_post_results_endpoint_with_basic_level_authorization_registers_test_result( self): post_data = { 'test_id': self.create_test(), 'time': datetime.datetime.utcnow(), 'value': 3 } res = self.client().post('/results', json=post_data, headers=basic_auth_header(True)) data = json.loads(res.data) result = self.check_if_operation_was_successful_and_get_payload(data) res = self.client().get('/results', headers=basic_auth_header()) data = json.loads(res.data) message = self.check_if_operation_was_successful_and_get_payload(data) self.assertTrue(self.is_result_in_list(message['data'], result))
def test_patch_profile_endpoint_with_basic_level_authorization_updates_user_info( self): patch_data = {'name': 'newName'} res = self.client().get('/profile', headers=basic_auth_header()) data = json.loads(res.data) message = self.check_if_operation_was_successful_and_get_payload(data) self.assertFalse(message['name'] == patch_data['name']) res = self.client().patch('/profile', json=patch_data, headers=basic_auth_header(True)) data = json.loads(res.data) self.check_if_operation_was_successful_and_get_payload(data) res = self.client().get('/profile', headers=basic_auth_header()) data = json.loads(res.data) message = self.check_if_operation_was_successful_and_get_payload(data) self.assertTrue(message['name'] == patch_data['name'])
def test_delete_results_endpoint_with_result_id_belonging_to_other_user_throws_403(self): post_data = {'test_id': self.create_test(), 'time': datetime.datetime.utcnow(), 'value': 3} res = self.client().post('/results', json=post_data, headers=basic_auth_header(True)) data = json.loads(res.data) post_result = self.check_if_operation_was_successful_and_get_payload(data) res = self.client().delete('/results/' + str(post_result['id']), headers=advanced_auth_header(True)) data = json.loads(res.data) self.check_if_operation_failed_with_error_code(data, 403)
def test_patch_results_endpoint_with_no_time_or_value_data_throws_422( self): post_data = { 'test_id': self.create_test(), 'time': datetime.datetime.utcnow(), 'value': 3 } res = self.client().post('/results', json=post_data, headers=basic_auth_header(True)) data = json.loads(res.data) post_result = self.check_if_operation_was_successful_and_get_payload( data) patch_data = {} res = self.client().patch('/results/' + str(post_result['id']), json=patch_data, headers=basic_auth_header(True)) data = json.loads(res.data) self.check_if_operation_failed_with_error_code(data, 422)
def test_get_results_endpoint_does_not_returns_results_of_other_users( self): self.add_data_to_database([{ 'test': 1, 'user': 1, 'data': 10 }, { 'test': 2, 'user': 2, 'data': 10 }]) res = self.client().get('/results', headers=basic_auth_header()) data = json.loads(res.data) message = self.check_if_operation_was_successful_and_get_payload(data) self.assertTrue(len(message['data']) == 0)
def test_get_results_endpoint_with_basic_level_authorization_returns_results_of_user( self): res = self.client().get('/results', headers=basic_auth_header()) data = json.loads(res.data) message = self.check_if_operation_was_successful_and_get_payload(data) self.assertTrue(len(message['data']) == 0)
def test_get_user_results_endpoint_with_basic_level_authorization_throws_403( self): res = self.client().get('/users', headers=basic_auth_header()) data = json.loads(res.data) self.check_if_operation_failed_with_error_code(data, 403)
def test_delete_results_endpoint_with_non_existing_result_id_throws_404(self): res = self.client().delete('/results/1', headers=basic_auth_header(True)) data = json.loads(res.data) self.check_if_operation_failed_with_error_code(data, 404)