"use strict";
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 types_1 = require("../../../polkadot/types");
const sinon_1 = __importDefault(require("sinon"));
const toggleFreezeSecondaryKeys_1 = require("../toggleFreezeSecondaryKeys");
const mocks_1 = require("../../../testUtils/mocks");
const types_2 = require("../../../types");
describe('toggleFreezeSecondaryKeys procedure', () => {
    let mockContext;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
    });
    let addTransactionStub;
    beforeEach(() => {
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        mockContext = mocks_1.dsMockUtils.getContextInstance({
            areScondaryKeysFrozen: true,
        });
    });
    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 freeze is set to true and the secondary keys are already frozen', () => {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(toggleFreezeSecondaryKeys_1.prepareToggleFreezeSecondaryKeys.call(proc, {
            freeze: true,
            identity: mocks_1.entityMockUtils.getIdentityInstance({
                areScondaryKeysFrozen: true,
            }),
        })).rejects.toThrow('The secondary keys are already frozen');
    });
    test('should throw an error if freeze is set to false and the secondary keys are already unfrozen', () => {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(toggleFreezeSecondaryKeys_1.prepareToggleFreezeSecondaryKeys.call(proc, {
            freeze: false,
            identity: mocks_1.entityMockUtils.getIdentityInstance({
                areScondaryKeysFrozen: false,
            }),
        })).rejects.toThrow('The secondary keys are already unfrozen');
    });
    test('should add a freeze secondary keys transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('identity', 'freezeSecondaryKeys');
        yield toggleFreezeSecondaryKeys_1.prepareToggleFreezeSecondaryKeys.call(proc, {
            freeze: true,
            identity: mocks_1.entityMockUtils.getIdentityInstance({
                areScondaryKeysFrozen: false,
            }),
        });
        sinon_1.default.assert.calledWith(addTransactionStub, transaction, {});
    }));
    test('should add a unfreeze secondary keys transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const transaction = mocks_1.dsMockUtils.createTxStub('identity', 'unfreezeSecondaryKeys');
        yield toggleFreezeSecondaryKeys_1.prepareToggleFreezeSecondaryKeys.call(proc, {
            freeze: false,
            identity: mocks_1.entityMockUtils.getIdentityInstance({
                areScondaryKeysFrozen: true,
            }),
        });
        sinon_1.default.assert.calledWith(addTransactionStub, transaction, {});
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = toggleFreezeSecondaryKeys_1.getAuthorization.bind(proc);
            expect(boundFunc({ freeze: true, identity: mocks_1.entityMockUtils.getIdentityInstance() })).toEqual({
                roles: [{ type: types_2.RoleType.Identity, did: 'someDid' }],
                permissions: {
                    transactions: [types_1.TxTags.identity.FreezeSecondaryKeys],
                    tokens: [],
                    portfolios: [],
                },
            });
            expect(boundFunc({ freeze: false, identity: mocks_1.entityMockUtils.getIdentityInstance() })).toEqual({
                roles: [{ type: types_2.RoleType.Identity, did: 'someDid' }],
                permissions: {
                    transactions: [types_1.TxTags.identity.UnfreezeSecondaryKeys],
                    tokens: [],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=toggleFreezeSecondayKeys.js.map