"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 HttpError_1 = __importDefault(require("../../../support/utils/HttpError"));
const utilities_1 = require("../../../support/utils/utilities");
describe('environments connection negative tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(2);
    let itAdminSession;
    let paSession;
    let researcherSession;
    let anonymousSession;
    let project1Id;
    let project2Id;
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        itAdminSession = paabResources.adminSession;
        paSession = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
        project1Id = paabResources.project1Id;
        project2Id = paabResources.project2Id;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    describe('itAdmin tests', () => {
        test('environment does not exist', async () => {
            const fakeEnvId = (0, utilities_1.getFakeEnvId)();
            try {
                await itAdminSession.resources.projects
                    .project(project1Id)
                    .environments()
                    .environment(fakeEnvId)
                    .connect();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test('project does not exist', async () => {
            const fakeEnvId = (0, utilities_1.getFakeEnvId)();
            const fakeProjectId = 'proj-12345678-1234-1234-1234-123456789012';
            try {
                await itAdminSession.resources.projects
                    .project(fakeProjectId)
                    .environments()
                    .environment(fakeEnvId)
                    .connect();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    describe('projectAdmin tests', () => {
        test('environment does not exist', async () => {
            const fakeEnvId = (0, utilities_1.getFakeEnvId)();
            try {
                await paSession.resources.projects
                    .project(project1Id)
                    .environments()
                    .environment(fakeEnvId)
                    .connect();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test('project does not exist', async () => {
            const fakeEnvId = (0, utilities_1.getFakeEnvId)();
            const fakeProjectId = 'proj-12345678-1234-1234-1234-123456789012';
            try {
                await paSession.resources.projects
                    .project(fakeProjectId)
                    .environments()
                    .environment(fakeEnvId)
                    .connect();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test('projectAdmin not assigned to project', async () => {
            const fakeEnvId = (0, utilities_1.getFakeEnvId)();
            try {
                await paSession.resources.projects
                    .project(project2Id)
                    .environments()
                    .environment(fakeEnvId)
                    .connect();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    describe('researcher tests', () => {
        test('environment does not exist', async () => {
            const fakeEnvId = (0, utilities_1.getFakeEnvId)();
            try {
                await researcherSession.resources.projects
                    .project(project1Id)
                    .environments()
                    .environment(fakeEnvId)
                    .connect();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test('project does not exist', async () => {
            const fakeEnvId = (0, utilities_1.getFakeEnvId)();
            const fakeProjectId = 'proj-12345678-1234-1234-1234-123456789012';
            try {
                await researcherSession.resources.projects
                    .project(fakeProjectId)
                    .environments()
                    .environment(fakeEnvId)
                    .connect();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test('researcher not assigned to project', async () => {
            const fakeEnvId = (0, utilities_1.getFakeEnvId)();
            try {
                await researcherSession.resources.projects
                    .project(project2Id)
                    .environments()
                    .environment(fakeEnvId)
                    .connect();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    test('Unauthenticated user cannot connect to environment', async () => {
        const fakeEnvId = (0, utilities_1.getFakeEnvId)();
        try {
            await anonymousSession.resources.projects
                .project(project1Id)
                .environments()
                .environment(fakeEnvId)
                .connect();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(401, {}));
        }
    });
});
//# sourceMappingURL=connections.test.js.map