"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");
describe('update user negative tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let adminSession;
    let pa1Session;
    let rs1Session;
    let anonymousSession;
    let mockUserId;
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        ({ adminSession, pa1Session, rs1Session, anonymousSession } = await paabHelper.createResources(__filename));
        // create user
        const mockUserInput = {
            firstName: 'Test',
            lastName: 'User'
        };
        const { data } = await adminSession.resources.users.create(Object.assign(Object.assign({}, mockUserInput), { email: `success+activate-deactivate-user-${(0, uuid_1.v4)()}@simulator.amazonses.com` }));
        mockUserId = data.id;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    it('should return a 400 error when the provided email is not a valid email address', async () => {
        try {
            const updateMockUserInput = { email: 'notanemail' };
            const response = await adminSession.resources.users.user(mockUserId).update(updateMockUserInput, true);
            console.log(JSON.stringify(response));
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: 'email: Invalid Email'
            }));
        }
    });
    it('should return a 400 error when the provided names are not valid', async () => {
        try {
            const updateMockUserInput = { firstName: '!nv@lid Name' };
            const response = await adminSession.resources.users.user(mockUserId).update(updateMockUserInput, true);
            console.log(JSON.stringify(response));
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: 'firstName: must contain only letters, spaces, numbers, hyphens, and periods'
            }));
        }
        try {
            const updateMockUserInput = { lastName: '!nv@lid Name' };
            const response = await adminSession.resources.users.user(mockUserId).update(updateMockUserInput, true);
            console.log(JSON.stringify(response));
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: 'lastName: must contain only letters, spaces, numbers, hyphens, and periods'
            }));
        }
    });
    it('should return a 400 error when the provided status is unknown', async () => {
        try {
            const updateMockUserInput = { status: 'someInvalidStatus' };
            const response = await adminSession.resources.users.user(mockUserId).update(updateMockUserInput, true);
            console.log(JSON.stringify(response));
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: "status: Invalid enum value. Expected 'ACTIVE' | 'INACTIVE', received 'someInvalidStatus'"
            }));
        }
    });
    it('should return a 400 error when role is being updated', async () => {
        try {
            const updateMockUserInput = { roles: ['ITAdmin'] };
            const response = await adminSession.resources.users.user(mockUserId).update(updateMockUserInput, true);
            console.log(JSON.stringify(response));
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: ": Unrecognized key(s) in object: 'roles'"
            }));
        }
    });
    test('negative test ProjectAdmin: should return 403 error when update a user', async () => {
        let userId = '';
        const updateMockUserInput = { firstName: 'updatedFirstName', lastName: 'updatedLastName' };
        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).update(updateMockUserInput, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('negative test Researcher: should return 403 error when update a user', async () => {
        let userId = '';
        const updateMockUserInput = { firstName: 'updatedFirstName', lastName: 'updatedLastName' };
        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).update(updateMockUserInput, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('negative test unauthenticated user: should return 403 error when update a user', async () => {
        let userId = '';
        const updateMockUserInput = { firstName: 'updatedFirstName', lastName: 'updatedLastName' };
        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).update(updateMockUserInput, true);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
        }
    });
});
//# sourceMappingURL=updateUser.test.js.map