"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('update environment types', () => {
    let adminSession;
    let paSession;
    let researcherSession;
    let anonymousSession;
    const paabHelper = new paabHelper_1.PaabHelper(1);
    const testEnvTypeId = 'et-prod-0123456789012,pa-0123456789012';
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        adminSession = paabResources.adminSession;
        paSession = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    test('fails when trying to update environment type as projectAdmin', async () => {
        try {
            await paSession.resources.environmentTypes.environmentType(testEnvTypeId).update({
                name: 'updated_name'
            }, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('fails when trying to update environment type as researcher', async () => {
        try {
            await researcherSession.resources.environmentTypes.environmentType(testEnvTypeId).update({
                name: 'updated_name'
            }, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('fails when trying to update invalid prop', async () => {
        try {
            await adminSession.resources.environmentTypes.environmentType(testEnvTypeId).update({
                invalidProp: 'invalidValue'
            }, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: ": Unrecognized key(s) in object: 'invalidProp'"
            }));
        }
    });
    test('fails when trying to update invalid environment Type', async () => {
        try {
            await adminSession.resources.environmentTypes.environmentType(testEnvTypeId).update({
                name: 'new-Name',
                status: 'APPROVED'
            }, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find environment type ${testEnvTypeId} to update`
            }));
        }
    });
    test('fails when trying to update invalid environment Type id format', async () => {
        try {
            await adminSession.resources.environmentTypes.environmentType('wrong-format-id').update({
                name: 'new_Name'
            }, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: 'envTypeId: Invalid ID'
            }));
        }
    });
    test('fails when trying to update name surpassing length', async () => {
        try {
            await adminSession.resources.environmentTypes.environmentType(testEnvTypeId).update({
                name: (0, utilities_1.generateRandomAlphaNumericString)(workbench_core_base_1.swbNameMaxLength + 1)
            }, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `name: ${(0, workbench_core_base_1.lengthValidationMessage)(workbench_core_base_1.swbNameMaxLength)}`
            }));
        }
    });
    test('fails when trying to update description surpassing length', async () => {
        try {
            await adminSession.resources.environmentTypes.environmentType(testEnvTypeId).update({
                description: (0, utilities_1.generateRandomAlphaNumericString)(workbench_core_base_1.swbDescriptionMaxLength + 1)
            }, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `description: ${(0, workbench_core_base_1.lengthValidationMessage)(workbench_core_base_1.swbDescriptionMaxLength)}`
            }));
        }
    });
    test(`Unauthenticated user cannot call update ET`, async () => {
        try {
            await anonymousSession.resources.environmentTypes.environmentType(testEnvTypeId).update({
                name: 'updated_name'
            }, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
        }
    });
});
//# sourceMappingURL=update.test.js.map