"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("jest-extended");
const uuid_1 = require("uuid");
const jose_1 = require("jose");
const axios_1 = __importDefault(require("axios"));
const http_status_codes_1 = require("http-status-codes");
const cactus_common_1 = require("@hyperledger/cactus-common");
const cactus_core_1 = require("@hyperledger/cactus-core");
const public_api_1 = require("../../../main/typescript/public-api");
const plugin_ledger_connector_stub_1 = require("../fixtures/plugin-ledger-connector-stub/plugin-ledger-connector-stub");
const unprotected_action_endpoint_1 = require("../fixtures/plugin-ledger-connector-stub/web-services/unprotected-action-endpoint");
const testCase = "API server enforces scope requirements on top of generic authz";
const logLevel = "TRACE";
const log = cactus_common_1.LoggerProvider.getOrCreate({
    level: logLevel,
    label: __filename,
});
describe(testCase, () => {
    let apiServer;
    afterAll(async () => {
        await apiServer.shutdown();
    });
    test(testCase, async () => {
        try {
            const jwtKeyPair = await (0, jose_1.generateKeyPair)("RS256", {
                modulusLength: 4096,
            });
            const jwtPublicKey = await (0, jose_1.exportSPKI)(jwtKeyPair.publicKey);
            const expressJwtOptions = {
                algorithms: ["RS256"],
                secret: jwtPublicKey,
                audience: (0, uuid_1.v4)(),
                issuer: (0, uuid_1.v4)(),
            };
            expect(expressJwtOptions).toBeTruthy();
            const ep = new unprotected_action_endpoint_1.UnprotectedActionEndpoint({
                connector: {},
                logLevel,
            });
            const authorizationConfig = {
                unprotectedEndpointExemptions: [ep.getPath()],
                expressJwtOptions,
                socketIoJwtOptions: { secret: jwtPublicKey },
            };
            const pluginRegistry = new cactus_core_1.PluginRegistry();
            const plugin = new plugin_ledger_connector_stub_1.PluginLedgerConnectorStub({
                logLevel,
                pluginRegistry,
                instanceId: (0, uuid_1.v4)(),
            });
            pluginRegistry.add(plugin);
            const configService = new public_api_1.ConfigService();
            const apiSrvOpts = await configService.newExampleConfig();
            apiSrvOpts.authorizationProtocol = public_api_1.AuthorizationProtocol.JSON_WEB_TOKEN;
            apiSrvOpts.authorizationConfigJson = authorizationConfig;
            apiSrvOpts.configFile = "";
            apiSrvOpts.apiCorsDomainCsv = "*";
            apiSrvOpts.apiPort = 0;
            apiSrvOpts.cockpitPort = 0;
            apiSrvOpts.grpcPort = 0;
            apiSrvOpts.crpcPort = 0;
            apiSrvOpts.apiTlsEnabled = false;
            apiSrvOpts.plugins = [];
            const config = await configService.newExampleConfigConvict(apiSrvOpts);
            apiServer = new public_api_1.ApiServer({
                config: config.getProperties(),
                pluginRegistry,
            });
            const startResponse = apiServer.start();
            await expect(startResponse).not.toReject();
            expect(startResponse).toBeTruthy();
            const addressInfoApi = (await startResponse).addressInfoApi;
            const protocol = apiSrvOpts.apiTlsEnabled ? "https" : "http";
            const { address, port } = addressInfoApi;
            const apiHost = `${protocol}://${address}:${port}`;
            const req1 = {
                requestId: (0, uuid_1.v4)(),
            };
            // look Ma, no access token
            const res1 = await axios_1.default.request({
                data: req1,
                url: `${apiHost}${ep.getPath()}`,
                method: ep.getVerbLowerCase(),
            });
            expect(res1).toBeTruthy();
            expect(res1.status).toBe(http_status_codes_1.StatusCodes.OK);
            expect(typeof res1.data).toBe("object");
            expect(typeof res1.data.data).toBeTruthy();
            expect(typeof res1.data.data.reqBodyrequestId).toBeTruthy();
            expect(typeof res1.data.data.reqBody.requestId).toBeTruthy();
            expect(res1.data.data.reqBody.requestId).toBe(req1.requestId);
        }
        catch (ex) {
            log.error(ex);
            fail("Exception thrown during test execution, see above for details!");
            throw ex;
        }
    });
});
//# sourceMappingURL=data:application/json;base64, 