def test_unverified_unprimary_email_sends_confirmation(self): """ If there is only one email, and it's not primary, set it as primary. If it's not verified, send a verification. """ user = BadgeUser(email="*****@*****.**", first_name="Test", last_name="User") user.save() email = CachedEmailAddress(email=user.email, user=user, verified=False, primary=False) email.save() user2 = BadgeUser(email="*****@*****.**", first_name="Error", last_name="User") user2.save() self.assertEqual(BadgeUser.objects.count(), 2) call_command('clean_email_records') email_record = CachedEmailAddress.objects.get(user=user) self.assertTrue(email_record.primary) self.assertEqual(len(mail.outbox), 1) self.assertEqual(BadgeUser.objects.count(), 1)
def handle(self, *args, **options): users_processed = 0 primaries_set = 0 email_errors = 0 user_errors = 0 for user in BadgeUser.objects.all(): users_processed += 1 try: emails = CachedEmailAddress.objects.filter(user=user) # handle users who don't have an EmailAddress record if emails.count() < 1: try: existing_email = CachedEmailAddress.objects.get(email=user.email) except CachedEmailAddress.DoesNotExist: new_primary = CachedEmailAddress( user=user, email=user.email, verified=False, primary=True ) new_primary.save() new_primary.send_confirmation(signup="canvas") else: user.delete() # User record has no email addresses and email address has been added under another account continue emails = CachedEmailAddress.objects.filter(user=user) # User has emails but none marked primary elif len([e for e in emails if e.primary is True]) == 0: new_primary = emails.first() new_primary.set_as_primary(conditional=True) self.stdout.write("Set {} as primary for user {}".format(new_primary.email, user.pk)) primaries_set += 1 prior_confirmations = EmailConfirmation.objects.filter(email_address=new_primary) if new_primary.verified is False and not prior_confirmations.exists(): try: new_primary.send_confirmation(signup="canvas") except SMTPException as e: raise e except Exception as e: raise SMTPException("Error sending mail to {} -- {}".format( new_primary.email, e.message )) except IntegrityError as e: user_errors += 1 self.stdout.write("Error in user {} record: {}".format(user.pk, e.message)) continue except SMTPException as e: email_errors += 1 self.stdout.write("Could not send mail: {}".format(e.message)) self.stdout.write( "Done cleaning email: {} users, {} updated primaries, {} user errors, {} email errors.".format( users_processed, primaries_set, user_errors, email_errors ) )
def test_lower_variant_autocreated_on_new_email(self): first_email = CachedEmailAddress( email="*****@*****.**", user=BadgeUser.objects.first(), verified=True ) first_email.save() self.assertIsNotNone(first_email.pk) variants = EmailAddressVariant.objects.filter(canonical_email=first_email) self.assertEqual(len(variants), 1) self.assertEqual(variants[0].email, '*****@*****.**')
def test_lower_variant_autocreated_on_new_email(self): first_email = CachedEmailAddress( email="*****@*****.**", user=BadgeUser.objects.first(), verified=True ) first_email.save() self.assertIsNotNone(first_email.pk) variants = EmailAddressVariant.objects.filter(canonical_email=first_email) self.assertEqual(len(variants), 1) self.assertEqual(variants[0].email, '*****@*****.**')
def test_user_can_create_variant_method(self): user = BadgeUser.objects.first() first_email = CachedEmailAddress( email="*****@*****.**", user=user, verified=True ) first_email.save() first_email.add_variant("*****@*****.**") self.assertTrue(user.can_add_variant("*****@*****.**")) self.assertFalse(user.can_add_variant("*****@*****.**")) # already exists self.assertFalse(user.can_add_variant("*****@*****.**")) # is the original self.assertFalse(user.can_add_variant("*****@*****.**")) # not a match of original
def test_user_can_create_variant_method(self): user = BadgeUser.objects.first() first_email = CachedEmailAddress( email="*****@*****.**", user=user, verified=True ) first_email.save() first_email.add_variant("*****@*****.**") self.assertTrue(user.can_add_variant("*****@*****.**")) self.assertFalse(user.can_add_variant("*****@*****.**")) # already exists self.assertFalse(user.can_add_variant("*****@*****.**")) # is the original self.assertFalse(user.can_add_variant("*****@*****.**")) # not a match of original
def test_can_create_account_with_same_email_since_deleted(self): email = '*****@*****.**' new_email = '*****@*****.**' first_user_data = user_data = { 'first_name': 'NEW Test', 'last_name': 'User', 'email': email, 'password': '******' } response = self.client.post('/v1/user/profile', user_data) first_user = BadgeUser.objects.get(email=email) first_email = CachedEmailAddress.objects.get(email=email) first_email.verified = True first_email.save() second_email = CachedEmailAddress(email=new_email, user=first_user, verified=True) second_email.save() self.assertEqual(len(first_user.cached_emails()), 2) self.client.force_authenticate(user=first_user) response = self.client.put( reverse('v1_api_user_email_detail', args=[second_email.pk]), {'primary': True}) self.assertEqual(response.status_code, 200) # Reload user and emails first_user = BadgeUser.objects.get(email=new_email) first_email = CachedEmailAddress.objects.get(email=email) second_email = CachedEmailAddress.objects.get(email=new_email) self.assertEqual(first_user.email, new_email) self.assertTrue(second_email.primary) self.assertFalse(first_email.primary) self.assertTrue(email in [e.email for e in first_user.cached_emails()]) first_email.delete() self.assertFalse( email in [e.email for e in first_user.cached_emails()]) user_data['name'] = 'NEWEST Test' self.client.force_authenticate(user=None) response = self.client.post('/v1/user/profile', user_data) self.assertEqual(response.status_code, 201)
def test_email_added_for_user_missing_one(self): user = BadgeUser(email="*****@*****.**", first_name="Test", last_name="User") user.save() self.assertFalse(CachedEmailAddress.objects.filter(user=user).exists()) user2 = BadgeUser(email="*****@*****.**", first_name="Test2", last_name="User") user2.save() email2 = CachedEmailAddress(user=user2, email="*****@*****.**", verified=False, primary=True) email2.save() call_command('clean_email_records') email_record = CachedEmailAddress.objects.get(user=user) self.assertFalse(email_record.verified) self.assertTrue(email_record.emailconfirmation_set.exists()) self.assertEqual(len(mail.outbox), 1)
def test_email_added_for_user_missing_one(self): user = BadgeUser(email="*****@*****.**", first_name="Test", last_name="User") user.save() self.assertFalse(CachedEmailAddress.objects.filter(user=user).exists()) user2 = BadgeUser(email="*****@*****.**", first_name="Test2", last_name="User") user2.save() email2 = CachedEmailAddress(user=user2, email="*****@*****.**", verified=False, primary=True) email2.save() call_command('clean_email_records') email_record = CachedEmailAddress.objects.get(user=user) self.assertFalse(email_record.verified) self.assertTrue(email_record.emailconfirmation_set.exists()) self.assertEqual(len(mail.outbox), 1)
def test_can_create_new_variant_api(self): user = BadgeUser.objects.first() first_email = CachedEmailAddress( email="*****@*****.**", user=user, verified=True ) first_email.save() self.assertIsNotNone(first_email.pk) self.client.force_authenticate(user=user) response = self.client.post('/v1/user/emails', {'email': '*****@*****.**'}) self.assertEqual(response.status_code, 400) self.assertTrue('Matching address already exists. New case variant registered.' in response.data) variants = first_email.cached_variants() self.assertEqual(len(variants), 1) self.assertEqual(variants[0].email, '*****@*****.**')
def test_can_create_new_variant_api(self): user = BadgeUser.objects.first() first_email = CachedEmailAddress( email="*****@*****.**", user=user, verified=True ) first_email.save() self.assertIsNotNone(first_email.pk) self.client.force_authenticate(user=user) response = self.client.post('/v1/user/emails', {'email': '*****@*****.**'}) self.assertEqual(response.status_code, 400) self.assertTrue('Matching address already exists. New case variant registered.' in response.data) variants = first_email.cached_variants() self.assertEqual(len(variants), 1) self.assertEqual(variants[0].email, '*****@*****.**')
def test_can_create_account_with_same_email_since_deleted(self): email = '*****@*****.**' new_email = '*****@*****.**' first_user_data = user_data = { 'first_name': 'NEW Test', 'last_name': 'User', 'email': email, 'password': '******' } response = self.client.post('/v1/user/profile', user_data) first_user = BadgeUser.objects.get(email=email) first_email = CachedEmailAddress.objects.get(email=email) first_email.verified = True first_email.save() second_email = CachedEmailAddress(email=new_email, user=first_user, verified=True) second_email.save() self.assertEqual(len(first_user.cached_emails()), 2) self.client.force_authenticate(user=first_user) response = self.client.put( reverse('v1_api_user_email_detail', args=[second_email.pk]), {'primary': True} ) self.assertEqual(response.status_code, 200) # Reload user and emails first_user = BadgeUser.objects.get(email=new_email) first_email = CachedEmailAddress.objects.get(email=email) second_email = CachedEmailAddress.objects.get(email=new_email) self.assertEqual(first_user.email, new_email) self.assertTrue(second_email.primary) self.assertFalse(first_email.primary) self.assertTrue(email in [e.email for e in first_user.cached_emails()]) first_email.delete() self.assertFalse(email in [e.email for e in first_user.cached_emails()]) user_data['name'] = 'NEWEST Test' self.client.force_authenticate(user=None) response = self.client.post('/v1/user/profile', user_data) self.assertEqual(response.status_code, 201)
def test_unverified_unprimary_email_sends_confirmation(self): """ If there is only one email, and it's not primary, set it as primary. If it's not verified, send a verification. """ user = BadgeUser(email="*****@*****.**", first_name="Test", last_name="User") user.save() email = CachedEmailAddress(email=user.email, user=user, verified=False, primary=False) email.save() user2 = BadgeUser(email="*****@*****.**", first_name="Error", last_name="User") user2.save() self.assertEqual(BadgeUser.objects.count(), 2) call_command('clean_email_records') email_record = CachedEmailAddress.objects.get(user=user) self.assertTrue(email_record.primary) self.assertEqual(len(mail.outbox), 1) self.assertEqual(BadgeUser.objects.count(), 1)
def handle(self, *args, **options): users_processed = 0 primaries_set = 0 email_errors = 0 user_errors = 0 for user in BadgeUser.objects.all(): users_processed += 1 try: emails = CachedEmailAddress.objects.filter(user=user) # handle users who don't have an EmailAddress record if emails.count() < 1: try: existing_email = CachedEmailAddress.objects.get( email=user.email) except CachedEmailAddress.DoesNotExist: new_primary = CachedEmailAddress(user=user, email=user.email, verified=False, primary=True) new_primary.save() new_primary.send_confirmation(signup="canvas") else: user.delete( ) # User record has no email addresses and email address has been added under another account continue emails = CachedEmailAddress.objects.filter(user=user) # User has emails but none marked primary elif len([e for e in emails if e.primary is True]) == 0: new_primary = emails.first() new_primary.set_as_primary(conditional=True) self.stdout.write("Set {} as primary for user {}".format( new_primary.email, user.pk)) primaries_set += 1 prior_confirmations = EmailConfirmation.objects.filter( email_address=new_primary) if new_primary.verified is False and not prior_confirmations.exists( ): try: new_primary.send_confirmation(signup="canvas") except SMTPException as e: raise e except Exception as e: raise SMTPException( "Error sending mail to {} -- {}".format( new_primary.email, e.message)) except IntegrityError as e: user_errors += 1 self.stdout.write("Error in user {} record: {}".format( user.pk, e.message)) continue except SMTPException as e: email_errors += 1 self.stdout.write("Could not send mail: {}".format(e.message)) self.stdout.write( "Done cleaning email: {} users, {} updated primaries, {} user errors, {} email errors." .format(users_processed, primaries_set, user_errors, email_errors))