"use strict";
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 delete negative tests', () => {
    let pa1Session;
    let adminSession;
    let rs1Session;
    let anonymousSession;
    let project1Id;
    let paabHelper;
    let setup;
    let settings;
    let dataSet1Id;
    beforeAll(async () => {
        setup = setup_1.default.getSetup();
        paabHelper = new paabHelper_1.PaabHelper(1);
        settings = setup.getSettings();
        const paabResources = await paabHelper.createResources(__filename);
        project1Id = paabResources.project1Id;
        pa1Session = paabResources.pa1Session;
        adminSession = paabResources.adminSession;
        rs1Session = paabResources.rs1Session;
        anonymousSession = paabResources.anonymousSession;
        const randomTextGenerator = new randomTextGenerator_1.default(settings.get('runId'));
        const dataset1Name = randomTextGenerator.getFakeText('isolated-datasets-delete-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;
    });
    beforeEach(() => {
        expect.hasAssertions();
    });
    afterAll(async () => {
        await paabHelper.cleanup();
        await setup.cleanup();
    });
    describe('when the dataset does not exist', () => {
        test('it returns a 403', async () => {
            try {
                await pa1Session.resources.projects
                    .project(project1Id)
                    .dataSets()
                    .dataset('dataset-00000000-0000-0000-0000-000000000000')
                    .delete();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    describe('When the project does not exist', () => {
        test('It returns a 403', async () => {
            try {
                await pa1Session.resources.projects
                    .project('proj-00000000-0000-0000-0000-000000000000')
                    .dataSets()
                    .dataset('dataset-00000000-0000-0000-0000-000000000000')
                    .delete();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    describe('boundary tests', () => {
        test('ITAdmin should not be able to soft delete datasets', async () => {
            try {
                await adminSession.resources.datasets.dataset(dataSet1Id).delete();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        test('Researcher should not be able to soft delete datasets', async () => {
            try {
                await rs1Session.resources.projects.project(project1Id).dataSets().dataset(dataSet1Id).delete();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
        describe('user with no project', () => {
            beforeAll(async () => {
                //Remove researcher 1 from project 1 to make it a user with no project associated
                await adminSession.resources.projects
                    .project(project1Id)
                    .removeUserFromProject(rs1Session.getUserId());
            });
            afterAll(async () => {
                await adminSession.resources.projects
                    .project(project1Id)
                    .assignUserToProject(rs1Session.getUserId(), {
                    role: 'Researcher'
                });
            });
            test('User with no project cannot delete dataset', async () => {
                try {
                    await rs1Session.resources.datasets.dataset(dataSet1Id).delete();
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                        error: 'User is not authorized'
                    }));
                }
            });
            test('Unauthenticated user cannot delete dataset', async () => {
                try {
                    await anonymousSession.resources.datasets.dataset(dataSet1Id).delete();
                }
                catch (e) {
                    (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {}));
                }
            });
        });
    });
});
//# sourceMappingURL=delete.test.js.map