"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('negative tests for get dataset permissions', () => {
    let pa1Session;
    let pa2Session;
    let rs1Session;
    let anonymousSession;
    let project1Id;
    let project2Id;
    let paabHelper;
    let adminSession;
    let setup;
    let settings;
    let dataSet1Id;
    beforeAll(async () => {
        paabHelper = new paabHelper_1.PaabHelper(2);
        setup = setup_1.default.getSetup();
        settings = setup.getSettings();
        const paabResources = await paabHelper.createResources(__filename);
        project1Id = paabResources.project1Id;
        project2Id = paabResources.project2Id;
        pa1Session = paabResources.pa1Session;
        pa2Session = paabResources.pa2Session;
        rs1Session = paabResources.rs1Session;
        adminSession = paabResources.adminSession;
        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();
    });
    test('ITAdmin cannot get dataset permissions', async () => {
        try {
            await adminSession.resources.datasets.dataset(dataSet1Id).listAccessPermissions();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    test('Researcher cannot get dataset permissions', async () => {
        try {
            await rs1Session.resources.projects
                .project(project1Id)
                .dataSets()
                .dataset(dataSet1Id)
                .listAccessPermissions();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                error: 'User is not authorized'
            }));
        }
    });
    describe('Associated dataset', () => {
        beforeEach(async () => {
            await pa1Session.resources.projects
                .project(project1Id)
                .dataSets()
                .dataset(dataSet1Id)
                .associateWithProject(project2Id, 'read-write');
        });
        afterEach(async () => {
            await pa1Session.resources.projects
                .project(project1Id)
                .dataSets()
                .dataset(dataSet1Id)
                .disassociateFromProject(project2Id);
        });
        test('Project admin of the project that does not own the dataset cannot get dataset permissions', async () => {
            try {
                await pa2Session.resources.projects
                    .project(project2Id)
                    .dataSets()
                    .dataset(dataSet1Id)
                    .listAccessPermissions();
            }
            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 list dataset access permissions', async () => {
            try {
                await rs1Session.resources.datasets.dataset(dataSet1Id).listAccessPermissions();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: 'User is not authorized'
                }));
            }
        });
    });
    test('Unauthenticated user cannot list dataset access permissions', async () => {
        try {
            await anonymousSession.resources.datasets.dataset(dataSet1Id).listAccessPermissions();
        }
        catch (e) {
            (0, utilities_1.checkHttpError)(e, new HttpError_1.default(401, {}));
        }
    });
});
//# sourceMappingURL=listPermissions.test.js.map