"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 sinon_1 = __importDefault(require("sinon"));
const waivePermissions_1 = require("../waivePermissions");
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('waivePermissions procedure', () => {
    const ticker = 'SOMETICKER';
    const did = 'someDid';
    const rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
    let mockContext;
    let addTransactionStub;
    let externalAgentsAbdicateTransaction;
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
        sinon_1.default.stub(utilsConversionModule, 'stringToTicker').returns(rawTicker);
    });
    beforeEach(() => {
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        externalAgentsAbdicateTransaction = mocks_1.dsMockUtils.createTxStub('externalAgents', 'abdicate');
        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 Identity is not an Agent for the Security Token', () => __awaiter(void 0, void 0, void 0, function* () {
        const token = mocks_1.entityMockUtils.getSecurityTokenInstance({
            ticker,
            permissionsGetAgents: [
                {
                    group: mocks_1.entityMockUtils.getKnownPermissionGroupInstance(),
                    agent: mocks_1.entityMockUtils.getIdentityInstance({
                        did: 'aDifferentDid',
                    }),
                },
                {
                    group: mocks_1.entityMockUtils.getKnownPermissionGroupInstance(),
                    agent: mocks_1.entityMockUtils.getIdentityInstance({
                        did: 'anotherDifferentDid',
                    }),
                },
            ],
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext, {
            token,
        });
        let error;
        try {
            yield waivePermissions_1.prepareWaivePermissions.call(proc, {
                token,
                identity: mocks_1.entityMockUtils.getIdentityInstance({
                    did,
                }),
            });
        }
        catch (err) {
            error = err;
        }
        expect(error.message).toBe('The Identity is not an Agent for the Security Token');
    }));
    test('should add an abdicate transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const token = mocks_1.entityMockUtils.getSecurityTokenInstance({
            ticker,
            permissionsGetAgents: [
                {
                    group: mocks_1.entityMockUtils.getKnownPermissionGroupInstance(),
                    agent: mocks_1.entityMockUtils.getIdentityInstance({
                        did,
                    }),
                },
            ],
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext, {
            token,
        });
        yield waivePermissions_1.prepareWaivePermissions.call(proc, {
            token,
            identity: mocks_1.entityMockUtils.getIdentityInstance({
                did,
            }),
        });
        sinon_1.default.assert.calledWith(addTransactionStub, externalAgentsAbdicateTransaction, {}, rawTicker);
    }));
    describe('prepareStorage', () => {
        test('should return the security token', () => {
            const token = mocks_1.entityMockUtils.getSecurityTokenInstance({
                ticker,
            });
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
            const boundFunc = waivePermissions_1.prepareStorage.bind(proc);
            const result = boundFunc({
                identity: mocks_1.entityMockUtils.getIdentityInstance({
                    did,
                }),
                token,
            });
            expect(result).toEqual({
                token,
            });
        });
    });
    describe('getAuthorization', () => {
        test('should return the appropriate roles and permissions', () => {
            const token = mocks_1.entityMockUtils.getSecurityTokenInstance({
                ticker,
            });
            const proc = mocks_1.procedureMockUtils.getInstance(mockContext, {
                token,
            });
            const boundFunc = waivePermissions_1.getAuthorization.bind(proc);
            expect(boundFunc({
                identity: mocks_1.entityMockUtils.getIdentityInstance({
                    did,
                }),
                token,
            })).toEqual({
                signerPermissions: {
                    transactions: [types_1.TxTags.externalAgents.Abdicate],
                    tokens: [token],
                    portfolios: [],
                },
                roles: [{ type: types_1.RoleType.Identity, did }],
            });
        });
    });
});
//# sourceMappingURL=waivePermissions.js.map