Added functionality to archive a template.

Renamed migration file.
This commit is contained in:
Nicholas Staples
2016-04-25 16:28:08 +01:00
parent 74c3746d23
commit e6cc3b1724
7 changed files with 111 additions and 26 deletions

View File

@@ -174,6 +174,7 @@ class Template(db.Model, Versioned):
nullable=True, nullable=True,
onupdate=datetime.datetime.utcnow) onupdate=datetime.datetime.utcnow)
content = db.Column(db.Text, index=False, unique=False, nullable=False) content = db.Column(db.Text, index=False, unique=False, nullable=False)
archived = db.Column(db.Boolean, index=False, nullable=False, default=False)
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=False) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=False)
service = db.relationship('Service', backref=db.backref('templates', lazy='dynamic')) service = db.relationship('Service', backref=db.backref('templates', lazy='dynamic'))
subject = db.Column(db.Text, index=False, unique=True, nullable=True) subject = db.Column(db.Text, index=False, unique=True, nullable=True)

View File

@@ -0,0 +1,33 @@
"""empty message
Revision ID: 0008_archive_template
Revises: 0007_template_history
Create Date: 2016-04-25 14:16:49.787229
"""
# revision identifiers, used by Alembic.
revision = '0008_archive_template'
down_revision = '0007_template_history'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('templates', sa.Column('archived', sa.Boolean(), nullable=True))
op.add_column('templates_history', sa.Column('archived', sa.Boolean(), nullable=True))
op.get_bind()
op.execute('UPDATE templates SET archived = FALSE')
op.execute('UPDATE templates_history set archived = FALSE')
op.alter_column('templates', 'archived', nullable=False)
op.alter_column('templates', 'archived', nullable=False)
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_column('templates_history', 'archived')
op.drop_column('templates', 'archived')
### end Alembic commands ###

View File

@@ -1,13 +1,24 @@
import uuid
from flask import current_app from flask import current_app
from notifications_python_client.authentication import create_jwt_token from notifications_python_client.authentication import create_jwt_token
from app.models import ApiKey
from app.dao.api_key_dao import get_unsigned_secrets from app.dao.api_key_dao import (get_unsigned_secrets, save_model_api_key)
from app.dao.services_dao import dao_fetch_service_by_id
def create_authorization_header(path, method, request_body=None, service_id=None): def create_authorization_header(path, method, request_body=None, service_id=None):
if service_id: if service_id:
client_id = str(service_id) client_id = str(service_id)
secrets = get_unsigned_secrets(service_id)
if secrets:
secret = secrets[0]
else:
service = dao_fetch_service_by_id(service_id)
data = {'service': service, 'name': uuid.uuid4(), 'created_by': service.created_by}
api_key = ApiKey(**data)
save_model_api_key(api_key)
secret = get_unsigned_secrets(service_id)[0] secret = get_unsigned_secrets(service_id)[0]
else: else:
client_id = current_app.config.get('ADMIN_CLIENT_USER_NAME') client_id = current_app.config.get('ADMIN_CLIENT_USER_NAME')
secret = current_app.config.get('ADMIN_CLIENT_SECRET') secret = current_app.config.get('ADMIN_CLIENT_SECRET')

View File

@@ -44,7 +44,6 @@ def sample_user(notify_db,
notify_db_session, notify_db_session,
mobile_numnber="+447700900986", mobile_numnber="+447700900986",
email="notify@digital.cabinet-office.gov.uk"): email="notify@digital.cabinet-office.gov.uk"):
try:
data = { data = {
'name': 'Test User', 'name': 'Test User',
'email_address': email, 'email_address': email,
@@ -56,9 +55,7 @@ def sample_user(notify_db,
if not usr: if not usr:
usr = User(**data) usr = User(**data)
save_model_user(usr) save_model_user(usr)
except Exception:
import traceback
traceback.print_exc()
return usr return usr
@@ -134,20 +131,24 @@ def sample_template(notify_db,
template_name="Template Name", template_name="Template Name",
template_type="sms", template_type="sms",
content="This is a template", content="This is a template",
archived=False,
subject_line='Subject', subject_line='Subject',
user=None, user=None,
service=None): service=None,
created_by=None):
if user is None: if user is None:
user = sample_user(notify_db, notify_db_session) user = sample_user(notify_db, notify_db_session)
if service is None: if service is None:
service = sample_service(notify_db, notify_db_session) service = sample_service(notify_db, notify_db_session)
sample_api_key(notify_db, notify_db_session, service=service) if created_by is None:
created_by = sample_user(notify_db, notify_db_session)
data = { data = {
'name': template_name, 'name': template_name,
'template_type': template_type, 'template_type': template_type,
'content': content, 'content': content,
'service': service, 'service': service,
'created_by': user 'created_by': created_by,
'archived': archived
} }
if template_type == 'email': if template_type == 'email':
data.update({ data.update({
@@ -177,7 +178,6 @@ def sample_email_template(
user = sample_user(notify_db, notify_db_session) user = sample_user(notify_db, notify_db_session)
if service is None: if service is None:
service = sample_service(notify_db, notify_db_session) service = sample_service(notify_db, notify_db_session)
sample_api_key(notify_db, notify_db_session, service=service)
data = { data = {
'name': template_name, 'name': template_name,
'template_type': template_type, 'template_type': template_type,

View File

@@ -21,7 +21,10 @@ def test_secret_is_signed_and_can_be_read_again(notify_api):
assert signed_secret != token assert signed_secret != token
def test_save_api_key_should_create_new_api_key_and_history(notify_api, notify_db, notify_db_session, sample_service): def test_save_api_key_should_create_new_api_key_and_history(notify_api,
notify_db,
notify_db_session,
sample_service):
api_key = ApiKey(**{'service': sample_service, api_key = ApiKey(**{'service': sample_service,
'name': sample_service.name, 'name': sample_service.name,
'created_by': sample_service.created_by}) 'created_by': sample_service.created_by})

View File

@@ -98,8 +98,13 @@ def test_get_api_keys_should_return_all_keys_for_service(notify_api, notify_db,
with notify_api.test_client() as client: with notify_api.test_client() as client:
another_user = create_user(notify_db, notify_db_session, email='another@it.gov.uk') another_user = create_user(notify_db, notify_db_session, email='another@it.gov.uk')
another_service = create_sample_service(notify_db, notify_db_session, service_name='another', another_service = create_sample_service(
user=another_user, email_from='another') notify_db,
notify_db_session,
service_name='another',
user=another_user,
email_from='another'
)
# key for another service # key for another service
create_sample_api_key(notify_db, notify_db_session, service=another_service) create_sample_api_key(notify_db, notify_db_session, service=another_service)

View File

@@ -1,6 +1,6 @@
import json import json
import uuid import uuid
from app.models import Template
from tests import create_authorization_header from tests import create_authorization_header
@@ -275,6 +275,38 @@ def test_should_be_able_to_update_a_template(notify_api, sample_user, sample_ser
assert update_json_resp['data']['content'] == 'my template has new content alert("foo")' assert update_json_resp['data']['content'] == 'my template has new content alert("foo")'
def test_should_be_able_to_archive_template(notify_api, sample_user, sample_service, sample_template):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = {
'name': sample_template.name,
'template_type': sample_template.template_type,
'content': sample_template.content,
'archived': True,
'service': str(sample_template.service.id),
'created_by': str(sample_template.created_by.id)
}
json_data = json.dumps(data)
auth_header = create_authorization_header(
path='/service/{}/template/{}'.format(
str(sample_template.service.id),
str(sample_template.id)),
method='POST',
request_body=json_data
)
resp = client.post(
'/service/{}/template/{}'.format(sample_template.service.id, sample_template.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=json_data
)
assert resp.status_code == 200
assert Template.query.first().archived
def test_should_be_able_to_get_all_templates_for_a_service(notify_api, sample_user, sample_service): def test_should_be_able_to_get_all_templates_for_a_service(notify_api, sample_user, sample_service):
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client: