"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 createDataSetRequestParser_1 = require("@aws/swb-app/lib/dataSets/createDataSetRequestParser");
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('get dataset for project tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(2);
    const setup = setup_1.default.getSetup();
    const settings = setup.getSettings();
    let itAdminSession;
    let researcher1Session;
    let pa1Session;
    let pa2Session;
    let anonymousSession;
    let project1Id;
    let project2Id;
    let dataSet1Id;
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        itAdminSession = paabResources.adminSession;
        researcher1Session = paabResources.rs1Session;
        pa1Session = paabResources.pa1Session;
        pa2Session = paabResources.pa2Session;
        anonymousSession = paabResources.anonymousSession;
        project1Id = paabResources.project1Id;
        project2Id = paabResources.project2Id;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    describe('negative tests', () => {
        const invalidDatasets = (0, utilities_1.generateInvalidIds)('dataset');
        test.each(invalidDatasets)('invalid dataset Id throws validation exception for Admin', async (invalidDataset) => {
            try {
                await itAdminSession.resources.projects
                    .project(project1Id)
                    .dataSets()
                    .dataset(invalidDataset)
                    .get();
            }
            catch (error) {
                (0, utilities_1.checkHttpError)(error, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: `dataSetId: Invalid ID`
                }));
            }
        });
        test.each(invalidDatasets)('non-existing dataset Id throws 403 for PA', async (invalidDataset) => {
            try {
                await pa1Session.resources.projects.project(project1Id).dataSets().dataset(invalidDataset).get();
            }
            catch (error) {
                (0, utilities_1.checkHttpError)(error, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test.each(invalidDatasets)('non-existing dataset Id throws 403 for Researcher', async (invalidDataset) => {
            try {
                await researcher1Session.resources.projects
                    .project(project1Id)
                    .dataSets()
                    .dataset(invalidDataset)
                    .get();
            }
            catch (error) {
                (0, utilities_1.checkHttpError)(error, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        describe('unauthorized to access', () => {
            beforeAll(async () => {
                const randomTextGenerator = new randomTextGenerator_1.default(settings.get('runId'));
                const dataset1Name = randomTextGenerator.getFakeText('isolated-get-datasets-for-project-ds1');
                const dataSetBody = createDataSetRequestParser_1.CreateDataSetRequestParser.parse({
                    storageName: settings.get('DataSetsBucketName'),
                    awsAccountId: settings.get('mainAccountId'),
                    path: dataset1Name,
                    name: dataset1Name,
                    region: settings.get('awsRegion'),
                    type: 'internal'
                });
                const { data: dataSet1 } = await pa2Session.resources.projects
                    .project(project2Id)
                    .dataSets()
                    .create(dataSetBody, false);
                dataSet1Id = dataSet1.id;
            });
            test('PA without access to dataset cannot get', async () => {
                try {
                    await pa1Session.resources.projects.project(project2Id).dataSets().dataset(dataSet1Id).get();
                }
                catch (error) {
                    (0, utilities_1.checkHttpError)(error, new HttpError_1.default(403, {
                        error: 'User is not authorized'
                    }));
                }
            });
            test('Researcher without access to dataset cannot get', async () => {
                try {
                    await researcher1Session.resources.projects
                        .project(project2Id)
                        .dataSets()
                        .dataset(dataSet1Id)
                        .get();
                }
                catch (error) {
                    (0, utilities_1.checkHttpError)(error, new HttpError_1.default(403, {
                        error: 'User is not authorized'
                    }));
                }
            });
            test('Unauthenticated user cannot get', async () => {
                try {
                    await anonymousSession.resources.projects.project(project2Id).dataSets().dataset(dataSet1Id).get();
                }
                catch (error) {
                    (0, utilities_1.checkHttpError)(error, new HttpError_1.default(401, {}));
                }
            });
        });
    });
});
//# sourceMappingURL=getDataSetForProject.test.js.map