mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
Update model to remove the string length restriction.
Moved logic to the dao from the endpoint.
This commit is contained in:
@@ -17,7 +17,14 @@ def save_service_inbound_api(service_inbound_api):
|
||||
|
||||
@transactional
|
||||
@version_class(ServiceInboundApi)
|
||||
def reset_service_inbound_api(service_inbound_api):
|
||||
def reset_service_inbound_api(service_inbound_api, updated_by_id, url=None, bearer_token=None):
|
||||
if url:
|
||||
service_inbound_api.url = url
|
||||
if bearer_token:
|
||||
service_inbound_api.bearer_token = generate_secret(bearer_token)
|
||||
service_inbound_api.updated_by_id = updated_by_id
|
||||
service_inbound_api.updated_at = datetime.utcnow()
|
||||
|
||||
db.session.add(service_inbound_api)
|
||||
|
||||
|
||||
|
||||
@@ -300,8 +300,8 @@ class ServiceInboundApi(db.Model, Versioned):
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False, unique=True)
|
||||
service = db.relationship('Service', backref='inbound_api')
|
||||
url = db.Column(db.String(255), nullable=False)
|
||||
bearer_token = db.Column(db.String(255), nullable=False)
|
||||
url = db.Column(db.String(), nullable=False)
|
||||
bearer_token = db.Column(db.String(), nullable=False)
|
||||
created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False)
|
||||
updated_at = db.Column(db.DateTime, nullable=True)
|
||||
updated_by = db.relationship('User')
|
||||
|
||||
@@ -551,15 +551,7 @@ def create_service_inbound_api(service_id):
|
||||
try:
|
||||
save_service_inbound_api(inbound_api)
|
||||
except SQLAlchemyError as e:
|
||||
if hasattr(e, 'orig') and hasattr(e.orig, 'pgerror')and e.orig.pgerror\
|
||||
and ('duplicate key value violates unique constraint "ix_service_inbound_api_service_id"'
|
||||
in e.orig.pgerror):
|
||||
return jsonify(
|
||||
result='error',
|
||||
message={'name': ["You can only have one URL and bearer token for your service."]}
|
||||
), 400
|
||||
else:
|
||||
raise e
|
||||
return handle_sql_errror(e)
|
||||
|
||||
return jsonify(data=inbound_api.serialize()), 201
|
||||
|
||||
@@ -571,14 +563,10 @@ def update_service_inbound_api(service_id, inbound_api_id):
|
||||
|
||||
to_update = get_service_inbound_api(inbound_api_id, service_id)
|
||||
|
||||
if data.get("url", None):
|
||||
to_update.url = data["url"]
|
||||
if data.get("bearer_token", None):
|
||||
to_update.bearer_token = generate_secret(data["bearer_token"])
|
||||
to_update.updated_by_id = data["updated_by_id"]
|
||||
to_update.updated_at = datetime.utcnow()
|
||||
|
||||
reset_service_inbound_api(to_update)
|
||||
reset_service_inbound_api(service_inbound_api=to_update,
|
||||
updated_by_id=data["updated_by_id"],
|
||||
url=data.get("url", None),
|
||||
bearer_token=data.get("bearer_token", None))
|
||||
return jsonify(data=to_update.serialize()), 200
|
||||
|
||||
|
||||
@@ -587,3 +575,20 @@ def fetch_service_inbound_api(service_id, inbound_api_id):
|
||||
inbound_api = get_service_inbound_api(inbound_api_id, service_id)
|
||||
|
||||
return jsonify(data=inbound_api.serialize()), 200
|
||||
|
||||
|
||||
def handle_sql_errror(e):
|
||||
if hasattr(e, 'orig') and hasattr(e.orig, 'pgerror') and e.orig.pgerror \
|
||||
and ('duplicate key value violates unique constraint "ix_service_inbound_api_service_id"'
|
||||
in e.orig.pgerror):
|
||||
return jsonify(
|
||||
result='error',
|
||||
message={'name': ["You can only have one URL and bearer token for your service."]}
|
||||
), 400
|
||||
elif hasattr(e, 'orig') and hasattr(e.orig, 'pgerror') and e.orig.pgerror \
|
||||
and ('insert or update on table "service_inbound_api" violates '
|
||||
'foreign key constraint "service_inbound_api_service_id_fkey"'
|
||||
in e.orig.pgerror):
|
||||
return jsonify(result='error', message="No result found"), 404
|
||||
else:
|
||||
raise e
|
||||
|
||||
@@ -17,8 +17,8 @@ def upgrade():
|
||||
op.create_table('service_inbound_api_history',
|
||||
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('url', sa.String(length=255), nullable=False),
|
||||
sa.Column('bearer_token', sa.String(length=255), nullable=False),
|
||||
sa.Column('url', sa.String(), nullable=False),
|
||||
sa.Column('bearer_token', sa.String(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('updated_by_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
@@ -32,8 +32,8 @@ def upgrade():
|
||||
op.create_table('service_inbound_api',
|
||||
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('url', sa.String(length=255), nullable=False),
|
||||
sa.Column('bearer_token', sa.String(length=255), nullable=False),
|
||||
sa.Column('url', sa.String(), nullable=False),
|
||||
sa.Column('bearer_token', sa.String(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
||||
sa.Column('updated_by_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
|
||||
@@ -4,15 +4,18 @@ import pytest
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app.authentication.utils import get_secret
|
||||
from app.dao.service_inbound_api_dao import save_service_inbound_api, reset_service_inbound_api, \
|
||||
from app.dao.service_inbound_api_dao import (
|
||||
save_service_inbound_api,
|
||||
reset_service_inbound_api,
|
||||
get_service_inbound_api
|
||||
)
|
||||
from app.models import ServiceInboundApi
|
||||
|
||||
|
||||
def test_save_service_inbound_api(sample_service):
|
||||
service_inbound_api = ServiceInboundApi(
|
||||
service_id=sample_service.id,
|
||||
url="https::/some_service/inbound_messages",
|
||||
url="https://some_service/inbound_messages",
|
||||
bearer_token="some_unique_string",
|
||||
updated_by_id=sample_service.users[0].id
|
||||
)
|
||||
@@ -22,28 +25,28 @@ def test_save_service_inbound_api(sample_service):
|
||||
results = ServiceInboundApi.query.all()
|
||||
assert len(results) == 1
|
||||
inbound_api = results[0]
|
||||
assert inbound_api.id
|
||||
assert inbound_api.id is not None
|
||||
assert inbound_api.service_id == sample_service.id
|
||||
assert inbound_api.updated_by_id == sample_service.users[0].id
|
||||
assert inbound_api.url == "https::/some_service/inbound_messages"
|
||||
assert inbound_api.url == "https://some_service/inbound_messages"
|
||||
assert inbound_api.unsigned_bearer_token == "some_unique_string"
|
||||
assert inbound_api.bearer_token != "some_unique_string"
|
||||
assert not inbound_api.updated_at
|
||||
assert inbound_api.updated_at is None
|
||||
|
||||
versioned = ServiceInboundApi.get_history_model().query.filter_by(id=inbound_api.id).one()
|
||||
assert versioned.id == inbound_api.id
|
||||
assert versioned.service_id == sample_service.id
|
||||
assert versioned.updated_by_id == sample_service.users[0].id
|
||||
assert versioned.url == "https::/some_service/inbound_messages"
|
||||
assert versioned.url == "https://some_service/inbound_messages"
|
||||
assert versioned.bearer_token != "some_unique_string"
|
||||
assert not versioned.updated_at
|
||||
assert versioned.updated_at is None
|
||||
assert versioned.version == 1
|
||||
|
||||
|
||||
def test_save_service_inbound_api_fails_if_service_does_not_exist(notify_db, notify_db_session):
|
||||
service_inbound_api = ServiceInboundApi(
|
||||
service_id=uuid.uuid4(),
|
||||
url="https::/some_service/inbound_messages",
|
||||
url="https://some_service/inbound_messages",
|
||||
bearer_token="some_unique_string",
|
||||
updated_by_id=uuid.uuid4()
|
||||
)
|
||||
@@ -55,7 +58,7 @@ def test_save_service_inbound_api_fails_if_service_does_not_exist(notify_db, not
|
||||
def test_update_service_inbound_api(sample_service):
|
||||
service_inbound_api = ServiceInboundApi(
|
||||
service_id=sample_service.id,
|
||||
url="https::/some_service/inbound_messages",
|
||||
url="https://some_service/inbound_messages",
|
||||
bearer_token="some_unique_string",
|
||||
updated_by_id=sample_service.users[0].id
|
||||
)
|
||||
@@ -65,33 +68,31 @@ def test_update_service_inbound_api(sample_service):
|
||||
assert len(results) == 1
|
||||
saved_inbound_api = results[0]
|
||||
|
||||
updated_inbound_api = saved_inbound_api
|
||||
updated_inbound_api.url = "https::/some_service/changed_url"
|
||||
|
||||
reset_service_inbound_api(updated_inbound_api)
|
||||
reset_service_inbound_api(saved_inbound_api, updated_by_id=sample_service.users[0].id,
|
||||
url="https://some_service/changed_url")
|
||||
updated_results = ServiceInboundApi.query.all()
|
||||
assert len(updated_results) == 1
|
||||
updated = updated_results[0]
|
||||
assert updated.id
|
||||
assert updated.id is not None
|
||||
assert updated.service_id == sample_service.id
|
||||
assert updated.updated_by_id == sample_service.users[0].id
|
||||
assert updated.url == "https::/some_service/changed_url"
|
||||
assert updated.url == "https://some_service/changed_url"
|
||||
assert updated.unsigned_bearer_token == "some_unique_string"
|
||||
assert updated.bearer_token != "some_unique_string"
|
||||
assert updated.updated_at
|
||||
assert updated.updated_at is not None
|
||||
|
||||
versioned_results = ServiceInboundApi.get_history_model().query.filter_by(id=saved_inbound_api.id).all()
|
||||
assert len(versioned_results) == 2
|
||||
for x in versioned_results:
|
||||
if x.version == 1:
|
||||
assert x.url == "https::/some_service/inbound_messages"
|
||||
assert x.url == "https://some_service/inbound_messages"
|
||||
assert not x.updated_at
|
||||
elif x.version == 2:
|
||||
assert x.url == "https::/some_service/changed_url"
|
||||
assert x.url == "https://some_service/changed_url"
|
||||
assert x.updated_at
|
||||
else:
|
||||
pytest.fail("version should not exist")
|
||||
assert x.id
|
||||
assert x.id is not None
|
||||
assert x.service_id == sample_service.id
|
||||
assert x.updated_by_id == sample_service.users[0].id
|
||||
assert get_secret(x.bearer_token) == "some_unique_string"
|
||||
@@ -100,17 +101,17 @@ def test_update_service_inbound_api(sample_service):
|
||||
def test_get_service_inbound_api(sample_service):
|
||||
service_inbound_api = ServiceInboundApi(
|
||||
service_id=sample_service.id,
|
||||
url="https::/some_service/inbound_messages",
|
||||
url="https://some_service/inbound_messages",
|
||||
bearer_token="some_unique_string",
|
||||
updated_by_id=sample_service.users[0].id
|
||||
)
|
||||
save_service_inbound_api(service_inbound_api)
|
||||
|
||||
inbound_api = get_service_inbound_api(service_inbound_api.id, sample_service.id)
|
||||
assert inbound_api.id
|
||||
assert inbound_api.id is not None
|
||||
assert inbound_api.service_id == sample_service.id
|
||||
assert inbound_api.updated_by_id == sample_service.users[0].id
|
||||
assert inbound_api.url == "https::/some_service/inbound_messages"
|
||||
assert inbound_api.url == "https://some_service/inbound_messages"
|
||||
assert inbound_api.unsigned_bearer_token == "some_unique_string"
|
||||
assert inbound_api.bearer_token != "some_unique_string"
|
||||
assert not inbound_api.updated_at
|
||||
assert inbound_api.updated_at is None
|
||||
|
||||
@@ -2174,7 +2174,7 @@ def test_create_service_inbound_api(client, sample_service):
|
||||
assert not resp_json["updated_at"]
|
||||
|
||||
|
||||
def test_set_service_inbound_api_raises_500_when_service_does_not_exist(client):
|
||||
def test_set_service_inbound_api_raises_404_when_service_does_not_exist(client):
|
||||
data = {
|
||||
"url": "https://some_service/inbound-sms",
|
||||
"bearer_token": "some-unique-string",
|
||||
@@ -2185,7 +2185,8 @@ def test_set_service_inbound_api_raises_500_when_service_does_not_exist(client):
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), create_authorization_header()]
|
||||
)
|
||||
assert response.status_code == 500
|
||||
assert response.status_code == 404
|
||||
assert json.loads(response.get_data(as_text=True))['message'] == 'No result found'
|
||||
|
||||
|
||||
def test_update_service_inbound_api_updates_url(client, sample_service):
|
||||
|
||||
Reference in New Issue
Block a user