"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const paabHelper_1 = require("../../../support/complex/paabHelper");
const setup_1 = __importDefault(require("../../../support/setup"));
const HttpError_1 = __importDefault(require("../../../support/utils/HttpError"));
const utilities_1 = require("../../../support/utils/utilities");
describe('list projects associated to environment type config', () => {
    const paabHelper = new paabHelper_1.PaabHelper(1);
    const setup = setup_1.default.getSetup();
    let adminSession;
    let paSession;
    let researcherSession;
    let anonymousSession;
    const envTypeId = setup.getSettings().get('envTypeId');
    const envTypeConfigId = setup.getSettings().get('envTypeConfigId');
    const nonExistentEnvTypeId = 'et-prod-0123456789012,pa-0123456789012';
    const nonExistentEnvTypeConfigId = 'etc-12345678-1234-1234-1234-123456789012';
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        adminSession = paabResources.adminSession;
        paSession = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
        await setup.cleanup();
    });
    test('list envTypeConfigProjects executes successfully', async () => {
        const { data: response } = await adminSession.resources.environmentTypes
            .environmentType(envTypeId)
            .configurations()
            .environmentTypeConfig(envTypeConfigId)
            .projects()
            .get({});
        expect(Array.isArray(response.data)).toBe(true);
    });
    test('Project Admin list projects by environments type config throws 403', async () => {
        try {
            await paSession.resources.environmentTypes
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .projects()
                .get();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('Researcher tests list projects by environments type config throws 403', async () => {
        try {
            await researcherSession.resources.environmentTypes
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .projects()
                .get();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('Unauthenticated user tests list projects by environments type config throws 401', async () => {
        try {
            await anonymousSession.resources.environmentTypes
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .projects()
                .get();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(401, {}));
        }
    });
    test('list envTypeConfigProjects fails when using invalid format environment type config Id', async () => {
        try {
            await adminSession.resources.environmentTypes
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig('invalid-config-id')
                .projects()
                .get({});
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `envTypeConfigId: Invalid ID`
            }));
        }
    });
    test('list envTypeConfigProjects fails when using non existing environment type config Id', async () => {
        try {
            await adminSession.resources.environmentTypes
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig(nonExistentEnvTypeConfigId)
                .projects()
                .get({});
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find environment type config ${nonExistentEnvTypeConfigId}`
            }));
        }
    });
    test('list projectEnvTypeConfigs fails when using invalid format envType Id', async () => {
        try {
            await adminSession.resources.environmentTypes
                .environmentType('invalid-env-type-id')
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .projects()
                .get({});
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `envTypeId: Invalid ID`
            }));
        }
    });
    test('list projectEnvTypeConfigs fails when using non existing envType Id', async () => {
        try {
            await adminSession.resources.environmentTypes
                .environmentType(nonExistentEnvTypeId)
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .projects()
                .get({});
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find environment type config ${envTypeConfigId}`
            }));
        }
    });
    describe('with invalid paginationToken', () => {
        const pagToken = '1';
        const queryParams = { paginationToken: pagToken };
        describe('as IT Admin', () => {
            test('it throws 400 error', async () => {
                try {
                    await adminSession.resources.environmentTypes
                        .environmentType(envTypeId)
                        .configurations()
                        .environmentTypeConfig(envTypeConfigId)
                        .projects()
                        .get(queryParams);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `Invalid Pagination Token: ${queryParams.paginationToken}`
                    }));
                }
            });
        });
    });
});
//# sourceMappingURL=listProjects.test.js.map