var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React from 'react';
import { renderHook } from '@testing-library/react';
import { MdmModuleProvider } from '../../../contexts/MdmModuleContext';
import { useWorkflowCheckPermission } from './useWorkflowCheckPermission';
var createUser = function (permissions) {
    if (permissions === void 0) { permissions = {}; }
    return ({
        tenants: ['tenant'],
        userPermissions: { permissions: permissions, permissionsByService: {}, roles: {} }
    });
};
var defaultMdmValues = {
    tenant: { id: 'tenant', name: 'tenant' },
    workflowPath: 'https://tst-01-workflow.reltio.com/workflow-adapter',
    user: createUser()
};
var setUp = function (mdmValues) {
    if (mdmValues === void 0) { mdmValues = defaultMdmValues; }
    var Providers = function (_a) {
        var children = _a.children;
        return (React.createElement(MdmModuleProvider, { values: mdmValues }, children));
    };
    return renderHook(useWorkflowCheckPermission, { wrapper: Providers });
};
describe('useWorkflowCheckPermission custom sandbox behaviour', function () {
    beforeEach(function () {
        jest.clearAllMocks();
    });
    it('should return true for right with permission VIEW_PROCESS_DEFINITIONS', function () {
        var user = createUser({ 'workflow:config.definition': { READ: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('VIEW_PROCESS_DEFINITIONS')).toBeTruthy();
    });
    it('should return true for right with permission DEPLOY_GENERIC_PROCESS_DEFINITION', function () {
        var user = createUser({
            'workflow:environment.config.definition': {
                CREATE: ['tenant'],
                DELETE: ['tenant']
            }
        });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('DEPLOY_GENERIC_PROCESS_DEFINITION')).toBeTruthy();
    });
    it('should return true for right with permission VIEW_JOBS', function () {
        var user = createUser({ 'workflow:jobs': { READ: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('VIEW_JOBS')).toBeTruthy();
    });
    it('should return true for right with permission START_JOB if permission workflow:jobs exist', function () {
        var user = createUser({ 'workflow:jobs': { EXECUTE: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('START_JOB')).toBeTruthy();
    });
    it('should return true for right with permission START_JOB if no permission workflow:jobs', function () {
        var user = createUser({ 'workflow:jobs.terminateProcessInstances': { EXECUTE: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('START_JOB')).toBeFalsy();
    });
    it('should return true for right with permission START_JOB', function () {
        var user = createUser({
            'workflow:jobs.terminateProcessInstances': {
                EXECUTE: ['tenant']
            },
            'workflow:jobs.updateTasks': {
                EXECUTE: ['tenant']
            },
            'workflow:jobs.validateTasks': {
                EXECUTE: ['tenant']
            },
            'workflow:jobs.syncBusinessProcessData': {
                EXECUTE: ['tenant']
            }
        });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('START_JOB')).toBeTruthy();
    });
    it('should return true for right with permission VIEW_CUSTOM_JARS with existing workflow:config.jar', function () {
        var user = createUser({ 'workflow:config.jar': { READ: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('VIEW_CUSTOM_JARS')).toBeTruthy();
    });
    it('should return true for right with permission VIEW_CUSTOM_JARS with existing workflow:environment.config.jar', function () {
        var user = createUser({ 'workflow:environment.config.jar': { READ: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('VIEW_CUSTOM_JARS')).toBeTruthy();
    });
    it('should return true for right with permission VIEW_CUSTOM_JARS with existing both workflow:environment.config.jar and workflow:config.jar', function () {
        var user = createUser({
            'workflow:environment.config.jar': {
                READ: ['tenant']
            },
            'workflow:config.jar': {
                READ: ['tenant']
            }
        });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('VIEW_CUSTOM_JARS')).toBeTruthy();
    });
    it('should return true for right with permission UPDATE_PROCESS_INSTANCE', function () {
        var user = createUser({ 'workflow:data.process': { UPDATE: ['tenant'], DELETE: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('UPDATE_PROCESS_INSTANCE')).toBeTruthy();
    });
    it('should return true for right with permission VIEW_TASKS', function () {
        var user = createUser({ 'workflow:data.tasks': { READ: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('VIEW_TASKS')).toBeTruthy();
    });
    it('should return true for right with permission START_PROCESS_INSTANCE', function () {
        var user = createUser({ 'workflow:data.process': { CREATE: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('START_PROCESS_INSTANCE')).toBeTruthy();
    });
    it('should return true for right with permission VIEW_PROCESS_INSTANCE_COMMENTS', function () {
        var user = createUser({ 'workflow:data.process': { READ: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('VIEW_PROCESS_INSTANCE_COMMENTS')).toBeTruthy();
    });
    it('should return true for right with permission VIEW_PROCESS_INSTANCES', function () {
        var user = createUser({ 'workflow:data.process': { READ: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('VIEW_PROCESS_INSTANCES')).toBeTruthy();
    });
    it('should return true for right with permission UPDATE_TASK', function () {
        var user = createUser({ 'workflow:data.tasks': { UPDATE: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('UPDATE_TASK')).toBeTruthy();
    });
    it('should return true for right with permission DEPLOY_TENANT_PROCESS_DEFINITION', function () {
        var user = createUser({ 'workflow:config.definition': { CREATE: ['tenant'], DELETE: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('DEPLOY_TENANT_PROCESS_DEFINITION')).toBeTruthy();
    });
    it('should return true for right with permission DEPLOY_CUSTOM_JAR with config.jar and environment.config.jar', function () {
        var user = createUser({
            'workflow:config.jar': {
                CREATE: ['tenant'],
                DELETE: ['tenant']
            },
            'workflow:environment.config.jar': {
                CREATE: ['tenant'],
                DELETE: ['tenant']
            }
        });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('DEPLOY_CUSTOM_JAR')).toBeTruthy();
    });
    it('should return false for right with permission DEPLOY_CUSTOM_JAR with environment.config.jar', function () {
        var user = createUser({ 'workflow:environment.config.jar': { CREATE: ['tenant'], DELETE: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('DEPLOY_CUSTOM_JAR')).toBeFalsy();
    });
    it('should return false for right with permission DEPLOY_CUSTOM_JAR with config.jar', function () {
        var user = createUser({ 'workflow:config.jar': { CREATE: ['tenant'], DELETE: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('DEPLOY_CUSTOM_JAR')).toBeFalsy();
    });
    it('should return true for right with permission START_PROCESS_INSTANCE_SEARCH_QUERY', function () {
        var user = createUser({ 'workflow:data.process': { CREATE: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('START_PROCESS_INSTANCE_SEARCH_QUERY')).toBeTruthy();
    });
    it('should return true for right with permission VIEW_PROCESS_INSTANCE_COUNT', function () {
        var user = createUser({ 'workflow:data.process': { READ: ['tenant'] } });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('VIEW_PROCESS_INSTANCE_COUNT')).toBeTruthy();
    });
    it('should return false for any permission if no permissions', function () {
        var result = setUp().result;
        expect(result.current('VIEW_PROCESS_DEFINITIONS')).toBeFalsy();
        expect(result.current('DEPLOY_GENERIC_PROCESS_DEFINITION')).toBeFalsy();
        expect(result.current('VIEW_JOBS')).toBeFalsy();
        expect(result.current('START_JOB')).toBeFalsy();
        expect(result.current('VIEW_CUSTOM_JARS')).toBeFalsy();
        expect(result.current('UPDATE_PROCESS_INSTANCE')).toBeFalsy();
        expect(result.current('VIEW_TASKS')).toBeFalsy();
        expect(result.current('REGISTER_TENANT')).toBeFalsy();
        expect(result.current('START_PROCESS_INSTANCE')).toBeFalsy();
        expect(result.current('VIEW_PROCESS_INSTANCE_COMMENTS')).toBeFalsy();
        expect(result.current('VIEW_PROCESS_INSTANCES')).toBeFalsy();
        expect(result.current('UPDATE_TASK')).toBeFalsy();
        expect(result.current('DEPLOY_TENANT_PROCESS_DEFINITION')).toBeFalsy();
        expect(result.current('DEPLOY_CUSTOM_JAR')).toBeFalsy();
        expect(result.current('START_PROCESS_INSTANCE_SEARCH_QUERY')).toBeFalsy();
        expect(result.current('VIEW_PROCESS_INSTANCE_COUNT')).toBeFalsy();
    });
    it('should return true for existed permissions', function () {
        var user = createUser({
            'workflow:config.definition': {
                READ: ['tenant']
            },
            'workflow:jobs': {
                EXECUTE: ['tenant']
            },
            'workflow:data.tasks': {
                READ: ['tenant']
            },
            'workflow:data.process': {
                READ: ['tenant'],
                CREATE: ['tenant']
            }
        });
        var result = setUp(__assign(__assign({}, defaultMdmValues), { user: user })).result;
        expect(result.current('VIEW_PROCESS_DEFINITIONS')).toBeTruthy();
        expect(result.current('DEPLOY_GENERIC_PROCESS_DEFINITION')).toBeFalsy();
        expect(result.current('VIEW_JOBS')).toBeFalsy();
        expect(result.current('START_JOB')).toBeTruthy();
        expect(result.current('VIEW_CUSTOM_JARS')).toBeFalsy();
        expect(result.current('UPDATE_PROCESS_INSTANCE')).toBeFalsy();
        expect(result.current('VIEW_TASKS')).toBeTruthy();
        expect(result.current('REGISTER_TENANT')).toBeFalsy();
        expect(result.current('START_PROCESS_INSTANCE')).toBeTruthy();
        expect(result.current('VIEW_PROCESS_INSTANCE_COMMENTS')).toBeTruthy();
        expect(result.current('VIEW_PROCESS_INSTANCES')).toBeTruthy();
        expect(result.current('UPDATE_TASK')).toBeFalsy();
        expect(result.current('DEPLOY_TENANT_PROCESS_DEFINITION')).toBeFalsy();
        expect(result.current('DEPLOY_CUSTOM_JAR')).toBeFalsy();
        expect(result.current('START_PROCESS_INSTANCE_SEARCH_QUERY')).toBeTruthy();
        expect(result.current('VIEW_PROCESS_INSTANCE_COUNT')).toBeTruthy();
    });
});
