Refactor ApiKeys.secret and ServiceInboundApi.bearer_token to use the same encryption method and get rid of the duplicate code.

This commit is contained in:
Rebecca Law
2017-06-19 14:32:22 +01:00
parent 6202da7dea
commit 3a66027d6a
15 changed files with 55 additions and 79 deletions

View File

@@ -4,7 +4,7 @@ import pytest
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm.exc import NoResultFound
from app.authentication.utils import get_secret
from app import encryption
from app.dao.api_key_dao import (save_model_api_key,
get_model_api_keys,
get_unsigned_secrets,
@@ -63,14 +63,14 @@ def test_should_return_api_key_for_service(notify_api, notify_db, notify_db_sess
def test_should_return_unsigned_api_keys_for_service_id(sample_api_key):
unsigned_api_key = get_unsigned_secrets(sample_api_key.service_id)
assert len(unsigned_api_key) == 1
assert sample_api_key.secret != unsigned_api_key[0]
assert unsigned_api_key[0] == get_secret(sample_api_key.secret)
assert sample_api_key._secret != unsigned_api_key[0]
assert unsigned_api_key[0] == sample_api_key.secret
def test_get_unsigned_secret_returns_key(sample_api_key):
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)
assert sample_api_key._secret != unsigned_api_key
assert unsigned_api_key == sample_api_key.secret
def test_should_not_allow_duplicate_key_names_per_service(sample_api_key, fake_uuid):

View File

@@ -3,7 +3,7 @@ import uuid
import pytest
from sqlalchemy.exc import SQLAlchemyError
from app.authentication.utils import get_secret
from app import encryption
from app.dao.service_inbound_api_dao import (
save_service_inbound_api,
reset_service_inbound_api,
@@ -29,8 +29,8 @@ def test_save_service_inbound_api(sample_service):
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.unsigned_bearer_token == "some_unique_string"
assert inbound_api.bearer_token != "some_unique_string"
assert inbound_api.bearer_token == "some_unique_string"
assert inbound_api._bearer_token != "some_unique_string"
assert inbound_api.updated_at is None
versioned = ServiceInboundApi.get_history_model().query.filter_by(id=inbound_api.id).one()
@@ -38,7 +38,7 @@ def test_save_service_inbound_api(sample_service):
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.bearer_token != "some_unique_string"
assert encryption.decrypt(versioned._bearer_token) == "some_unique_string"
assert versioned.updated_at is None
assert versioned.version == 1
@@ -77,8 +77,8 @@ def test_update_service_inbound_api(sample_service):
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.unsigned_bearer_token == "some_unique_string"
assert updated.bearer_token != "some_unique_string"
assert updated.bearer_token == "some_unique_string"
assert updated._bearer_token != "some_unique_string"
assert updated.updated_at is not None
versioned_results = ServiceInboundApi.get_history_model().query.filter_by(id=saved_inbound_api.id).all()
@@ -95,7 +95,7 @@ def test_update_service_inbound_api(sample_service):
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"
assert encryption.decrypt(x._bearer_token) == "some_unique_string"
def test_get_service_inbound_api(sample_service):
@@ -112,6 +112,6 @@ def test_get_service_inbound_api(sample_service):
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.unsigned_bearer_token == "some_unique_string"
assert inbound_api.bearer_token != "some_unique_string"
assert inbound_api.bearer_token == "some_unique_string"
assert inbound_api._bearer_token != "some_unique_string"
assert inbound_api.updated_at is None