diff --git a/tests/app/service/test_api_key_endpoints.py b/tests/app/service/test_api_key_endpoints.py index de5909518..b535d5b6d 100644 --- a/tests/app/service/test_api_key_endpoints.py +++ b/tests/app/service/test_api_key_endpoints.py @@ -10,85 +10,85 @@ from tests.app.conftest import sample_service as create_sample_service from tests.app.conftest import sample_user as create_user -# def test_api_key_should_create_new_api_key_for_service(notify_api, notify_db, -# notify_db_session, -# sample_service): -# with notify_api.test_request_context(): -# with notify_api.test_client() as client: -# data = {'name': 'some secret name', 'created_by': str(sample_service.created_by.id)} -# auth_header = create_authorization_header(path=url_for('service.renew_api_key', -# service_id=sample_service.id), -# method='POST', -# request_body=json.dumps(data)) -# response = client.post(url_for('service.renew_api_key', service_id=sample_service.id), -# data=json.dumps(data), -# headers=[('Content-Type', 'application/json'), auth_header]) -# assert response.status_code == 201 -# assert response.get_data is not None -# saved_api_key = ApiKey.query.filter_by(service_id=sample_service.id).first() -# assert saved_api_key.service_id == sample_service.id -# assert saved_api_key.name == 'some secret name' +def test_api_key_should_create_new_api_key_for_service(notify_api, notify_db, + notify_db_session, + sample_service): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + data = {'name': 'some secret name', 'created_by': str(sample_service.created_by.id)} + auth_header = create_authorization_header(path=url_for('service.renew_api_key', + service_id=sample_service.id), + method='POST', + request_body=json.dumps(data)) + response = client.post(url_for('service.renew_api_key', service_id=sample_service.id), + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + assert response.status_code == 201 + assert response.get_data is not None + saved_api_key = ApiKey.query.filter_by(service_id=sample_service.id).first() + assert saved_api_key.service_id == sample_service.id + assert saved_api_key.name == 'some secret name' -# def test_api_key_should_return_error_when_service_does_not_exist(notify_api, notify_db, notify_db_session, -# sample_service): -# with notify_api.test_request_context(): -# with notify_api.test_client() as client: -# import uuid -# missing_service_id = uuid.uuid4() -# auth_header = create_authorization_header(path=url_for('service.renew_api_key', -# service_id=missing_service_id), -# method='POST') -# response = client.post(url_for('service.renew_api_key', service_id=missing_service_id), -# headers=[('Content-Type', 'application/json'), auth_header]) -# assert response.status_code == 404 +def test_api_key_should_return_error_when_service_does_not_exist(notify_api, notify_db, notify_db_session, + sample_service): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + import uuid + missing_service_id = uuid.uuid4() + auth_header = create_authorization_header(path=url_for('service.renew_api_key', + service_id=missing_service_id), + method='POST') + response = client.post(url_for('service.renew_api_key', service_id=missing_service_id), + headers=[('Content-Type', 'application/json'), auth_header]) + assert response.status_code == 404 -# def test_revoke_should_expire_api_key_for_service(notify_api, notify_db, notify_db_session, -# sample_api_key): -# with notify_api.test_request_context(): -# with notify_api.test_client() as client: -# assert ApiKey.query.count() == 1 -# auth_header = create_authorization_header(path=url_for('service.revoke_api_key', -# service_id=sample_api_key.service_id, -# api_key_id=sample_api_key.id), -# method='POST') -# response = client.post(url_for('service.revoke_api_key', -# service_id=sample_api_key.service_id, -# api_key_id=sample_api_key.id), -# headers=[auth_header]) -# assert response.status_code == 202 -# api_keys_for_service = ApiKey.query.get(sample_api_key.id) -# assert api_keys_for_service.expiry_date is not None +def test_revoke_should_expire_api_key_for_service(notify_api, notify_db, notify_db_session, + sample_api_key): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + assert ApiKey.query.count() == 1 + auth_header = create_authorization_header(path=url_for('service.revoke_api_key', + service_id=sample_api_key.service_id, + api_key_id=sample_api_key.id), + method='POST') + response = client.post(url_for('service.revoke_api_key', + service_id=sample_api_key.service_id, + api_key_id=sample_api_key.id), + headers=[auth_header]) + assert response.status_code == 202 + api_keys_for_service = ApiKey.query.get(sample_api_key.id) + assert api_keys_for_service.expiry_date is not None -# def test_api_key_should_create_multiple_new_api_key_for_service(notify_api, notify_db, -# notify_db_session, -# sample_service): -# with notify_api.test_request_context(): -# with notify_api.test_client() as client: -# assert ApiKey.query.count() == 0 -# data = {'name': 'some secret name', 'created_by': str(sample_service.created_by.id)} -# auth_header = create_authorization_header(path=url_for('service.renew_api_key', -# service_id=sample_service.id), -# method='POST', -# request_body=json.dumps(data)) -# response = client.post(url_for('service.renew_api_key', service_id=sample_service.id), -# data=json.dumps(data), -# headers=[('Content-Type', 'application/json'), auth_header]) -# assert response.status_code == 201 -# assert ApiKey.query.count() == 1 -# data = {'name': 'another secret name', 'created_by': str(sample_service.created_by.id)} -# auth_header = create_authorization_header(path=url_for('service.renew_api_key', -# service_id=sample_service.id), -# method='POST', -# request_body=json.dumps(data)) -# response2 = client.post(url_for('service.renew_api_key', service_id=sample_service.id), -# data=json.dumps(data), -# headers=[('Content-Type', 'application/json'), auth_header]) -# assert response2.status_code == 201 -# assert response2.get_data != response.get_data -# assert ApiKey.query.count() == 2 +def test_api_key_should_create_multiple_new_api_key_for_service(notify_api, notify_db, + notify_db_session, + sample_service): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + assert ApiKey.query.count() == 0 + data = {'name': 'some secret name', 'created_by': str(sample_service.created_by.id)} + auth_header = create_authorization_header(path=url_for('service.renew_api_key', + service_id=sample_service.id), + method='POST', + request_body=json.dumps(data)) + response = client.post(url_for('service.renew_api_key', service_id=sample_service.id), + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + assert response.status_code == 201 + assert ApiKey.query.count() == 1 + data = {'name': 'another secret name', 'created_by': str(sample_service.created_by.id)} + auth_header = create_authorization_header(path=url_for('service.renew_api_key', + service_id=sample_service.id), + method='POST', + request_body=json.dumps(data)) + response2 = client.post(url_for('service.renew_api_key', service_id=sample_service.id), + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + assert response2.status_code == 201 + assert response2.get_data != response.get_data + assert ApiKey.query.count() == 2 def test_get_api_keys_should_return_all_keys_for_service(notify_api, notify_db,