"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const tape_1 = __importDefault(require("tape-promise/tape"));
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,
});
tape_1.default(testCase, async (t) => {
    try {
        const jwtKeyPair = await jose_1.JWK.generate("RSA", 4096);
        const jwtPublicKey = jwtKeyPair.toPEM(false);
        const expressJwtOptions = {
            algorithms: ["RS256"],
            secret: jwtPublicKey,
            audience: uuid_1.v4(),
            issuer: uuid_1.v4(),
        };
        t.ok(expressJwtOptions, "Express JWT config truthy OK");
        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: uuid_1.v4(),
        });
        pluginRegistry.add(plugin);
        const configService = new public_api_1.ConfigService();
        const apiSrvOpts = 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.apiTlsEnabled = false;
        apiSrvOpts.plugins = [];
        const config = configService.newExampleConfigConvict(apiSrvOpts);
        const apiServer = new public_api_1.ApiServer({
            config: config.getProperties(),
            pluginRegistry,
        });
        tape_1.default.onFinish(async () => await apiServer.shutdown());
        const startResponse = apiServer.start();
        await t.doesNotReject(startResponse, "failed to start API server with dynamic plugin imports configured for it...");
        t.ok(startResponse, "startResponse truthy OK");
        const addressInfoApi = (await startResponse).addressInfoApi;
        const protocol = apiSrvOpts.apiTlsEnabled ? "https" : "http";
        const { address, port } = addressInfoApi;
        const apiHost = `${protocol}://${address}:${port}`;
        const req1 = {
            requestId: uuid_1.v4(),
        };
        // look Ma, no access token
        const res1 = await axios_1.default.request({
            data: req1,
            url: `${apiHost}${ep.getPath()}`,
            method: ep.getVerbLowerCase(),
        });
        t.ok(res1, "stub unprotected action response truthy OK");
        t.equal(res1.status, http_status_codes_1.StatusCodes.OK, "stub unprotected action response status === 200 OK");
        t.equal(typeof res1.data, "object", "typeof res1.data is 'object' OK");
        t.ok(typeof res1.data.data, "res1.data.data truthy OK");
        t.ok(typeof res1.data.data.reqBodyrequestId, "res1.data.data.reqBody truthy OK");
        t.ok(typeof res1.data.data.reqBody.requestId, "res1.data.data.reqBody.requestId truthy OK");
        t.equal(res1.data.data.reqBody.requestId, req1.requestId, "res1.data.requestId === req1.requestId OK");
    }
    catch (ex) {
        log.error(ex);
        t.fail("Exception thrown during test execution, see above for details!");
        throw ex;
    }
});
//# sourceMappingURL=data:application/json;base64, 