Inject dependencies into tornado app

This commit is contained in:
richard-dds 2018-06-18 10:44:06 -04:00
parent 6eb71cfe63
commit 64c71c0aa7
6 changed files with 65 additions and 12 deletions

5
app.py
View File

@ -2,10 +2,11 @@
import tornado.ioloop
from atst.app import make_app, make_config
from atst.app import make_app, make_deps, make_config
config = make_config()
app = make_app(config)
deps = make_deps(config)
app = make_app(config, deps)
if __name__ == '__main__':
port = int(config['default']['PORT'])

View File

@ -16,17 +16,14 @@ from atst.api_client import ApiClient
ENV = os.getenv("TORNADO_ENV", "dev")
def make_app(config,**kwargs):
authz_client = ApiClient(config["default"]["AUTHZ_BASE_URL"])
authnid_client = ApiClient(config["default"]["AUTHNID_BASE_URL"])
def make_app(config, deps, **kwargs):
routes = [
url(r"/", Home, {"page": "login"}, name="main"),
url(
r"/login",
Login,
{"authnid_client": authnid_client},
{"authnid_client": deps["authnid_client"]},
name="login",
),
url(r"/home", MainHandler, {"page": "home"}, name="home"),
@ -34,15 +31,19 @@ def make_app(config,**kwargs):
url(
r"/workspaces",
Workspace,
{"page": "workspaces", "authz_client": authz_client},
{"page": "workspaces", "authz_client": deps["authz_client"]},
name="workspaces",
),
url(r"/requests", Request, {"page": "requests"}, name="requests"),
url(r"/requests/new", RequestNew, {"page": "requests_new"}, name="request_new"),
url(
r"/requests/new",
RequestNew,
{"page": "requests_new", "requests_client": deps["requests_client"]},
name="request_new"),
url(
r"/requests/new/([0-9])",
RequestNew,
{"page": "requests_new"},
{"page": "requests_new", "requests_client": deps["requests_client"]},
name="request_form",
),
url(r"/users", MainHandler, {"page": "users"}, name="users"),
@ -67,6 +68,14 @@ def make_app(config,**kwargs):
return app
def make_deps(config):
return {
'authz_client': ApiClient(config["default"]["AUTHZ_BASE_URL"]),
'authnid_client': ApiClient(config["default"]["AUTHNID_BASE_URL"]),
'requests_client': ApiClient(config["default"]["REQUESTS_QUEUE_BASE_URL"])
}
def make_config():
BASE_CONFIG_FILENAME = os.path.join(
os.path.dirname(__file__),

View File

@ -7,3 +7,4 @@ AUTHNID_BASE_URL= http://localhost
COOKIE_SECRET = some-secret-please-replace
SECRET = change_me_into_something_secret
CAC_URL = http://localhost:8888/home
REQUESTS_QUEUE_BASE_URL = http://localhost:8003

0
tests/__init__.py Normal file
View File

View File

@ -1,9 +1,19 @@
import pytest
from atst.app import make_app, make_config
from atst.app import make_app, make_deps, make_config
from tests.mocks import MockApiClient
@pytest.fixture
def app():
TEST_DEPS = {
'authz_client': MockApiClient('authz'),
'requests_client': MockApiClient('requests'),
'authnid_client': MockApiClient('authnid'),
}
config = make_config()
return make_app(config)
deps = make_deps(config)
deps.update(TEST_DEPS)
return make_app(config, deps)

32
tests/mocks.py Normal file
View File

@ -0,0 +1,32 @@
import tornado.gen
from tornado.httpclient import HTTPRequest, HTTPResponse
class MockApiClient(object):
def __init__(self, service):
self.service = service
@tornado.gen.coroutine
def get(self, path, **kwargs):
return self._get_response('GET', path)
@tornado.gen.coroutine
def put(self, path, **kwargs):
return self._get_response('PUT', path)
@tornado.gen.coroutine
def patch(self, path, **kwargs):
return self._get_response('PATCH', path)
@tornado.gen.coroutine
def post(self, path, **kwargs):
return self._get_response('POST', path)
@tornado.gen.coroutine
def delete(self, path, **kwargs):
return self._get_response('DELETE', path)
def _get_response(self, verb, path):
response = HTTPResponse(request=HTTPRequest(path, verb), code=200)
setattr(response, 'json', {})
return response