def test_last_login_date_gets_filled_in_when_authorising(self):
        user = User(account_locked=False,
                    account_verified=True,
                    hashed_password=bcrypt.hash("password"),
                    failed_logins=0)

        user.authorise("password")

        self.assertIsNotNone(user.last_login_date)
    def test_authorise_successfully_authorised(self):
        user = User(account_locked=False,
                    account_verified=True,
                    hashed_password=bcrypt.hash("password"),
                    failed_logins=0)

        authorised = user.authorise("password")

        self.assertEqual(True, authorised)
    def test_authorise_correct_password_but_user_is_not_verified(self):
        user = User(account_locked=False,
                    account_verified=False,
                    hashed_password=bcrypt.hash("password"),
                    failed_logins=0)

        with self.assertRaises(Unauthorized) as err:
            authorised = user.authorise("password")
            self.assertEqual(False, authorised)

        self.assertEqual("User account not verified", err.exception.description)
    def test_authorise_wrong_password_user_becomes_locked(self):
        user = User(account_locked=False,
                    account_verified=True,
                    hashed_password=bcrypt.hash("password"),
                    failed_logins=MAX_FAILED_LOGINS - 1)

        with self.assertRaises(Unauthorized) as err:
            authorised = user.authorise("wrongpassword")
            self.assertEqual(False, authorised)

        self.assertEqual("User account locked", err.exception.description)
        self.assertEqual(MAX_FAILED_LOGINS, user.failed_logins)
    def test_authorise_wrong_password(self):
        user = User(account_locked=False,
                    account_verified=True,
                    hashed_password=bcrypt.hash("password"),
                    failed_logins=0)

        with self.assertRaises(Unauthorized) as err:
            authorised = user.authorise("wrongpassword")
            self.assertEqual(False, authorised)

        self.assertEqual("Unauthorized user credentials", err.exception.description)
        self.assertEqual(1, user.failed_logins)