"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_base_1 = require("@aws/workbench-core-base");
const lodash_1 = __importDefault(require("lodash"));
const accountHelper_1 = require("../../../support/complex/accountHelper");
const paabHelper_1 = require("../../../support/complex/paabHelper");
const setup_1 = __importDefault(require("../../../support/setup"));
const HttpError_1 = __importDefault(require("../../../support/utils/HttpError"));
const randomTextGenerator_1 = __importDefault(require("../../../support/utils/randomTextGenerator"));
const utilities_1 = require("../../../support/utils/utilities");
describe('awsAccounts create negative tests', () => {
    const setup = setup_1.default.getSetup();
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let adminSession;
    const randomTextGenerator = new randomTextGenerator_1.default(setup.getSettings().get('runId'));
    let paSession;
    let researcherSession;
    let anonymousSession;
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        adminSession = paabResources.adminSession;
        paSession = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
        await setup.cleanup();
    });
    const validLaunchParameters = {
        awsAccountId: '123456789012',
        envMgmtRoleArn: `arn:aws:iam::123456789012:role/${randomTextGenerator.getFakeText('fakeEnvMgmtRoleArn')}-env-mgmt`,
        hostingAccountHandlerRoleArn: `arn:aws:iam::123456789012:role/${randomTextGenerator.getFakeText('fakeHostingAccountHandlerRoleArn')}-hosting-account-role`,
        name: randomTextGenerator.getFakeText('fakeName'),
        externalId: randomTextGenerator.getFakeText('fakeExternalId')
    };
    describe('when creating an account', () => {
        describe('and the creation params are invalid', () => {
            test('with empty body it throws a validation error', async () => {
                try {
                    await adminSession.resources.accounts.create({}, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'name: Required. awsAccountId: Required. envMgmtRoleArn: Required. hostingAccountHandlerRoleArn: Required. externalId: Required'
                    }));
                }
            });
            test('with accountId that is too long it throws a validation error', async () => {
                try {
                    const body = Object.assign({}, validLaunchParameters);
                    body.awsAccountId = '123456789012345';
                    await adminSession.resources.accounts.create(body, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'awsAccountId: must be a 12 digit number'
                    }));
                }
            });
            test('with hostingAccountHandlerRoleArn that is too long it throws a validation error', async () => {
                try {
                    const body = Object.assign({}, validLaunchParameters);
                    body.hostingAccountHandlerRoleArn =
                        'arn:aws:iam::123456789012:role/message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-abcde-hosting-account-role';
                    await adminSession.resources.accounts.create(body, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `hostingAccountHandlerRoleArn: ${(0, workbench_core_base_1.lengthValidationMessage)(400)}`
                    }));
                }
            });
            test('with envMgmtRoleArn that is too long it throws a validation error', async () => {
                try {
                    const body = Object.assign({}, validLaunchParameters);
                    body.envMgmtRoleArn =
                        'arn:aws:iam::123456789012:role/message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-abcdefghijklmnopq-env-mgmt';
                    await adminSession.resources.accounts.create(body, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `envMgmtRoleArn: ${(0, workbench_core_base_1.lengthValidationMessage)(400)}`
                    }));
                }
            });
            test('with name that is too long it throws a validation error', async () => {
                try {
                    const body = Object.assign({}, validLaunchParameters);
                    body.name =
                        'string-longer-than-112-characters-string-longer-than-112-characters-string-longer-than-112-characters-abcdefghijk';
                    await adminSession.resources.accounts.create(body, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: `name: ${(0, workbench_core_base_1.lengthValidationMessage)(112)}`
                    }));
                }
            });
            test('with externalId that is too long it throws a validation error', async () => {
                try {
                    const body = Object.assign({}, validLaunchParameters);
                    body.externalId =
                        'message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-message-that-is-longer-than-400-characters-abcdefghijklmn';
                    await adminSession.resources.accounts.create(body, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'externalId: must contain only letters, numbers, hyphens, underscores, plus, equal, comma, period, at (@), colon (:), and forward slash (/). String length must be between 2 and 400 characters inclusively'
                    }));
                }
            });
            test('with accountId of account that does not exist it throws a validation error', async () => {
                try {
                    const body = Object.assign({}, validLaunchParameters);
                    body.awsAccountId = '123456789012';
                    await adminSession.resources.accounts.create(body, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: "Please provide a valid 'awsAccountId' for the hosting account"
                    }));
                }
            });
            test('with incorrect envMgtmRoleArn and hostingAccountHandlerRoleArn it throws a validation error', async () => {
                try {
                    const body = Object.assign({}, validLaunchParameters);
                    body.envMgmtRoleArn = 'fakeValue';
                    body.hostingAccountHandlerRoleArn = 'fakeValue';
                    await adminSession.resources.accounts.create(body, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'envMgmtRoleArn: must be a valid envMgmtRoleArn. hostingAccountHandlerRoleArn: must be a valid hostingAccountHandlerRoleArn'
                    }));
                }
            });
        });
        describe('and the account is already onboarded', () => {
            test('it throws an error', async () => {
                const accountHelper = new accountHelper_1.AccountHelper();
                const invalidParam = Object.assign({}, validLaunchParameters);
                const existingAccounts = await accountHelper.listOnboardedAccounts();
                invalidParam.awsAccountId = lodash_1.default.first(existingAccounts).awsAccountId;
                try {
                    await adminSession.resources.accounts.create(invalidParam, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                        error: 'Bad Request',
                        message: 'This AWS Account was found in DDB. Please provide the correct id value in request body'
                    }));
                }
            });
        });
    });
    describe('Project admin or researcher can not create aws Accounts', () => {
        describe('As project admin', () => {
            test('it throws 403 error', async () => {
                try {
                    await paSession.resources.accounts.create({}, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                        error: 'User is not authorized'
                    }));
                }
            });
        });
        describe('As researcher', () => {
            test('it throws 403 error', async () => {
                try {
                    await researcherSession.resources.accounts.create({}, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                        error: 'User is not authorized'
                    }));
                }
            });
        });
        describe('As unauthenticated user', () => {
            test('it throws 403 error', async () => {
                try {
                    await anonymousSession.resources.accounts.create({}, false);
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
                }
            });
        });
    });
});
//# sourceMappingURL=create.test.js.map