"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
 *  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  SPDX-License-Identifier: Apache-2.0
 */
const workbench_core_base_1 = require("@aws/workbench-core-base");
const uuid_1 = require("uuid");
const paabHelper_1 = require("../../../support/complex/paabHelper");
const HttpError_1 = __importDefault(require("../../../support/utils/HttpError"));
const utilities_1 = require("../../../support/utils/utilities");
describe('assign user to project negative tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(2);
    let adminSession;
    let pa1Session;
    let rs1Session;
    let anonymousSession;
    let project1Id;
    let project2Id;
    const forbiddenHttpError = new HttpError_1.default(403, { error: 'User is not authorized' });
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        ({ adminSession, pa1Session, rs1Session, anonymousSession, project1Id, project2Id } =
            await paabHelper.createResources(__filename));
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    describe('missing parameters', () => {
        const userId = '00000000-0000-0000-0000-000000000000';
        const projectId = `${workbench_core_base_1.resourceTypeToKey.project.toLowerCase()}-00000000-0000-0000-0000-000000000000`;
        test('role', async () => {
            try {
                await adminSession.resources.projects.project(projectId).assignUserToProject(userId, {});
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: 'role: Required'
                }));
            }
        });
    });
    test('user does not exist', async () => {
        const fakeUserId = '00000000-0000-0000-0000-000000000000';
        const projectId = `${workbench_core_base_1.resourceTypeToKey.project.toLowerCase()}-00000000-0000-0000-0000-000000000000`;
        try {
            await adminSession.resources.projects
                .project(projectId)
                .assignUserToProject(fakeUserId, { role: 'Researcher' });
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find user ${fakeUserId}`
            }));
        }
    });
    test('admin user cannot be assigned to a project', async () => {
        let adminUserId = '';
        const projectId = `${workbench_core_base_1.resourceTypeToKey.project.toLowerCase()}-00000000-0000-0000-0000-000000000000`;
        try {
            adminUserId = adminSession.getUserId();
            await adminSession.resources.projects
                .project(projectId)
                .assignUserToProject(adminUserId !== null && adminUserId !== void 0 ? adminUserId : '', { role: 'Researcher' });
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `IT Admin ${adminUserId} cannot be assigned to the project ${projectId}`
            }));
        }
    });
    describe('user based tests', () => {
        let userId = '';
        beforeEach(async () => {
            const response = await adminSession.resources.users.create({
                firstName: 'Test',
                lastName: 'User',
                email: `success+user-${(0, uuid_1.v4)()}@simulator.amazonses.com`
            });
            const user = response.data;
            userId = user.id;
        });
        test('project does not exist', async () => {
            const projectId = `${workbench_core_base_1.resourceTypeToKey.project.toLowerCase()}-00000000-0000-0000-0000-000000000000`;
            try {
                await adminSession.resources.projects
                    .project(projectId)
                    .assignUserToProject(userId !== null && userId !== void 0 ? userId : '', { role: 'Researcher' });
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                    error: 'Not Found',
                    message: `Could not find project ${projectId}`
                }));
            }
        });
        test('cannot assign user to the same project', async () => {
            const response = await adminSession.resources.projects
                .project(project1Id)
                .assignUserToProject(userId !== null && userId !== void 0 ? userId : '', { role: 'Researcher' });
            expect(response.status).toBe(204);
            // expect subsequent call to fail
            try {
                await adminSession.resources.projects
                    .project(project1Id)
                    .assignUserToProject(userId !== null && userId !== void 0 ? userId : '', { role: 'Researcher' });
                // if we are here - expectation is not fulfilled
                console.error(`Assigning user ${userId} to the same project ${project1Id} did not cause error`);
                expect(false).toBeTruthy();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: `User ${userId} is already assigned to the project ${project1Id}`
                }));
            }
        });
        test('Project Admin passing in project it does not belong to gets 403', async () => {
            try {
                await pa1Session.resources.projects
                    .project(project2Id)
                    .assignUserToProject(userId !== null && userId !== void 0 ? userId : rs1Session.getUserId, { role: 'Researcher' });
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, forbiddenHttpError);
            }
        });
        test('Researcher gets 403', async () => {
            try {
                await rs1Session.resources.projects
                    .project(project1Id)
                    .assignUserToProject(userId !== null && userId !== void 0 ? userId : '', { role: 'Researcher' });
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, forbiddenHttpError);
            }
        });
        test('Unauthenticated user gets 403', async () => {
            try {
                await anonymousSession.resources.projects
                    .project(project1Id)
                    .assignUserToProject(userId !== null && userId !== void 0 ? userId : rs1Session.getUserId, { role: 'Researcher' });
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
            }
        });
    });
});
//# sourceMappingURL=assignUserToProject.test.js.map