From 8b1410aed593e3ba59215a145cffad3ce9d2909d Mon Sep 17 00:00:00 2001 From: richard-dds Date: Mon, 18 Jun 2018 11:14:27 -0400 Subject: [PATCH] Send new requests to requests-queue --- atst/api_client.py | 1 + atst/handlers/request_new.py | 18 ++++++++++++++++-- tests/handlers/test_request_new.py | 4 +++- tests/mocks.py | 12 ++++++++---- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/atst/api_client.py b/atst/api_client.py index c7e35fe7..1bc57f49 100644 --- a/atst/api_client.py +++ b/atst/api_client.py @@ -43,4 +43,5 @@ class ApiClient(object): if response.headers['Content-Type'] == 'application/json': json = loads(response.body) setattr(response, 'json', json) + setattr(response, 'ok', 200 <= response.code < 300) return response diff --git a/atst/handlers/request_new.py b/atst/handlers/request_new.py index 5b5f7f40..8d0f0e6b 100644 --- a/atst/handlers/request_new.py +++ b/atst/handlers/request_new.py @@ -45,13 +45,18 @@ class RequestNew(BaseHandler): self.requests_client = requests_client @tornado.web.authenticated + @tornado.gen.coroutine def post(self, screen = 1): self.check_xsrf_cookie() screen = int(screen) form = self.screens[ screen - 1 ]['form'](self.request.arguments) if form.validate(): - where = self.application.default_router.reverse_url('request_form', str(screen + 1)) - self.redirect(where) + response = yield self.create_or_update_request(form.data) + if response.ok: + where = self.application.default_router.reverse_url('request_form', str(screen + 1)) + self.redirect(where) + else: + self.set_status(response.code) else: self.show_form(screen, form) @@ -68,3 +73,12 @@ class RequestNew(BaseHandler): screens = self.screens, current = int(screen), next_screen = int(screen) + 1 ) + + @tornado.gen.coroutine + def create_or_update_request(self, form_data): + request_data = { + 'creator_id': self.current_user, + 'request': form_data + } + response = yield self.requests_client.post('/requests', json=request_data) + return response diff --git a/tests/handlers/test_request_new.py b/tests/handlers/test_request_new.py index ddaad13e..53ad9a25 100644 --- a/tests/handlers/test_request_new.py +++ b/tests/handlers/test_request_new.py @@ -1,9 +1,9 @@ import re import pytest - ERROR_CLASS = 'usa-input-error-message' + @pytest.mark.gen_test def test_submit_invalid_request_form(monkeypatch, http_client, base_url): monkeypatch.setattr('atst.handlers.request_new.RequestNew.get_current_user', lambda s: True) @@ -18,11 +18,13 @@ def test_submit_invalid_request_form(monkeypatch, http_client, base_url): assert response.effective_url == base_url + '/requests/new' assert re.search(ERROR_CLASS, response.body.decode()) + @pytest.mark.gen_test def test_submit_valid_request_form(monkeypatch, http_client, base_url): monkeypatch.setattr('atst.handlers.request_new.RequestNew.get_current_user', lambda s: True) monkeypatch.setattr('atst.handlers.request_new.RequestNew.check_xsrf_cookie', lambda s: True) monkeypatch.setattr('atst.forms.request.RequestForm.validate', lambda s: True) + # this just needs to send a known invalid form value response = yield http_client.fetch( base_url + "/requests/new", diff --git a/tests/mocks.py b/tests/mocks.py index e58e46d8..5561c5aa 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -1,8 +1,10 @@ import tornado.gen from tornado.httpclient import HTTPRequest, HTTPResponse +from atst.api_client import ApiClient -class MockApiClient(object): + +class MockApiClient(ApiClient): def __init__(self, service): self.service = service @@ -27,6 +29,8 @@ class MockApiClient(object): 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 + response = HTTPResponse( + request=HTTPRequest(path, verb), + code=200, + headers={'Content-Type': 'application-json'}) + return self.adapt_response(response)