"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const permissions_1 = __importDefault(require("../../../test/fixtures/permissions"));
const test_config_1 = require("../../../test/config/test-config");
const store_1 = __importDefault(require("../../../test/fixtures/store"));
const services_1 = require("../../services");
const app_1 = __importDefault(require("../../app"));
const supertest_1 = __importDefault(require("supertest"));
const date_fns_1 = require("date-fns");
async function getSetup(adminTokenKillSwitchEnabled) {
    const base = `/random${Math.round(Math.random() * 1000)}`;
    const perms = (0, permissions_1.default)();
    const config = (0, test_config_1.createTestConfig)({
        preHook: perms.hook,
        server: { baseUriPath: base },
        experimental: {
            flags: {
                adminTokenKillSwitch: adminTokenKillSwitchEnabled,
            },
        },
        //@ts-ignore - Just testing, so only need the isEnabled call here
    });
    const stores = (0, store_1.default)();
    await stores.environmentStore.create({
        name: 'development',
        type: 'development',
        enabled: true,
    });
    const services = (0, services_1.createServices)(stores, config);
    const app = await (0, app_1.default)(config, stores, services);
    return {
        base,
        request: (0, supertest_1.default)(app),
    };
}
describe('Admin token killswitch', () => {
    test('If killswitch is off we can still create admin tokens', async () => {
        const setup = await getSetup(false);
        return setup.request
            .post(`${setup.base}/api/admin/api-tokens`)
            .set('Content-Type', 'application/json')
            .send({
            expiresAt: (0, date_fns_1.addDays)(new Date(), 60),
            type: 'ADMIN',
            tokenName: 'Non killswitched',
        })
            .expect(201)
            .expect((res) => {
            expect(res.body.secret).toBeTruthy();
        });
    });
    test('If killswitch is on we will get an operation denied if we try to create an admin token', async () => {
        const setup = await getSetup(true);
        return setup.request
            .post(`${setup.base}/api/admin/api-tokens`)
            .set('Content-Type', 'application/json')
            .send({
            expiresAt: (0, date_fns_1.addDays)(new Date(), 60),
            type: 'ADMIN',
            tokenName: 'Killswitched',
        })
            .expect(403)
            .expect((res) => {
            expect(res.body.message).toBe('Admin tokens are disabled in this instance. Use a Service account or a PAT to access admin operations instead');
        });
    });
    test('If killswitch is on we can still create a client token', async () => {
        const setup = await getSetup(true);
        return setup.request
            .post(`${setup.base}/api/admin/api-tokens`)
            .set('Content-Type', 'application/json')
            .send({
            expiresAt: (0, date_fns_1.addDays)(new Date(), 60),
            type: 'CLIENT',
            environment: 'development',
            projects: ['*'],
            tokenName: 'Client',
        })
            .expect(201)
            .expect((res) => {
            expect(res.body.secret).toBeTruthy();
        });
    });
    test('If killswitch is on we can still create a frontend token', async () => {
        const setup = await getSetup(true);
        return setup.request
            .post(`${setup.base}/api/admin/api-tokens`)
            .set('Content-Type', 'application/json')
            .send({
            expiresAt: (0, date_fns_1.addDays)(new Date(), 60),
            type: 'FRONTEND',
            environment: 'development',
            projects: ['*'],
            tokenName: 'Frontend',
        })
            .expect(201)
            .expect((res) => {
            expect(res.body.secret).toBeTruthy();
        });
    });
});
//# sourceMappingURL=api-token.test.js.map