"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 workbench_core_user_management_1 = require("@aws/workbench-core-user-management");
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");
const fakeUuid = '00000000-0000-0000-0000-000000000000';
const invalidUuid = '12345';
describe('userManagement activate/deactivate user integration test', () => {
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let adminSession;
    let pa1Session;
    let rs1Session;
    let anonymousSession;
    let userId;
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        ({ adminSession, pa1Session, rs1Session, anonymousSession } = await paabHelper.createResources(__filename));
        const { data } = await adminSession.resources.users.create({
            firstName: 'Test',
            lastName: 'User',
            email: `success+activate-deactivate-user-${(0, uuid_1.v4)()}@simulator.amazonses.com`
        });
        userId = data.id;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    it('should be able to activate an inactive user', async () => {
        await adminSession.resources.users.user(userId).deactivate();
        await adminSession.resources.users.user(userId).activate();
        const { data: activeUser } = await adminSession.resources.users.user(userId).get();
        expect(activeUser).toMatchObject({
            status: workbench_core_user_management_1.Status.ACTIVE
        });
    });
    it('should be able to deactivate an active user', async () => {
        await adminSession.resources.users.user(userId).activate();
        await adminSession.resources.users.user(userId).deactivate();
        const { data: inactiveUser } = await adminSession.resources.users.user(userId).get();
        expect(inactiveUser).toMatchObject({
            status: workbench_core_user_management_1.Status.INACTIVE
        });
    });
    it('should do nothing when a user is already active', async () => {
        await adminSession.resources.users.user(userId).activate();
        await adminSession.resources.users.user(userId).activate();
        const { data: activeUser } = await adminSession.resources.users.user(userId).get();
        expect(activeUser).toMatchObject({
            status: workbench_core_user_management_1.Status.ACTIVE
        });
    });
    it('should do nothing when a user is already inactive', async () => {
        await adminSession.resources.users.user(userId).deactivate();
        await adminSession.resources.users.user(userId).deactivate();
        const { data: activeUser } = await adminSession.resources.users.user(userId).get();
        expect(activeUser).toMatchObject({
            status: workbench_core_user_management_1.Status.INACTIVE
        });
    });
    it('should return a 404 error when activating a user that doesnt exist', async () => {
        try {
            await adminSession.resources.users.user(fakeUuid).activate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find user ${fakeUuid}`
            }));
        }
    });
    it('should return a 404 error when deactivating a user that doesnt exist', async () => {
        try {
            await adminSession.resources.users.user(fakeUuid).deactivate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find user ${fakeUuid}`
            }));
        }
    });
    it('should return an error when deactivating a user with an invalid UUID', async () => {
        try {
            await adminSession.resources.users.user(invalidUuid).deactivate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: 'userId: Invalid ID'
            }));
        }
    });
    describe('Unauthorized tests', () => {
        beforeEach(async () => {
            const users = await adminSession.resources.users.get();
            const user = users.data.users.data.find((user) => user.status === workbench_core_user_management_1.Status.ACTIVE);
            expect(user).toBeDefined();
            userId = user.id;
        });
        it('ProjectAdmin: should return 403 error when try to activate a user', async () => {
            try {
                await pa1Session.resources.users.user(userId).activate();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        it('Researcher: should return 403 error when try to activate a user', async () => {
            try {
                await rs1Session.resources.users.user(userId).activate();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        it('Unauthenticated user: should return 403 error when try to activate a user', async () => {
            try {
                await anonymousSession.resources.users.user(userId).activate();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
            }
        });
        it('ProjectAdmin: should return 403 error when try to deactivate a user', async () => {
            try {
                await pa1Session.resources.users.user(userId).deactivate();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        it('Researcher: should return 403 error when try to deactivate a user', async () => {
            try {
                await rs1Session.resources.users.user(userId).deactivate();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        it('Unauthenticated user: should return 403 error when try to deactivate a user', async () => {
            try {
                await anonymousSession.resources.users.user(userId).deactivate();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
            }
        });
    });
});
//# sourceMappingURL=activateDeactivateUser.test.js.map