def setUp(self):
        super(LoggedInTestCase, self).setUp()
        # set up sessions in unit tests
        session_engine = import_module(settings.SESSION_ENGINE)
        store = session_engine.SessionStore()
        store.save()
        # set the client
        self.client = Client()
        self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
        manager = UserManager()
        manager.model = User

        # create a sample admin user
        self.admin = manager.create_superuser(
            username='******',
            first_name='admin',
            last_name='admin',
            email='*****@*****.**',
            password='******'
        )

        # create sample basic user
        self.user = manager.create_user(
            username='******',
            first_name='user',
            last_name='user',
            email='*****@*****.**',
            password='******'
        )
class UserManagerTestCase(TestCase):
    """
    Test case for UserManager object
    that is found with the user model.
    """
    def setUp(self):
        self.manager = UserManager()
        self.manager.model = User

    def test_creating_standard_user(self):
        """
        Test creation of basic user
        """
        user = self.manager.create_user(
            username='******',
            first_name='test',
            last_name='user',
            email='*****@*****.**',
            password='******'
        )

        self.assertEqual(user.username, "test1")
        # make sure the manager gave all false permissions
        self.assertFalse(user.is_admin)
        self.assertFalse(user.is_staff())
        self.assertFalse(user.is_module_leader)
        self.assertFalse(user.is_office_admin)
        self.assertFalse(user.is_year_tutor)

    def test_creating_standard_user_no_username(self):
        """
        Test to determine exception is raised
        when no username is provided.
        """
        with self.assertRaises(ValueError):
            user = self.manager.create_user(
                username='',
                first_name='test',
                last_name='user',
                email='*****@*****.**',
                password='******'
            )

    def test_creating_standard_user_no_first_name(self):
        """
        Test to determine exception is raised
        when no first name is provided.
        """
        with self.assertRaises(ValueError):
            user = self.manager.create_user(
                username='******',
                first_name='',
                last_name='user',
                email='*****@*****.**',
                password='******'
            )

    def test_creating_standard_user_no_last_name(self):
        """
        Test to determine exception is raised
        when no last name is provided.
        """
        with self.assertRaises(ValueError):
            user = self.manager.create_user(
                username='******',
                first_name='test',
                last_name='',
                email='*****@*****.**',
                password='******'
            )

    def test_creating_standard_user_no_email(self):
        """
        Test to determine exception is raised
        when no email is provided.
        """
        with self.assertRaises(ValueError):
            user = self.manager.create_user(
                username='******',
                first_name='test',
                last_name='user',
                email='',
                password='******'
            )

    def test_creating_user_same_username(self):
        """
        Test to determine what happens when
        a user creates an account with an
        existing username.
        """

        # create the test user
        self.manager.create_user(
            username='******',
            first_name='test',
            last_name='user',
            email='*****@*****.**',
            password='******'
        )

        # check that the exception is raised
        with self.assertRaises(ValueError):
            user = self.manager.create_user(
                username='******',
                first_name='test',
                last_name='user',
                email='test',
                password='******'
            )

    def test_creating_super_user(self):
        """
        Test for creating a user user
        """
        user = self.manager.create_superuser(
            username='******',
            first_name='admin',
            last_name='user',
            email='*****@*****.**',
            password='******'
        )

        self.assertEqual(user.username, "admin")
        # make sure it is a superuser
        self.assertTrue(user.is_admin)
        self.assertTrue(user.is_staff())