"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
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('Associate Project with EnvTypeConfig', () => {
    const setup = setup_1.default.getSetup();
    const paabHelper = new paabHelper_1.PaabHelper(1);
    let adminSession;
    let paSession;
    let researcherSession;
    let anonymousSession;
    const envTypeId = setup.getSettings().get('envTypeId');
    const envTypeConfigId = setup.getSettings().get('envTypeConfigId');
    const nonExistentProjectId = 'proj-12345678-1234-1234-1234-123456789012';
    const nonExistentEnvTypeId = 'et-prod-0123456789012,pa-0123456789012';
    const nonExistentEnvTypeConfigId = 'etc-12345678-1234-1234-1234-123456789012';
    let projectId;
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        adminSession = paabResources.adminSession;
        paSession = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
        projectId = paabResources.project1Id;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    test('Project Admin cannot associate project with ETC', async () => {
        try {
            await paSession.resources.projects
                .project(projectId)
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .associate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('Researcher cannot associate project with ETC', async () => {
        try {
            await researcherSession.resources.projects
                .project(projectId)
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .associate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('Unauthenticated user cannot associate project with ETC', async () => {
        try {
            await anonymousSession.resources.projects
                .project(projectId)
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .associate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
        }
    });
    test('fails when using invalid format project Id', async () => {
        try {
            await adminSession.resources.projects
                .project('invalid-project-id')
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .associate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `projectId: Invalid ID`
            }));
        }
    });
    test('fails when using non existing project Id', async () => {
        try {
            await adminSession.resources.projects
                .project(nonExistentProjectId)
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .associate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find project ${nonExistentProjectId}`
            }));
        }
    });
    test('fails when using deleted project', async () => {
        const settings = setup.getSettings();
        const randomTextGenerator = new randomTextGenerator_1.default(settings.get('runId'));
        const testName = randomTextGenerator.getFakeText('projectEnvTypeConfig-isolatedTest-create-failsWhenUsingDeletedProject');
        const { data: costCenter } = await adminSession.resources.costCenters.create({
            name: (0, utilities_1.generateRandomString)(10, utilities_1.validSwbName),
            accountId: setup.getSettings().get('defaultHostingAccountId'),
            description: 'a test object'
        });
        const { data: createdProject } = await adminSession.resources.projects.create({
            name: `${testName}Project`,
            description: 'test description',
            costCenterId: costCenter.id
        });
        const projectId = createdProject.id;
        await adminSession.resources.projects.project(projectId).delete();
        try {
            await adminSession.resources.projects
                .project(projectId)
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .associate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `Project ${projectId} was deleted`
            }));
        }
    });
    test('fails when using invalid format envType Id', async () => {
        try {
            await adminSession.resources.projects
                .project(projectId)
                .environmentTypes()
                .environmentType('invalid-envType-id')
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .associate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `envTypeId: Invalid ID`
            }));
        }
    });
    test('fails when using non existing envType Id', async () => {
        try {
            await adminSession.resources.projects
                .project(projectId)
                .environmentTypes()
                .environmentType(nonExistentEnvTypeId)
                .configurations()
                .environmentTypeConfig(envTypeConfigId)
                .associate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find environment type config ${envTypeConfigId}`
            }));
        }
    });
    test('fails when using invalid format envTypeConfig Id', async () => {
        try {
            await adminSession.resources.projects
                .project(projectId)
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig('invalid-etc-id')
                .associate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                error: 'Bad Request',
                message: `envTypeConfigId: Invalid ID`
            }));
        }
    });
    test('fails when using non existing envTypeConfig Id', async () => {
        try {
            await adminSession.resources.projects
                .project(projectId)
                .environmentTypes()
                .environmentType(envTypeId)
                .configurations()
                .environmentTypeConfig(nonExistentEnvTypeConfigId)
                .associate();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(404, {
                error: 'Not Found',
                message: `Could not find environment type config ${nonExistentEnvTypeConfigId}`
            }));
        }
    });
});
//# sourceMappingURL=create.test.js.map