From e7fb19c8b0ce34407f1c4a5502193ce280fe2e5c Mon Sep 17 00:00:00 2001 From: Challa Venkata Raghava Reddy Date: Thu, 14 Feb 2019 18:29:23 +0530 Subject: [PATCH] invitations: Fix email validation errors for deactivated accounts. This provides a much clearer error message when trying to invite a user who has a deactivated account. Fixes part of #8144. --- zerver/lib/actions.py | 9 +++++---- zerver/tests/test_auth_backends.py | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 2b9f9eaf03..90b3db18c1 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -4754,9 +4754,10 @@ def validate_email_for_realm(target_realm: Realm, email: str) -> None: if existing_user_profile.is_mirror_dummy: raise AssertionError("Mirror dummy user is already active!") # Other users should not already exist at all. - raise ValidationError('%s already has an account' % (email,)) + raise ValidationError('%s already has an account' % (email,), code = _("Already has an account.")) elif not existing_user_profile.is_mirror_dummy: - raise ValidationError('The account for %s has been deactivated' % (email,)) + raise ValidationError('The account for %s has been deactivated' % (email,), + code = _("Account has been deactivated.")) def validate_email(user_profile: UserProfile, email: str) -> Tuple[Optional[str], Optional[str]]: try: @@ -4775,8 +4776,8 @@ def validate_email(user_profile: UserProfile, email: str) -> Tuple[Optional[str] try: validate_email_for_realm(user_profile.realm, email) - except ValidationError: - return None, _("Already has an account.") + except ValidationError as error: + return None, (error.code) return None, None diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index e09035f9ac..f7e0b2dd4e 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -3041,7 +3041,7 @@ class EmailValidatorTestCase(ZulipTestCase): cordelia.save() _, error = validate_email(inviter, cordelia.email) - self.assertEqual(error, 'Already has an account.') + self.assertEqual(error, 'Account has been deactivated.') _, error = validate_email(inviter, 'fred-is-fine@zulip.com') self.assertEqual(error, None)