"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_user_management_1 = require("@aws/workbench-core-user-management");
const paabHelper_1 = require("../../../support/complex/paabHelper");
const HttpError_1 = __importDefault(require("../../../support/utils/HttpError"));
const utilities_1 = require("../../../support/utils/utilities");
describe('delete user negative tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let adminSession;
    let pa1Session;
    let rs1Session;
    let anonymousSession;
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        ({ adminSession, pa1Session, rs1Session, anonymousSession } = await paabHelper.createResources(__filename));
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    test('user does not exist', async () => {
        const fakeUserId = '00000000-0000-0000-0000-000000000000';
        try {
            await adminSession.resources.users.user(fakeUserId).purge();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find user ${fakeUserId}`
            }));
        }
    });
    test('active user cannot be deleted', async () => {
        let userId = '';
        try {
            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;
            await adminSession.resources.users.user(userId).purge();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `Could not delete user ${userId}. Expected status: ${workbench_core_user_management_1.Status[workbench_core_user_management_1.Status.INACTIVE]}; received: ${workbench_core_user_management_1.Status[workbench_core_user_management_1.Status.ACTIVE]}`
            }));
        }
    });
    test('ProjectAdmin: should return 403 error when delete a user', async () => {
        let userId = '';
        try {
            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;
            await pa1Session.resources.users.user(userId).purge();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('Researcher: should return 403 error when delete a user', async () => {
        let userId = '';
        try {
            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;
            await rs1Session.resources.users.user(userId).purge();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('Unauthenticated user: should return 403 error when delete a user', async () => {
        let userId = '';
        try {
            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;
            await anonymousSession.resources.users.user(userId).purge();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
        }
    });
});
//# sourceMappingURL=delete.test.js.map