"use strict";
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 utilities_1 = require("../../../support/utils/utilities");
describe('update environment type configs', () => {
    const paabHelper = new paabHelper_1.PaabHelper(1);
    const setup = setup_1.default.getSetup();
    const envTypeId = setup.getSettings().get('envTypeId');
    const envTypeConfigId = setup.getSettings().get('envTypeConfigId');
    let itAdminSession;
    let paSession;
    let researcherSession;
    let anonymousSession;
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        itAdminSession = paabResources.adminSession;
        paSession = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
        await setup.cleanup();
    });
    describe('ITAdmin tests', () => {
        test('fails when trying to update invalid prop', async () => {
            try {
                await itAdminSession.resources.environmentTypes
                    .environmentType(envTypeId)
                    .configurations()
                    .environmentTypeConfig(envTypeConfigId)
                    .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 Config', async () => {
            try {
                await itAdminSession.resources.environmentTypes
                    .environmentType(envTypeId)
                    .configurations()
                    .environmentTypeConfig('etc-12345678-1234-1234-1234-123456789012')
                    .update({
                    description: 'new Description'
                }, true);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                    error: 'Not Found',
                    message: `Could not find envType ${envTypeId} with envTypeConfig etc-12345678-1234-1234-1234-123456789012 to update`
                }));
            }
        });
        test('fails when trying to update invalid environment type id format', async () => {
            try {
                await itAdminSession.resources.environmentTypes
                    .environmentType(envTypeId)
                    .configurations()
                    .environmentTypeConfig('wrong-format-id')
                    .update({
                    description: 'new Description'
                }, true);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: `envTypeConfigId: Invalid ID`
                }));
            }
        });
    });
    describe('Project Admin tests', () => {
        test('unauthorized to call UpdateEnvironmentTypeConfig', async () => {
            try {
                await paSession.resources.environmentTypes
                    .environmentType(envTypeId)
                    .configurations()
                    .environmentTypeConfig('etc-12345678-1234-1234-1234-123456789012')
                    .update({
                    description: 'new Description'
                }, true);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    describe('Researcher tests', () => {
        test('unauthorized to call UpdateEnvironmentTypeConfig', async () => {
            try {
                await researcherSession.resources.environmentTypes
                    .environmentType(envTypeId)
                    .configurations()
                    .environmentTypeConfig('etc-12345678-1234-1234-1234-123456789012')
                    .update({
                    description: 'new Description'
                }, true);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    test('Unauthenticated user cannot call UpdateEnvironmentTypeConfig', async () => {
        try {
            await anonymousSession.resources.environmentTypes
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig('etc-12345678-1234-1234-1234-123456789012')
                .update({
                description: 'new Description'
            }, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
        }
    });
});
//# sourceMappingURL=update.test.js.map