"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('create environment type configs', () => {
    const paabHelper = new paabHelper_1.PaabHelper(1);
    const setup = setup_1.default.getSetup();
    const envTypeId = setup.getSettings().get('envTypeId');
    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 create without name', async () => {
            try {
                await itAdminSession.resources.environmentTypes.environmentType(envTypeId).configurations().create({
                    type: 'typeTest',
                    description: 'description',
                    params: []
                }, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: 'name: Required'
                }));
            }
        });
        test('fails when trying to create without description', async () => {
            try {
                await itAdminSession.resources.environmentTypes.environmentType(envTypeId).configurations().create({
                    type: 'typeTest',
                    name: 'name',
                    params: []
                }, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: 'description: Required'
                }));
            }
        });
        test('fails when trying to create without type', async () => {
            try {
                await itAdminSession.resources.environmentTypes.environmentType(envTypeId).configurations().create({
                    name: 'name',
                    description: 'description',
                    params: []
                }, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: 'type: Required'
                }));
            }
        });
        test('fails when trying to create without params', async () => {
            try {
                await itAdminSession.resources.environmentTypes.environmentType(envTypeId).configurations().create({
                    name: 'name',
                    description: 'description',
                    type: 'type'
                }, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: 'params: Required'
                }));
            }
        });
        test('fails when trying to create with invalid prop', async () => {
            try {
                await itAdminSession.resources.environmentTypes.environmentType(envTypeId).configurations().create({
                    type: 'typeTest',
                    description: 'description',
                    name: 'name',
                    invalidProp: 'invalidValue',
                    params: []
                }, false);
            }
            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 create with invalid environment Type Id', async () => {
            try {
                await itAdminSession.resources.environmentTypes
                    .environmentType('et-prod-0123456789012,pa-0123456789012')
                    .configurations()
                    .create({
                    type: 'typeTest',
                    description: 'description',
                    name: 'name',
                    params: []
                }, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: `Could not create environment type config because environment type et-prod-0123456789012,pa-0123456789012 does not exist`
                }));
            }
        });
        test('fails when trying to create with invalid environment Type id format', async () => {
            try {
                await itAdminSession.resources.environmentTypes
                    .environmentType('wrong-format-env-type-id')
                    .configurations()
                    .create({
                    type: 'typeTest',
                    description: 'description',
                    name: 'name',
                    params: []
                }, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: `envTypeId: Invalid ID`
                }));
            }
        });
    });
    describe('Project Admin tests', () => {
        test('unauthorized to create ETC', async () => {
            try {
                await paSession.resources.environmentTypes.environmentType(envTypeId).configurations().create({
                    name: 'this should fail',
                    type: 'typeTest',
                    description: 'description',
                    params: []
                }, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    describe('Researcher tests', () => {
        test('unauthorized to create ETC', async () => {
            try {
                await researcherSession.resources.environmentTypes.environmentType(envTypeId).configurations().create({
                    name: 'this should fail',
                    type: 'typeTest',
                    description: 'description',
                    params: []
                }, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    test('Unauthenticated user cannot create ETC', async () => {
        try {
            await anonymousSession.resources.environmentTypes.environmentType(envTypeId).configurations().create({
                name: 'this should fail',
                type: 'typeTest',
                description: 'description',
                params: []
            }, false);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
        }
    });
});
//# sourceMappingURL=create.test.js.map