diff --git a/atst/routes/requests/jedi_request_flow.py b/atst/routes/requests/jedi_request_flow.py index 1a3a7163..2b8bc312 100644 --- a/atst/routes/requests/jedi_request_flow.py +++ b/atst/routes/requests/jedi_request_flow.py @@ -11,13 +11,15 @@ class JEDIRequestFlow(object): def __init__( self, current_step, + current_user=None, request=None, post_data=None, request_id=None, - current_user=None, existing_request=None, ): self.current_step = current_step + + self.current_user = current_user self.request = request self.post_data = post_data @@ -26,7 +28,6 @@ class JEDIRequestFlow(object): self.request_id = request_id self.form = self._form() - self.current_user = current_user self.existing_request = existing_request def _form(self): @@ -59,6 +60,18 @@ class JEDIRequestFlow(object): def form_class(self): return self.current_screen["form"] + # maps user data to fields in OrgForm; this should be moved into the + # request initialization process when we have a request schema + def map_current_user(self): + if self.current_user.id == self.request.creator: + return { + "fname_request": self.current_user.first_name, + "lname_request": self.current_user.last_name, + "email_request": self.current_user.email + } + else: + return {} + @property def current_step_data(self): data = {} @@ -69,6 +82,9 @@ class JEDIRequestFlow(object): if self.request: if self.form_section == "review_submit": data = self.request.body + if self.form_section == "information_about_you": + form_data = self.request.body.get(self.form_section, {}) + data = { **form_data, **self.map_current_user() } else: data = self.request.body.get(self.form_section, {}) diff --git a/atst/routes/requests/requests_form.py b/atst/routes/requests/requests_form.py index d384abdf..1c6a7c26 100644 --- a/atst/routes/requests/requests_form.py +++ b/atst/routes/requests/requests_form.py @@ -31,7 +31,7 @@ def requests_form_update(screen=1, request_id=None): _check_can_view_request(request_id) request = Requests.get(request_id) if request_id is not None else None - jedi_flow = JEDIRequestFlow(screen, request, request_id=request_id) + jedi_flow = JEDIRequestFlow(screen, request=request, request_id=request_id, current_user=g.current_user) return render_template( "requests/screen-%d.html" % int(screen), diff --git a/tests/routes/test_request_new.py b/tests/routes/test_request_new.py index e31aae79..565f7886 100644 --- a/tests/routes/test_request_new.py +++ b/tests/routes/test_request_new.py @@ -66,3 +66,28 @@ def test_nonexistent_request(client, user_session): response = client.get("/requests/new/1/foo", follow_redirects=True) assert response.status_code == 404 + + +def test_creator_info_is_autopopulated(monkeypatch, client, user_session): + user = UserFactory.create() + user_session(user) + request = RequestFactory.create(creator=user.id, body={"information_about_you": {}}) + + response = client.get("/requests/new/2/{}".format(request.id)) + body = response.data.decode() + assert 'value="{}"'.format(user.first_name) in body + assert 'value="{}"'.format(user.last_name) in body + assert 'value="{}"'.format(user.email) in body + + +def test_non_creator_info_is_not_autopopulated(monkeypatch, client, user_session): + user = UserFactory.create() + creator = UserFactory.create() + user_session(user) + request = RequestFactory.create(creator=creator.id, body={"information_about_you": {}}) + + response = client.get("/requests/new/2/{}".format(request.id)) + body = response.data.decode() + assert not user.first_name in body + assert not user.last_name in body + assert not user.email in body