"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 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('Create Project negative tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let pa1Session;
    let rs1Session;
    let anonymousSession;
    const setup = setup_1.default.getSetup();
    let adminSession;
    let costCenterId;
    let existingProjectName;
    const randomTextGenerator = new randomTextGenerator_1.default(setup.getSettings().get('runId'));
    let createRequest;
    const forbiddenHttpError = new HttpError_1.default(403, { error: 'User is not authorized' });
    beforeAll(async () => {
        ({ adminSession, pa1Session, rs1Session, anonymousSession } = await paabHelper.createResources(__filename));
    });
    afterAll(async () => {
        await paabHelper.cleanup();
        await setup.cleanup();
    });
    beforeEach(async () => {
        expect.hasAssertions();
        const { data: costCenter } = await adminSession.resources.costCenters.create({
            name: (0, utilities_1.generateRandomString)(10, utilities_1.validSwbName),
            accountId: setup.getSettings().get('defaultHostingAccountId'),
            description: (0, utilities_1.generateRandomString)(10, utilities_1.validSwbDescription)
        });
        costCenterId = costCenter.id;
        createRequest = {
            name: 'validName',
            description: 'valid description',
            costCenterId
        };
    });
    describe('with a name', () => {
        describe('that belongs to an existing project', () => {
            beforeEach(async () => {
                existingProjectName = (0, utilities_1.generateRandomString)(10, utilities_1.validSwbName);
                await adminSession.resources.projects.create({
                    name: existingProjectName,
                    description: 'Create Project negative tests--Project for TOP SECRET dragon research',
                    costCenterId
                });
                createRequest.name = existingProjectName;
            });
            test('it throws 400 error', async () => {
                try {
                    await adminSession.resources.projects.create(createRequest);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `Project name "${existingProjectName}" is in use by a non deleted project. Please use another name.`
                    }));
                }
            });
        });
        describe('that is missing', () => {
            beforeEach(async () => {
                delete createRequest.name;
            });
            test('it throws 400 error', async () => {
                try {
                    await adminSession.resources.projects.create(createRequest);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'name: Required'
                    }));
                }
            });
        });
    });
    describe('with a description', () => {
        describe('that is missing', () => {
            beforeEach(async () => {
                delete createRequest.description;
            });
            test('it throws 400 error', async () => {
                try {
                    await adminSession.resources.projects.create(createRequest);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'description: Required'
                    }));
                }
            });
        });
    });
    describe('with a cost center', () => {
        describe('that does not exist', () => {
            beforeEach(async () => {
                createRequest.costCenterId = 'cc-1234abcd-1234-abcd-1234-abcd1234abcd';
            });
            test('it throws 400 error', async () => {
                try {
                    await adminSession.resources.projects.create(createRequest);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `Could not find cost center cc-1234abcd-1234-abcd-1234-abcd1234abcd`
                    }));
                }
            });
        });
        describe('that is missing', () => {
            beforeEach(async () => {
                delete createRequest.costCenterId;
            });
            test('it throws 400 error', async () => {
                try {
                    await adminSession.resources.projects.create(createRequest);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'costCenterId: Required'
                    }));
                }
            });
        });
        describe('that was deleted', () => {
            beforeEach(async () => {
                const { data: costCenter } = await adminSession.resources.costCenters.create({
                    name: (0, utilities_1.generateRandomString)(10, utilities_1.validSwbName),
                    accountId: setup.getSettings().get('defaultHostingAccountId'),
                    description: (0, utilities_1.generateRandomString)(10, utilities_1.validSwbDescription)
                });
                costCenterId = costCenter.id;
                await adminSession.resources.costCenters.costCenter(costCenterId).delete();
            });
            test('it throws 400 error', async () => {
                try {
                    existingProjectName = randomTextGenerator.getFakeText('test-project-name');
                    await adminSession.resources.projects.create({
                        name: existingProjectName,
                        description: 'Project for TOP SECRET dragon research',
                        costCenterId
                    });
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `Cost center ${costCenterId} was deleted`
                    }));
                }
            });
            test('Project Admin gets 403', async () => {
                try {
                    await pa1Session.resources.projects.create();
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, forbiddenHttpError);
                }
            });
            test('Researcher gets 403', async () => {
                try {
                    await rs1Session.resources.projects.create();
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, forbiddenHttpError);
                }
            });
            test('Unauthenticated user gets 403', async () => {
                try {
                    await anonymousSession.resources.projects.create();
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
                }
            });
        });
    });
});
//# sourceMappingURL=create.test.js.map