def test_email_address_validate_for_check_dns(email_models, clean_mixin_db): """Validate_for with check_dns=True. Separate test as DNS lookup may fail.""" db = clean_mixin_db models = email_models user1 = models.EmailUser() user2 = models.EmailUser() anon_user = None db.session.add_all([user1, user2]) # A domain without MX records is invalid if check_dns=True # This uses hsgk.in, a known domain without MX. # example.* use null MX as per RFC 7505, but the underlying validator in pyIsEmail # does not support this. assert EmailAddress.validate_for(user1, '*****@*****.**', check_dns=True) == 'nomx' assert EmailAddress.validate_for(user2, '*****@*****.**', check_dns=True) == 'nomx' assert (EmailAddress.validate_for(anon_user, '*****@*****.**', check_dns=True) == 'nomx')
def test_email_address_validate_for(email_models, clean_mixin_db): """EmailAddress.validate_for can be used to determine availability""" db = clean_mixin_db models = email_models user1 = models.EmailUser() user2 = models.EmailUser() anon_user = None db.session.add_all([user1, user2]) # A new email address is available to all assert EmailAddress.validate_for(user1, '*****@*****.**') is True assert EmailAddress.validate_for(user2, '*****@*****.**') is True assert EmailAddress.validate_for(anon_user, '*****@*****.**') is True # Once it's assigned to a user, availability changes link = models.EmailLink(emailuser=user1, email='*****@*****.**') db.session.add(link) assert EmailAddress.validate_for(user1, '*****@*****.**') is True assert EmailAddress.validate_for(user2, '*****@*****.**') is False assert EmailAddress.validate_for(anon_user, '*****@*****.**') is False # An address in use is not available to claim as new assert (EmailAddress.validate_for(user1, '*****@*****.**', new=True) == 'not_new') assert EmailAddress.validate_for(user2, '*****@*****.**', new=True) is False assert (EmailAddress.validate_for( anon_user, '*****@*****.**', new=True) is False) # When delivery state changes, validate_for's result changes too ea = link.email_address assert ea.delivery_state.UNKNOWN ea.mark_sent() assert ea.delivery_state.SENT assert EmailAddress.validate_for(user1, '*****@*****.**') is True assert EmailAddress.validate_for(user2, '*****@*****.**') is False assert EmailAddress.validate_for(anon_user, '*****@*****.**') is False ea.mark_soft_fail() assert ea.delivery_state.SOFT_FAIL assert EmailAddress.validate_for(user1, '*****@*****.**') == 'soft_fail' assert EmailAddress.validate_for(user2, '*****@*****.**') is False assert EmailAddress.validate_for(anon_user, '*****@*****.**') is False ea.mark_hard_fail() assert ea.delivery_state.HARD_FAIL assert EmailAddress.validate_for(user1, '*****@*****.**') == 'hard_fail' assert EmailAddress.validate_for(user2, '*****@*****.**') is False assert EmailAddress.validate_for(anon_user, '*****@*****.**') is False # A blocked address is available to no one db.session.add(EmailAddress('*****@*****.**')) EmailAddress.mark_blocked('*****@*****.**') assert EmailAddress.validate_for(user1, '*****@*****.**') is False assert EmailAddress.validate_for(user2, '*****@*****.**') is False assert EmailAddress.validate_for(anon_user, '*****@*****.**') is False # An invalid address is available to no one assert EmailAddress.validate_for(user1, 'invalid') == 'invalid' assert EmailAddress.validate_for(user2, 'invalid') == 'invalid' assert EmailAddress.validate_for(anon_user, 'invalid') == 'invalid'