"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const sinon_1 = __importDefault(require("sinon"));
const setGroupPermissions_1 = require("../setGroupPermissions");
const mocks_1 = require("../../../testUtils/mocks");
const types_1 = require("../../../types");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/SecurityToken', require("../../../testUtils/mocks/entities").mockSecurityTokenModule('~/api/entities/SecurityToken'));
describe('setGroupPermissions procedure', () => {
    const ticker = 'SOME_TICKER';
    const permissions = {
        transactions: {
            type: types_1.PermissionType.Include,
            values: [types_1.TxTags.sto.Invest],
        },
    };
    const rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
    const rawExtrinsicPermissions = mocks_1.dsMockUtils.createMockExtrinsicPermissions({
        These: [
            /* eslint-disable @typescript-eslint/naming-convention */
            mocks_1.dsMockUtils.createMockPalletPermissions({
                pallet_name: mocks_1.dsMockUtils.createMockPalletName('Sto'),
                dispatchable_names: mocks_1.dsMockUtils.createMockDispatchableNames({
                    These: [mocks_1.dsMockUtils.createMockDispatchableName('invest')],
                }),
            }),
            /* eslint-enable @typescript-eslint/naming-convention */
        ],
    });
    const customId = new bignumber_js_1.default(1);
    const rawAgId = mocks_1.dsMockUtils.createMockU32(customId.toNumber());
    let mockContext;
    let addTransactionStub;
    let externalAgentsSetGroupPermissionsTransaction;
    let permissionsLikeToPermissionsStub;
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        sinon_1.default.stub(utilsConversionModule, 'stringToTicker').returns(rawTicker);
        sinon_1.default
            .stub(utilsConversionModule, 'transactionPermissionsToExtrinsicPermissions')
            .returns(rawExtrinsicPermissions);
        sinon_1.default.stub(utilsConversionModule, 'numberToU32').returns(rawAgId);
        permissionsLikeToPermissionsStub = sinon_1.default.stub(utilsConversionModule, 'permissionsLikeToPermissions');
    });
    beforeEach(() => {
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        externalAgentsSetGroupPermissionsTransaction = mocks_1.dsMockUtils.createTxStub('externalAgents', 'setGroupPermissions');
        mockContext = mocks_1.dsMockUtils.getContextInstance();
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.entityMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should throw an error if new permissions are the same as the current ones', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        permissionsLikeToPermissionsStub.returns(permissions);
        let error;
        try {
            yield setGroupPermissions_1.prepareSetGroupPermissions.call(proc, {
                group: mocks_1.entityMockUtils.getCustomPermissionGroupInstance({
                    ticker,
                    id: customId,
                    getPermissions: {
                        transactions: permissions.transactions,
                        transactionGroups: [],
                    },
                }),
                permissions: { transactions: permissions.transactions },
            });
        }
        catch (err) {
            error = err;
        }
        expect(error.message).toBe('New permissions are the same as the current ones');
    }));
    test('should add a set group permissions transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const fakePermissions = { transactions: permissions.transactions };
        permissionsLikeToPermissionsStub
            .withArgs(fakePermissions, mockContext)
            .returns(permissions.transactions);
        yield setGroupPermissions_1.prepareSetGroupPermissions.call(proc, {
            group: mocks_1.entityMockUtils.getCustomPermissionGroupInstance({
                ticker,
                id: customId,
                getPermissions: {
                    transactions: permissions.transactions,
                    transactionGroups: [],
                },
            }),
            permissions: fakePermissions,
        });
        sinon_1.default.assert.calledWith(addTransactionStub, externalAgentsSetGroupPermissionsTransaction, {}, rawTicker, rawAgId, rawExtrinsicPermissions);
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = setGroupPermissions_1.getAuthorization.bind(proc);
            expect(boundFunc({
                permissions: { transactionGroups: [] },
                group: mocks_1.entityMockUtils.getCustomPermissionGroupInstance(),
            })).toEqual({
                permissions: {
                    transactions: [types_1.TxTags.externalAgents.SetGroupPermissions],
                    tokens: [mocks_1.entityMockUtils.getSecurityTokenInstance({ ticker })],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=setGroupPermissions.js.map