import uuid

from django import test
from django.contrib import auth
from django.test.utils import override_settings

from pcassandra import tests_utils
from pcassandra.dj18.auth import models


PCASSANDRA_AUTH_USER_MODEL = 'pcassandra.dj18.auth.models.CassandraUser'


class PCassandraBaseTest(test.TestCase, tests_utils.PCassandraTestUtilsMixin):
    @classmethod
    @override_settings(PCASSANDRA_AUTH_USER_MODEL=PCASSANDRA_AUTH_USER_MODEL)
    def setUpClass(cls):
        test.TestCase.setUpClass()
        tests_utils.setup()


class TestCassandraUserCreation(PCassandraBaseTest):
    @override_settings(PCASSANDRA_AUTH_USER_MODEL=PCASSANDRA_AUTH_USER_MODEL)
    def test_create_valid_user(self):
        rnd = uuid.uuid4().hex[0:20]
        username = "user-{}".format(rnd)

        all_users = models.CassandraUser.all()
        all_usernames = [_.username for _ in all_users]
        self.assertNotIn(username, all_usernames)

        cassandra_user = self._create_user(username)

        all_users = models.CassandraUser.all()
        all_usernames = [_.username for _ in all_users]
        self.assertIn(username, all_usernames)


class TestAuthentication(PCassandraBaseTest):
    @override_settings(PCASSANDRA_AUTH_USER_MODEL=PCASSANDRA_AUTH_USER_MODEL)
    def test_authenticate_works(self):
        password = '<PASSWORD>(uuid.uuid4().hex)
        cassandra_user = self._create_user()

        self.assertIsNone(auth.authenticate(
            username=cassandra_user.username,
            password=password))

        cassandra_user.set_password(password)
        cassandra_user.save()

        auth_user = auth.authenticate(
            username=cassandra_user.username,
            password=password)

        self.assertIsNotNone(auth_user)
        self.assertEquals(auth_user.username,
                          cassandra_user.username)

import uuid

from django import test
from django.contrib import auth
from django.test.utils import override_settings

from pcassandra import tests_utils
from pcassandra.dj18.auth import models


PCASSANDRA_AUTH_USER_MODEL = 'pcassandra.dj18.auth.models.CassandraUser'


class PCassandraBaseTest(test.TestCase, tests_utils.PCassandraTestUtilsMixin):
    @classmethod
    @override_settings(PCASSANDRA_AUTH_USER_MODEL=PCASSANDRA_AUTH_USER_MODEL)
    def setUpClass(cls):
        test.TestCase.setUpClass()
        tests_utils.setup()


class TestCassandraUserCreation(PCassandraBaseTest):
    @override_settings(PCASSANDRA_AUTH_USER_MODEL=PCASSANDRA_AUTH_USER_MODEL)
    def test_create_valid_user(self):
        rnd = uuid.uuid4().hex[0:20]
        username = "user-{}".format(rnd)

        all_users = models.CassandraUser.all()
        all_usernames = [_.username for _ in all_users]
        self.assertNotIn(username, all_usernames)

        cassandra_user = self._create_user(username)

        all_users = models.CassandraUser.all()
        all_usernames = [_.username for _ in all_users]
        self.assertIn(username, all_usernames)


class TestAuthentication(PCassandraBaseTest):
    @override_settings(PCASSANDRA_AUTH_USER_MODEL=PCASSANDRA_AUTH_USER_MODEL)
    def test_authenticate_works(self):
        password = '<PASSWORD>(uuid.uuid4().hex)
        cassandra_user = self._create_user()

        self.assertIsNone(auth.authenticate(
            username=cassandra_user.username,
            password=password))

        cassandra_user.set_password(password)
        cassandra_user.save()

        auth_user = auth.authenticate(
            username=cassandra_user.username,
            password=password)

        self.assertIsNotNone(auth_user)
        self.assertEquals(auth_user.username,
                          cassandra_user.username)
