def create_auth_profile(user: User) -> AuthProfile: auth = AuthProfile( user=user, verification_code=user.username, verified_at=utcnow(), ) auth.save() return auth
def create_auth_profile(user: User) -> AuthProfile: auth = AuthProfile( user=user, verification_code=user.username, verified_at=Delorean().datetime, ) auth.save() return auth
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)
def test_auth_profile(self): placeholder = urandom(4).hex() user = User.objects.create_user(username=placeholder, email=placeholder) auth = AuthProfile(user=user, verification_code=placeholder) auth.save() self.assertTrue(auth.pk) self.assertFalse(auth.is_verified) self.assertFalse(auth.link) auth.verified_at = delorean.parse("2020-01-01").datetime auth.save() self.assertTrue(auth.is_verified) self.assertFalse(auth.link) auth.verified_at = utcnow() + timedelta(minutes=1) auth.save() self.assertFalse(auth.is_verified) self.assertFalse(auth.link) auth.site = Site.objects.first() auth.save() self.assertFalse(auth.is_verified) self.assertTrue(auth.link) auth.verified_at = utcnow() - timedelta(seconds=1) auth.save() self.assertTrue(auth.is_verified) self.assertTrue(auth.link) self.assertEqual( str(auth), f"AuthProfile #{auth.pk} for {placeholder!r}", )
def setup_auth_profile(user: User, site: Site) -> AuthProfile: code = urandom(16).hex() # FIXME: magic; not secure auth = AuthProfile(user=user, site=site, verification_code=code) auth.save() return auth
def setup_auth_profile(user: User, site: Site) -> AuthProfile: code = token_urlsafe(consts.LEN_AUTH_CODE) auth = AuthProfile(user=user, site=site, verification_code=code) auth.save() return auth