Merge branch 'master' of github.com:alphagov/notifications-api

This commit is contained in:
Rebecca Law
2016-01-22 10:03:09 +00:00
3 changed files with 45 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
from sqlalchemy import UniqueConstraint
from . import db
import datetime
from sqlalchemy.dialects.postgresql import UUID
from app.encryption import (
hashpw,
@@ -95,6 +96,10 @@ class ApiKey(db.Model):
service = db.relationship('Service', backref=db.backref('api_keys', lazy='dynamic'))
expiry_date = db.Column(db.DateTime)
__table_args__ = (
UniqueConstraint('service_id', 'name', name='uix_service_to_key_name'),
)
TEMPLATE_TYPES = ['sms', 'email', 'letter']
@@ -123,7 +128,6 @@ class Template(db.Model):
class Job(db.Model):
__tablename__ = 'jobs'
id = db.Column(UUID(as_uuid=True), primary_key=True)

View File

@@ -0,0 +1,26 @@
"""empty message
Revision ID: 0008_unique_service_to_key_name
Revises: 0007_change_to_api_keys
Create Date: 2016-01-21 16:14:51.773001
"""
# revision identifiers, used by Alembic.
revision = '0008_unique_service_to_key_name'
down_revision = '0007_change_to_api_keys'
from alembic import op
import sqlalchemy as sa
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_unique_constraint('uix_service_to_key_name', 'api_key', ['service_id', 'name'])
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('uix_service_to_key_name', 'api_key', type_='unique')
### end Alembic commands ###

View File

@@ -73,3 +73,16 @@ def test_get_unsigned_secret_returns_key(notify_api,
unsigned_api_key = get_unsigned_secret(sample_api_key.id)
assert sample_api_key.secret != unsigned_api_key
assert unsigned_api_key == _get_secret(sample_api_key.secret)
def test_should_not_allow_duplicate_key_names_per_service(notify_api,
notify_db,
notify_db_session,
sample_api_key):
api_key = ApiKey(
**{'id': sample_api_key.id + 1, 'service_id': sample_api_key.service_id, 'name': sample_api_key.name})
try:
save_model_api_key(api_key)
fail("should throw IntegrityError")
except:
pass