def check_user_id_exists(self, id: str) -> bool:
        """Checks the user exists by id"""

        if User.objects(id__exists=id) is False:
            return False

        return True
def test_can_authenticate_password():
    """Tests whether a users hashed password can be authenticated"""
    User.drop_collection()
    User(id=str(uuid4()),
         email="*****@*****.**",
         password=_service.hash_password("S0meFunkyP455")).save()
    result = User.objects(email="*****@*****.**").first()

    assert _service.check_password("S0meFunkyP455", result.password)
Example #3
0
def test_user_is_created():
    """Tests whether a user can be added to the database"""
    User.drop_collection()

    user = generate_user()
    user.save()

    result = User.objects(email=user.email).first()

    assert result.email == user.email
Example #4
0
def test_register_user_returns_correct_users_email():
    """Tests the user registration returns user"""
    user = {}
    user["email"] = "*****@*****.**"
    user["password"] = "******"

    _auth_repo.register_user(user)

    result = User.objects(email="*****@*****.**").first()

    assert result.email == "*****@*****.**"
    def check_email_exists(self, email: str) -> bool:
        """Checks to see if email exists in database"""

        # if User.objects(email__iexact=email) is True:
        #     return True

        # if User.objects(email__match=email) is True:
        #     return True

        # if User.objects(email__exists=email) is True:
        #     return True

        # only one that works correctly: ?

        if User.objects(email=email).first() is not None:
            return True

        return False
Example #6
0
    def login(self, data: dict) -> User:
        """
        Allows a user to login with email and password.
        """
        email = data["email"]
        password = data["password"]

        if email is None:
            raise ValueError("Email is required.")

        if password is None:
            raise ValueError("Password is required.")

        user = None

        if self._val_service.validate_email(email) is False:
            raise ValueError("Invalid email.")

        if self._val_service.check_email_exists(email) is False:
            raise ValueError("Login incorrect.")

        user = User.objects(email=email).first()

        if user is None:
            raise ValueError("Login incorrect")

        if self._val_service.validate_password(password) is False:
            raise ValueError("Invalid password.")

        if self._auth_service.check_password(password, user.password) is False:
            raise ValueError("Login incorrect.")

        token = self._auth_service.get_token(email)

        user.access_token = token
        user.updated_at = dt.datetime.utcnow()
        user.save()

        return user
Example #7
0
    def get_user_from_token(self, token: str) -> User:
        """Retrieves the tokens user from the database"""

        if token is None:
            raise ValueError("Token not found.")

        decoded = self.decode_jwt(token)

        email = decoded["email"]

        # We cannot refactor this to use the User repo as it adds a circular
        # dependency.
        user = User.objects(email=email).first()

        if user is not None:
            return user
        else:
            self._logger.log(
                LogEntry(
                    LogLevel.ERROR, __name__,
                    "We decoded a valid token but did not find the "
                    "user with corresponding email in the database!"))

        raise ValueError("User not found.")