From 6e8f4ffc77a49a0fc20a7dc208f843af9c9bb808 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 29 Nov 2017 22:17:06 -0800 Subject: [PATCH] test_signup: Expand tests for invalid confirmation links. We'll need the expanded test coverage when we move check_prereg_key_and_redirect to zerver/views/registration.py to avoid test failures, and these are also tests we should really have anyway. --- zerver/tests/test_signup.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index 34bd3e8d26..8d22102299 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -860,14 +860,39 @@ so we didn't send them an invitation. We did send invitations to everyone else!" # make sure users can't take a valid confirmation key from another # pathway and use it with the invitation url route - # Mainly a test of get_object_from_key, rather than of the invitation pathway def test_confirmation_key_of_wrong_type(self) -> None: user = self.example_user('hamlet') - registration_key = create_confirmation_link(user, 'host', Confirmation.USER_REGISTRATION).split('/')[-1] + url = create_confirmation_link(user, 'host', Confirmation.USER_REGISTRATION) + registration_key = url.split('/')[-1] + + # Mainly a test of get_object_from_key, rather than of the invitation pathway with self.assertRaises(ConfirmationKeyException) as cm: get_object_from_key(registration_key, Confirmation.INVITATION) self.assertEqual(cm.exception.error_type, ConfirmationKeyException.DOES_NOT_EXIST) + # Verify that using the wrong type doesn't work in the main confirm code path + email_change_url = create_confirmation_link(user, 'host', Confirmation.EMAIL_CHANGE) + email_change_key = email_change_url.split('/')[-1] + url = '/accounts/do_confirm/' + email_change_key + result = self.client_get(url) + self.assert_in_success_response(["Whoops. We couldn't find your " + "confirmation link in the system."], result) + + def test_confirmation_expired(self) -> None: + user = self.example_user('hamlet') + url = create_confirmation_link(user, 'host', Confirmation.USER_REGISTRATION) + registration_key = url.split('/')[-1] + + conf = Confirmation.objects.filter(confirmation_key=registration_key).first() + conf.date_sent -= datetime.timedelta(weeks=3) + conf.save() + + target_url = '/' + url.split('/', 3)[3] + print(target_url) + result = self.client_get(target_url) + self.assert_in_success_response(["Whoops. The confirmation link has expired " + "or been deactivated."], result) + class InvitationsTestCase(InviteUserBase): def test_successful_get_open_invitations(self) -> None: """