"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 randomTextGenerator_1 = __importDefault(require("../../../support/utils/randomTextGenerator"));
const utilities_1 = require("../../../support/utils/utilities");
describe('environments launch negative tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(2);
    const setup = setup_1.default.getSetup();
    let itAdminSession;
    let pa1Session;
    let project1Id;
    let project2Id;
    let researcherSession;
    let anonymousSession;
    const randomTextGenerator = new randomTextGenerator_1.default(setup.getSettings().get('runId'));
    const validLaunchParameters = {
        name: randomTextGenerator.getFakeText('name'),
        description: randomTextGenerator.getFakeText('description'),
        envTypeId: setup.getSettings().get('envTypeId'),
        envTypeConfigId: setup.getSettings().get('envTypeConfigId'),
        datasetIds: [],
        envType: setup.getSettings().get('envType')
    };
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        itAdminSession = paabResources.adminSession;
        pa1Session = paabResources.pa1Session;
        project1Id = paabResources.project1Id;
        project2Id = paabResources.project2Id;
        researcherSession = paabResources.rs1Session;
        anonymousSession = await setup.createAnonymousSession();
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    describe('403 error is thrown when', () => {
        test('IT Admin launch environment', async () => {
            try {
                await itAdminSession.resources.projects
                    .project(project1Id)
                    .environments()
                    .create(validLaunchParameters, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test('Project admin launch environment under project they are not associated with', async () => {
            try {
                await pa1Session.resources.projects
                    .project(project2Id)
                    .environments()
                    .create(validLaunchParameters, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test('Researcher launch environment under project they are not associated with', async () => {
            try {
                await researcherSession.resources.projects
                    .project(project2Id)
                    .environments()
                    .create(validLaunchParameters, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test('Unauthenticated user cannot launch environment', async () => {
            try {
                await anonymousSession.resources.projects
                    .project(project1Id)
                    .environments()
                    .create(validLaunchParameters, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
            }
        });
    });
    describe('missing parameters', () => {
        describe('ProjectAdmin tests', () => {
            test('name', async () => {
                try {
                    //eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    delete invalidParam.name;
                    await pa1Session.resources.projects.project(project1Id).environments().create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'name: Required'
                    }));
                }
            });
            test('envTypeId', async () => {
                try {
                    //eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    delete invalidParam.envTypeId;
                    await pa1Session.resources.projects.project(project1Id).environments().create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'envTypeId: Required'
                    }));
                }
            });
            test('envTypeConfigId', async () => {
                try {
                    //eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    delete invalidParam.envTypeConfigId;
                    await pa1Session.resources.projects.project(project1Id).environments().create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'envTypeConfigId: Required'
                    }));
                }
            });
            test('envType', async () => {
                try {
                    //eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    delete invalidParam.envType;
                    await pa1Session.resources.projects.project(project1Id).environments().create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'envType: Required'
                    }));
                }
            });
            test('description', async () => {
                try {
                    //eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    delete invalidParam.description;
                    await pa1Session.resources.projects.project(project1Id).environments().create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'description: Required'
                    }));
                }
            });
            test('all parameters', async () => {
                try {
                    await pa1Session.resources.projects.project(project1Id).environments().create({}, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'name: Required. description: Required. envTypeId: Required. envTypeConfigId: Required. envType: Required'
                    }));
                }
            });
            test('fails when trying to create with invalid environment type id format', async () => {
                try {
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    invalidParam.envTypeId = 'wrong-id-format';
                    await pa1Session.resources.projects.project(project1Id).environments().create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `envTypeId: Invalid ID`
                    }));
                }
            });
            test('fails when trying to create with invalid environment type config id format', async () => {
                try {
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    invalidParam.envTypeConfigId = 'wrong-id-format';
                    await pa1Session.resources.projects.project(project1Id).environments().create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `envTypeConfigId: Invalid ID`
                    }));
                }
            });
            test('error when project does not exist', async () => {
                const fakeProjectId = 'proj-12345678-1234-1234-1234-123456789012';
                try {
                    await pa1Session.resources.projects.project(fakeProjectId).environments().create({});
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                        error: 'User is not authorized'
                    }));
                }
            });
        });
        describe('Researcher tests', () => {
            test('name', async () => {
                try {
                    //eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    delete invalidParam.name;
                    await researcherSession.resources.projects
                        .project(project1Id)
                        .environments()
                        .create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'name: Required'
                    }));
                }
            });
            test('envTypeId', async () => {
                try {
                    //eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    delete invalidParam.envTypeId;
                    await researcherSession.resources.projects
                        .project(project1Id)
                        .environments()
                        .create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'envTypeId: Required'
                    }));
                }
            });
            test('envTypeConfigId', async () => {
                try {
                    //eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    delete invalidParam.envTypeConfigId;
                    await researcherSession.resources.projects
                        .project(project1Id)
                        .environments()
                        .create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'envTypeConfigId: Required'
                    }));
                }
            });
            test('envType', async () => {
                try {
                    //eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    delete invalidParam.envType;
                    await researcherSession.resources.projects
                        .project(project1Id)
                        .environments()
                        .create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'envType: Required'
                    }));
                }
            });
            test('description', async () => {
                try {
                    //eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    delete invalidParam.description;
                    await researcherSession.resources.projects
                        .project(project1Id)
                        .environments()
                        .create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'description: Required'
                    }));
                }
            });
            test('all parameters', async () => {
                try {
                    await researcherSession.resources.projects.project(project1Id).environments().create({}, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'name: Required. description: Required. envTypeId: Required. envTypeConfigId: Required. envType: Required'
                    }));
                }
            });
            test('fails when trying to create with invalid environment type id format', async () => {
                try {
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    invalidParam.envTypeId = 'wrong-id-format';
                    await researcherSession.resources.projects
                        .project(project1Id)
                        .environments()
                        .create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `envTypeId: Invalid ID`
                    }));
                }
            });
            test('fails when trying to create with invalid environment type config id format', async () => {
                try {
                    // eslint-disable-next-line @typescript-eslint/no-explicit-any
                    const invalidParam = Object.assign({}, validLaunchParameters);
                    invalidParam.envTypeConfigId = 'wrong-id-format';
                    await researcherSession.resources.projects
                        .project(project1Id)
                        .environments()
                        .create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `envTypeConfigId: Invalid ID`
                    }));
                }
            });
            test('error when project does not exist', async () => {
                const fakeProjectId = 'proj-12345678-1234-1234-1234-123456789012';
                try {
                    await researcherSession.resources.projects.project(fakeProjectId).environments().create({});
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                        error: 'User is not authorized'
                    }));
                }
            });
        });
    });
});
//# sourceMappingURL=launch.test.js.map