"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/*
 *  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  SPDX-License-Identifier: Apache-2.0
 */
const swb_app_1 = require("@aws/swb-app");
const projectStatus_1 = require("@aws/workbench-core-accounts/lib/constants/projectStatus");
const projectEnvService_1 = require("./projectEnvService");
describe('ProjectEnvService', () => {
    let projectEnvService;
    let mockWorkbenchEnvironmentService;
    let mockWorkbenchProjectService;
    let mockDynamicAuthZService;
    let mockUser;
    const fakeEnvId = 'env-fake-id';
    let mockEnv;
    let mockProject;
    const fakeProjectId = 'proj-fake-id';
    const fakeDate = '2021-02-26T22:42:16.652Z';
    beforeEach(() => {
        mockWorkbenchEnvironmentService = {};
        mockWorkbenchProjectService = {};
        mockDynamicAuthZService = {};
        mockUser = {
            id: '12345678-1234-1234-1234-123456789012',
            roles: []
        };
        projectEnvService = new projectEnvService_1.ProjectEnvService(mockDynamicAuthZService, mockWorkbenchEnvironmentService, mockWorkbenchProjectService);
        mockEnv = {
            id: fakeEnvId,
            cidr: '0.0.0.0/0',
            createdAt: '2022-05-13T20:03:54.055Z',
            description: 'test 123',
            envTypeConfigId: 'envTypeConfig-123',
            name: 'testEnv',
            owner: 'user-123',
            projectId: fakeProjectId,
            status: 'PENDING',
            updatedAt: '2022-05-13T20:03:54.055Z',
            instanceId: 'instance-123',
            provisionedProductId: ''
        };
        mockProject = {
            id: fakeProjectId,
            name: 'fakeProjectName',
            description: 'fakeProjectDescription',
            costCenterId: 'cc-123',
            status: projectStatus_1.ProjectStatus.AVAILABLE,
            createdAt: fakeDate,
            updatedAt: fakeDate,
            awsAccountId: 'awsacc-123',
            envMgmtRoleArn: 'fakeEnvMgmtRoleArn',
            hostingAccountHandlerRoleArn: 'fakeHostingAccountHandlerRoleArn',
            vpcId: 'vpc-123',
            subnetId: 'subnet-123',
            environmentInstanceFiles: '',
            encryptionKeyArn: 'fakeEncryptionKeyArn',
            externalId: 'workbench',
            accountId: 'acc-123'
        };
        mockWorkbenchProjectService.getProject = jest.fn().mockReturnValue(mockProject);
        mockDynamicAuthZService.createIdentityPermissions = jest.fn();
    });
    describe('createEnvironment', () => {
        const envIdentityPermissions = [];
        const envConnectionIdentityPermissions = [];
        const identityIds = [`${fakeProjectId}#ProjectAdmin`, `${fakeProjectId}#Researcher`];
        const identityType = 'GROUP';
        const subjectId = fakeEnvId;
        const envSubjectType = "SWB_ENVIRONMENT" /* SwbAuthZSubject.SWB_ENVIRONMENT */;
        const envConnectionSubjectType = "SWB_ENVIRONMENT_CONNECTION" /* SwbAuthZSubject.SWB_ENVIRONMENT_CONNECTION */;
        const allowEffect = 'ALLOW';
        const actions = ['READ', 'UPDATE', 'DELETE'];
        for (const identityId of identityIds) {
            envConnectionIdentityPermissions.push({
                action: 'READ',
                effect: allowEffect,
                identityId: identityId,
                identityType: identityType,
                subjectId: subjectId,
                subjectType: envConnectionSubjectType,
                conditions: {
                    projectId: { $eq: fakeProjectId }
                }
            });
            for (const action of actions) {
                envIdentityPermissions.push({
                    action: action,
                    effect: allowEffect,
                    identityId: identityId,
                    identityType: identityType,
                    subjectId: subjectId,
                    subjectType: envSubjectType,
                    conditions: {
                        projectId: { $eq: fakeProjectId }
                    }
                });
            }
        }
        const createEnvReq = {
            instanceId: 'inid-123',
            cidr: '0.0.0.0/0',
            description: 'test 123',
            name: 'testEnv',
            outputs: [],
            envTypeId: 'et-123',
            envTypeConfigId: 'etc-123',
            projectId: fakeProjectId,
            datasetIds: ['dataset-123'],
            status: 'PENDING'
        };
        test('create and return environment', async () => {
            mockWorkbenchEnvironmentService.createEnvironment = jest.fn().mockReturnValueOnce(mockEnv);
            const env = await projectEnvService.createEnvironment(createEnvReq, mockUser);
            expect(mockWorkbenchProjectService.getProject).toHaveBeenCalledWith({ projectId: fakeProjectId });
            expect(mockDynamicAuthZService.createIdentityPermissions).toHaveBeenCalledWith({
                authenticatedUser: mockUser,
                identityPermissions: envIdentityPermissions
            });
            expect(mockDynamicAuthZService.createIdentityPermissions).toHaveBeenCalledWith({
                authenticatedUser: mockUser,
                identityPermissions: envConnectionIdentityPermissions
            });
            expect(env).toEqual(mockEnv);
        });
        test('fails when project is deleted', async () => {
            mockWorkbenchProjectService.getProject = jest
                .fn()
                .mockReturnValue(Object.assign(Object.assign({}, mockProject), { status: projectStatus_1.ProjectStatus.DELETED }));
            await expect(projectEnvService.createEnvironment(createEnvReq, mockUser)).rejects.toThrowError(swb_app_1.ProjectDeletedError);
        });
    });
    describe('getEnvironment', () => {
        test('should call projectService and environmentService, and return environment', async () => {
            mockWorkbenchEnvironmentService.getEnvironment = jest.fn().mockReturnValueOnce(mockEnv);
            const env = await projectEnvService.getEnvironment(fakeProjectId, fakeEnvId);
            expect(mockWorkbenchProjectService.getProject).toHaveBeenCalledWith({ projectId: fakeProjectId });
            expect(mockWorkbenchEnvironmentService.getEnvironment).toHaveBeenCalledWith(fakeEnvId, false);
            expect(env).toEqual(mockEnv);
        });
    });
    describe('listProjectEnvs', () => {
        test('should call projectService and environmentService, and return list of environments', async () => {
            mockWorkbenchEnvironmentService.listEnvironmentsByProject = jest.fn().mockReturnValueOnce([mockEnv]);
            const envs = await projectEnvService.listProjectEnvs(fakeProjectId);
            expect(mockWorkbenchProjectService.getProject).toHaveBeenCalledWith({ projectId: fakeProjectId });
            expect(mockWorkbenchEnvironmentService.listEnvironmentsByProject).toHaveBeenLastCalledWith({
                projectId: fakeProjectId
            });
            expect(envs).toEqual([mockEnv]);
        });
    });
    describe('updateEnvironment', () => {
        test('should call projectService and environmentService, and return environment', async () => {
            mockWorkbenchEnvironmentService.updateEnvironment = jest.fn().mockReturnValueOnce(mockEnv);
            const env = await projectEnvService.updateEnvironment(fakeProjectId, fakeEnvId, {});
            expect(mockWorkbenchProjectService.getProject).toHaveBeenCalledWith({ projectId: fakeProjectId });
            expect(mockWorkbenchEnvironmentService.updateEnvironment).toHaveBeenCalledWith(fakeEnvId, {});
            expect(env).toEqual(mockEnv);
        });
    });
});
//# sourceMappingURL=projectEnvService.test.js.map