"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', () => {
    const setup = setup_1.default.getSetup();
    const envTypeId = setup.getSettings().get('envTypeId');
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let itAdminSession;
    let paSession;
    let researcherSession;
    let anonymousSession;
    let projectId;
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        itAdminSession = paabResources.adminSession;
        paSession = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
        projectId = paabResources.project1Id;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
        await setup.cleanup();
    });
    describe('ITAdmin tests', () => {
        test('list environments type configs executes successfully', async () => {
            const { data: response } = await itAdminSession.resources.environmentTypes
                .environmentType(envTypeId)
                .configurations()
                .get({});
            expect(Array.isArray(response.data)).toBe(true);
        });
    });
    describe('Project Admin tests', () => {
        test('list environments type configs for project excecutes successfully', async () => {
            const { data: response } = await paSession.resources.projects
                .project(projectId)
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .get({});
            expect(Array.isArray(response.data)).toBe(true);
        });
        test('list environments type configs (non-project route) fails', async () => {
            try {
                await paSession.resources.environmentTypes.environmentType(envTypeId).configurations().get({});
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    describe('Researcher tests', () => {
        test('list environments type configs for project excecutes successfully', async () => {
            const { data: response } = await researcherSession.resources.projects
                .project(projectId)
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .get({});
            expect(Array.isArray(response.data)).toBe(true);
        });
        test('list environments type configs (non-project route) fails', async () => {
            try {
                await researcherSession.resources.environmentTypes
                    .environmentType(envTypeId)
                    .configurations()
                    .get({});
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    describe('with invalid paginationToken', () => {
        const pagToken = '1';
        const queryParams = { paginationToken: pagToken };
        describe('as IT Admin', () => {
            test('it throws 400 error', async () => {
                try {
                    await itAdminSession.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}`
                    }));
                }
            });
        });
    });
    test('Unauthenticated user cannot call list environments type configs (non-project route)', async () => {
        try {
            await anonymousSession.resources.environmentTypes.environmentType(envTypeId).configurations().get({});
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(401, {}));
        }
    });
});
//# sourceMappingURL=list.test.js.map