"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 paabHelper_1 = require("../../../support/complex/paabHelper");
const HttpError_1 = __importDefault(require("../../../support/utils/HttpError"));
const utilities_1 = require("../../../support/utils/utilities");
describe('listUserSshKeysForProject negative tests', () => {
    const paabHelper = new paabHelper_1.PaabHelper(2);
    let adminSession;
    let pa1Session;
    let rs1Session;
    let anonymousSession;
    let project1Id;
    let project2Id;
    beforeAll(async () => {
        ({ adminSession, pa1Session, rs1Session, project1Id, project2Id, anonymousSession } =
            await paabHelper.createResources(__filename));
    });
    beforeEach(async () => {
        expect.hasAssertions();
    });
    afterAll(async () => {
        await paabHelper.cleanup();
    });
    describe('for project that does not exist', () => {
        let invalidProjectId;
        const testBundle = [
            {
                username: 'projectAdmin1',
                session: () => pa1Session
            },
            {
                username: 'researcher1',
                session: () => rs1Session
            }
        ];
        beforeEach(() => {
            invalidProjectId = 'proj-00000000-0000-0000-0000-000000000000';
        });
        test.each(testBundle)('it throws 403 error', async (testCase) => {
            const { username, session: sessionFunc } = testCase;
            const session = sessionFunc();
            console.log(`as ${username}`);
            try {
                await session.resources.projects.project(invalidProjectId).sshKeys().get();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: `User is not authorized`
                }));
            }
        });
    });
    describe('for project that user does not have access to', () => {
        const testBundle = [
            {
                username: 'projectAdmin1',
                session: () => pa1Session,
                projectId: () => project2Id
            },
            {
                username: 'researcher1',
                session: () => rs1Session,
                projectId: () => project2Id
            }
        ];
        test.each(testBundle)('it throws 403 error', async (testCase) => {
            const { username, session: sessionFunc, projectId: projectIdFunc } = testCase;
            const session = sessionFunc();
            const projectId = projectIdFunc();
            console.log(`as ${username}`);
            try {
                await session.resources.projects.project(projectId).sshKeys().get();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: `User is not authorized`
                }));
            }
        });
    });
    describe('with ITAdmin that cannot list keys for a valid project', () => {
        test('it throws 403 error', async () => {
            try {
                await adminSession.resources.projects.project(project1Id).sshKeys().get();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(403, {
                    error: `User is not authorized`
                }));
            }
        });
    });
    describe('with unauthenticated user that cannot list keys for a valid project', () => {
        test('it throws 401 error', async () => {
            try {
                await anonymousSession.resources.projects.project(project1Id).sshKeys().get();
            }
            catch (e) {
                (0, utilities_1.checkHttpError)(e, new HttpError_1.default(401, {}));
            }
        });
    });
});
//# sourceMappingURL=listUserSshKeysForProject.test.js.map