Exemplo n.º 1
0
    def register(self, username: str, password: str, repeat_password: str):
        """
            Register new user if user is new and passwords match
        :param username:
        :param password:
        :param repeat_password:
        :return:
        """
        response = {"flash_message": "Invalid Credentials"}

        new_user = User({
            "username": username,
            "password": password,
        })
        # Check if User exists
        is_existing_user = self.get_user(new_user.get_username())

        if is_existing_user and is_existing_user.get_username() == username:
            response["flash_message"] = "username already in use"

        elif password == repeat_password:
            # hashing user password before storing in DB
            new_user.hash_password()
            create = self.create_user(new_user)  # returns None if create works
            if create:
                response[
                    "flash_message"] = "There was a problem when trying to create this user, Try Again!"
            else:
                response["flash_message"] = "Registration Successful!"
        else:
            response["flash_message"] = "Passwords do not match"

        return response["flash_message"]
Exemplo n.º 2
0
class UserTest(unittest.TestCase):
    def setUp(self):
        file = open('test/data/user.json')
        user_data = json.load(file)
        file.close()
        self.user = User(user_data)

    def test_get_username(self):
        self.assertEqual('willfarnaby', self.user.get_username())

    def test_get_user_first_name(self):
        self.assertEqual('William', self.user.get_first_name())

    def test_get_user_last_name(self):
        self.assertEqual('Farnaby', self.user.get_last_name())

    def test_get_favorite_books(self):
        favorite_books = self.user.get_favorite_books()
        self.assertIn("A book", favorite_books)
        self.assertEqual(2, len(favorite_books))

    def test_is_moderator(self):
        self.assertFalse(self.user.is_moderator())

    def test_check_password(self):
        given_password = "******"
        self.assertTrue(self.user.check_password(given_password))

    def test_get_dict(self):
        user_repr = self.user.get_dict()
        self.assertEqual("willfarnaby", user_repr['username'])
        self.assertFalse(user_repr['moderator'])

    def test_hash_and_check_password(self):
        given_password = "******"
        new_user = User({
            "username": "******",
            "password": "******"
        })
        new_user.hash_password()
        self.assertTrue(self.user.check_password(given_password))