Skip to content

Commit

Permalink
Application submit failure should not redirect to success page
Browse files Browse the repository at this point in the history
Shows an error flash message instead and remains on Check Your Answers
page.
  • Loading branch information
katstevens committed Jun 19, 2018
1 parent 2804cb5 commit 5beff3b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
19 changes: 11 additions & 8 deletions app/main/views/briefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,18 @@ def check_brief_response_answers(brief_id, brief_response_id):
section.inject_brief_questions_into_boolean_list_question(brief)

if request.method == 'POST':
data_api_client.submit_brief_response(
submit_response = data_api_client.submit_brief_response(
brief_response_id,
current_user.email_address
)
flash(APPLICATION_SUBMITTED_FIRST_MESSAGE)
# To trigger the analytics Virtual Page View
redirect_url = url_for('.application_submitted', brief_id=brief_id)
flash('{}?result=success'.format(redirect_url), 'track-page-view')
return redirect(redirect_url)
if submit_response.get('error') or submit_response['briefResponses'].get('status') == 'draft':
flash("There was a problem submitting your application.", 'error')
else:
flash(APPLICATION_SUBMITTED_FIRST_MESSAGE)
# To trigger the analytics Virtual Page View
redirect_url = url_for('.application_submitted', brief_id=brief_id)
flash('{}?result=success'.format(redirect_url), 'track-page-view')
return redirect(redirect_url)

return render_template(
"briefs/check_your_answers.html",
Expand All @@ -296,8 +299,8 @@ def application_submitted(brief_id):
if len(brief_response) == 0:
# No application
return redirect(url_for(".start_brief_response", brief_id=brief_id))
if 'essentialRequirementsMet' not in brief_response[0]:
# Legacy application
if 'essentialRequirementsMet' not in brief_response[0] or brief_response[0].get('status') == 'draft':
# Incomplete or Legacy application
return redirect(
url_for(".check_brief_response_answers", brief_id=brief_id, brief_response_id=brief_response[0]['id'])
)
Expand Down
48 changes: 46 additions & 2 deletions tests/app/main/test_briefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ def setup_method(self, method):
self.data_api_client.get_brief.return_value = self.brief
self.data_api_client.get_framework.return_value = self.framework
self.data_api_client.get_brief_response.return_value = self.brief_response()
self.data_api_client.submit_brief_response.return_value = self.brief_response(data={'status': 'submitted'})

self.login()

Expand Down Expand Up @@ -2010,17 +2011,46 @@ def test_will_show_not_eligible_response_if_supplier_is_not_eligible_for_brief(
assert res.status_code == 403
_render_not_eligible_for_brief_error_page.assert_called_once_with(self.brief['briefs'])

def test_error_message_shown_if_submit_fails(self):
self.set_framework_and_eligibility_for_api_client()
self.data_api_client.get_brief.return_value = self.brief
self.data_api_client.get_brief_response.return_value = self.brief_response()
self.data_api_client.find_brief_responses.return_value = {
'briefResponses': [self.brief_response(data={'essentialRequirementsMet': True})['briefResponses']]
}
self.data_api_client.is_supplier_eligible_for_brief.return_value = True
# The 'submit' action failed
self.data_api_client.submit_brief_response.return_value = {"error": "oh dear"}

res = self.client.post(
'/suppliers/opportunities/{brief_id}/responses/{brief_response_id}/application'.format(
brief_id=self.brief['briefs']['id'],
brief_response_id=self.brief_response()['briefResponses']['id']
),
data={}
)
assert res.status_code == 200 # No redirect
data = res.get_data(as_text=True)

# Error flash message should be shown
assert 'There was a problem submitting your application.' in data

def test_analytics_and_messages_applied_on_first_submission(self):
"""Go through submitting to edit_brief_response and the redirect to view_response_result. Assert messages."""
self.set_framework_and_eligibility_for_api_client()
self.data_api_client.get_brief.return_value = self.brief
self.data_api_client.get_brief_response.return_value = self.brief_response()
self.data_api_client.find_brief_responses.return_value = {
'briefResponses': [self.brief_response(data={'essentialRequirementsMet': True})['briefResponses']]
'briefResponses': [
self.brief_response(data={'essentialRequirementsMet': True, 'status': 'submitted'})['briefResponses']
]
}

self.data_api_client.is_supplier_eligible_for_brief.return_value = True

# The submit action succeeded
self.data_api_client.submit_brief_response.return_value = {'briefResponses': {'status': 'submitted'}}

res = self.client.post(
'/suppliers/opportunities/{brief_id}/responses/{brief_response_id}/application'.format(
brief_id=self.brief['briefs']['id'],
Expand All @@ -2040,7 +2070,7 @@ def test_analytics_and_messages_applied_on_first_submission(self):
# Assert we get the correct banner message (and only the correct one).
assert 'Your application has been submitted.' in data

def test_view_response_result_not_submitted_redirect_to_start_page(self):
def test_view_response_result_no_application_redirect_to_start_page(self):
self.set_framework_and_eligibility_for_api_client()
self.data_api_client.get_brief.return_value = self.brief
self.data_api_client.find_brief_responses.return_value = {"briefResponses": []}
Expand All @@ -2049,6 +2079,20 @@ def test_view_response_result_not_submitted_redirect_to_start_page(self):
assert res.status_code == 302
assert res.location == 'http://localhost/suppliers/opportunities/1234/responses/start'

def test_view_response_result_draft_application_redirect_to_check_your_answers_page(self):
self.set_framework_and_eligibility_for_api_client()
self.data_api_client.get_brief.return_value = self.brief
self.data_api_client.find_brief_responses.return_value = {
"briefResponses": [
{"id": 999, 'essentialRequirementsMet': True, 'status': 'draft'}
]
}

res = self.client.get('/suppliers/opportunities/1234/responses/result')

assert res.status_code == 302
assert res.location == 'http://localhost/suppliers/opportunities/1234/responses/999/application'

@mock.patch("app.main.views.briefs.is_supplier_eligible_for_brief")
def test_view_result_legacy_flow_redirects_to_check_your_answer(self, is_supplier_eligible_for_brief):
self.set_framework_and_eligibility_for_api_client()
Expand Down

0 comments on commit 5beff3b

Please sign in to comment.