# Copyright (c) 2011-2013 <NAME>. All rights reserved.
# Use of this source code is governed by a BSD License found in README.md.

from django.test import TestCase

from huxley.accounts.constants import *
from huxley.accounts.models import *

class HuxleyUserTest(TestCase):

    def test_authenticate(self):
        """ Tests that the function correctly authenticates and returns a
            user, or returns an error message. """
        kunal = HuxleyUser.objects.create(username='kunal', email='<EMAIL>')
        kunal.set_password('<PASSWORD>')
        kunal.save()

        user, error = HuxleyUser.authenticate('kunal', '')
        self.assertIsNone(user)
        self.assertEqual(error, AuthenticationErrors.MISSING_FIELDS)

        user, error = HuxleyUser.authenticate('', 'kunalmehta')
        self.assertIsNone(user)
        self.assertEqual(error, AuthenticationErrors.MISSING_FIELDS)

        user, error = HuxleyUser.authenticate('roflrofl', 'roflrofl')
        self.assertIsNone(user)
        self.assertEqual(error, AuthenticationErrors.INVALID_LOGIN)

        user, error = HuxleyUser.authenticate('kunal', 'kunalmehta')
        self.assertEqual(user, kunal)
        self.assertIsNone(error)

    def test_change_password(self):
        """ Tests that the function correctly changes a user's password, or
            returns an error message. """
        user = HuxleyUser.objects.create(username='adavis', email='<EMAIL>')
        user.set_password('<PASSWORD>')

        success, error = user.change_password('', 'lololol', 'lololol')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.MISSING_FIELDS, error)

        success, error = user.change_password('<PASSWORD>', '', 'lololol')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.MISSING_FIELDS, error)

        success, error = user.change_password('<PASSWORD>', '<PASSWORD>', '')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.MISSING_FIELDS, error)

        success, error = user.change_password('<PASSWORD>', '<PASSWORD>', '<PASSWORD>')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.MISMATCHED_PASSWORDS, error)

        success, error = user.change_password('<PASSWORD>', 'lol', 'lol')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.PASSWORD_TOO_SHORT, error)

        success, error = user.change_password('<PASSWORD>', '<PASSWORD><', 'lololol<')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.INVALID_CHARACTERS, error)

        success, error = user.change_password('<PASSWORD>', '<PASSWORD>', '<PASSWORD>')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.INCORRECT_PASSWORD, error)

        success, error = user.change_password('<PASSWORD>', '<PASSWORD>', '<PASSWORD>')
        self.assertTrue(success)
        self.assertTrue(user.check_password('<PASSWORD>'))
# Copyright (c) 2011-2013 <NAME>. All rights reserved.
# Use of this source code is governed by a BSD License found in README.md.

from django.test import TestCase

from huxley.accounts.constants import *
from huxley.accounts.models import *

class HuxleyUserTest(TestCase):

    def test_authenticate(self):
        """ Tests that the function correctly authenticates and returns a
            user, or returns an error message. """
        kunal = HuxleyUser.objects.create(username='kunal', email='<EMAIL>')
        kunal.set_password('<PASSWORD>')
        kunal.save()

        user, error = HuxleyUser.authenticate('kunal', '')
        self.assertIsNone(user)
        self.assertEqual(error, AuthenticationErrors.MISSING_FIELDS)

        user, error = HuxleyUser.authenticate('', 'kunalmehta')
        self.assertIsNone(user)
        self.assertEqual(error, AuthenticationErrors.MISSING_FIELDS)

        user, error = HuxleyUser.authenticate('roflrofl', 'roflrofl')
        self.assertIsNone(user)
        self.assertEqual(error, AuthenticationErrors.INVALID_LOGIN)

        user, error = HuxleyUser.authenticate('kunal', 'kunalmehta')
        self.assertEqual(user, kunal)
        self.assertIsNone(error)

    def test_change_password(self):
        """ Tests that the function correctly changes a user's password, or
            returns an error message. """
        user = HuxleyUser.objects.create(username='adavis', email='<EMAIL>')
        user.set_password('<PASSWORD>')

        success, error = user.change_password('', 'lololol', 'lololol')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.MISSING_FIELDS, error)

        success, error = user.change_password('<PASSWORD>', '', 'lololol')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.MISSING_FIELDS, error)

        success, error = user.change_password('<PASSWORD>', '<PASSWORD>', '')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.MISSING_FIELDS, error)

        success, error = user.change_password('<PASSWORD>', '<PASSWORD>', '<PASSWORD>')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.MISMATCHED_PASSWORDS, error)

        success, error = user.change_password('<PASSWORD>', 'lol', 'lol')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.PASSWORD_TOO_SHORT, error)

        success, error = user.change_password('<PASSWORD>', '<PASSWORD><', 'lololol<')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.INVALID_CHARACTERS, error)

        success, error = user.change_password('<PASSWORD>', '<PASSWORD>', '<PASSWORD>')
        self.assertFalse(success)
        self.assertEquals(ChangePasswordErrors.INCORRECT_PASSWORD, error)

        success, error = user.change_password('<PASSWORD>', '<PASSWORD>', '<PASSWORD>')
        self.assertTrue(success)
        self.assertTrue(user.check_password('<PASSWORD>'))
# Copyright (c) 2011-2013 <NAME>. All rights reserved. # Use of this source code is governed by a BSD License found in README.md. Tests that the function correctly authenticates and returns a             user, or returns an error message. Tests that the function correctly changes a user's password, or             returns an error message.