"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 environment type configs associated to project', () => {
    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 nonExistentProjectId = 'proj-12345678-1234-1234-1234-123456789012';
    const nonExistentEnvTypeId = 'et-prod-0123456789012,pa-0123456789012';
    let projectId;
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        adminSession = paabResources.adminSession;
        paSession = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
        projectId = paabResources.project1Id;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    test('list projectEnvTypeConfigs executes successfully', async () => {
        const { data: response } = await adminSession.resources.projects
            .project(projectId)
            .environmentTypes()
            .environmentType(envTypeId)
            .configurations()
            .get({});
        expect(Array.isArray(response.data)).toBe(true);
    });
    test('list projectEnvTypeConfigs fails when using invalid format project Id', async () => {
        try {
            await adminSession.resources.projects
                .project('invalid-project-id')
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .get({});
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `projectId: Invalid ID`
            }));
        }
    });
    test('list projectEnvTypeConfigs fails when using non existing project Id', async () => {
        try {
            await adminSession.resources.projects
                .project(nonExistentProjectId)
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .get({});
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find project ${nonExistentProjectId}`
            }));
        }
    });
    test('list projectEnvTypeConfigs fails when using invalid format envType Id', async () => {
        try {
            await adminSession.resources.projects
                .project(projectId)
                .environmentTypes()
                .environmentType('invalid-envType-id')
                .configurations()
                .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.projects
                .project(projectId)
                .environmentTypes()
                .environmentType(nonExistentEnvTypeId)
                .configurations()
                .get({});
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find environment type ${nonExistentEnvTypeId}`
            }));
        }
    });
    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()
                        .get(queryParams);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `Invalid Pagination Token: ${queryParams.paginationToken}`
                    }));
                }
            });
        });
        // PA & Researcher must be through project route
        const testBundle = [
            {
                username: 'projectAdmin',
                session: () => paSession,
                projectId: () => projectId
            },
            {
                username: 'researcher',
                session: () => researcherSession,
                projectId: () => projectId
            }
        ];
        describe.each(testBundle)('for each user', (testCase) => {
            const { username, session: sessionFunc, projectId: projectFunc } = testCase;
            let session;
            let projectId;
            beforeEach(async () => {
                session = sessionFunc();
                projectId = projectFunc();
            });
            test(`it throws 400 error as ${username}`, async () => {
                try {
                    await session.resources.projects
                        .project(projectId)
                        .environmentTypes()
                        .environmentType(envTypeId)
                        .configurations()
                        .get(queryParams);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `Invalid Pagination Token: ${queryParams.paginationToken}`
                    }));
                }
            });
        });
    });
    describe('Researcher test', () => {
        test('cannot list ETCs for project where researcher is not a part of the project', async () => {
            try {
                await researcherSession.resources.projects
                    .project('proj-30a71a7a-f450-4188-941c-de1482e4dd92')
                    .environmentTypes()
                    .environmentType(envTypeId)
                    .configurations()
                    .get();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    describe('Project Admin test', () => {
        test('cannot list ETCs for project where Project Admin is not a part of the project', async () => {
            try {
                await paSession.resources.projects
                    .project('proj-30a71a7a-f450-4188-941c-de1482e4dd92')
                    .environmentTypes()
                    .environmentType(envTypeId)
                    .configurations()
                    .get();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    test('Unauthenticated user cannot list ETCs', async () => {
        try {
            await anonymousSession.resources.projects
                .project('proj-30a71a7a-f450-4188-941c-de1482e4dd92')
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .get();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(401, {}));
        }
    });
});
//# sourceMappingURL=list.test.js.map