Inject dependencies into tornado app
This commit is contained in:
parent
6eb71cfe63
commit
64c71c0aa7
5
app.py
5
app.py
@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
import tornado.ioloop
|
import tornado.ioloop
|
||||||
|
|
||||||
from atst.app import make_app, make_config
|
from atst.app import make_app, make_deps, make_config
|
||||||
|
|
||||||
config = make_config()
|
config = make_config()
|
||||||
app = make_app(config)
|
deps = make_deps(config)
|
||||||
|
app = make_app(config, deps)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
port = int(config['default']['PORT'])
|
port = int(config['default']['PORT'])
|
||||||
|
25
atst/app.py
25
atst/app.py
@ -16,17 +16,14 @@ from atst.api_client import ApiClient
|
|||||||
ENV = os.getenv("TORNADO_ENV", "dev")
|
ENV = os.getenv("TORNADO_ENV", "dev")
|
||||||
|
|
||||||
|
|
||||||
def make_app(config,**kwargs):
|
def make_app(config, deps, **kwargs):
|
||||||
|
|
||||||
authz_client = ApiClient(config["default"]["AUTHZ_BASE_URL"])
|
|
||||||
authnid_client = ApiClient(config["default"]["AUTHNID_BASE_URL"])
|
|
||||||
|
|
||||||
routes = [
|
routes = [
|
||||||
url(r"/", Home, {"page": "login"}, name="main"),
|
url(r"/", Home, {"page": "login"}, name="main"),
|
||||||
url(
|
url(
|
||||||
r"/login",
|
r"/login",
|
||||||
Login,
|
Login,
|
||||||
{"authnid_client": authnid_client},
|
{"authnid_client": deps["authnid_client"]},
|
||||||
name="login",
|
name="login",
|
||||||
),
|
),
|
||||||
url(r"/home", MainHandler, {"page": "home"}, name="home"),
|
url(r"/home", MainHandler, {"page": "home"}, name="home"),
|
||||||
@ -34,15 +31,19 @@ def make_app(config,**kwargs):
|
|||||||
url(
|
url(
|
||||||
r"/workspaces",
|
r"/workspaces",
|
||||||
Workspace,
|
Workspace,
|
||||||
{"page": "workspaces", "authz_client": authz_client},
|
{"page": "workspaces", "authz_client": deps["authz_client"]},
|
||||||
name="workspaces",
|
name="workspaces",
|
||||||
),
|
),
|
||||||
url(r"/requests", Request, {"page": "requests"}, name="requests"),
|
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(
|
url(
|
||||||
r"/requests/new/([0-9])",
|
r"/requests/new/([0-9])",
|
||||||
RequestNew,
|
RequestNew,
|
||||||
{"page": "requests_new"},
|
{"page": "requests_new", "requests_client": deps["requests_client"]},
|
||||||
name="request_form",
|
name="request_form",
|
||||||
),
|
),
|
||||||
url(r"/users", MainHandler, {"page": "users"}, name="users"),
|
url(r"/users", MainHandler, {"page": "users"}, name="users"),
|
||||||
@ -67,6 +68,14 @@ def make_app(config,**kwargs):
|
|||||||
return app
|
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():
|
def make_config():
|
||||||
BASE_CONFIG_FILENAME = os.path.join(
|
BASE_CONFIG_FILENAME = os.path.join(
|
||||||
os.path.dirname(__file__),
|
os.path.dirname(__file__),
|
||||||
|
@ -7,3 +7,4 @@ AUTHNID_BASE_URL= http://localhost
|
|||||||
COOKIE_SECRET = some-secret-please-replace
|
COOKIE_SECRET = some-secret-please-replace
|
||||||
SECRET = change_me_into_something_secret
|
SECRET = change_me_into_something_secret
|
||||||
CAC_URL = http://localhost:8888/home
|
CAC_URL = http://localhost:8888/home
|
||||||
|
REQUESTS_QUEUE_BASE_URL = http://localhost:8003
|
||||||
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
@ -1,9 +1,19 @@
|
|||||||
import pytest
|
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
|
@pytest.fixture
|
||||||
def app():
|
def app():
|
||||||
|
TEST_DEPS = {
|
||||||
|
'authz_client': MockApiClient('authz'),
|
||||||
|
'requests_client': MockApiClient('requests'),
|
||||||
|
'authnid_client': MockApiClient('authnid'),
|
||||||
|
}
|
||||||
|
|
||||||
config = make_config()
|
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
32
tests/mocks.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user