from django.test import TestCase

# Create your tests here.

'''
The MIT License (MIT)

Copyright (c) 2014 NTHUOJ team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
from django.core.urlresolvers import reverse
from django.test import TestCase, Client
from users.models import User
# Create your tests here.


class UserTestCase(TestCase):
    def setUp(self):
        User.objects.create_user(username='henry', password='correct')

    def test_login(self):
        '''Test basic login function'''
        response = self.client.login(username='henry', password='wrong')
        self.assertEqual(response, False)
        response = self.client.login(username='henry', password='correct')
        self.assertEqual(response, True)
        self.client.logout()

    def test_block_wrong_tries(self):
        '''Test if the client will be blocked for over 3 wrong tries'''
        # wrong tries for 3 times
        for wrong_tries in range(3):
            response = self.client.post(
                reverse('users:login'),
                {'username': 'henry', 'password': 'wrong'})
            self.assertNotContains(response, 'Page Not Found')

        # get blocked for forth wrong tries
        response = self.client.post(
            reverse('users:login'),
            {'username': 'henry', 'password': 'wrong'})
        self.assertContains(response, 'Page Not Found')

    def test_sign_up(self):
        '''Test if the client is able to sign up'''
        # initial user count
        user_count = User.objects.all().count()

        # sign up with different password confirmation
        response = self.client.post(
            reverse('users:create'),
            {'username': 'boss', 'password1': 'correct1',
            'password2':'correct2', 'email': 'oj@nthucs.edu.tw'})
        self.assertNotEqual(User.objects.all().count(), user_count + 1)

        # sign up with invalid email format
        response = self.client.post(
            reverse('users:create'),
            {'username': 'boss', 'password1': 'correct1',
            'password2':'correct2', 'email': 'oj'})
        self.assertNotEqual(User.objects.all().count(), user_count + 1)

        # sign up with used username
        response = self.client.post(
            reverse('users:create'),
            {'username': 'henry', 'password1': 'correct',
            'password2':'correct', 'email': 'oj'})
        self.assertNotEqual(User.objects.all().count(), user_count + 1)

        # valid sign up
        response = self.client.post(
            reverse('users:create'),
            {'username': 'boss', 'password1': 'correct',
            'password2':'correct', 'email': 'oj@nthucs.edu.tw'})
        self.assertEqual(User.objects.all().count(), user_count + 1)

add unit test for auth function;