# Path: seoul_serenity/public/forms.py
# class LoginForm(Form):
#     username = TextField('Username', 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
# 
#         self.user = User.query.filter_by(username=self.username.data).first()
#         if not self.user:
#             self.username.errors.append('Unknown username')
#             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.username.errors.append('User not activated')
#             return False
#         return True
# 
# Path: seoul_serenity/user/forms.py
# class RegisterForm(Form):
#     username = TextField('Username',
#                     validators=[DataRequired(), Length(min=3, max=25)])
#     email = TextField('Email',
#                     validators=[DataRequired(), Email(), Length(min=6, max=40)])
#     password = PasswordField('Password',
#                                 validators=[DataRequired(), Length(min=6, max=40)])
#     confirm = PasswordField('Verify password',
#                 [DataRequired(), EqualTo('password', message='Passwords must match')])
# 
#     firstname = TextField('이름', validators=[DataRequired(), Length(min=1, max=25)])
# 
#     lastname = TextField('성', validators=[DataRequired(), Length(min=1, max=25)])
# 
#     def __init__(self, *args, **kwargs):
#         super(RegisterForm, self).__init__(*args, **kwargs)
#         self.user = None
# 
#     def validate(self):
#         initial_validation = super(RegisterForm, self).validate()
#         if not initial_validation:
#             return False
#         user = User.query.filter_by(username=self.username.data).first()
#         if user:
#             self.username.errors.append("Username already registered")
#             return False
#         user = User.query.filter_by(email=self.email.data).first()
#         if user:
#             self.email.errors.append("Email already registered")
#             return False
#         return True
# 
# Path: tests/factories.py
# class UserFactory(BaseFactory):
#     username = Sequence(lambda n: "user{0}".format(n))
#     email = Sequence(lambda n: "user{0}@example.com".format(n))
#     password = PostGenerationMethodCall('set_password', 'example')
#     active = True
# 
#     class Meta:
#         model = User


import pytest
from seoul_serenity.public.forms import LoginForm
from seoul_serenity.user.forms import RegisterForm
from .factories import UserFactory
        form = RegisterForm(username=user.username, email='foo@bar.com',
            password='example', confirm='example')

        assert form.validate() is False
        assert 'Username already registered' in form.username.errors

    def test_validate_email_already_registered(self, user):
        # enters email that is already registered
        form = RegisterForm(username='unique', email=user.email,
            password='example', confirm='example')

        assert form.validate() is False
        assert 'Email already registered' in form.email.errors

    def test_validate_success(self, db):
        form = RegisterForm(username='newusername', email='new@test.test',
            password='example', confirm='example')
        assert form.validate() is True


class TestLoginForm:

    def test_validate_success(self, user):
        user.set_password('example')
        user.save()
        form = LoginForm(username=user.username, password='example')
        assert form.validate() is True
        assert form.user == user

    def test_validate_unknown_username(self, db):

# Path: seoul_serenity/public/forms.py
# class LoginForm(Form):
#     username = TextField('Username', 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
# 
#         self.user = User.query.filter_by(username=self.username.data).first()
#         if not self.user:
#             self.username.errors.append('Unknown username')
#             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.username.errors.append('User not activated')
#             return False
#         return True
# 
# Path: seoul_serenity/user/forms.py
# class RegisterForm(Form):
#     username = TextField('Username',
#                     validators=[DataRequired(), Length(min=3, max=25)])
#     email = TextField('Email',
#                     validators=[DataRequired(), Email(), Length(min=6, max=40)])
#     password = PasswordField('Password',
#                                 validators=[DataRequired(), Length(min=6, max=40)])
#     confirm = PasswordField('Verify password',
#                 [DataRequired(), EqualTo('password', message='Passwords must match')])
# 
#     firstname = TextField('이름', validators=[DataRequired(), Length(min=1, max=25)])
# 
#     lastname = TextField('성', validators=[DataRequired(), Length(min=1, max=25)])
# 
#     def __init__(self, *args, **kwargs):
#         super(RegisterForm, self).__init__(*args, **kwargs)
#         self.user = None
# 
#     def validate(self):
#         initial_validation = super(RegisterForm, self).validate()
#         if not initial_validation:
#             return False
#         user = User.query.filter_by(username=self.username.data).first()
#         if user:
#             self.username.errors.append("Username already registered")
#             return False
#         user = User.query.filter_by(email=self.email.data).first()
#         if user:
#             self.email.errors.append("Email already registered")
#             return False
#         return True
# 
# Path: tests/factories.py
# class UserFactory(BaseFactory):
#     username = Sequence(lambda n: "user{0}".format(n))
#     email = Sequence(lambda n: "user{0}@example.com".format(n))
#     password = PostGenerationMethodCall('set_password', 'example')
#     active = True
# 
#     class Meta:
#         model = User


# Path: tests/test_forms.py
import pytest
from seoul_serenity.public.forms import LoginForm
from seoul_serenity.user.forms import RegisterForm
from .factories import UserFactory

        form = RegisterForm(username=user.username, email='foo@bar.com',
            password='example', confirm='example')

        assert form.validate() is False
        assert 'Username already registered' in form.username.errors

    def test_validate_email_already_registered(self, user):
        # enters email that is already registered
        form = RegisterForm(username='unique', email=user.email,
            password='example', confirm='example')

        assert form.validate() is False
        assert 'Email already registered' in form.email.errors

    def test_validate_success(self, db):
        form = RegisterForm(username='newusername', email='new@test.test',
            password='example', confirm='example')
        assert form.validate() is True


class TestLoginForm:

    def test_validate_success(self, user):
        user.set_password('example')
        user.save()
        form = LoginForm(username=user.username, password='example')
        assert form.validate() is True
        assert form.user == user

    def test_validate_unknown_username(self, db):

        form = LoginForm(username='unknown', password='example')