Put API keys on service model

Similar to how we put templates on the service model, it means less
logic needs to happen in the view code.
This commit is contained in:
Chris Hill-Scott
2018-11-07 11:35:24 +00:00
parent 302078ac28
commit d1c9dcfb1d
4 changed files with 14 additions and 10 deletions

View File

@@ -80,7 +80,6 @@ def whitelist(service_id):
def api_keys(service_id):
return render_template(
'views/api/keys.html',
keys=api_key_api_client.get_api_keys(service_id=service_id)['apiKeys']
)
@@ -88,10 +87,7 @@ def api_keys(service_id):
@login_required
@user_has_permissions('manage_api_keys', restrict_admin_usage=True)
def create_api_key(service_id):
key_names = [
key['name'] for key in api_key_api_client.get_api_keys(service_id=service_id)['apiKeys']
]
form = CreateKeyForm(key_names)
form = CreateKeyForm(current_service.api_key_names)
form.key_type.choices = [
(KEY_TYPE_NORMAL, 'Live sends to anyone'),
(KEY_TYPE_TEAM, 'Team and whitelist limits who you can send to'),
@@ -137,7 +133,6 @@ def revoke_api_key(service_id, key_id):
return render_template(
'views/api/keys.html',
revoke_key=key_name,
keys=api_key_api_client.get_api_keys(service_id=service_id)['apiKeys'],
)
elif request.method == 'POST':
api_key_api_client.revoke_api_key(service_id=service_id, key_id=key_id)

View File

@@ -1,6 +1,7 @@
from notifications_utils.field import Field
from werkzeug.utils import cached_property
from app.notify_client.api_key_api_client import api_key_api_client
from app.notify_client.billing_api_client import billing_api_client
from app.notify_client.email_branding_client import email_branding_client
from app.notify_client.inbound_number_client import inbound_number_client
@@ -335,3 +336,11 @@ class Service():
template_ids=ids_to_move & self.all_template_ids,
folder_ids=ids_to_move & self.all_template_folder_ids,
)
@cached_property
def api_keys(self):
return api_key_api_client.get_api_keys(self.id)['apiKeys']
@property
def api_key_names(self):
return [key['name'] for key in self.api_keys]

View File

@@ -37,7 +37,7 @@
<div class="body-copy-table">
{% call(item, row_number) list_table(
keys,
current_service.api_keys,
empty_message="You havent created any API keys yet",
caption="API keys",
caption_visible=false,

View File

@@ -186,7 +186,7 @@ def test_should_show_empty_api_keys_page(
assert response.status_code == 200
assert 'You havent created any API keys yet' in response.get_data(as_text=True)
assert 'Create an API key' in response.get_data(as_text=True)
mock_get_no_api_keys.assert_called_once_with(service_id=service_id)
mock_get_no_api_keys.assert_called_once_with(service_id)
def test_should_show_api_keys_page(
@@ -205,7 +205,7 @@ def test_should_show_api_keys_page(
assert 'some key name' in resp_data
assert 'another key name' in resp_data
assert 'Revoked 1 January at 1:00am' in resp_data
mock_get_api_keys.assert_called_once_with(service_id=fake_uuid)
mock_get_api_keys.assert_called_once_with(fake_uuid)
@pytest.mark.parametrize('service_mock, expected_options', [
@@ -323,7 +323,7 @@ def test_should_show_confirm_revoke_api_key(
service_id='596364a0-858e-42c8-9062-a8fe822260eb',
),
call(
service_id='596364a0-858e-42c8-9062-a8fe822260eb'
'596364a0-858e-42c8-9062-a8fe822260eb'
),
]