"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 leaveIdentity_1 = require("../leaveIdentity");
const mocks_1 = require("../../../testUtils/mocks");
jest.mock('~/api/entities/SecurityToken', require("../../../testUtils/mocks/entities").mockSecurityTokenModule('~/api/entities/SecurityToken'));
describe('modifyCaCheckpoint procedure', () => {
    let mockContext;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
    });
    beforeEach(() => {
        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 the Account is not associated to any Identity', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const account = mocks_1.entityMockUtils.getAccountInstance({
            getIdentity: null,
        });
        let error;
        try {
            yield leaveIdentity_1.prepareLeaveIdentity.call(proc, { account });
        }
        catch (err) {
            error = err;
        }
        expect(error.message).toBe('There is no Identity associated to this Account');
    }));
    test('should throw an error if the Account is not a secondary key', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        const account = mocks_1.entityMockUtils.getAccountInstance();
        let error;
        try {
            yield leaveIdentity_1.prepareLeaveIdentity.call(proc, { account });
        }
        catch (err) {
            error = err;
        }
        expect(error.message).toBe('Only Seconday Keys are allowed to leave an Identity');
    }));
    test('should add a leave identity as key transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const address = 'someAddress';
        const addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        const leaveIdentityAsKeyTransaction = mocks_1.dsMockUtils.createTxStub('identity', 'leaveIdentityAsKey');
        const account = mocks_1.entityMockUtils.getAccountInstance({
            address,
            getIdentity: mocks_1.entityMockUtils.getIdentityInstance({
                getSecondaryKeys: [
                    {
                        signer: mocks_1.entityMockUtils.getAccountInstance({ address }),
                    },
                ],
            }),
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield leaveIdentity_1.prepareLeaveIdentity.call(proc, { account });
        sinon_1.default.assert.calledWith(addTransactionStub, leaveIdentityAsKeyTransaction, {});
    }));
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = leaveIdentity_1.getAuthorization.bind(proc);
            let account = mocks_1.entityMockUtils.getAccountInstance({ isEqual: false });
            expect(boundFunc({ account })).toEqual({
                roles: false,
                permissions: {
                    tokens: [],
                    transactions: [types_1.TxTags.identity.LeaveIdentityAsKey],
                    portfolios: [],
                },
            });
            account = mocks_1.entityMockUtils.getAccountInstance({ isEqual: true });
            expect(boundFunc({ account })).toEqual({
                roles: true,
                permissions: {
                    tokens: [],
                    transactions: [types_1.TxTags.identity.LeaveIdentityAsKey],
                    portfolios: [],
                },
            });
        });
    });
});
//# sourceMappingURL=leaveIdentity.js.map