<reponame>tilgovi/pyramid_fullauth
"""Test various validators present on User class."""
import pytest
import transaction
from pyramid.compat import text_type

from pyramid_fullauth.models import User
from pyramid_fullauth.exceptions import EmptyError, EmailValidationError


@pytest.mark.parametrize('email', [
    text_type('<EMAIL>'),
    text_type('<EMAIL>'),
    text_type('disposable.style.email.<EMAIL>'),
    text_type('"<EMAIL>"@example.<EMAIL>'),
    text_type('!#$%&\'*+-/=?^_`{}|~<EMAIL>'),
    text_type('""<EMAIL>'),
    text_type('"much.more unusual"@<EMAIL>'),
    text_type('"()<>[]:,;@\\\"!#$%&\'*+-/=?^_`{}| ~  ? ^_`{}|~.a"@<EMAIL>'),
])
def test_email_valid_formats(db_session, user, email):
    """Check all valid formats of Email (RFC 5321) can be set by user."""
    user = db_session.merge(user)

    user.email = email
    transaction.commit()

    user = db_session.query(User).filter(
        User.username == text_type('u1')).one()
    assert user.email == email


def test_email_empty(user):
    """Test reaction of email validator for empty email."""
    with pytest.raises(EmptyError):
        user.email = text_type('')


def test_email_invalid_formats(db_session, user, invalid_email):
    """Check all invalid formats of Email (RFC 5321) can not be set by user."""
    user = db_session.merge(user)

    with pytest.raises(EmailValidationError):
        user.email = invalid_email
        transaction.commit()
