From b5c662eca89427b8e209e14e6477c49b80d495cd Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Tue, 2 Feb 2016 14:16:08 +0000 Subject: [PATCH] Change services.id to a UUID Ideally all the primary keys in the db would be UUID in order to guarantee unique ids across distributed dbs. This updates the services.id to a UUID. All the tables with a foreign key to the services.id are also updated. The endpoints no longer state a data type of the path param. All the tests are updated to reflect this update. The thing to pay attention to is the 0011_uuid_service_id.py migration script. This commit must go with a commit on the notifications_admin app to keep things working. There will be a small outage until both deploys have happened. --- app/job/rest.py | 2 +- app/models.py | 15 +-- app/notifications/rest.py | 4 +- app/schemas.py | 2 +- app/service/rest.py | 20 +-- app/user/rest.py | 2 +- migrations/versions/0011_uuid_service_id.py | 121 ++++++++++++++++++ tests/__init__.py | 2 +- .../app/authentication/test_authentication.py | 26 ++-- tests/app/conftest.py | 3 +- tests/app/job/test_job_rest.py | 4 +- tests/app/service/test_api_key_endpoints.py | 7 +- tests/app/service/test_rest.py | 41 +++--- tests/app/user/test_rest.py | 8 +- tests/conftest.py | 1 + 15 files changed, 195 insertions(+), 63 deletions(-) create mode 100644 migrations/versions/0011_uuid_service_id.py diff --git a/app/job/rest.py b/app/job/rest.py index b3920f43b..a1dc1f376 100644 --- a/app/job/rest.py +++ b/app/job/rest.py @@ -18,7 +18,7 @@ from app.schemas import ( jobs_schema ) -job = Blueprint('job', __name__, url_prefix='/service//job') +job = Blueprint('job', __name__, url_prefix='/service//job') @job.route('/', methods=['GET']) diff --git a/app/models.py b/app/models.py index 5aafef795..08115ce96 100644 --- a/app/models.py +++ b/app/models.py @@ -1,7 +1,6 @@ import uuid -from sqlalchemy import UniqueConstraint - +from sqlalchemy import UniqueConstraint, Sequence from . import db import datetime from sqlalchemy.dialects.postgresql import UUID @@ -58,14 +57,15 @@ user_to_service = db.Table( 'user_to_service', db.Model.metadata, db.Column('user_id', db.Integer, db.ForeignKey('users.id')), - db.Column('service_id', db.Integer, db.ForeignKey('services.id')) + db.Column('service_id', UUID(as_uuid=True), db.ForeignKey('services.id')) ) class Service(db.Model): __tablename__ = 'services' - id = db.Column(db.Integer, primary_key=True) + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + old_id = db.Column(db.Integer, Sequence('services_id_seq'), nullable=False) name = db.Column(db.String(255), nullable=False) created_at = db.Column( db.DateTime, @@ -86,7 +86,6 @@ class Service(db.Model): secondary=user_to_service, backref=db.backref('user_to_service', lazy='dynamic')) restricted = db.Column(db.Boolean, index=False, unique=False, nullable=False) - queue_name = db.Column(UUID(as_uuid=True), default=uuid.uuid4) class ApiKey(db.Model): @@ -95,7 +94,7 @@ class ApiKey(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), nullable=False) secret = db.Column(db.String(255), unique=True, nullable=False) - service_id = db.Column(db.Integer, db.ForeignKey('services.id'), index=True, nullable=False) + service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False) service = db.relationship('Service', backref=db.backref('api_keys', lazy='dynamic')) expiry_date = db.Column(db.DateTime) @@ -126,7 +125,7 @@ class Template(db.Model): nullable=True, onupdate=datetime.datetime.now) content = db.Column(db.Text, index=False, unique=False, nullable=False) - service_id = db.Column(db.BigInteger, db.ForeignKey('services.id'), index=True, unique=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')) @@ -137,7 +136,7 @@ class Job(db.Model): original_file_name = db.Column(db.String, nullable=False) bucket_name = db.Column(db.String, nullable=False) file_name = db.Column(db.String, nullable=False) - service_id = db.Column(db.BigInteger, db.ForeignKey('services.id'), index=True, unique=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('jobs', lazy='dynamic')) template_id = db.Column(db.BigInteger, db.ForeignKey('templates.id'), index=True, unique=False) template = db.relationship('Template', backref=db.backref('jobs', lazy='dynamic')) diff --git a/app/notifications/rest.py b/app/notifications/rest.py index a6c32eb94..a130ade45 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -136,11 +136,11 @@ def validate_required_and_something(json_body, field): def _add_notification_to_queue(template_id, service, msg_type, to): q = boto3.resource('sqs', region_name=current_app.config['AWS_REGION']).create_queue( - QueueName=str(service.queue_name)) + QueueName=str(service.id)) import uuid message_id = str(uuid.uuid4()) notification = json.dumps({'message_id': message_id, - 'service_id': service.id, + 'service_id': str(service.id), 'to': to, 'message_type': msg_type, 'template_id': template_id}) diff --git a/app/schemas.py b/app/schemas.py index 34a4498d6..4f2c3e3b8 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -38,7 +38,7 @@ class UserSchema(BaseSchema): class ServiceSchema(BaseSchema): class Meta: model = models.Service - exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs", "queue_name") + exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs", 'old_id') class TemplateSchema(BaseSchema): diff --git a/app/service/rest.py b/app/service/rest.py index af427490f..dd12cada5 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -35,7 +35,7 @@ def create_service(): return jsonify(data=service_schema.dump(service).data), 201 -@service.route('/', methods=['PUT', 'DELETE']) +@service.route('/', methods=['PUT', 'DELETE']) def update_service(service_id): try: service = get_model_services(service_id=service_id) @@ -58,7 +58,7 @@ def update_service(service_id): return jsonify(data=service_schema.dump(service).data), status_code -@service.route('/', methods=['GET']) +@service.route('/', methods=['GET']) @service.route('', methods=['GET']) def get_service(service_id=None): user_id = request.args.get('user_id', None) @@ -72,7 +72,7 @@ def get_service(service_id=None): return jsonify(data=data) -@service.route('//api-key', methods=['POST']) +@service.route('//api-key', methods=['POST']) def renew_api_key(service_id=None): try: service = get_model_services(service_id=service_id) @@ -93,7 +93,7 @@ def renew_api_key(service_id=None): return jsonify(data=unsigned_api_key), 201 -@service.route('//api-key/revoke/', methods=['POST']) +@service.route('//api-key/revoke/', methods=['POST']) def revoke_api_key(service_id, api_key_id): try: service_api_key = get_model_api_keys(service_id=service_id, id=api_key_id) @@ -106,8 +106,8 @@ def revoke_api_key(service_id, api_key_id): return jsonify(), 202 -@service.route('//api-keys', methods=['GET']) -@service.route('//api-keys/', methods=['GET']) +@service.route('//api-keys', methods=['GET']) +@service.route('//api-keys/', methods=['GET']) def get_api_keys(service_id, key_id=None): try: service = get_model_services(service_id=service_id) @@ -129,7 +129,7 @@ def get_api_keys(service_id, key_id=None): return jsonify(apiKeys=api_keys_schema.dump(api_keys).data), 200 -@service.route('//template', methods=['POST']) +@service.route('//template', methods=['POST']) def create_template(service_id): try: service = get_model_services(service_id=service_id) @@ -147,7 +147,7 @@ def create_template(service_id): return jsonify(data=template_schema.dump(template).data), 201 -@service.route('//template/', methods=['PUT', 'DELETE']) +@service.route('//template/', methods=['PUT', 'DELETE']) def update_template(service_id, template_id): try: service = get_model_services(service_id=service_id) @@ -176,8 +176,8 @@ def update_template(service_id, template_id): return jsonify(data=template_schema.dump(template).data), status_code -@service.route('//template/', methods=['GET']) -@service.route('//template', methods=['GET']) +@service.route('//template/', methods=['GET']) +@service.route('//template', methods=['GET']) def get_service_template(service_id, template_id=None): try: templates = get_model_templates(service_id=service_id, template_id=template_id) diff --git a/app/user/rest.py b/app/user/rest.py index 9c1262298..b4ee71d1e 100644 --- a/app/user/rest.py +++ b/app/user/rest.py @@ -166,7 +166,7 @@ def get_user(user_id=None): @user.route('//service', methods=['GET']) -@user.route('//service/', methods=['GET']) +@user.route('//service/', methods=['GET']) def get_service_by_user_id(user_id, service_id=None): try: user = get_model_users(user_id=user_id) diff --git a/migrations/versions/0011_uuid_service_id.py b/migrations/versions/0011_uuid_service_id.py new file mode 100644 index 000000000..d332930ef --- /dev/null +++ b/migrations/versions/0011_uuid_service_id.py @@ -0,0 +1,121 @@ +"""empty message + +Revision ID: 0011_uuid_service_id +Revises: 0010_add_queue_name_to_service +Create Date: 2016-02-01 15:47:30.553052 + +""" + +# revision identifiers, used by Alembic. +from sqlalchemy.dialects import postgresql + +revision = '0011_uuid_service_id' +down_revision = '0010_add_queue_name_to_service' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + + # op.execute('update jobs set new_service_id = (SELECT queue_name from services where service_id = services.id)') + + op.add_column('user_to_service', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True)) + op.get_bind() + op.execute('update user_to_service '\ + 'set new_service_id = (SELECT queue_name from services where service_id = services.id)') + + op.drop_column('user_to_service', 'service_id') + op.alter_column('user_to_service', 'new_service_id', new_column_name='service_id') + + op.add_column('jobs', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True)) + op.execute('update jobs '\ + 'set new_service_id = (SELECT queue_name from services where service_id = services.id)') + + op.drop_column('jobs', 'service_id') + op.alter_column('jobs', 'new_service_id', new_column_name='service_id', nullable=False) + + op.add_column('api_key', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True)) + op.execute('update api_key '\ + 'set new_service_id = (SELECT queue_name from services where service_id = services.id)') + + op.drop_column('api_key', 'service_id') + op.alter_column('api_key', 'new_service_id', new_column_name='service_id', nullable=False) + + op.add_column('templates', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True)) + op.execute('update templates '\ + 'set new_service_id = (SELECT queue_name from services where service_id = services.id)') + + op.drop_column('templates', 'service_id') + op.alter_column('templates', 'new_service_id', new_column_name='service_id', nullable=False) + + op.drop_constraint('services_pkey', 'services') + op.alter_column('services', 'id', new_column_name='old_id') + op.alter_column('services', 'queue_name', new_column_name='id', nullable=False) + + op.create_primary_key('services_pkey', 'services', ['id']) + + op.create_foreign_key('user_to_service_service_id_fkey', 'user_to_service', 'services', ['service_id'], ['id']) + op.create_foreign_key('api_key_service_id_fkey', 'api_key', 'services', ['service_id'], ['id']) + op.create_foreign_key('jobs_service_id_fkey', 'jobs', 'services', ['service_id'], ['id']) + op.create_foreign_key('templates_service_id_fkey', 'templates', 'services', ['service_id'], ['id']) + + op.create_index(op.f('ix_templates_service_id'), 'templates', ['service_id'], unique=False) + op.create_index(op.f('ix_jobs_service_id'), 'jobs', ['service_id'], unique=False) + op.create_index(op.f('ix_api_key_service_id'), 'api_key', ['service_id'], unique=False) + 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.add_column('user_to_service', sa.Column('new_service_id', sa.Integer, nullable=True)) + op.get_bind() + op.execute('update user_to_service '\ + 'set new_service_id = (SELECT old_id from services where service_id = services.id)') + + op.drop_column('user_to_service', 'service_id') + op.alter_column('user_to_service', 'new_service_id', new_column_name='service_id', nullable=False) + + op.add_column('jobs', sa.Column('new_service_id', sa.Integer, nullable=True)) + op.execute('update jobs '\ + 'set new_service_id = (SELECT old_id from services where service_id = services.id)') + + op.drop_column('jobs', 'service_id') + op.alter_column('jobs', 'new_service_id', new_column_name='service_id', nullable=False) + + op.add_column('api_key', sa.Column('new_service_id', sa.Integer, nullable=True)) + op.execute('update api_key '\ + 'set new_service_id = (SELECT old_id from services where service_id = services.id)') + + op.drop_column('api_key', 'service_id') + op.alter_column('api_key', 'new_service_id', new_column_name='service_id', nullable=False) + + op.add_column('templates', sa.Column('new_service_id', sa.Integer, nullable=True)) + op.execute('update templates '\ + 'set new_service_id = (SELECT old_id from services where service_id = services.id)') + + op.drop_column('templates', 'service_id') + op.alter_column('templates', 'new_service_id', new_column_name='service_id', nullable=False) + + op.drop_constraint('services_pkey', 'services') + + op.alter_column('services', 'id', new_column_name='queue_name', nullable=False) + op.alter_column('services', 'old_id', new_column_name='id', nullable=True) + + op.create_primary_key('services_pkey', 'services', ['id']) + + op.create_foreign_key('user_to_service_service_id_fkey', 'user_to_service', 'services', ['service_id'], ['id']) + op.create_foreign_key('api_key_service_id_fkey', 'api_key', 'services', ['service_id'], ['id']) + op.create_foreign_key('jobs_service_id_fkey', 'jobs', 'services', ['service_id'], ['id']) + op.create_foreign_key('templates_service_id_fkey', 'templates', 'services', ['service_id'], ['id']) + + op.create_index(op.f('ix_api_key_service_id'), 'api_key', ['service_id'], unique=False) + op.create_unique_constraint('uix_service_to_key_name', 'api_key', ['service_id', 'name']) + op.create_index(op.f('ix_jobs_service_id'), 'jobs', ['service_id'], unique=False) + op.create_index(op.f('ix_templates_service_id'), 'templates', ['service_id'], unique=False) + + ### end Alembic commands ### diff --git a/tests/__init__.py b/tests/__init__.py index 2fdb13ad3..e76b036be 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -6,7 +6,7 @@ from app.dao.api_key_dao import get_unsigned_secrets def create_authorization_header(path, method, request_body=None, service_id=None): if service_id: - client_id = service_id + client_id = str(service_id) secret = get_unsigned_secrets(service_id)[0] else: client_id = current_app.config.get('ADMIN_CLIENT_USER_NAME') diff --git a/tests/app/authentication/test_authentication.py b/tests/app/authentication/test_authentication.py index 537dd750a..ea6567a8a 100644 --- a/tests/app/authentication/test_authentication.py +++ b/tests/app/authentication/test_authentication.py @@ -41,7 +41,7 @@ def test_should_not_allow_incorrect_path(notify_api, notify_db, notify_db_sessio token = create_jwt_token(request_method="GET", request_path="/bad", secret=get_unsigned_secrets(sample_api_key.service_id)[0], - client_id=sample_api_key.service_id) + client_id=str(sample_api_key.service_id)) response = client.get(url_for('service.get_service'), headers={'Authorization': "Bearer {}".format(token)}) assert response.status_code == 403 @@ -64,7 +64,7 @@ def test_should_not_allow_invalid_secret(notify_api, notify_db, notify_db_sessio with notify_api.test_request_context(): with notify_api.test_client() as client: token = create_jwt_token(request_method="POST", request_path="/", secret="not-so-secret", - client_id=sample_api_key.service_id) + client_id=str(sample_api_key.service_id)) response = client.get(url_for('service.get_service'), headers={'Authorization': "Bearer {}".format(token)}) assert response.status_code == 403 @@ -76,7 +76,7 @@ def test_should_allow_valid_token(notify_api, notify_db, notify_db_session, samp with notify_api.test_request_context(): with notify_api.test_client() as client: token = __create_get_token(sample_api_key.service_id) - response = client.get(url_for('service.get_service', service_id=sample_api_key.service_id), + response = client.get(url_for('service.get_service', service_id=str(sample_api_key.service_id)), headers={'Authorization': 'Bearer {}'.format(token)}) assert response.status_code == 200 @@ -86,7 +86,7 @@ def test_should_allow_valid_token_for_request_with_path_params(notify_api, notif with notify_api.test_request_context(): with notify_api.test_client() as client: token = __create_get_token(sample_api_key.service_id) - response = client.get(url_for('service.get_service', service_id=sample_api_key.service_id), + response = client.get(url_for('service.get_service', service_id=str(sample_api_key.service_id)), headers={'Authorization': 'Bearer {}'.format(token)}) assert response.status_code == 200 @@ -99,7 +99,7 @@ def test_should_allow_valid_token_when_service_has_multiple_keys(notify_api, not api_key = ApiKey(**data) save_model_api_key(api_key) token = __create_get_token(sample_api_key.service_id) - response = client.get(url_for('service.get_service', service_id=sample_api_key.service_id), + response = client.get(url_for('service.get_service', service_id=str(sample_api_key.service_id)), headers={'Authorization': 'Bearer {}'.format(token)}) assert response.status_code == 200 @@ -123,9 +123,9 @@ def test_should_allow_valid_token_with_post_body(notify_api, notify_db, notify_d token = create_jwt_token( request_method="PUT", - request_path=url_for('service.update_service', service_id=sample_api_key.service_id), + request_path=url_for('service.update_service', service_id=str(sample_api_key.service_id)), secret=get_unsigned_secret(sample_api_key.id), - client_id=sample_api_key.service_id, + client_id=str(sample_api_key.service_id), request_body=json.dumps(data) ) headers = [('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(token))] @@ -139,7 +139,7 @@ def test_should_allow_valid_token_with_post_body(notify_api, notify_db, notify_d def test_should_not_allow_valid_token_with_invalid_post_body(notify_api, notify_db, notify_db_session, sample_api_key): with notify_api.test_request_context(): with notify_api.test_client() as client: - token = __create_post_token(sample_api_key.service_id, JSON_BODY) + token = __create_post_token(str(sample_api_key.service_id), JSON_BODY) response = client.post(url_for('service.create_service'), data="spurious", headers={'Authorization': 'Bearer {}'.format(token)}) @@ -179,7 +179,7 @@ def test_authentication_passes_when_service_has_multiple_keys_some_expired(notif token = create_jwt_token(request_method="GET", request_path=url_for('service.get_service'), secret=get_unsigned_secret(api_key.id), - client_id=sample_api_key.service_id) + client_id=str(sample_api_key.service_id)) response = client.get(url_for('service.get_service'), headers={'Authorization': 'Bearer {}'.format(token)}) assert response.status_code == 200 @@ -200,10 +200,10 @@ def test_authentication_returns_token_expired_when_service_uses_expired_key_and_ token = create_jwt_token(request_method="GET", request_path=url_for('service.get_service'), secret=get_unsigned_secret(expired_api_key.id), - client_id=sample_api_key.service_id) + client_id=str(sample_api_key.service_id)) # expire the key expire_the_key = {'id': expired_api_key.id, - 'service_id': sample_api_key.service_id, + 'service_id': str(sample_api_key.service_id), 'name': 'expired_key', 'expiry_date': datetime.now() + timedelta(hours=-2)} save_model_api_key(expired_api_key, expire_the_key) @@ -219,7 +219,7 @@ def __create_get_token(service_id): return create_jwt_token(request_method="GET", request_path=url_for('service.get_service', service_id=service_id), secret=get_unsigned_secrets(service_id)[0], - client_id=service_id) + client_id=str(service_id)) else: return create_jwt_token(request_method="GET", request_path=url_for('service.get_service'), @@ -232,6 +232,6 @@ def __create_post_token(service_id, request_body): request_method="POST", request_path=url_for('service.create_service'), secret=get_unsigned_secrets(service_id)[0], - client_id=service_id, + client_id=str(service_id), request_body=request_body ) diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 8ab965606..e1abea888 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -78,8 +78,7 @@ def sample_service(notify_db, 'users': [user], 'limit': 1000, 'active': False, - 'restricted': False, - 'queue_name': str(uuid.uuid4())} + 'restricted': False} service = Service.query.filter_by(name=service_name).first() if not service: service = Service(**data) diff --git a/tests/app/job/test_job_rest.py b/tests/app/job/test_job_rest.py index 9b4bcfd3d..b9b7eb20e 100644 --- a/tests/app/job/test_job_rest.py +++ b/tests/app/job/test_job_rest.py @@ -82,7 +82,7 @@ def test_post_job(notify_api, notify_db, notify_db_session, sample_template): file_name = '{}.csv'.format(job_id) data = { 'id': str(job_id), - 'service': service_id, + 'service': str(service_id), 'template': template_id, 'original_file_name': original_file_name, 'bucket_name': bucket_name, @@ -105,7 +105,7 @@ def test_post_job(notify_api, notify_db, notify_db_session, sample_template): resp_json = json.loads(response.get_data(as_text=True)) assert resp_json['data']['id'] == str(job_id) - assert resp_json['data']['service'] == service_id + assert resp_json['data']['service'] == str(service_id) assert resp_json['data']['template'] == template_id assert resp_json['data']['original_file_name'] == original_file_name diff --git a/tests/app/service/test_api_key_endpoints.py b/tests/app/service/test_api_key_endpoints.py index d288f995b..aee852396 100644 --- a/tests/app/service/test_api_key_endpoints.py +++ b/tests/app/service/test_api_key_endpoints.py @@ -34,9 +34,12 @@ def test_api_key_should_return_error_when_service_does_not_exist(notify_api, not sample_service): with notify_api.test_request_context(): with notify_api.test_client() as client: - auth_header = create_authorization_header(path=url_for('service.renew_api_key', service_id="123"), + import uuid + missing_service_id = uuid.uuid4() + auth_header = create_authorization_header(path=url_for('service.renew_api_key', + service_id=missing_service_id), method='POST') - response = client.post(url_for('service.renew_api_key', service_id=123), + response = client.post(url_for('service.renew_api_key', service_id=missing_service_id), headers=[('Content-Type', 'application/json'), auth_header]) assert response.status_code == 404 diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 89f780363..cf21ade81 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -1,4 +1,5 @@ import json +import uuid from collections import Set from flask import url_for @@ -25,7 +26,7 @@ def test_get_service_list(notify_api, notify_db, notify_db_session, sample_servi # TODO assert correct json returned assert len(json_resp['data']) == 2 assert json_resp['data'][0]['name'] == sample_service.name - assert json_resp['data'][0]['id'] == sample_service.id + assert json_resp['data'][0]['id'] == str(sample_service.id) def test_get_service(notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id): @@ -43,7 +44,7 @@ def test_get_service(notify_api, notify_db, notify_db_session, sample_service, s assert resp.status_code == 200 json_resp = json.loads(resp.get_data(as_text=True)) assert json_resp['data']['name'] == sample_service.name - assert json_resp['data']['id'] == sample_service.id + assert json_resp['data']['id'] == str(sample_service.id) def test_get_service_for_user(notify_api, notify_db, notify_db_session, sample_service): @@ -93,7 +94,6 @@ def test_post_service(notify_api, notify_db, notify_db_session, sample_user, sam json_resp = json.loads(resp.get_data(as_text=True)) assert json_resp['data']['name'] == service.name assert json_resp['data']['limit'] == service.limit - assert service.queue_name is not None def test_post_service_multiple_users(notify_api, notify_db, notify_db_session, sample_user, sample_admin_service_id): @@ -204,12 +204,14 @@ def test_put_service_not_exists(notify_api, notify_db, notify_db_session, sample 'limit': 1000, 'restricted': False, 'active': False} + missing_service_id = uuid.uuid4() auth_header = create_authorization_header(service_id=sample_admin_service_id, - path=url_for('service.update_service', service_id="123"), + path=url_for('service.update_service', + service_id=missing_service_id), method='PUT', request_body=json.dumps(data)) resp = client.put( - url_for('service.update_service', service_id="123"), + url_for('service.update_service', service_id=missing_service_id), data=json.dumps(data), headers=[('Content-Type', 'application/json'), auth_header]) assert resp.status_code == 404 @@ -325,11 +327,13 @@ def test_delete_service_not_exists(notify_api, notify_db, notify_db_session, sam with notify_api.test_request_context(): with notify_api.test_client() as client: assert Service.query.count() == 2 + missing_service_id = uuid.uuid4() auth_header = create_authorization_header(service_id=sample_admin_service_id, - path=url_for('service.update_service', service_id="123"), + path=url_for('service.update_service', + service_id=missing_service_id), method='DELETE') resp = client.delete( - url_for('service.update_service', service_id="123"), + url_for('service.update_service', service_id=missing_service_id), headers=[('Content-Type', 'application/json'), auth_header]) assert resp.status_code == 404 assert Service.query.count() == 2 @@ -371,7 +375,7 @@ def test_create_template(notify_api, notify_db, notify_db_session, sample_servic 'name': template_name, 'template_type': template_type, 'content': template_content, - 'service': sample_service.id + 'service': str(sample_service.id) } auth_header = create_authorization_header(service_id=sample_admin_service_id, path=url_for('service.create_template', @@ -406,14 +410,16 @@ def test_create_template_service_not_exists(notify_api, notify_db, notify_db_ses 'name': template_name, 'template_type': template_type, 'content': template_content, - 'service': sample_service.id + 'service': str(sample_service.id) } + missing_service_id = uuid.uuid4() auth_header = create_authorization_header(service_id=sample_admin_service_id, - path=url_for('service.create_template', service_id="123"), + path=url_for('service.create_template', + service_id=missing_service_id), method='POST', request_body=json.dumps(data)) resp = client.post( - url_for('service.create_template', service_id="123"), + url_for('service.create_template', service_id=missing_service_id), data=json.dumps(data), headers=[('Content-Type', 'application/json'), auth_header]) assert resp.status_code == 404 @@ -439,7 +445,7 @@ def test_update_template(notify_api, notify_db, notify_db_session, sample_templa 'name': template_name, 'template_type': template_type, 'content': template_content, - 'service': sample_service.id + 'service': str(sample_service.id) } auth_header = create_authorization_header(service_id=sample_admin_service_id, path=url_for('service.update_template', @@ -478,17 +484,18 @@ def test_update_template_service_not_exists(notify_api, notify_db, notify_db_ses 'name': template_name, 'template_type': template_type, 'content': template_content, - 'service': sample_template.service_id + 'service': str(sample_template.service_id) } + missing_service_id = uuid.uuid4() auth_header = create_authorization_header(service_id=sample_admin_service_id, path=url_for('service.update_template', - service_id="123", + service_id=missing_service_id, template_id=sample_template.id), method='PUT', request_body=json.dumps(data)) resp = client.put( url_for('service.update_template', - service_id="123", + service_id=missing_service_id, template_id=sample_template.id), data=json.dumps(data), headers=[('Content-Type', 'application/json'), auth_header]) @@ -515,7 +522,7 @@ def test_update_template_template_not_exists(notify_api, notify_db, notify_db_se 'name': template_name, 'template_type': template_type, 'content': template_content, - 'service': sample_service.id + 'service': str(sample_service.id) } auth_header = create_authorization_header(service_id=sample_admin_service_id, path=url_for('service.update_template', @@ -552,7 +559,7 @@ def test_create_template_unicode_content(notify_api, notify_db, notify_db_sessio 'name': template_name, 'template_type': template_type, 'content': template_content, - 'service': sample_service.id + 'service': str(sample_service.id) } auth_header = create_authorization_header(service_id=sample_admin_service_id, path=url_for('service.create_template', diff --git a/tests/app/user/test_rest.py b/tests/app/user/test_rest.py index e49076503..008da0558 100644 --- a/tests/app/user/test_rest.py +++ b/tests/app/user/test_rest.py @@ -315,7 +315,7 @@ def test_get_user_service(notify_api, notify_db, notify_db_session, sample_servi assert resp.status_code == 200 json_resp = json.loads(resp.get_data(as_text=True)) assert json_resp['data']['name'] == another_name - assert json_resp['data']['id'] == another_service.id + assert json_resp['data']['id'] == str(another_service.id) def test_get_user_service_user_not_exists(notify_api, notify_db, notify_db_session, sample_service, @@ -348,12 +348,14 @@ def test_get_user_service_service_not_exists(notify_api, notify_db, notify_db_se with notify_api.test_client() as client: user = User.query.first() assert Service.query.count() == 2 + import uuid + missing_service_id = uuid.uuid4() auth_header = create_authorization_header(service_id=sample_admin_service_id, path=url_for('user.get_service_by_user_id', user_id=user.id, - service_id="12323423"), + service_id=missing_service_id), method='GET') resp = client.get( - url_for('user.get_service_by_user_id', user_id=user.id, service_id="12323423"), + url_for('user.get_service_by_user_id', user_id=user.id, service_id=missing_service_id), headers=[('Content-Type', 'application/json'), auth_header]) assert resp.status_code == 404 json_resp = json.loads(resp.get_data(as_text=True)) diff --git a/tests/conftest.py b/tests/conftest.py index a61f1e724..9ca193f57 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -38,6 +38,7 @@ def notify_db(notify_api, request): def teardown(): db.session.remove() + db.engine.execute("drop sequence services_id_seq cascade") db.drop_all() db.engine.execute("drop table alembic_version") db.get_engine(notify_api).dispose()