# -*- coding: utf-8 -*-
"""Test forms."""

from dribdat.user.forms import RegisterForm, LoginForm
from dribdat.admin.forms import EventForm

from datetime import time, datetime


class TestRegisterForm:
    """Register form."""

    def test_validate_user_already_registered(self, user):
        """Enter username that is already registered."""
        form = RegisterForm(username=user.username, email='foo@bar.com',
                            password='example', confirm='example')

        assert form.validate() is False
        assert 'A user with this name already exists' in form.username.errors

    def test_validate_email_already_registered(self, user):
        """Enter 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):
        """Register with success."""
        form = RegisterForm(username='newusername', email='new@dribdat.cc',
                            password='example', confirm='example')
        assert form.validate() is True


class TestLoginForm:
    """Login form."""

    def test_validate_success(self, user):
        """Login successful."""
        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):
        """Unknown username."""
        form = LoginForm(username='unknown', password='example')
        assert form.validate() is False
        assert 'Unknown username' in form.username.errors
        assert form.user is None

    def test_validate_invalid_password(self, user):
        """Invalid password."""
        user.set_password('example')
        user.save()
        form = LoginForm(username=user.username, password='wrongpassword')
        assert form.validate() is False
        assert 'Invalid password' in form.password.errors

    def test_validate_inactive_user(self, user, testapp):
        """Inactive user."""
        user.active = False
        user.set_password('example')
        user.save()
        # Correct username and password, but user is not activated
        form = LoginForm(username=user.username, password='example')
        # Deactivated use can still log in
        assert form.validate() is True
        res = testapp.get('/user/%s' % user.username)
        assert 'under review' in res


class TestEventForm:
    """Event entry form."""

    def test_create_new_event(self, event):
        """Sample event."""
        form = EventForm()
        # A name is required
        assert form.validate() is False
        assert 'This field is required.' in form.name.errors
        form = EventForm(name="test")
        assert form.validate() is True

    def test_event_validate_time(self, event):
        """Time validation test."""
        form = EventForm(
            event_id=event.id, name="Name",
            starts_date=datetime.now(), ends_date=datetime.now(),
            starts_time=time(2, 0, 0), ends_time=time(1, 0, 0))
        # Ends before starting
        # assert form.validate() is False
        # assert 'Start time must be before end time.'
        # in form.starts_time.errors
        assert form.validate() is True  # TODO: fixme
