Skip to content

Commit

Permalink
Merge pull request #34 from nefarioustim/task/31/clean-api
Browse files Browse the repository at this point in the history
Provide clean Python API
  • Loading branch information
nefarioustim committed Jan 1, 2019
2 parents 770b601 + 1b57ba3 commit ba9ac18
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 28 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ lock: clean-pyc
test: clean-pyc
-docker-compose run --rm --no-deps app pipenv run pytest

e2etest: clean-pyc
-docker-compose run --rm --no-deps app pipenv run pytest e2etests/

docs: clean-pyc
docker-compose run --rm --no-deps app pipenv run make -C docs html

Expand Down
23 changes: 23 additions & 0 deletions e2etests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Test fixtures and configuration for e2e tests.
"""

import pytest
from sqlalchemy import create_engine
import cerberusauth
from cerberusauth.storage import sql as sqlstorage


@pytest.fixture(autouse=True)
def storage_fixture(monkeypatch):
"""Storage fixture for all e2e tests."""
engine = create_engine('sqlite:///:memory:')
monkeypatch.setattr(sqlstorage, "engine", engine)


@pytest.fixture
def cerberus_fixture():
"""Session fixture for e2e tests."""
cerberus = cerberusauth.cerberus()
cerberus.create_schema()
return cerberus
16 changes: 16 additions & 0 deletions e2etests/test_e2e.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
E2E tests.
"""

from sqlalchemy.inspection import inspect
from cerberusauth.schema import sql as sqlschema

EXPECTED_MODELS = ['user', 'role', 'permission']


def test_schema_creation(cerberus_fixture):
"""."""
assert "sqlite" in str(sqlschema.sqlstorage.engine)
iengine = inspect(sqlschema.sqlstorage.engine)

assert set(iengine.get_table_names()) == set(EXPECTED_MODELS)
25 changes: 0 additions & 25 deletions e2etests/test_schema_creation.py

This file was deleted.

28 changes: 25 additions & 3 deletions src/cerberusauth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
Cerberus - Authentication and authorisation microservice.
"""

from . import config
from . import registration
from . import schema
from . import storage


def cerberus():
return CerberusAuth(
registration_service=registration.create_registration_service()
storage_strategy=config.STORAGE_STRATEGY
)


Expand All @@ -16,6 +19,25 @@ class CerberusAuth(object):
Provides authentication and authorisation as a microservice.
"""

def __init__(self, registration_service):
def __init__(self, storage_strategy):
"""Initialise an instance."""
self.registration = registration_service
self.storage_strategy = storage_strategy

self._setup_storage()
self._setup_services()

def _setup_storage(self):
"""Setup storage from self.storage_strategy."""
self.has_storage_session = storage.has_storage_session(
self.storage_strategy)
self.storage_session = storage.get_storage_session(
self.storage_strategy) if self.has_storage_session else None

def _setup_services(self):
"""Setup services with self.storage_session."""
self.registration = registration.create_registration_service(
session=self.storage_session)

def create_schema(self):
"""Create storage schema."""
schema.create_schema()
14 changes: 14 additions & 0 deletions src/cerberusauth/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
from .. import config
from .. import strategy

STORAGE_MAP = {
'sql': {
'has_storage_session': True
}
}


def has_storage_session(storage_strategy=None):
"""Return boolean flagging the need for a storage session."""
return bool(
STORAGE_MAP[storage_strategy or config.STORAGE_STRATEGY]
.get('has_storage_session', True)
)


def get_storage_session(storage_strategy=None):
"""Return Schema class."""
Expand Down
14 changes: 14 additions & 0 deletions tests/test_cerberusauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
Tests for core objects.
"""

from unittest import mock
import cerberusauth
import cerberusauth.schema


def test_cerberus():
Expand All @@ -12,3 +14,15 @@ def test_cerberus():
assert cerberus
assert isinstance(cerberus, cerberusauth.CerberusAuth)
assert cerberus.registration


def test_create_schema(monkeypatch):
"""."""
create_schema_mock = mock.Mock()
monkeypatch.setattr(
cerberusauth.schema, "create_schema", create_schema_mock)

cerberus = cerberusauth.cerberus()
cerberus.create_schema()

create_schema_mock.assert_called_once()
8 changes: 8 additions & 0 deletions tests/test_storage_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
Tests for SQL get_storage_session.
"""

import pytest

from sqlalchemy.orm.session import Session
from cerberusauth import storage


@pytest.mark.parametrize("storage_strategy", (False, "sql"))
def test_has_storage_session(storage_strategy):
"""."""
assert storage.has_storage_session(storage_strategy)


def test_get_storage_session():
"""."""
session = storage.get_storage_session()
Expand Down

0 comments on commit ba9ac18

Please sign in to comment.