From e6cc3b172434aa3008e52f5cee572caeb302787a Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Mon, 25 Apr 2016 16:28:08 +0100 Subject: [PATCH] Added functionality to archive a template. Renamed migration file. --- app/models.py | 1 + migrations/versions/0008_archive_template.py | 33 +++++++++++++++++ tests/__init__.py | 17 +++++++-- tests/app/conftest.py | 38 ++++++++++---------- tests/app/dao/test_api_key_dao.py | 5 ++- tests/app/service/test_api_key_endpoints.py | 9 +++-- tests/app/template/test_rest.py | 34 +++++++++++++++++- 7 files changed, 111 insertions(+), 26 deletions(-) create mode 100644 migrations/versions/0008_archive_template.py diff --git a/app/models.py b/app/models.py index b9f30ce22..d644289bf 100644 --- a/app/models.py +++ b/app/models.py @@ -174,6 +174,7 @@ class Template(db.Model, Versioned): nullable=True, onupdate=datetime.datetime.utcnow) 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 = db.relationship('Service', backref=db.backref('templates', lazy='dynamic')) subject = db.Column(db.Text, index=False, unique=True, nullable=True) diff --git a/migrations/versions/0008_archive_template.py b/migrations/versions/0008_archive_template.py new file mode 100644 index 000000000..97563bbee --- /dev/null +++ b/migrations/versions/0008_archive_template.py @@ -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 ### diff --git a/tests/__init__.py b/tests/__init__.py index 7491049d4..bc22b7b36 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,13 +1,24 @@ +import uuid from flask import current_app from notifications_python_client.authentication import create_jwt_token - -from app.dao.api_key_dao import get_unsigned_secrets +from app.models import ApiKey +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): if service_id: client_id = str(service_id) - secret = get_unsigned_secrets(service_id)[0] + 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] + else: client_id = current_app.config.get('ADMIN_CLIENT_USER_NAME') secret = current_app.config.get('ADMIN_CLIENT_SECRET') diff --git a/tests/app/conftest.py b/tests/app/conftest.py index d88542980..b356fba30 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -44,21 +44,18 @@ def sample_user(notify_db, notify_db_session, mobile_numnber="+447700900986", email="notify@digital.cabinet-office.gov.uk"): - try: - data = { - 'name': 'Test User', - 'email_address': email, - 'password': 'password', - 'mobile_number': mobile_numnber, - 'state': 'active' - } - usr = User.query.filter_by(email_address=email).first() - if not usr: - usr = User(**data) - save_model_user(usr) - except Exception: - import traceback - traceback.print_exc() + data = { + 'name': 'Test User', + 'email_address': email, + 'password': 'password', + 'mobile_number': mobile_numnber, + 'state': 'active' + } + usr = User.query.filter_by(email_address=email).first() + if not usr: + usr = User(**data) + save_model_user(usr) + return usr @@ -134,20 +131,24 @@ def sample_template(notify_db, template_name="Template Name", template_type="sms", content="This is a template", + archived=False, subject_line='Subject', user=None, - service=None): + service=None, + created_by=None): if user is None: user = sample_user(notify_db, notify_db_session) if service is None: 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 = { 'name': template_name, 'template_type': template_type, 'content': content, 'service': service, - 'created_by': user + 'created_by': created_by, + 'archived': archived } if template_type == 'email': data.update({ @@ -177,7 +178,6 @@ def sample_email_template( user = sample_user(notify_db, notify_db_session) if service is None: service = sample_service(notify_db, notify_db_session) - sample_api_key(notify_db, notify_db_session, service=service) data = { 'name': template_name, 'template_type': template_type, diff --git a/tests/app/dao/test_api_key_dao.py b/tests/app/dao/test_api_key_dao.py index df81370e2..2c2cd6857 100644 --- a/tests/app/dao/test_api_key_dao.py +++ b/tests/app/dao/test_api_key_dao.py @@ -21,7 +21,10 @@ def test_secret_is_signed_and_can_be_read_again(notify_api): 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, 'name': sample_service.name, 'created_by': sample_service.created_by}) diff --git a/tests/app/service/test_api_key_endpoints.py b/tests/app/service/test_api_key_endpoints.py index b535d5b6d..90f60908f 100644 --- a/tests/app/service/test_api_key_endpoints.py +++ b/tests/app/service/test_api_key_endpoints.py @@ -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: 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', - user=another_user, email_from='another') + another_service = create_sample_service( + notify_db, + notify_db_session, + service_name='another', + user=another_user, + email_from='another' + ) # key for another service create_sample_api_key(notify_db, notify_db_session, service=another_service) diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index 40c6c5d44..5e43f1775 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -1,6 +1,6 @@ import json import uuid - +from app.models import Template 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")' +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): with notify_api.test_request_context(): with notify_api.test_client() as client: