"use strict";
/*
 *  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  SPDX-License-Identifier: Apache-2.0
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const workbench_core_base_1 = require("@aws/workbench-core-base");
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 Cost Center negative tests', () => {
    const setup = setup_1.default.getSetup();
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let itAdminSession;
    let pa1Session;
    let researcherSession;
    let anonymousSession;
    const forbiddenHttpError = new HttpError_1.default(403, { error: 'User is not authorized' });
    const unauthorizedHttpError = new HttpError_1.default(401, {});
    beforeEach(async () => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        itAdminSession = paabResources.adminSession;
        pa1Session = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
        await setup.cleanup();
    });
    describe('authorization test:', () => {
        let costCenterId;
        beforeAll(async () => {
            const accountId = setup.getSettings().get('defaultHostingAccountId');
            const { data: costCenter } = await itAdminSession.resources.costCenters.create({
                accountId,
                name: 'costCenterIntegTest'
            });
            costCenterId = costCenter.id;
        });
        afterAll(async () => {
            await itAdminSession.resources.costCenters.costCenter(costCenterId).delete();
        });
        test('ITAdmin can list Cost Centers', async () => {
            const response = await itAdminSession.resources.costCenters.get();
            expect(response.status).toEqual(200);
        });
        test('ProjectAdmin cannot list Cost Centers', async () => {
            await expect(pa1Session.resources.costCenters.get()).rejects.toThrow(forbiddenHttpError);
        });
        test('Researcher cannot list Cost Centers', async () => {
            await expect(researcherSession.resources.costCenters.get()).rejects.toThrow(forbiddenHttpError);
        });
        test('Unauthenticated user cannot list Cost Centers', async () => {
            await expect(anonymousSession.resources.costCenters.get()).rejects.toThrow(unauthorizedHttpError);
        });
    });
    describe('with filter', () => {
        describe('with name that is empty', () => {
            beforeEach(async () => { });
            test('it throws 400 error', async () => {
                try {
                    await itAdminSession.resources.costCenters.get({
                        filter: { name: { eq: '' } }
                    });
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `filter.name.eq: ${workbench_core_base_1.nonEmptyMessage}`
                    }));
                }
            });
        });
    });
    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.costCenters.get(queryParams);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `Invalid Pagination Token: ${queryParams.paginationToken}`
                    }));
                }
            });
        });
    });
});
//# sourceMappingURL=list.test.js.map