From a462e6f2c4b7311a57f9a92a564fb2c25b7fa16f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Tue, 18 Feb 2014 18:37:01 +0100 Subject: [PATCH] Store userid to simplepush mapping (in a memory db for now). Fix 971993 --- development.ini | 1 + pants/__init__.py | 8 ++++++-- pants/views.py | 5 +++++ tests/test_functional.py | 10 ++++++++-- tests/tests.ini | 1 + 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/development.ini b/development.ini index 2af1596..5837313 100644 --- a/development.ini +++ b/development.ini @@ -1,5 +1,6 @@ [app:main] token-secret = this is not a secret +storage = pants.storage.memory.MemoryStorage use = egg:pants diff --git a/pants/__init__.py b/pants/__init__.py index 33038a0..30ebd99 100644 --- a/pants/__init__.py +++ b/pants/__init__.py @@ -18,8 +18,12 @@ def main(global_config, **settings): token_manager = TokenManager(secret=settings['token-secret']) config.registry.token_manager = token_manager - def add_db_to_request(event): + backend_class = config.maybe_dotted(settings['storage']) + config.registry.storage = storage = backend_class(config) + + def attach_services_to_request(event): event.request.token_manager = token_manager - config.add_subscriber(add_db_to_request, NewRequest) + event.request.storage = storage + config.add_subscriber(attach_services_to_request, NewRequest) return config.make_wsgi_app() diff --git a/pants/views.py b/pants/views.py index f1aef25..9867354 100644 --- a/pants/views.py +++ b/pants/views.py @@ -33,6 +33,11 @@ def generate_callurl(request): Generate a callurl based on user ID. """ userid = authenticated_userid(request) + + # We need to try/except here in case the db fails. + request.storage.store_simplepush_url( + userid, request.validated['simple-push-url']) + token = request.token_manager.make_token({"userid": userid}) call_url = '{root}/call/{token}'.format(root=request.application_url, token=token) diff --git a/tests/test_functional.py b/tests/test_functional.py index c8f433a..5fa979e 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -14,6 +14,7 @@ class FunctionalTest(unittest.TestCase): def setUp(self): self.app = webtest.TestApp("config:tests.ini", relative_to=__HERE__) self.token_manager = self.app.app.registry.token_manager + self.storage = self.app.app.registry.storage self.simple_push_url = 'https://token.services.mozilla.org' # POST /call-url @@ -62,8 +63,13 @@ def test_token_creation_requires_authn(self): auth_header = resp.headers.get('WWW-Authenticate') self.assertEquals(auth_header, 'Hawk', auth_header) - def test_token_creation_validates_authn(self): - pass + @sign_requests(user='n1k0') + def test_token_creation_stores_push_url(self): + self.app.post_json('/call-url', { + 'simple-push-url': self.simple_push_url + }, status=200) + self.assertIn(self.simple_push_url, + self.storage.get_simplepush_urls('n1k0')) # GET /call/ def test_invalid_callurl_token_returns_400(self): diff --git a/tests/tests.ini b/tests/tests.ini index d4a7888..b24932f 100644 --- a/tests/tests.ini +++ b/tests/tests.ini @@ -1,5 +1,6 @@ [app:main] token-secret = this is not a secret +storage = pants.storage.memory.MemoryStorage use = egg:pants