"use strict";
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 randomTextGenerator_1 = __importDefault(require("../../../support/utils/randomTextGenerator"));
const utilities_1 = require("../../../support/utils/utilities");
describe('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;
    let validCreateRequest;
    const randomTextGenerator = new randomTextGenerator_1.default(setup.getSettings().get('runId'));
    const unauthorizedHttpError = new HttpError_1.default(403, { error: 'User is not authorized' });
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        itAdminSession = paabResources.adminSession;
        pa1Session = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
        validCreateRequest = {
            name: randomTextGenerator.getFakeText('costCenterName'),
            description: randomTextGenerator.getFakeText('costCenterDescription'),
            accountId: setup.getSettings().get('defaultHostingAccountId')
        };
    });
    afterAll(async () => {
        await paabHelper.cleanup();
        await setup.cleanup();
    });
    describe('authorization test:', () => {
        test('ITAdmin can create Cost Center', async () => {
            const response = await itAdminSession.resources.costCenters.create(validCreateRequest);
            expect(response.status).toEqual(201);
        });
        test('ProjectAdmin cannot create CostCenter', async () => {
            await expect(pa1Session.resources.costCenters.create(validCreateRequest)).rejects.toThrow(unauthorizedHttpError);
        });
        test('Researcher cannot create CostCenter', async () => {
            await expect(researcherSession.resources.costCenters.create(validCreateRequest)).rejects.toThrow(unauthorizedHttpError);
        });
        test('Unauthenticated user cannot create CostCenter', async () => {
            await expect(anonymousSession.resources.costCenters.create(validCreateRequest)).rejects.toThrow(new HttpError_1.default(403, {}));
        });
    });
    describe('with invalid parameters', () => {
        describe('with missing name', () => {
            test('it throw 400 error', async () => {
                const invalidCreateRequest = {
                    description: validCreateRequest.description,
                    accountId: validCreateRequest.accountId
                };
                try {
                    await itAdminSession.resources.costCenters.create(invalidCreateRequest, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'name: Required'
                    }));
                }
            });
        });
        describe('with missing description', () => {
            test('it throw 400 error', async () => {
                const invalidCreateRequest = {
                    name: validCreateRequest.name,
                    accountId: validCreateRequest.accountId
                };
                try {
                    await itAdminSession.resources.costCenters.create(invalidCreateRequest, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'description: Required'
                    }));
                }
            });
        });
        describe('with missing accountId', () => {
            test('it throw 400 error', async () => {
                const invalidCreateRequest = {
                    name: validCreateRequest.name,
                    description: validCreateRequest.description
                };
                try {
                    await itAdminSession.resources.costCenters.create(invalidCreateRequest, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'accountId: Required'
                    }));
                }
            });
        });
        test('with invalid accountId', async () => {
            const invalidAccountIds = (0, utilities_1.generateInvalidIds)(workbench_core_base_1.resourceTypeToKey.account.toLowerCase());
            const invalidCreateRequests = invalidAccountIds.map((accountId) => ({
                accountId,
                name: validCreateRequest.name,
                description: validCreateRequest.description
            }));
            for (const invalidRequest of invalidCreateRequests) {
                try {
                    await itAdminSession.resources.costCenters.create(invalidRequest, false);
                }
                catch (error) {
                    (0, utilities_1.checkHttpError)(error, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `accountId: Invalid ID`
                    }));
                }
            }
        });
    });
});
//# sourceMappingURL=create.test.js.map