"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('Update Project negative tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(2);
    let pa1Session;
    let rs1Session;
    let anonymousSession;
    const setup = setup_1.default.getSetup();
    let adminSession;
    let existingProject;
    const randomTextGenerator = new randomTextGenerator_1.default(setup.getSettings().get('runId'));
    let costCenterId;
    let project1Id;
    let project2Id;
    beforeAll(async () => {
        ({ adminSession, pa1Session, rs1Session, anonymousSession, project1Id, project2Id } =
            await paabHelper.createResources(__filename));
    });
    afterAll(async () => {
        await paabHelper.cleanup();
        await setup.cleanup();
    });
    beforeEach(async () => {
        expect.hasAssertions();
        const projectName = randomTextGenerator.getFakeText('test-project-name');
        const { data: costCenter } = await adminSession.resources.costCenters.create({
            name: 'projectIntegrationTestCostCenter',
            accountId: setup.getSettings().get('defaultHostingAccountId'),
            description: 'a test object'
        });
        costCenterId = costCenter.id;
        const { data: project } = await adminSession.resources.projects.create({
            name: projectName,
            description: 'Update Project negative tests--Project for TOP SECRET dragon research',
            costCenterId: costCenter.id
        });
        existingProject = project;
    });
    describe('updating name to be same as existing project', () => {
        let project;
        beforeEach(async () => {
            const { data: newProject } = await adminSession.resources.projects.create({
                name: (0, utilities_1.generateRandomString)(10, utilities_1.validSwbName),
                description: 'Update Project negative tests--Second Project',
                costCenterId: costCenterId
            });
            project = newProject;
        });
        test('it throws 400 error', async () => {
            try {
                await adminSession.resources.projects
                    .project(project.id)
                    .update({ name: existingProject.name }, true);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: `Project name "${existingProject.name}" is in use by a non deleted project. Please use another name.`
                }));
            }
        });
    });
    test('Project Admin passing in project it does not belong to gets 403', async () => {
        try {
            await pa1Session.resources.projects.project(project2Id).update({}, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('Researcher gets 403', async () => {
        try {
            await rs1Session.resources.projects.project(project1Id).update({}, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('unauthenticated user gets 403', async () => {
        try {
            await anonymousSession.resources.projects.project(project1Id).update({}, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
        }
    });
});
//# sourceMappingURL=update.test.js.map