def test_local_strategy_authenticate():
    class Local(LocalStrategy):
        def get_or_create_user(self, user: Dict) -> Dict:
            return super().get_or_create_user(user)

        def validate_credentials(self, username: str, password: str) -> Dict:
            return {}

    # Create customs
    app = Flask("TESTS")
    app.secret_key = "630738a8-3b13-4311-8018-87554d6f7e85"
    Customs(app)

    # Create the strategy
    strategy = Local()

    with app.test_request_context("/?test=123", json={"bla": "bla"}):

        with pytest.raises(UnauthorizedException):
            user = strategy.authenticate(request)

    with app.test_request_context("/?username=test&password=test"):
        user = strategy.authenticate(request)
        assert user == {}

    # Cleanup of the Customs object used for testing
    Customs.remove_instance()
def test_local_strategy_extract_crendentials():
    class Local(LocalStrategy):
        def get_or_create_user(self, user: Dict) -> Dict:
            return super().get_or_create_user(user)

        def validate_credentials(self, username: str, password: str) -> Dict:
            return super().validate_credentials(username, password)

    # Create customs
    app = Flask("TESTS")
    app.secret_key = "630738a8-3b13-4311-8018-87554d6f7e85"
    Customs(app)

    # Create the strategy
    strategy = Local()

    with app.test_request_context("/?test=123", json={"bla": "bla"}):
        credentials = strategy.extract_credentials(request)
        assert credentials == {}

    with app.test_request_context("/?username=test&password=test"):
        credentials = strategy.extract_credentials(request)
        assert "username" in credentials
        assert "password" in credentials

    # Cleanup of the Customs object used for testing
    Customs.remove_instance()
Example #3
0
def test_jwt_strategy_authenticate():
    class JWT(JWTStrategy):
        def get_or_create_user(self, user: Dict) -> Dict:
            return super().get_or_create_user(user)

    # Create customs
    app = Flask("TESTS")
    app.secret_key = "630738a8-3b13-4311-8018-87554d6f7e85"
    Customs(app)

    # Create the strategy
    strategy = JWT()

    with app.test_request_context("/?test=123", json={"bla": "bla"}):

        with pytest.raises(UnauthorizedException):
            user = strategy.authenticate(request)

    test_user = {}
    token = strategy.sign(test_user)

    with app.test_request_context("/",
                                  headers={"Authorization":
                                           f"Bearer {token}"}):
        user = strategy.authenticate(request)
        assert user == {}

    # Cleanup of the Customs object used for testing
    Customs.remove_instance()
def test_basic_strategy_authenticate():
    class Basic(BasicStrategy):
        def get_or_create_user(self, user: Dict) -> Dict:
            return {}

        def validate_credentials(self, username: str, password: str) -> Dict:
            return {}

    # Create customs
    app = Flask("TESTS")
    app.secret_key = "630738a8-3b13-4311-8018-87554d6f7e85"
    Customs(app)

    # Create the strategy
    strategy = Basic()

    with app.test_request_context("/?test=123", json={"bla": "bla"}):

        with pytest.raises(UnauthorizedException):
            user = strategy.authenticate(request)

    with app.test_request_context(
            "/",
            headers={
                "Authorization":
                "Basic %s" % base64.b64encode("test:test".encode()).decode()
            },
    ):
        user = strategy.authenticate(request)
        assert user == {}

    # Cleanup of the Customs object used for testing
    Customs.remove_instance()
Example #5
0
def test_jwt_strategy_initialization_with_customs():
    class JWT(JWTStrategy):
        def get_or_create_user(self, user: Dict) -> Dict:
            return super().get_or_create_user(user)

    # Create customs
    app = Flask("TESTS")
    app.secret_key = "630738a8-3b13-4311-8018-87554d6f7e85"
    Customs(app)

    # Create the strategy
    strategy = JWT()

    assert strategy.name == "jwt"

    # Cleanup of the Customs object used for testing
    Customs.remove_instance()
def test_local_strategy_initialization_with_customs():
    class Local(LocalStrategy):
        def get_or_create_user(self, user: Dict) -> Dict:
            return super().get_or_create_user(user)

        def validate_credentials(self, username: str, password: str) -> Dict:
            return super().validate_credentials(username, password)

    # Create customs
    app = Flask("TESTS")
    app.secret_key = "630738a8-3b13-4311-8018-87554d6f7e85"
    Customs(app)

    # Create the strategy
    strategy = Local()

    assert strategy.name == "local"

    # Cleanup of the Customs object used for testing
    Customs.remove_instance()