"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 HttpError_1 = __importDefault(require("../../../support/utils/HttpError"));
const utilities_1 = require("../../../support/utils/utilities");
describe('datasets create negative tests', () => {
    let pa1Session;
    let pa2Session;
    let researcher1Sesssion;
    let anonymousSession;
    let project1Id;
    let paabHelper;
    let adminSession;
    beforeAll(async () => {
        paabHelper = new paabHelper_1.PaabHelper(1);
        const paabResources = await paabHelper.createResources(__filename);
        project1Id = paabResources.project1Id;
        pa1Session = paabResources.pa1Session;
        pa2Session = paabResources.pa2Session;
        researcher1Sesssion = paabResources.rs1Session;
        adminSession = paabResources.adminSession;
        anonymousSession = paabResources.anonymousSession;
    });
    beforeEach(() => {
        expect.hasAssertions();
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    describe('Validation error', () => {
        test('Missing parameters', async () => {
            try {
                await pa1Session.resources.projects.project(project1Id).dataSets().create({}, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: 'name: Required. storageName: Required. path: Required. awsAccountId: Required. region: Required. ' +
                        'type: Invalid literal value, expected "internal"'
                }));
            }
        });
        test('Extra parameters', async () => {
            try {
                const createRequest = paabHelper.createDatasetRequest(project1Id);
                await pa1Session.resources.projects
                    .project(project1Id)
                    .dataSets()
                    .create(Object.assign(Object.assign({}, createRequest), { ownerType: 'GROUP' }), false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: ": Unrecognized key(s) in object: 'ownerType'"
                }));
            }
        });
        test('Duplicate creation', async () => {
            const createRequest = paabHelper.createDatasetRequest(project1Id);
            console.log(createRequest);
            await pa1Session.resources.projects.project(project1Id).dataSets().create(createRequest);
            console.log(createRequest);
            try {
                await pa1Session.resources.projects.project(project1Id).dataSets().create(createRequest);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(409, {
                    error: 'Conflict',
                    message: "Cannot create the DataSet. A DataSet must have a unique 'name', and the requested name already exists."
                }));
            }
        });
        test('Storage name not equal to main account S3 bucket name', async () => {
            const createRequest = paabHelper.createDatasetRequest(project1Id);
            try {
                await pa1Session.resources.projects
                    .project(project1Id)
                    .dataSets()
                    .create(Object.assign(Object.assign({}, createRequest), { storageName: 'wrong-s3-bucket-name' }), false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: "Please use data set S3 bucket name from main account for 'storageName'."
                }));
            }
        });
        test('Account number not equal to main account number', async () => {
            const createRequest = paabHelper.createDatasetRequest(project1Id);
            try {
                await pa1Session.resources.projects
                    .project(project1Id)
                    .dataSets()
                    .create(Object.assign(Object.assign({}, createRequest), { awsAccountId: '123456789012' }), false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: "Please use main account ID for 'awsAccountId'."
                }));
            }
        });
        test('Syntax error in parameters', async () => {
            try {
                await pa1Session.resources.projects.project(project1Id).dataSets().create({
                    storageName: 'illegal?bucket?name',
                    awsAccountId: '1234',
                    path: 'illegal?path',
                    name: 'illegal?name',
                    region: 'us-test-1',
                    type: 'external'
                }, false);
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(400, {
                    error: 'Bad Request',
                    message: 'name: must contain only letters, numbers, hyphens, underscores, and periods. ' +
                        'storageName: must contain only letters, numbers, hyphens, underscores, and periods. ' +
                        'path: must contain only letters, numbers, hyphens, underscores, and periods. ' +
                        'awsAccountId: must be a 12 digit number. ' +
                        'region: must be valid AWS region. ' +
                        'type: Invalid literal value, expected "internal"'
                }));
            }
        });
    });
    describe('datasets creation requests returns a 403 for', () => {
        const expectedError = new HttpError_1.default(403, {
            error: 'User is not authorized'
        });
        test('Researchers who belong to the project', async () => {
            try {
                const createRequest = paabHelper.createDatasetRequest(project1Id);
                await researcher1Sesssion.resources.projects
                    .project(project1Id)
                    .dataSets()
                    .create(createRequest, false);
            }
            catch (actualError) {
                (0, utilities_1.checkHttpError)(actualError, expectedError);
            }
        });
        test('ProjectAdmins who belong to another project', async () => {
            try {
                const createRequest = paabHelper.createDatasetRequest(project1Id);
                await pa2Session.resources.projects.project(project1Id).dataSets().create(createRequest, false);
            }
            catch (actualError) {
                (0, utilities_1.checkHttpError)(actualError, expectedError);
            }
        });
        test('ITAdmins', async () => {
            try {
                const createRequest = paabHelper.createDatasetRequest(project1Id);
                await adminSession.resources.projects.project(project1Id).dataSets().create(createRequest, false);
            }
            catch (actualError) {
                console.log(JSON.stringify(actualError));
                console.log(JSON.stringify(expectedError));
                (0, utilities_1.checkHttpError)(actualError, expectedError);
            }
        });
        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(researcher1Sesssion.getUserId());
            });
            afterAll(async () => {
                await adminSession.resources.projects
                    .project(project1Id)
                    .assignUserToProject(researcher1Sesssion.getUserId(), {
                    role: 'Researcher'
                });
            });
            test('User with no project cannot create datasets', async () => {
                try {
                    const createRequest = paabHelper.createDatasetRequest(project1Id);
                    await researcher1Sesssion.resources.projects
                        .project(project1Id)
                        .dataSets()
                        .create(createRequest, false);
                }
                catch (actualError) {
                    (0, utilities_1.checkHttpError)(actualError, expectedError);
                }
            });
            test('Unauthenticated user cannot create dataset', async () => {
                try {
                    const createRequest = paabHelper.createDatasetRequest(project1Id);
                    await anonymousSession.resources.projects
                        .project(project1Id)
                        .dataSets()
                        .create(createRequest, false);
                }
                catch (actualError) {
                    (0, utilities_1.checkHttpError)(actualError, new HttpError_1.default(403, {}));
                }
            });
        });
    });
});
//# sourceMappingURL=create.test.js.map