From 9784a9936cedbf12076d82384c2411895fbe2aab Mon Sep 17 00:00:00 2001
From: Chris Hill-Scott
+ To connect to the API you will need to send your service ID, encrypted with
+ an API key. The API key stays secret.
+
+ There are client libraries for several languages which manage this for
+ you. See
+ the
+ developer documentation for more information.
+
- blah blah blah this is where we tell you how the API works
-
+ {{ service_id }}
+
- GOV.UK Notify Python client
-
- https://www.notify.works/api/endpoint
-
+ ‘Test key 1’ will no longer let you connect to GOV.UK Notify.
+
+ You can’t undo this.
+
+ Copy your key to somewhere safe. You won’t be able to see it again
+ once you leave this page.
+
+ blah blah blah this is where we tell you how the API works
+
+ GOV.UK Notify Python client
+
+ https://www.notify.works/api/endpoint
+
+ {{ name }}
+
+
+{% endblock %}
diff --git a/app/templates/views/api-keys/revoke.html b/app/templates/views/api-keys/revoke.html
new file mode 100644
index 000000000..3597a6270
--- /dev/null
+++ b/app/templates/views/api-keys/revoke.html
@@ -0,0 +1,37 @@
+{% extends "withnav_template.html" %}
+{% from "components/page-footer.html" import page_footer %}
+{% from "components/api-key.html" import api_key %}
+
+{% block page_title %}
+ GOV.UK Notify | API keys and documentation
+{% endblock %}
+
+{% block maincolumn_content %}
+
+
+ API keys
+
-
- API keys and documentation
-
+
- How to integrate GOV.UK Notify into your service
-
+
+ Service ID
+
+ Repositories
+ {% call(item) list_table(
+ keys,
+ empty_message="You haven’t created any API keys yet",
+ caption="API keys",
+ caption_visible=False,
+ field_headings=['Key name', 'Created at', hidden_field_heading('Action')]
+ ) %}
+ {% call field() %}
+ {{ item.name }}
+ {% endcall %}
+ {% call field() %}
+ {{ item.last_used }}
+ {% endcall %}
+ {% if item.revoked %}
+ {% call field(align='right', status='default') %}
+ Revoked {{ item.revoked }}
+ {% endcall %}
+ {% else %}
+ {% call field(align='right', status='error') %}
+ Revoke
+ {% endcall %}
+ {% endif %}
+ {% endcall %}
-
-
-
-
- API key for [service name]
-
- {{ api_key('d30512af92e1386d63b90e5973b49a10') }}
-
- API endpoint
-
-
+ Add a new API key
+
+
+
+ Revoke API key
+
+
+
+ New API key
+
+
+
+ Developer documentation
+
+
+
+ How to integrate GOV.UK Notify into your service
+
+
+ Repositories
+
+
+
+ API endpoint
+
+
- ‘Test key 1’ will no longer let you connect to GOV.UK Notify. + ‘{{ key_name }}’ will no longer let you connect to GOV.UK Notify.
You can’t undo this.
diff --git a/tests/__init__.py b/tests/__init__.py
index fc5f15fb3..c0806dcbb 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -39,6 +39,12 @@ def template_json(id_, name, type_, content, service_id):
}
+def api_key_json(id_, name, expiry_date=None):
+ return {'id': id_,
+ 'name': name,
+ 'expiry_date': expiry_date
+ }
+
TEST_USER_EMAIL = 'test@user.gov.uk'
diff --git a/tests/app/main/views/test_api_keys.py b/tests/app/main/views/test_api_keys.py
index 1e274486d..94ef8fe41 100644
--- a/tests/app/main/views/test_api_keys.py
+++ b/tests/app/main/views/test_api_keys.py
@@ -1,3 +1,4 @@
+from datetime import date
from flask import url_for
@@ -13,16 +14,37 @@ def test_should_show_documentation_page(app_,
assert response.status_code == 200
-def test_should_show_api_keys_page(app_,
- db_,
- db_session,
- active_user):
+def test_should_show_empty_api_keys_page(app_,
+ db_,
+ db_session,
+ active_user,
+ mock_get_no_api_keys):
with app_.test_request_context():
with app_.test_client() as client:
client.login(active_user)
response = client.get(url_for('main.api_keys', service_id=123))
assert response.status_code == 200
+ assert 'You haven’t created any API keys yet' in response.get_data(as_text=True)
+ assert 'Create a new API key' in response.get_data(as_text=True)
+ mock_get_no_api_keys.assert_called_once_with(service_id=123)
+
+
+def test_should_show_api_keys_page(app_,
+ db_,
+ db_session,
+ active_user,
+ mock_get_api_keys):
+ with app_.test_request_context():
+ with app_.test_client() as client:
+ client.login(active_user)
+ response = client.get(url_for('main.api_keys', service_id=123))
+
+ assert response.status_code == 200
+ assert 'some key name' in response.get_data(as_text=True)
+ assert 'another key name' in response.get_data(as_text=True)
+ assert 'Revoked Thursday 01 January 1970 at 00:00' in response.get_data(as_text=True)
+ mock_get_api_keys.assert_called_once_with(service_id=123)
def test_should_show_name_api_key_page(app_,
@@ -37,47 +59,43 @@ def test_should_show_name_api_key_page(app_,
assert response.status_code == 200
-def test_should_redirect_to_new_api_key(app_,
- db_,
- db_session,
- active_user):
+def test_should_render_show_api_key(app_,
+ db_,
+ db_session,
+ active_user,
+ mock_create_api_key):
with app_.test_request_context():
with app_.test_client() as client:
client.login(active_user)
- response = client.post(url_for('main.create_api_key', service_id=123))
-
- assert response.status_code == 302
- assert response.location == url_for('main.show_api_key', service_id=123, _external=True)
-
-
-def test_should_show_new_api_key(app_,
- db_,
- db_session,
- active_user):
- with app_.test_request_context():
- with app_.test_client() as client:
- client.login(active_user)
- response = client.get(url_for('main.show_api_key', service_id=123))
+ response = client.post(url_for('main.create_api_key', service_id=123),
+ data={'key_name': 'some default key name'})
assert response.status_code == 200
+ assert 'some default key name' in response.get_data(as_text=True)
+ mock_create_api_key.assert_called_once_with(service_id=123, key_name='some default key name')
def test_should_show_confirm_revoke_api_key(app_,
db_,
db_session,
- active_user):
+ active_user,
+ mock_get_api_keys):
with app_.test_request_context():
with app_.test_client() as client:
client.login(active_user)
response = client.get(url_for('main.revoke_api_key', service_id=123, key_id=321))
assert response.status_code == 200
+ assert 'some key name' in response.get_data(as_text=True)
+ mock_get_api_keys.assert_called_once_with(service_id=123, key_id=321)
def test_should_redirect_after_revoking_api_key(app_,
db_,
db_session,
- active_user):
+ active_user,
+ mock_revoke_api_key,
+ mock_get_api_keys):
with app_.test_request_context():
with app_.test_client() as client:
client.login(active_user)
@@ -85,3 +103,5 @@ def test_should_redirect_after_revoking_api_key(app_,
assert response.status_code == 302
assert response.location == url_for('.api_keys', service_id=123, _external=True)
+ mock_revoke_api_key.assert_called_once_with(service_id=123, key_id=321)
+ mock_get_api_keys.assert_called_once_with(service_id=123, key_id=321)
diff --git a/tests/conftest.py b/tests/conftest.py
index 7bbf805d8..ab89761f1 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,14 +1,17 @@
import os
+from datetime import date
+
import pytest
from alembic.command import upgrade
from alembic.config import Config
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.script import Manager
from sqlalchemy.schema import MetaData
-from . import (
- create_test_user, create_another_test_user, service_json, TestClient,
- get_test_user, template_json)
+
from app import create_app, db
+from . import (
+ create_test_user, service_json, TestClient,
+ get_test_user, template_json, api_key_json)
@pytest.fixture(scope='session')
@@ -89,6 +92,7 @@ def mock_get_service(mocker, active_user):
service_id, "Test Service", [active_user.id], limit=1000,
active=False, restricted=True)
return {'data': service, 'token': 1}
+
return mocker.patch('app.notifications_api_client.get_service', side_effect=_create)
@@ -99,6 +103,7 @@ def mock_create_service(mocker):
101, service_name, [user_id], limit=limit,
active=active, restricted=restricted)
return {'data': service}
+
mock_class = mocker.patch(
'app.notifications_api_client.create_service', side_effect=_create)
return mock_class
@@ -116,6 +121,7 @@ def mock_update_service(mocker):
service_id, service_name, users, limit=limit,
active=active, restricted=restricted)
return {'data': service}
+
mock_class = mocker.patch(
'app.notifications_api_client.update_service', side_effect=_update)
return mock_class
@@ -129,6 +135,7 @@ def mock_get_services(mocker, active_user):
service_two = service_json(
2, "service_two", [active_user.id], 1000, True, False)
return {'data': [service_one, service_two]}
+
mock_class = mocker.patch(
'app.notifications_api_client.get_services', side_effect=_create)
return mock_class
@@ -138,6 +145,7 @@ def mock_get_services(mocker, active_user):
def mock_delete_service(mocker, mock_get_service):
def _delete(service_id):
return mock_get_service.side_effect(service_id)
+
mock_class = mocker.patch(
'app.notifications_api_client.delete_service', side_effect=_delete)
return mock_class
@@ -149,6 +157,7 @@ def mock_get_service_template(mocker):
template = template_json(
template_id, "Template Name", "sms", "template content", service_id)
return {'data': template}
+
return mocker.patch(
'app.notifications_api_client.get_service_template',
side_effect=_create)
@@ -160,6 +169,7 @@ def mock_create_service_template(mocker):
template = template_json(
101, name, type_, content, service)
return {'data': template}
+
mock_class = mocker.patch(
'app.notifications_api_client.create_service_template',
side_effect=_create)
@@ -172,6 +182,7 @@ def mock_update_service_template(mocker):
template = template_json(
id_, name, type_, content, service)
return {'data': template}
+
mock_class = mocker.patch(
'app.notifications_api_client.update_service_template',
side_effect=_update)
@@ -186,6 +197,7 @@ def mock_get_service_templates(mocker):
template_two = template_json(
2, "template_two", "sms", "template two content", service_id)
return {'data': [template_one, template_two]}
+
mock_class = mocker.patch(
'app.notifications_api_client.get_service_templates',
side_effect=_create)
@@ -199,6 +211,7 @@ def mock_delete_service_template(mocker):
template_id, "Template to delete",
"sms", "content to be deleted", service_id)
return {'data': template}
+
return mocker.patch(
'app.notifications_api_client.delete_service_template', side_effect=_delete)
@@ -212,5 +225,41 @@ def mock_register_user(mocker, user_data):
return mock_class
-def mock_create_api_key(mocker, key_name):
- mock_class = mocker.patch('app.api')
+@pytest.fixture(scope='function')
+def mock_create_api_key(mocker):
+ def _create(service_id, key_name):
+ import uuid
+ return {'data': str(uuid.uuid4())}
+
+ mock_class = mocker.patch('app.api_key_api_client.create_api_key', side_effect=_create)
+ return mock_class
+
+
+@pytest.fixture(scope='function')
+def mock_revoke_api_key(mocker):
+ def _revoke(service_id, key_id):
+ return {}
+
+ mock_class = mocker.patch('app.api_key_api_client.revoke_api_key', side_effect=_revoke)
+ return mock_class
+
+
+@pytest.fixture(scope='function')
+def mock_get_api_keys(mocker):
+ def _get_keys(service_id, key_id=None):
+ keys = {'apiKeys': [api_key_json(1, 'some key name'),
+ api_key_json(2, 'another key name', expiry_date=str(date.fromtimestamp(0)))]}
+ return keys
+
+ mock_class = mocker.patch('app.api_key_api_client.get_api_keys', side_effect=_get_keys)
+ return mock_class
+
+
+@pytest.fixture(scope='function')
+def mock_get_no_api_keys(mocker):
+ def _get_keys(service_id):
+ keys = {'apiKeys': []}
+ return keys
+
+ mock_class = mocker.patch('app.api_key_api_client.get_api_keys', side_effect=_get_keys)
+ return mock_class
From 2b24909ca677bfe230ff47bea8d2f0ed0131502c Mon Sep 17 00:00:00 2001
From: Rebecca Law
- There are client libraries for several languages which manage this for - you. See + There are client libraries available which can do this for you. See the developer documentation for more information.
From 81a5164fa543f042bac85b0ac404736f8adf05b0 Mon Sep 17 00:00:00 2001 From: Rebecca Law