"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 uuid_1 = require("uuid");
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('multiStep user to project integration test', () => {
    const setup = setup_1.default.getSetup();
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let adminSession;
    let pa1Session;
    let project1Id;
    let costCenterId;
    beforeAll(async () => {
        ({ adminSession, pa1Session, project1Id } = await paabHelper.createResources(__filename));
        const { data: costCenter } = await adminSession.resources.costCenters.create({
            name: 'test-cost-center',
            accountId: setup.getSettings().get('defaultHostingAccountId'),
            description: 'a test object'
        });
        costCenterId = costCenter.id;
    });
    beforeEach(() => {
        expect.hasAssertions();
    });
    afterAll(async () => {
        await paabHelper.cleanup();
        await setup.cleanup();
    });
    describe('Happy path for ITAdmin', () => {
        test.each(['ProjectAdmin', 'Researcher'])('for role: %p', async (role) => {
            // create user
            console.log('ITAdmin creating a user...');
            const mockUserInput = {
                firstName: 'mockUser',
                lastName: role,
                email: `success+user-to-project-user-${(0, uuid_1.v4)()}@simulator.amazonses.com`
            };
            const { data: mockUser } = await adminSession.resources.users.create(mockUserInput);
            const mockUserId = mockUser.id;
            // assign user to project
            console.log(`ITAdmin assigning user ${mockUserId} to project ${project1Id}...`);
            await adminSession.resources.projects.project(project1Id).assignUserToProject(mockUserId, { role });
            // list users by role
            console.log(`ITAdmin listing user ${mockUserId} to project ${project1Id}...`);
            const usersByITAdminResponse = await adminSession.resources.projects
                .project(project1Id)
                .listUsersForProject(role);
            expect(usersByITAdminResponse.data.data).toContainEqual(expect.objectContaining({ id: mockUserId }));
            // remove mock user from project
            console.log(`Removing user ${mockUserId} from project ${project1Id}...`);
            await adminSession.resources.projects.project(project1Id).removeUserFromProject(mockUserId);
            // list users by role
            console.log(`retrieving user ${mockUserId} from project ${project1Id} should not return user`);
            const updatedUsersByITAdminResponse = await adminSession.resources.projects
                .project(project1Id)
                .listUsersForProject(role);
            expect(updatedUsersByITAdminResponse.data.data).not.toContainEqual(expect.objectContaining({ id: mockUserId }));
        });
    });
    describe('Happy path for ProjectAdmin', () => {
        test.each(['ProjectAdmin', 'Researcher'])('for role: %p', async (role) => {
            // create user by ITAdmin, not PA
            console.log('IT Admin creating a user...');
            const mockUserInput = {
                firstName: 'mockUser',
                lastName: role,
                email: `success+user-to-project-user-${(0, uuid_1.v4)()}@simulator.amazonses.com`
            };
            const { data: mockUser } = await adminSession.resources.users.create(mockUserInput);
            const mockUserId = mockUser.id;
            // assign user to project
            console.log(`Project Admin assigning user ${mockUserId} to project ${project1Id}...`);
            await pa1Session.resources.projects.project(project1Id).assignUserToProject(mockUserId, { role });
            // list users by role
            console.log(`Project Admin listing user ${mockUserId} to project ${project1Id}...`);
            const usersByPAResponse = await pa1Session.resources.projects
                .project(project1Id)
                .listUsersForProject(role);
            const usersByITAdminResponse = await adminSession.resources.projects
                .project(project1Id)
                .listUsersForProject(role);
            expect(usersByPAResponse.data.data).toEqual(usersByITAdminResponse.data.data);
            expect(usersByPAResponse.data.data).toContainEqual(expect.objectContaining({ id: mockUserId }));
            // remove users from project
            console.log(`Project Admin removing user ${mockUserId} from project ${project1Id}...`);
            await pa1Session.resources.projects.project(project1Id).removeUserFromProject(mockUserId);
            // list users by role
            const updatedUsersByPAResponse = await pa1Session.resources.projects
                .project(project1Id)
                .listUsersForProject(role);
            expect(updatedUsersByPAResponse.data.data).not.toContainEqual(expect.objectContaining({ id: mockUserId }));
        });
    });
    describe('negative tests', () => {
        test.each(['ProjectAdmin', 'Researcher'])('cannot list users with role "%p" for deleted project', async (role) => {
            console.log('Creating project...');
            const { data: createdProject } = await adminSession.resources.projects.create({
                name: `TestProject-${(0, uuid_1.v4)()}`,
                description: 'Project for negative integration tests',
                costCenterId
            });
            await adminSession.resources.projects.project(createdProject.id).delete();
            try {
                console.log(`Expecting failed attempt to list users for deleted project ${createdProject.id}`);
                await adminSession.resources.projects.project(createdProject.id).listUsersForProject(role);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                    error: 'Not Found',
                    message: `Could not find project ${createdProject.id}`
                }));
            }
        });
    });
});
//# sourceMappingURL=userProject.test.js.map