Beispiel #1
0
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.key_type.choices = [
        (KEY_TYPE_NORMAL, 'Live – sends to anyone'),
        (KEY_TYPE_TEAM, 'Team and whitelist – limits who you can send to'),
        (KEY_TYPE_TEST, 'Test – pretends to send messages'),
    ]
    disabled_options, option_hints = [], {}
    if current_service['restricted']:
        disabled_options = [KEY_TYPE_NORMAL]
        option_hints[KEY_TYPE_NORMAL] = Markup(
            'Not available because your service is in '
            '<a href="{}#trial-mode">trial mode</a>'.format(
                url_for(".using_notify")))
    if 'letter' in current_service['permissions']:
        option_hints[KEY_TYPE_TEAM] = 'Can’t be used to send letters'
    if form.validate_on_submit():
        if form.key_type.data in disabled_options:
            abort(400)
        secret = api_key_api_client.create_api_key(service_id=service_id,
                                                   key_name=form.key_name.data,
                                                   key_type=form.key_type.data)
        return render_template('views/api/keys/show.html',
                               secret=secret,
                               service_id=service_id,
                               key_name=email_safe(form.key_name.data,
                                                   whitespace='_'))
    return render_template('views/api/keys/create.html',
                           form=form,
                           disabled_options=disabled_options,
                           option_hints=option_hints)
Beispiel #2
0
def create_api_key(service_id):
    form = CreateKeyForm(current_service.api_keys)
    form.key_type.choices = [
        (KEY_TYPE_NORMAL, _l("Live – sends to anyone")),
        (KEY_TYPE_TEAM, _l("Team and safelist – limits who you can send to")),
        (KEY_TYPE_TEST, _l("Test – pretends to send messages")),
    ]
    disabled_options, option_hints = [], {}
    if current_service.trial_mode:
        disabled_options = [KEY_TYPE_NORMAL]
        option_hints[KEY_TYPE_NORMAL] = Markup(
            _l("Not available because your service is in trial mode."))
    if current_service.has_permission("letter"):
        option_hints[KEY_TYPE_TEAM] = ""
    if form.validate_on_submit():
        if form.key_type.data in disabled_options:
            abort(400)
        secret = api_key_api_client.create_api_key(
            service_id=service_id,
            key_name=form.key_name.data,
            key_type=form.key_type.data,
        )
        return render_template(
            "views/api/keys/show.html",
            secret=secret,
            service_id=service_id,
            key_name=email_safe(form.key_name.data, whitespace="_"),
        )
    return render_template(
        "views/api/keys/create.html",
        form=form,
        disabled_options=disabled_options,
        option_hints=option_hints,
    )
def create_api_key(service_id):
    form = CreateKeyForm(current_service.api_keys)
    form.key_type.choices = [
        (KEY_TYPE_NORMAL, 'Live – sends to anyone'),
        (KEY_TYPE_TEAM, 'Team and whitelist – limits who you can send to'),
        (KEY_TYPE_TEST, 'Test – pretends to send messages'),
    ]
    disabled_options, option_hints = [], {}
    if current_service.trial_mode:
        disabled_options = [KEY_TYPE_NORMAL]
        option_hints[KEY_TYPE_NORMAL] = Markup(
            'Not available because your service is in '
            '<a href="/features/trial-mode">trial mode</a>')
    if current_service.has_permission('letter'):
        option_hints[KEY_TYPE_TEAM] = 'Cannot be used to send letters'
    if form.validate_on_submit():
        if form.key_type.data in disabled_options:
            abort(400)
        secret = api_key_api_client.create_api_key(service_id=service_id,
                                                   key_name=form.key_name.data,
                                                   key_type=form.key_type.data)
        return render_template('views/api/keys/show.html',
                               secret=secret,
                               service_id=service_id,
                               key_name=email_safe(form.key_name.data,
                                                   whitespace='_'))
    return render_template('views/api/keys/create.html',
                           form=form,
                           disabled_options=disabled_options,
                           option_hints=option_hints)
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.key_type.choices = filter(None, [
        (KEY_TYPE_NORMAL, 'Send messages to anyone')
        if not current_service['restricted'] else None,
        (KEY_TYPE_TEST, 'Simulate sending messages to anyone'),
        (KEY_TYPE_TEAM, 'Only send messages to your team or whitelist')
    ])
    if form.validate_on_submit():
        secret = api_key_api_client.create_api_key(
            service_id=service_id,
            key_name=form.key_name.data,
            key_type=form.key_type.data
        )
        return render_template(
            'views/api/keys/show.html',
            secret=secret,
            service_id=service_id,
            key_name=email_safe(form.key_name.data, whitespace='_')
        )
    return render_template(
        'views/api/keys/create.html',
        form=form
    )
def test_return_validation_error_when_key_type_not_chosen(client, key_type, expected_error):

    form = CreateKeyForm(
        [],
        formdata=MultiDict([('key_name', 'Some key'), ('key_type', key_type)]))
    form.key_type.choices = [('a', 'a'), ('b', 'b')]
    form.validate()
    assert form.errors['key_type'] == [expected_error]
Beispiel #6
0
def test_return_validation_error_when_key_name_exists(client):
    def _get_names():
        return ['some key', 'another key']

    form = CreateKeyForm(_get_names(),
                         formdata=MultiDict([('key_name', 'Some key')]))
    form.key_type.choices = [('a', 'a'), ('b', 'b')]
    form.validate()
    assert form.errors['key_name'] == ['A key with this name already exists']
Beispiel #7
0
def test_return_validation_error_when_key_name_exists(
    client,
    expiry_date,
    expected_errors,
):
    _existing_keys = [
        {
            "name": "some key",
            "expiry_date": expiry_date,
        },
        {
            "name": "another key",
            "expiry_date": None,
        },
    ]

    form = CreateKeyForm(_existing_keys, formdata=MultiDict([("key_name", "Some key")]))

    form.key_type.choices = [("a", "a"), ("b", "b")]
    form.validate()
    assert form.errors.get("key_name") == expected_errors
Beispiel #8
0
def test_return_validation_error_when_key_name_exists(
    client,
    expiry_date,
    expected_errors,
):
    _existing_keys = [
        {
            'name': 'some key',
            'expiry_date': expiry_date,
        },
        {
            'name': 'another key',
            'expiry_date': None,
        },
    ]

    form = CreateKeyForm(_existing_keys,
                         formdata=MultiDict([('key_name', 'Some key')]))

    form.key_type.choices = [('a', 'a'), ('b', 'b')]
    form.validate()
    assert form.errors.get('key_name') == expected_errors
Beispiel #9
0
def create_api_key(service_id):
    form = CreateKeyForm(current_service.api_keys)
    form.key_type.choices = [
        (KEY_TYPE_NORMAL, 'Live – sends to anyone'),
        (KEY_TYPE_TEAM, 'Team and guest list – limits who you can send to'),
        (KEY_TYPE_TEST, 'Test – pretends to send messages'),
    ]
    # preserve order of items extended by starting with empty dicts
    form.key_type.param_extensions = {'items': [{}, {}]}
    if current_service.trial_mode:
        form.key_type.param_extensions['items'][0] = {
            'disabled': True,
            'hint': {
                'html':
                Markup(
                    'Not available because your service is in '
                    '<a class="govuk-link govuk-link--no-visited-state" href="/features/trial-mode">trial mode</a>'
                )
            }
        }
    if current_service.has_permission('letter'):
        form.key_type.param_extensions['items'][1]['hint'] = {
            'text': 'Cannot be used to send letters'
        }
    if form.validate_on_submit():
        if current_service.trial_mode and form.key_type.data == KEY_TYPE_NORMAL:
            abort(400)
        secret = api_key_api_client.create_api_key(service_id=service_id,
                                                   key_name=form.key_name.data,
                                                   key_type=form.key_type.data)
        return render_template('views/api/keys/show.html',
                               secret=secret,
                               service_id=service_id,
                               key_name=email_safe(form.key_name.data,
                                                   whitespace='_'))
    return render_template('views/api/keys/create.html', form=form)
Beispiel #10
0
def test_return_validation_error_when_key_type_not_chosen(client, key_type, expected_error):

    form = CreateKeyForm([], formdata=MultiDict([("key_name", "Some key"), ("key_type", key_type)]))
    form.key_type.choices = [("a", "a"), ("b", "b")]
    form.validate()
    assert form.errors["key_type"] == [expected_error]