# Path: flask_editablesite/public/forms.py
# class LoginForm(Form):
#     email = TextField('Email', validators=[DataRequired()])
#     password = PasswordField('Password', validators=[DataRequired()])
# 
#     def __init__(self, *args, **kwargs):
#         super(LoginForm, self).__init__(*args, **kwargs)
#         self.user = None
# 
#     def validate(self):
#         initial_validation = super(LoginForm, self).validate()
#         if not initial_validation:
#             return False
# 
#         # Log in with dummy credentials when we're set to 'sessionstore'
#         # instead of 'db'.
#         if app.config.get('USE_SESSIONSTORE_NOT_DB'):
#             if self.email.data != app.config['SESSIONSTORE_USER_EMAIL']:
#                 self.email.errors.append('Unknown email')
#                 return False
# 
#             if self.password.data != app.config['SESSIONSTORE_USER_PASSWORD']:
#                 self.password.errors.append('Invalid password')
#                 return False
# 
#             self.user = User.sessionstore_user()
#         # Regular login.
#         else:
#             self.user = User.query.filter_by(email=self.email.data).first()
#             if not self.user:
#                 self.email.errors.append('Unknown email')
#                 return False
# 
#             if not self.user.check_password(self.password.data):
#                 self.password.errors.append('Invalid password')
#                 return False
# 
#             if not self.user.active:
#                 self.email.errors.append('User {0} not activated'.format(
#                     self.user.full_name_or_email))
#                 return False
# 
#         return True


from flask_editablesite.public.forms import LoginForm

def test_login_success(user):
    user.set_password('example')
    user.save()
    form = LoginForm(email=user.email, password='example')
    assert form.validate() is True
    assert form.user == user


def test_login_unknown_email(db):
    form = LoginForm(email='unknown', password='example')
    assert form.validate() is False
    assert 'Unknown email' in form.email.errors
    assert form.user is None


def test_login_invalid_password(user):
    user.set_password('example')
    user.save()
    form = LoginForm(email=user.email, password='wrongpassword')
    assert form.validate() is False
    assert 'Invalid password' in form.password.errors


def test_login_inactive_user(user):
    user.active = False
    user.confirmed_at = None
    user.set_password('example')
    user.save()
    # Correct email and password, but user is not activated

# Path: flask_editablesite/public/forms.py
# class LoginForm(Form):
#     email = TextField('Email', validators=[DataRequired()])
#     password = PasswordField('Password', validators=[DataRequired()])
# 
#     def __init__(self, *args, **kwargs):
#         super(LoginForm, self).__init__(*args, **kwargs)
#         self.user = None
# 
#     def validate(self):
#         initial_validation = super(LoginForm, self).validate()
#         if not initial_validation:
#             return False
# 
#         # Log in with dummy credentials when we're set to 'sessionstore'
#         # instead of 'db'.
#         if app.config.get('USE_SESSIONSTORE_NOT_DB'):
#             if self.email.data != app.config['SESSIONSTORE_USER_EMAIL']:
#                 self.email.errors.append('Unknown email')
#                 return False
# 
#             if self.password.data != app.config['SESSIONSTORE_USER_PASSWORD']:
#                 self.password.errors.append('Invalid password')
#                 return False
# 
#             self.user = User.sessionstore_user()
#         # Regular login.
#         else:
#             self.user = User.query.filter_by(email=self.email.data).first()
#             if not self.user:
#                 self.email.errors.append('Unknown email')
#                 return False
# 
#             if not self.user.check_password(self.password.data):
#                 self.password.errors.append('Invalid password')
#                 return False
# 
#             if not self.user.active:
#                 self.email.errors.append('User {0} not activated'.format(
#                     self.user.full_name_or_email))
#                 return False
# 
#         return True


# Path: tests/test_user_forms.py
from flask_editablesite.public.forms import LoginForm


def test_login_success(user):
    user.set_password('example')
    user.save()
    form = LoginForm(email=user.email, password='example')
    assert form.validate() is True
    assert form.user == user


def test_login_unknown_email(db):
    form = LoginForm(email='unknown', password='example')
    assert form.validate() is False
    assert 'Unknown email' in form.email.errors
    assert form.user is None


def test_login_invalid_password(user):
    user.set_password('example')
    user.save()
    form = LoginForm(email=user.email, password='wrongpassword')
    assert form.validate() is False
    assert 'Invalid password' in form.password.errors


def test_login_inactive_user(user):
    user.active = False
    user.confirmed_at = None
    user.set_password('example')
    user.save()
    # Correct email and password, but user is not activated

    form = LoginForm(email=user.email, password='example')