Merge lp:~maxiberta/canonical-identity-provider/fix-429-crash into lp:canonical-identity-provider

Proposed by Maximiliano Bertacchini on 2018-12-19
Status: Merged
Approved by: Maximiliano Bertacchini on 2019-01-07
Approved revision: 1674
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: lp:~maxiberta/canonical-identity-provider/fix-429-crash
Merge into: lp:canonical-identity-provider
Diff against target: 78 lines (+26/-1)
2 files modified
src/webui/tests/test_views_registration.py (+16/-0)
src/webui/views/registration.py (+10/-1)
To merge this branch: bzr merge lp:~maxiberta/canonical-identity-provider/fix-429-crash
Reviewer Review Type Date Requested Status
Adam Collard 2018-12-19 Approve on 2018-12-21
Review via email: mp+361148@code.launchpad.net

Commit message

Show a nice error message at user registration and reset password views when the API returns 429 - too many requests.

To post a comment you must log in.
1674. By Maximiliano Bertacchini on 2018-12-20

Show the registration throttling error in the messages area.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/webui/tests/test_views_registration.py'
2--- src/webui/tests/test_views_registration.py 2018-10-25 17:56:27 +0000
3+++ src/webui/tests/test_views_registration.py 2018-12-20 18:24:52 +0000
4@@ -38,6 +38,7 @@
5 )
6 from webui.views.registration import (
7 ACCOUNT_CREATED,
8+ ERROR_TOO_MANY_REQUESTS,
9 VERIFY_EMAIL_SENT,
10 WEB_CREATION_SOURCE,
11 new_account,
12@@ -332,6 +333,13 @@
13 self.assert_form_displayed(response, email=VERIFY_EMAIL_MESSAGE)
14 self.assert_stat_calls(['error.email'])
15
16+ def test_post_too_many_requests(self):
17+ exc = api_errors.TooManyRequests(Mock())
18+ self.mock_api_register.side_effect = exc
19+ response = self.post(**self.TESTDATA)
20+ self.assertContains(response, ERROR_TOO_MANY_REQUESTS)
21+ self.assert_stat_calls(['error.too_many_requests'])
22+
23
24 class RegisterTimelineTestCase(
25 SSOBaseTestCase, RegisterTestMixin, TimelineActionMixin):
26@@ -565,6 +573,14 @@
27 self.assertEqual(response.status_code, 200)
28 self.assert_stat_calls(['error.too_many_tokens'])
29
30+ def test_post_too_many_requests(self):
31+ exc = api_errors.TooManyRequests(Mock())
32+ self.mock_api_request_password_reset.side_effect = exc
33+ response = self.post(data=self.data)
34+ self.assertEqual(response.status_code, 200)
35+ self.assert_form_displayed(response, __all__=ERROR_TOO_MANY_REQUESTS)
36+ self.assert_stat_calls(['error.too_many_requests'])
37+
38 def test_reset_password(self):
39 response = self.post(data=self.data)
40
41
42=== modified file 'src/webui/views/registration.py'
43--- src/webui/views/registration.py 2018-10-16 19:32:57 +0000
44+++ src/webui/views/registration.py 2018-12-20 18:24:52 +0000
45@@ -74,9 +74,10 @@
46 'to reset the corresponding account\'s password.')
47 ERROR_PASSWORD_RESET_GENERIC = _(
48 'It is not possible to reset this account\'s password.')
49-ERROR_PASSWORD_RESET_TOO_MANY_TOKENS = (
50+ERROR_PASSWORD_RESET_TOO_MANY_TOKENS = _(
51 'Too many active password reset requests. An email has already been sent '
52 'to your account; please verify your inbox.')
53+ERROR_TOO_MANY_REQUESTS = _('Too many requests. Please try again later.')
54
55 WEB_CREATION_SOURCE = 'web-flow'
56
57@@ -124,6 +125,10 @@
58 collect_stats('error.email')
59 form._errors['email'] = [VERIFY_EMAIL_MESSAGE]
60
61+ except api_errors.TooManyRequests as e:
62+ collect_stats('error.too_many_requests')
63+ messages.error(request, ERROR_TOO_MANY_REQUESTS)
64+
65 except Exception as e:
66 return HttpResponseServerError("exception: " + str(e))
67 else:
68@@ -227,6 +232,10 @@
69 ERROR_PASSWORD_RESET_TOO_MANY_TOKENS]
70 collect_stats('error.too_many_tokens')
71
72+ except api_errors.TooManyRequests as e:
73+ form.errors['__all__'] = [ERROR_TOO_MANY_REQUESTS]
74+ collect_stats('error.too_many_requests')
75+
76 else:
77 msgs = {
78 'email_to': email,