Esempio n. 1
0
 def test_expected_number_of_config_parameter(self, app: AppFixture,
                                              initialize_app):
     # this check is a reminder that if more configuration
     # is added then TestDefaultConfig needs to be updated
     flask_firebase_admin.FirebaseAdmin(app)
     assert len([k for k in app.config
                 if k.startswith("FIREBASE_ADMIN")]) == 8
Esempio n. 2
0
    def test_init_with___init__(
        self,
        app: AppFixture,
        try_initialize_app: TryInitializeAppFixture,
    ):
        # create and initialize the extension
        admin = flask_firebase_admin.FirebaseAdmin(app)

        assert admin.app is not None
        assert app.config["FIREBASE_ADMIN_APP"] is None
        assert app.config["FIREBASE_ADMIN_AUTHORIZATION_SCHEME"] == "Bearer"
        assert app.config["FIREBASE_ADMIN_CHECK_REVOKED"] is True
        assert app.config["FIREBASE_ADMIN_PAYLOAD_ATTR"] == "jwt_payload"
        assert app.config["FIREBASE_ADMIN_CREDENTIAL"] is None
        assert app.config["FIREBASE_ADMIN_OPTIONS"] is None
        assert app.config[
            "FIREBASE_ADMIN_NAME"] == firebase_admin._DEFAULT_APP_NAME
        assert app.config["FIREBASE_ADMIN_RAISE_IF_APP_EXISTS"] is True
        try_initialize_app.assert_called_once_with(
            None,
            None,
            firebase_admin._DEFAULT_APP_NAME,
            True,
        )
        assert admin.admin is not None
    def test_generated_response_object(
        self, app: AppFixture, message, auth_scheme, expected_header
    ):
        app.config["FIREBASE_ADMIN_AUTHORIZATION_SCHEME"] = auth_scheme

        # we don't need a fully initialized extension
        admin = flask_firebase_admin.FirebaseAdmin()

        response = admin.make_401(message)

        assert response.headers["WWW-Authenticate"] == expected_header
        assert response.status_code == 401
        assert response.is_json and response.json["error"]["message"] == message
Esempio n. 4
0
def sample_app_no_valid_token(
    app: AppFixture,
    try_initialize_app: TryInitializeAppFixture,
    decode_token_raises: DecodeTokenRaisesFixture,
):
    admin = flask_firebase_admin.FirebaseAdmin(app)

    @app.route("/protected", methods=["GET", "POST"])
    @admin.jwt_required
    def protected():
        # we now access the JWT payload using request.firebase_jwt
        return {"message": f"Hello {flask.request.jwt_payload['email']}!"}

    return app
    def test_method_calls_verify_id_token(
        self,
        app: AppFixture,
        try_initialize_app: TryInitializeAppFixture,  # to mock call to initialize_app
        verify_id_token: VerifyIdTokenFixture,
        check_revoked,
    ):
        app.config["FIREBASE_ADMIN_CHECK_REVOKED"] = check_revoked
        admin = flask_firebase_admin.FirebaseAdmin(app)

        token = "abc"
        decoded_token = admin.decode_token(token)

        verify_id_token.assert_called_once_with(token, admin.admin, check_revoked)
        assert decoded_token is verify_id_token.return_value
def sample_app(
    app: AppFixture,
    try_initialize_app: TryInitializeAppFixture,
    decode_token: DecodeTokenFixture,
):
    admin = flask_firebase_admin.FirebaseAdmin(app)

    @app.route("/protected", methods=["GET", "POST"])
    @admin.jwt_required
    def protected():
        # we now access the JWT payload using request.firebase_jwt
        email = flask.request.jwt_payload["email"]  # type: ignore
        return {"message": f"Hello {email}!"}

    return app
    def test_firebase_admin_app(
        self, app: AppFixture, try_initialize_app: TryInitializeAppFixture
    ):
        admin_app = "this-is-my-admin-app"
        app.config["FIREBASE_ADMIN_APP"] = admin_app
        admin = flask_firebase_admin.FirebaseAdmin(app)

        try_initialize_app.assert_not_called()
        # whatever was passed as config should simply be forwarded to FirebaseAdmin
        assert admin.admin == admin_app
        # becauase we explicitly specificied FIREBASE_ADMIN_APP the following
        # config should be ignored/left unconfigured
        assert "FIREBASE_ADMIN_CREDENTIAL" not in app.config
        assert "FIREBASE_ADMIN_OPTIONS" not in app.config
        assert "FIREBASE_ADMIN_NAME" not in app.config
        assert "FIREBASE_ADMIN_RAISE_IF_APP_EXISTS" not in app.config
    def test_firebase_firebase_admin_credentials(
        self, app: AppFixture, try_initialize_app: TryInitializeAppFixture
    ):
        cred = "my-creds"
        options = "my-options"
        name = "my-name"
        raise_if_app_exists = False
        app.config["FIREBASE_ADMIN_CREDENTIAL"] = cred
        app.config["FIREBASE_ADMIN_OPTIONS"] = options
        app.config["FIREBASE_ADMIN_NAME"] = name
        app.config["FIREBASE_ADMIN_RAISE_IF_APP_EXISTS"] = raise_if_app_exists

        admin = flask_firebase_admin.FirebaseAdmin(app)

        assert admin.admin is try_initialize_app.return_value
        try_initialize_app.assert_called_once_with(
            cred, options, name, raise_if_app_exists
        )
    def test_init_with_init_app(
        self, app: AppFixture, try_initialize_app: TryInitializeAppFixture
    ):
        # create the extension
        admin = flask_firebase_admin.FirebaseAdmin()

        assert admin.app is None
        assert "FIREBASE_ADMIN_APP" not in app.config
        assert "FIREBASE_ADMIN_AUTHORIZATION_SCHEME" not in app.config
        assert "FIREBASE_ADMIN_CHECK_REVOKED" not in app.config
        assert "FIREBASE_ADMIN_PAYLOAD_ATTR" not in app.config
        assert "FIREBASE_ADMIN_CREDENTIAL" not in app.config
        assert "FIREBASE_ADMIN_OPTIONS" not in app.config
        assert "FIREBASE_ADMIN_NAME" not in app.config
        assert "FIREBASE_ADMIN_RAISE_IF_APP_EXISTS" not in app.config
        assert admin.app is None
        try_initialize_app.assert_not_called()

        with pytest.raises(AttributeError):
            admin.admin

        # initialize the extension
        admin.init_app(app)

        assert app.config["FIREBASE_ADMIN_APP"] is None
        assert app.config["FIREBASE_ADMIN_AUTHORIZATION_SCHEME"] == "Bearer"
        assert app.config["FIREBASE_ADMIN_CHECK_REVOKED"] is True
        assert app.config["FIREBASE_ADMIN_PAYLOAD_ATTR"] == "jwt_payload"
        assert app.config["FIREBASE_ADMIN_CREDENTIAL"] is None
        assert app.config["FIREBASE_ADMIN_OPTIONS"] is None
        assert app.config["FIREBASE_ADMIN_NAME"] == firebase_admin._DEFAULT_APP_NAME
        assert app.config["FIREBASE_ADMIN_RAISE_IF_APP_EXISTS"] is True
        try_initialize_app.assert_called_once_with(
            None, None, firebase_admin._DEFAULT_APP_NAME, True
        )
        assert admin.admin is not None