"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
 *  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  SPDX-License-Identifier: Apache-2.0
 */
const workbench_core_base_1 = require("@aws/workbench-core-base");
const paabHelper_1 = require("../../../support/complex/paabHelper");
const HttpError_1 = __importDefault(require("../../../support/utils/HttpError"));
const utilities_1 = require("../../../support/utils/utilities");
describe('list environment types', () => {
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let itAdminSession;
    let paSession;
    let researcherSession;
    let anonymousSession;
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        itAdminSession = paabResources.adminSession;
        paSession = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    test('list environments types when filter and sorting by name', async () => {
        const { data: response } = await itAdminSession.resources.environmentTypes.get({
            filter: {
                name: { begins: 'Sage' }
            },
            sort: {
                name: 'desc'
            }
        });
        expect(Array.isArray(response.data)).toBe(true);
    });
    test('list environments types when filter and sorting by status', async () => {
        const { data: response } = await itAdminSession.resources.environmentTypes.get({
            filter: {
                status: { begins: 'NOT' }
            },
            sort: {
                status: 'desc'
            }
        });
        expect(Array.isArray(response.data)).toBe(true);
    });
    test('list environments types fails when filter by invalid prop', async () => {
        try {
            await itAdminSession.resources.environmentTypes.get({
                filter: {
                    someProperty: { begins: 'NOT' }
                }
            });
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: "filter: Unrecognized key(s) in object: 'someProperty'"
            }));
        }
    });
    test('list environments types fails when filter and sorting different props', async () => {
        try {
            await itAdminSession.resources.environmentTypes.get({
                filter: {
                    status: { begins: 'NOT' }
                },
                sort: {
                    name: 'desc'
                }
            });
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: 'Cannot apply a filter and sort to different properties at the same time'
            }));
        }
    });
    test('list environments types fails when filter by name exceeding length', async () => {
        try {
            await itAdminSession.resources.environmentTypes.get({
                filter: {
                    name: { begins: (0, utilities_1.generateRandomAlphaNumericString)(workbench_core_base_1.urlFilterMaxLength + 1) }
                }
            });
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `filter.name.begins: ${(0, workbench_core_base_1.lengthValidationMessage)(workbench_core_base_1.urlFilterMaxLength)}`
            }));
        }
    });
    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.get(queryParams);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `Invalid Pagination Token: ${queryParams.paginationToken}`
                    }));
                }
            });
        });
        const testBundle = [
            {
                username: 'projectAdmin',
                session: () => paSession
            },
            {
                username: 'researcher',
                session: () => researcherSession
            }
        ];
        describe.each(testBundle)('for each user', (testCase) => {
            const { username, session: sessionFunc } = testCase;
            let session;
            beforeEach(async () => {
                session = sessionFunc();
            });
            test(`it throws 400 error as ${username}`, async () => {
                try {
                    await session.resources.environmentTypes.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 ET`, async () => {
        try {
            await anonymousSession.resources.environmentTypes.get({});
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(401, {}));
        }
    });
});
//# sourceMappingURL=list.test.js.map