Merge lp:~ralsina/ubuntu-sso-client/error_message_key into lp:ubuntu-sso-client

Proposed by Roberto Alsina
Status: Merged
Approved by: Natalia Bidart
Approved revision: 765
Merged at revision: 760
Proposed branch: lp:~ralsina/ubuntu-sso-client/error_message_key
Merge into: lp:ubuntu-sso-client
Diff against target: 237 lines (+143/-11)
2 files modified
ubuntu_sso/qt/controllers.py (+32/-5)
ubuntu_sso/qt/tests/test_windows.py (+111/-6)
To merge this branch: bzr merge lp:~ralsina/ubuntu-sso-client/error_message_key
Reviewer Review Type Date Requested Status
Natalia Bidart (community) Approve
Review via email: mp+71966@code.launchpad.net

Commit message

Added a function to convert errordicts to strings, used that function to make on_login_error work better.

Description of the change

Added a function to convert errordicts to strings:

* Based on the one in the gtk implementation
* Made more robust (we are getting errordicts with "error_message" and no "message" and no "__all__")
* Made it more readable (IMHO)
* Done with TDD ;-)

I suspect that this version is an improvement over the gtk one and both should be moved to a "neutral" place.

Used that function to make on_login_error work better.

To post a comment you must log in.
762. By Roberto Alsina

style fixes

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

The branch looks great!

Only a couple of tests are missing:
 - when both msg1 and msg2 are not None (for two cases: when msg2 is 'message' and when is 'error_message').
 - when msg1 is not None (ie __all__ is present)

Can you please add those test cases? Thanks!

review: Needs Fixing
763. By Roberto Alsina

added suggested tests

Revision history for this message
Roberto Alsina (ralsina) wrote :

> The branch looks great!
>
> Only a couple of tests are missing:
> - when both msg1 and msg2 are not None (for two cases: when msg2 is 'message'
> and when is 'error_message').
> - when msg1 is not None (ie __all__ is present)
>
> Can you please add those test cases? Thanks!

Added!

764. By Roberto Alsina

use the new function everywhere, test it everywhere

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Can you please fix these lint issues?

ubuntu_sso/qt/controllers.py:
    614: [W0311] Bad indentation. Found 16 spaces, expected 12

ubuntu_sso/qt/tests/test_windows.py:
    219: [W0231, FakeCurrentUserPage.__init__] __init__ method from base class 'FakePage' is not called

review: Needs Fixing
765. By Roberto Alsina

lint

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_sso/qt/controllers.py'
2--- ubuntu_sso/qt/controllers.py 2011-08-16 20:34:28 +0000
3+++ ubuntu_sso/qt/controllers.py 2011-08-18 15:41:42 +0000
4@@ -79,6 +79,28 @@
5 # disabled warnings about TODO comments
6
7
8+# Based on the gtk implementation, but added error_message
9+# support, and rewritten.
10+def _build_general_error_message(errordict):
11+ """Concatenate __all__ and message from the errordict."""
12+ result = None
13+ msg1 = errordict.get('__all__')
14+ msg2 = errordict.get('message')
15+ if msg2 is None:
16+ # See the errordict in LP: 828417
17+ msg2 = errordict.get('error_message')
18+ if msg1 is not None and msg2 is not None:
19+ result = '\n'.join((msg1, msg2))
20+ elif msg1 is not None:
21+ result = msg1
22+ elif msg2 is not None:
23+ result = msg2
24+ else:
25+ # I give up.
26+ result = "Error: %r" % errordict
27+ return result
28+
29+
30 class BackendController(object):
31 """Represent a controller that talks with the sso self.backend."""
32
33@@ -222,7 +244,7 @@
34 # let the user know
35 logger.error('Got error when login %s, error: %s',
36 self.view.wizard().app_name, error)
37- self.message_box.critical(error['message'])
38+ self.message_box.critical(_build_general_error_message(error))
39
40 def on_logged_in(self, app_name, result):
41 """We managed to log in."""
42@@ -405,8 +427,7 @@
43 logger.debug('SetUpAccountController.on_user_registration_error')
44 # errors are returned as a dict with the data we want to show.
45 self._refresh_captcha()
46- errors = [v for _, v in sorted(error.iteritems())]
47- self.message_box.critical('\n'.join(errors))
48+ self.message_box.critical(_build_general_error_message(error))
49
50 def on_user_registered(self, app_name, result):
51 """Execute when the user did register."""
52@@ -590,7 +611,7 @@
53 """Signal thrown when there's a problem validating the email."""
54 msg = error.get('email_token')
55 if msg is None:
56- msg = '\n'.join([v for _, v in sorted(error.iteritems())])
57+ msg = _build_general_error_message(error)
58 self.message_box.critical(msg)
59
60 #pylint: disable=C0103
61@@ -720,8 +741,11 @@
62 class ResetPasswordController(BackendController):
63 """Controller used to deal with reseintg the password."""
64
65- def __init__(self, title='', subtitle=''):
66+ def __init__(self, title='', subtitle='', message_box=None):
67 """Create a new instance."""
68+ if message_box is None:
69+ message_box = QMessageBox
70+ self.message_box = message_box
71 super(ResetPasswordController, self).__init__(title, subtitle)
72
73 def _set_translated_strings(self):
74@@ -777,6 +801,9 @@
75
76 def on_password_change_error(self, app_name, error):
77 """Let the user know that there was an error."""
78+ logger.error('Got error changing password for %s, error: %s',
79+ self.view.wizard().app_name, error)
80+ self.message_box.critical(_build_general_error_message(error))
81
82 def set_new_password(self):
83 """Request a new password to be set."""
84
85=== modified file 'ubuntu_sso/qt/tests/test_windows.py'
86--- ubuntu_sso/qt/tests/test_windows.py 2011-08-16 20:34:28 +0000
87+++ ubuntu_sso/qt/tests/test_windows.py 2011-08-18 15:41:42 +0000
88@@ -201,12 +201,24 @@
89 self._text = text
90
91
92-class FakeCurrentUserPage(object):
93+class FakePage(object):
94+
95+ """A fake wizard page."""
96+
97+ app_name = "APP"
98+
99+ def wizard(self):
100+ """Fake wizard."""
101+ return self
102+
103+
104+class FakeCurrentUserPage(FakePage):
105
106 """Fake CurrentuserPage."""
107
108 def __init__(self, *args):
109 """Initialize."""
110+ super(FakeCurrentUserPage, self).__init__(*args)
111 self.ui = self
112 self.ui.email_edit = FakeLineEdit()
113 self.ui.password_edit = FakeLineEdit()
114@@ -250,6 +262,101 @@
115 self.controller.setupUi(self.view)
116
117
118+class CurrentUserControllerErrorTestCase(TestCase):
119+
120+ """Tests for CurrentUserController's error handler."""
121+
122+ on_error_method_name = "on_login_error"
123+ controller_class = CurrentUserController
124+
125+ def setUp(self):
126+ """Setup test."""
127+ super(CurrentUserControllerErrorTestCase, self).setUp()
128+ self.message_box = FakeMessageBox()
129+ self.controller = self.controller_class(
130+ message_box=self.message_box)
131+ self.controller.view = FakePage()
132+ self._called = False
133+ self.on_error_method = getattr(
134+ self.controller, self.on_error_method_name)
135+
136+ def test_error_message_key(self):
137+ """Test that on_login_error reacts to errors with "error_message"."""
138+ self.on_error_method({"error_message": "WORRY!"})
139+ self.assertEqual(self.message_box.critical_args, (('WORRY!',), {}))
140+
141+ def test_message_key(self):
142+ """Test that on_login_error reacts to errors with "message"."""
143+ self.on_error_method({"message": "WORRY!"})
144+ self.assertEqual(self.message_box.critical_args, (('WORRY!',), {}))
145+
146+ def test_broken_error(self):
147+ """Test that on_login_error reacts to broken errors."""
148+ self.on_error_method({"boo!": "WORRY!"})
149+ self.assertEqual(self.message_box.critical_args,
150+ (("Error: {'boo!': 'WORRY!'}",), {}))
151+
152+ def test_all_and_message(self):
153+ """Test that on_login_error reacts to broken errors."""
154+ self.on_error_method(
155+ {"message": "WORRY!", "__all__": "MORE!"})
156+ self.assertEqual(self.message_box.critical_args,
157+ (('MORE!\nWORRY!',), {}))
158+
159+ def test_all_and_error_message(self):
160+ """Test that on_login_error reacts to broken errors."""
161+ self.on_error_method(
162+ {"error_message": "WORRY!", "__all__": "MORE!"})
163+ self.assertEqual(self.message_box.critical_args,
164+ (('MORE!\nWORRY!',), {}))
165+
166+ def test_only_all(self):
167+ """Test that on_login_error reacts to broken errors."""
168+ self.on_error_method(
169+ {"__all__": "MORE!"})
170+ self.assertEqual(self.message_box.critical_args,
171+ (('MORE!',), {}))
172+
173+
174+class EmailVerificationControllerErrorTestCase(
175+ CurrentUserControllerErrorTestCase):
176+
177+ """Tests for EmailVerificationController's error handler."""
178+
179+ on_error_method_name = "on_email_validation_error"
180+ controller_class = EmailVerificationController
181+
182+ def setUp(self):
183+ """Setup test."""
184+ super(EmailVerificationControllerErrorTestCase, self).setUp()
185+ # This error handler takes one extra argument.
186+ self.on_error_method = lambda error: getattr(
187+ self.controller, self.on_error_method_name)('APP', error)
188+
189+
190+class SetUpAccountControllerErrorTestCase(
191+ EmailVerificationControllerErrorTestCase):
192+
193+ """Tests for SetUpAccountController's error handler."""
194+
195+ on_error_method_name = "on_user_registration_error"
196+ controller_class = SetUpAccountController
197+
198+ def setUp(self):
199+ """Setup test."""
200+ super(SetUpAccountControllerErrorTestCase, self).setUp()
201+ self.patch(self.controller, "_refresh_captcha", lambda *args: None)
202+
203+
204+class ResetPasswordControllerErrorTestCase(
205+ EmailVerificationControllerErrorTestCase):
206+
207+ """Tests for ResetPasswordController's error handler."""
208+
209+ on_error_method_name = "on_password_change_error"
210+ controller_class = ResetPasswordController
211+
212+
213 class CurrentUserControllerValidationTest(TestCase):
214
215 """Tests for CurrentUserController, but without Mocker."""
216@@ -613,9 +720,7 @@
217 'unknownfield': "Error in unknown",
218 })
219 self.assertEqual(self.message_box.critical_args, ((
220- "Error in All\nError in email\n"
221- "Error in password\nError in unknown",
222- ), {}))
223+ "Error in All", ), {}))
224
225
226 class TosControllerTestCase(MockerTestCase):
227@@ -725,8 +830,8 @@
228 error = dict(errtype='BadTokenError')
229 app_name = 'app_name'
230 self.controller.on_email_validation_error(app_name, error)
231- self.assertEqual(self.message_box.critical_args, ((
232- 'BadTokenError',), {}))
233+ self.assertEqual(self.message_box.critical_args,
234+ (("Error: {'errtype': 'BadTokenError'}",), {}))
235
236
237 class ErrorControllerTestCase(MockerTestCase):

Subscribers

People subscribed via source and target branches