"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('create user negative tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let adminSession;
    let pa1Session;
    let rs1Session;
    let anonymousSession;
    let user;
    beforeEach(() => {
        expect.hasAssertions();
        user = {
            firstName: 'Test',
            lastName: 'User',
            email: `success+create-user-${(0, uuid_1.v4)()}@simulator.amazonses.com`
        };
    });
    beforeAll(async () => {
        ({ adminSession, pa1Session, rs1Session, anonymousSession } = await paabHelper.createResources(__filename));
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    it('ITAdmin should return a created user', async () => {
        const response = await adminSession.resources.users.create(user);
        expect(response.data).toMatchObject(Object.assign(Object.assign({}, user), { status: workbench_core_user_management_1.Status.ACTIVE, roles: [] }));
    });
    it('should return a 400 error when a user with the provided email already exists', async () => {
        // create user
        await adminSession.resources.users.create(user);
        try {
            //try to create it again
            await adminSession.resources.users.create(user);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(409, {
                error: 'Conflict',
                message: 'User with this email already exist.'
            }));
        }
    });
    it('should return a 400 error when the provided email is not a valid email address', async () => {
        try {
            await adminSession.resources.users.create(Object.assign(Object.assign({}, user), { email: 'notanemail' }));
        }
        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 email parameter is missing', async () => {
        const invalidParam = Object.assign({}, user);
        delete invalidParam.email;
        try {
            await adminSession.resources.users.create(invalidParam, false);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `email: Required`
            }));
        }
    });
    it('should return a 400 error when the email parameter is the wrong type', async () => {
        const invalidParam = Object.assign({}, user);
        invalidParam.email = 123;
        try {
            await adminSession.resources.users.create(invalidParam, false);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `email: Expected string, received number`
            }));
        }
    });
    it('should return a 400 error when the firstName parameter is missing', async () => {
        const invalidParam = Object.assign({}, user);
        delete invalidParam.firstName;
        try {
            await adminSession.resources.users.create(invalidParam, false);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `firstName: Required`
            }));
        }
    });
    it('should return a 400 error when the firstName parameter is empty', async () => {
        const invalidParam = Object.assign({}, user);
        invalidParam.firstName = '';
        try {
            await adminSession.resources.users.create(invalidParam, false);
        }
        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. firstName: Required'
            }));
        }
    });
    it('should return a 400 error when the firstName parameter is the wrong type', async () => {
        const invalidParam = Object.assign({}, user);
        invalidParam.firstName = 123;
        try {
            await adminSession.resources.users.create(invalidParam, false);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `firstName: Expected string, received number`
            }));
        }
    });
    it('should return a 400 error when the lastName parameter is missing', async () => {
        const invalidParam = Object.assign({}, user);
        delete invalidParam.lastName;
        try {
            await adminSession.resources.users.create(invalidParam, false);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `lastName: Required`
            }));
        }
    });
    it('should return a 400 error when the lastName parameter is empty', async () => {
        const invalidParam = Object.assign({}, user);
        invalidParam.lastName = '';
        try {
            await adminSession.resources.users.create(invalidParam, false);
        }
        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. lastName: Required'
            }));
        }
    });
    it('should return a 400 error when the lastName parameter is the wrong type', async () => {
        const invalidParam = Object.assign({}, user);
        invalidParam.lastName = 123;
        try {
            await adminSession.resources.users.create(invalidParam, false);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `lastName: Expected string, received number`
            }));
        }
    });
    it('ProjectAdmin: should return 403 error when try to create a user', async () => {
        try {
            await pa1Session.resources.users.create(user);
        }
        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 create a user', async () => {
        try {
            await rs1Session.resources.users.create(user);
        }
        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 create a user', async () => {
        try {
            await anonymousSession.resources.users.create(user);
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
        }
    });
});
//# sourceMappingURL=createUser.test.js.map