"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 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('datasets get negative tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(3);
    let adminSession;
    let pa1Session;
    let anonymousSession;
    let project1Id;
    let project3Id;
    let researcherSession;
    let dataSet1Id;
    const setup = setup_1.default.getSetup();
    const settings = setup.getSettings();
    beforeEach(() => {
        expect.hasAssertions();
    });
    beforeAll(async () => {
        const paabResources = await paabHelper.createResources(__filename);
        adminSession = paabResources.adminSession;
        pa1Session = paabResources.pa1Session;
        researcherSession = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
        project1Id = paabResources.project1Id;
        project3Id = paabResources.project3Id;
        const randomTextGenerator = new randomTextGenerator_1.default(settings.get('runId'));
        const dataset1Name = randomTextGenerator.getFakeText('isolated-datasets-get-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 pa1Session.resources.projects
            .project(project1Id)
            .dataSets()
            .create(dataSetBody, false);
        dataSet1Id = dataSet1.id;
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    describe('ITAdmin tests', () => {
        const invalidDatasets = (0, utilities_1.generateInvalidIds)('dataset');
        test.each(invalidDatasets)('invalid dataset Id throws validation exception for Admin', async (invalidDataset) => {
            try {
                await adminSession.resources.datasets.dataset(invalidDataset).get();
            }
            catch (error) {
                (0, utilities_1.checkHttpError)(error, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: `dataSetId: Invalid ID`
                }));
            }
        });
    });
    describe('PA tests', () => {
        test('it should fail with unauthorized with no project', async () => {
            try {
                await pa1Session.resources.datasets.dataset(dataSet1Id).get();
            }
            catch (error) {
                (0, utilities_1.checkHttpError)(error, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test('it should fail with a different projectId', async () => {
            try {
                await pa1Session.resources.projects.project(project3Id).dataSets().dataset(dataSet1Id).get();
            }
            catch (error) {
                (0, utilities_1.checkHttpError)(error, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    describe('Researcher tests', () => {
        test('it should fail with unauthorized with no project', async () => {
            try {
                await researcherSession.resources.datasets.dataset(dataSet1Id).get();
            }
            catch (error) {
                (0, utilities_1.checkHttpError)(error, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test('it should fail with a different projectId', async () => {
            try {
                await researcherSession.resources.projects.project(project3Id).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 dataset', async () => {
        try {
            await anonymousSession.resources.projects.project(project3Id).dataSets().dataset(dataSet1Id).get();
        }
        catch (error) {
            (0, utilities_1.checkHttpError)(error, new HttpError_1.default(401, {}));
        }
    });
});
//# sourceMappingURL=get.test.js.map