def test_finalize_verification(self, mock_login): placeholder = urandom(4).hex() request = HttpRequest() user = User.objects.create_user( email=f"email_{placeholder}", is_active=False, username=f"username_{placeholder}", ) user.save() self.assertFalse(verification.finalize_verification(request, None)) self.assertFalse(verification.finalize_verification(request, "")) self.assertFalse( verification.finalize_verification(request, f"vc_{placeholder}")) auth = AuthProfile(user=user, verification_code=f"vc_{placeholder}") auth.save() self.assertTrue( verification.finalize_verification(request, f"vc_{placeholder}")) user.refresh_from_db() auth.refresh_from_db() self.assertTrue(user.is_active) self.assertTrue(auth.verified_at) mock_login.assert_called_once_with(request, user)
def test_invite_single(self, mock_send_email): atm = utcnow() request = MagicMock() request.site = Site.objects.first() user_degenerate = self.create_user() user_not_verified = self.create_user() user_verified = self.create_user(verified=True) ap_degenerated = AuthProfile(user=user_degenerate, site=request.site) ap_degenerated.save() start_verification(request, user_not_verified) start_verification(request, user_verified) tasks.invite_single_user(user_degenerate.email) self.assertFalse(mock_send_email.called) ap_degenerated.refresh_from_db() self.assertIsNone(ap_degenerated.verified_at) self.assertIsNone(ap_degenerated.notified_at) self.assertFalse(ap_degenerated.verification_code) mock_send_email.reset_mock() tasks.invite_single_user(user_not_verified.email) ap: AuthProfile = user_not_verified.auth_profile mock_send_email.assert_called_once_with( context={"link": ap.link, "service": "Sidorov.dev"}, email_to=user_not_verified.email, mail_template_name="invitation", subject=f"Registration at Sidorov.dev", ) ap.refresh_from_db() self.assertIsNone(ap.verified_at) self.assertTrue(near(atm, ap.notified_at, 1)) self.assertTrue(ap.link) mock_send_email.reset_mock() tasks.invite_single_user(user_verified.email) ap: AuthProfile = user_verified.auth_profile mock_send_email.assert_called_once_with( context={"link": ap.link, "service": "Sidorov.dev"}, email_to=user_verified.email, mail_template_name="invitation", subject=f"Registration at Sidorov.dev", ) ap.refresh_from_db() self.assertIsNone(ap.verified_at) self.assertTrue(near(atm, ap.notified_at, 1)) self.assertTrue(ap.link)