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
=== modified file 'ubuntu_sso/qt/controllers.py'
--- ubuntu_sso/qt/controllers.py 2011-08-16 20:34:28 +0000
+++ ubuntu_sso/qt/controllers.py 2011-08-18 15:41:42 +0000
@@ -79,6 +79,28 @@
79# disabled warnings about TODO comments79# disabled warnings about TODO comments
8080
8181
82# Based on the gtk implementation, but added error_message
83# support, and rewritten.
84def _build_general_error_message(errordict):
85 """Concatenate __all__ and message from the errordict."""
86 result = None
87 msg1 = errordict.get('__all__')
88 msg2 = errordict.get('message')
89 if msg2 is None:
90 # See the errordict in LP: 828417
91 msg2 = errordict.get('error_message')
92 if msg1 is not None and msg2 is not None:
93 result = '\n'.join((msg1, msg2))
94 elif msg1 is not None:
95 result = msg1
96 elif msg2 is not None:
97 result = msg2
98 else:
99 # I give up.
100 result = "Error: %r" % errordict
101 return result
102
103
82class BackendController(object):104class BackendController(object):
83 """Represent a controller that talks with the sso self.backend."""105 """Represent a controller that talks with the sso self.backend."""
84106
@@ -222,7 +244,7 @@
222 # let the user know244 # let the user know
223 logger.error('Got error when login %s, error: %s',245 logger.error('Got error when login %s, error: %s',
224 self.view.wizard().app_name, error)246 self.view.wizard().app_name, error)
225 self.message_box.critical(error['message'])247 self.message_box.critical(_build_general_error_message(error))
226248
227 def on_logged_in(self, app_name, result):249 def on_logged_in(self, app_name, result):
228 """We managed to log in."""250 """We managed to log in."""
@@ -405,8 +427,7 @@
405 logger.debug('SetUpAccountController.on_user_registration_error')427 logger.debug('SetUpAccountController.on_user_registration_error')
406 # errors are returned as a dict with the data we want to show.428 # errors are returned as a dict with the data we want to show.
407 self._refresh_captcha()429 self._refresh_captcha()
408 errors = [v for _, v in sorted(error.iteritems())]430 self.message_box.critical(_build_general_error_message(error))
409 self.message_box.critical('\n'.join(errors))
410431
411 def on_user_registered(self, app_name, result):432 def on_user_registered(self, app_name, result):
412 """Execute when the user did register."""433 """Execute when the user did register."""
@@ -590,7 +611,7 @@
590 """Signal thrown when there's a problem validating the email."""611 """Signal thrown when there's a problem validating the email."""
591 msg = error.get('email_token')612 msg = error.get('email_token')
592 if msg is None:613 if msg is None:
593 msg = '\n'.join([v for _, v in sorted(error.iteritems())])614 msg = _build_general_error_message(error)
594 self.message_box.critical(msg)615 self.message_box.critical(msg)
595616
596 #pylint: disable=C0103617 #pylint: disable=C0103
@@ -720,8 +741,11 @@
720class ResetPasswordController(BackendController):741class ResetPasswordController(BackendController):
721 """Controller used to deal with reseintg the password."""742 """Controller used to deal with reseintg the password."""
722743
723 def __init__(self, title='', subtitle=''):744 def __init__(self, title='', subtitle='', message_box=None):
724 """Create a new instance."""745 """Create a new instance."""
746 if message_box is None:
747 message_box = QMessageBox
748 self.message_box = message_box
725 super(ResetPasswordController, self).__init__(title, subtitle)749 super(ResetPasswordController, self).__init__(title, subtitle)
726750
727 def _set_translated_strings(self):751 def _set_translated_strings(self):
@@ -777,6 +801,9 @@
777801
778 def on_password_change_error(self, app_name, error):802 def on_password_change_error(self, app_name, error):
779 """Let the user know that there was an error."""803 """Let the user know that there was an error."""
804 logger.error('Got error changing password for %s, error: %s',
805 self.view.wizard().app_name, error)
806 self.message_box.critical(_build_general_error_message(error))
780807
781 def set_new_password(self):808 def set_new_password(self):
782 """Request a new password to be set."""809 """Request a new password to be set."""
783810
=== modified file 'ubuntu_sso/qt/tests/test_windows.py'
--- ubuntu_sso/qt/tests/test_windows.py 2011-08-16 20:34:28 +0000
+++ ubuntu_sso/qt/tests/test_windows.py 2011-08-18 15:41:42 +0000
@@ -201,12 +201,24 @@
201 self._text = text201 self._text = text
202202
203203
204class FakeCurrentUserPage(object):204class FakePage(object):
205
206 """A fake wizard page."""
207
208 app_name = "APP"
209
210 def wizard(self):
211 """Fake wizard."""
212 return self
213
214
215class FakeCurrentUserPage(FakePage):
205216
206 """Fake CurrentuserPage."""217 """Fake CurrentuserPage."""
207218
208 def __init__(self, *args):219 def __init__(self, *args):
209 """Initialize."""220 """Initialize."""
221 super(FakeCurrentUserPage, self).__init__(*args)
210 self.ui = self222 self.ui = self
211 self.ui.email_edit = FakeLineEdit()223 self.ui.email_edit = FakeLineEdit()
212 self.ui.password_edit = FakeLineEdit()224 self.ui.password_edit = FakeLineEdit()
@@ -250,6 +262,101 @@
250 self.controller.setupUi(self.view)262 self.controller.setupUi(self.view)
251263
252264
265class CurrentUserControllerErrorTestCase(TestCase):
266
267 """Tests for CurrentUserController's error handler."""
268
269 on_error_method_name = "on_login_error"
270 controller_class = CurrentUserController
271
272 def setUp(self):
273 """Setup test."""
274 super(CurrentUserControllerErrorTestCase, self).setUp()
275 self.message_box = FakeMessageBox()
276 self.controller = self.controller_class(
277 message_box=self.message_box)
278 self.controller.view = FakePage()
279 self._called = False
280 self.on_error_method = getattr(
281 self.controller, self.on_error_method_name)
282
283 def test_error_message_key(self):
284 """Test that on_login_error reacts to errors with "error_message"."""
285 self.on_error_method({"error_message": "WORRY!"})
286 self.assertEqual(self.message_box.critical_args, (('WORRY!',), {}))
287
288 def test_message_key(self):
289 """Test that on_login_error reacts to errors with "message"."""
290 self.on_error_method({"message": "WORRY!"})
291 self.assertEqual(self.message_box.critical_args, (('WORRY!',), {}))
292
293 def test_broken_error(self):
294 """Test that on_login_error reacts to broken errors."""
295 self.on_error_method({"boo!": "WORRY!"})
296 self.assertEqual(self.message_box.critical_args,
297 (("Error: {'boo!': 'WORRY!'}",), {}))
298
299 def test_all_and_message(self):
300 """Test that on_login_error reacts to broken errors."""
301 self.on_error_method(
302 {"message": "WORRY!", "__all__": "MORE!"})
303 self.assertEqual(self.message_box.critical_args,
304 (('MORE!\nWORRY!',), {}))
305
306 def test_all_and_error_message(self):
307 """Test that on_login_error reacts to broken errors."""
308 self.on_error_method(
309 {"error_message": "WORRY!", "__all__": "MORE!"})
310 self.assertEqual(self.message_box.critical_args,
311 (('MORE!\nWORRY!',), {}))
312
313 def test_only_all(self):
314 """Test that on_login_error reacts to broken errors."""
315 self.on_error_method(
316 {"__all__": "MORE!"})
317 self.assertEqual(self.message_box.critical_args,
318 (('MORE!',), {}))
319
320
321class EmailVerificationControllerErrorTestCase(
322 CurrentUserControllerErrorTestCase):
323
324 """Tests for EmailVerificationController's error handler."""
325
326 on_error_method_name = "on_email_validation_error"
327 controller_class = EmailVerificationController
328
329 def setUp(self):
330 """Setup test."""
331 super(EmailVerificationControllerErrorTestCase, self).setUp()
332 # This error handler takes one extra argument.
333 self.on_error_method = lambda error: getattr(
334 self.controller, self.on_error_method_name)('APP', error)
335
336
337class SetUpAccountControllerErrorTestCase(
338 EmailVerificationControllerErrorTestCase):
339
340 """Tests for SetUpAccountController's error handler."""
341
342 on_error_method_name = "on_user_registration_error"
343 controller_class = SetUpAccountController
344
345 def setUp(self):
346 """Setup test."""
347 super(SetUpAccountControllerErrorTestCase, self).setUp()
348 self.patch(self.controller, "_refresh_captcha", lambda *args: None)
349
350
351class ResetPasswordControllerErrorTestCase(
352 EmailVerificationControllerErrorTestCase):
353
354 """Tests for ResetPasswordController's error handler."""
355
356 on_error_method_name = "on_password_change_error"
357 controller_class = ResetPasswordController
358
359
253class CurrentUserControllerValidationTest(TestCase):360class CurrentUserControllerValidationTest(TestCase):
254361
255 """Tests for CurrentUserController, but without Mocker."""362 """Tests for CurrentUserController, but without Mocker."""
@@ -613,9 +720,7 @@
613 'unknownfield': "Error in unknown",720 'unknownfield': "Error in unknown",
614 })721 })
615 self.assertEqual(self.message_box.critical_args, ((722 self.assertEqual(self.message_box.critical_args, ((
616 "Error in All\nError in email\n"723 "Error in All", ), {}))
617 "Error in password\nError in unknown",
618 ), {}))
619724
620725
621class TosControllerTestCase(MockerTestCase):726class TosControllerTestCase(MockerTestCase):
@@ -725,8 +830,8 @@
725 error = dict(errtype='BadTokenError')830 error = dict(errtype='BadTokenError')
726 app_name = 'app_name'831 app_name = 'app_name'
727 self.controller.on_email_validation_error(app_name, error)832 self.controller.on_email_validation_error(app_name, error)
728 self.assertEqual(self.message_box.critical_args, ((833 self.assertEqual(self.message_box.critical_args,
729 'BadTokenError',), {}))834 (("Error: {'errtype': 'BadTokenError'}",), {}))
730835
731836
732class ErrorControllerTestCase(MockerTestCase):837class ErrorControllerTestCase(MockerTestCase):

Subscribers

People subscribed via source and target branches