"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 internal_1 = require("../../../../../internal");
const mocks_1 = require("../../../../../testUtils/mocks");
const types_1 = require("../../../../../types");
const utilsConversionModule = __importStar(require("../../../../../utils/conversion"));
const Authorizations_1 = require("../Authorizations");
jest.mock('~/api/entities/Identity', require("../../../../../testUtils/mocks/entities").mockIdentityModule('~/api/entities/Identity'));
jest.mock('~/api/entities/AuthorizationRequest', require("../../../../../testUtils/mocks/entities").mockAuthorizationRequestModule('~/api/entities/AuthorizationRequest'));
describe('Authorizations class', () => {
    beforeAll(() => {
        mocks_1.entityMockUtils.initMocks();
        mocks_1.dsMockUtils.initMocks();
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
    });
    test('should extend namespace', () => {
        expect(Authorizations_1.Authorizations.prototype instanceof internal_1.Namespace).toBe(true);
    });
    describe('method: getReceived', () => {
        let signerValueToSignatoryStub;
        let booleanToBoolStub;
        let authorizationTypeToMeshAuthorizationTypeStub;
        afterAll(() => {
            sinon_1.default.restore();
        });
        beforeAll(() => {
            signerValueToSignatoryStub = sinon_1.default.stub(utilsConversionModule, 'signerValueToSignatory');
            booleanToBoolStub = sinon_1.default.stub(utilsConversionModule, 'booleanToBool');
            authorizationTypeToMeshAuthorizationTypeStub = sinon_1.default.stub(utilsConversionModule, 'authorizationTypeToMeshAuthorizationType');
        });
        test('should retrieve all pending authorizations received by the identity and filter out expired ones', () => __awaiter(void 0, void 0, void 0, function* () {
            const did = 'someDid';
            const filter = types_1.AuthorizationType.RotatePrimaryKey;
            const context = mocks_1.dsMockUtils.getContextInstance({ did });
            const identity = mocks_1.entityMockUtils.getIdentityInstance({ did });
            const authsNamespace = new Authorizations_1.Authorizations(identity, context);
            const rawSignatory = mocks_1.dsMockUtils.createMockSignatory();
            const rawAuthorizationType = mocks_1.dsMockUtils.createMockAuthorizationType(filter);
            /* eslint-disable @typescript-eslint/naming-convention */
            const authParams = [
                {
                    authId: new bignumber_js_1.default(1),
                    expiry: null,
                    data: { type: types_1.AuthorizationType.TransferAssetOwnership, value: 'myTicker' },
                    target: identity,
                    issuer: mocks_1.entityMockUtils.getIdentityInstance({ did: 'alice' }),
                },
                {
                    authId: new bignumber_js_1.default(2),
                    expiry: new Date('10/14/3040'),
                    data: { type: types_1.AuthorizationType.TransferAssetOwnership, value: 'otherTicker' },
                    target: identity,
                    issuer: mocks_1.entityMockUtils.getIdentityInstance({ did: 'bob' }),
                },
            ];
            const fakeAuthorizations = authParams.map(({ authId, expiry, issuer, data }) => mocks_1.dsMockUtils.createMockAuthorization({
                auth_id: mocks_1.dsMockUtils.createMockU64(authId),
                expiry: mocks_1.dsMockUtils.createMockOption(expiry ? mocks_1.dsMockUtils.createMockMoment(new bignumber_js_1.default(expiry.getTime())) : expiry),
                authorization_data: mocks_1.dsMockUtils.createMockAuthorizationData({
                    TransferAssetOwnership: mocks_1.dsMockUtils.createMockTicker(data.value),
                }),
                authorized_by: mocks_1.dsMockUtils.createMockIdentityId(issuer.did),
            }));
            signerValueToSignatoryStub.returns(rawSignatory);
            booleanToBoolStub.withArgs(true, context).returns(mocks_1.dsMockUtils.createMockBool(true));
            booleanToBoolStub.withArgs(false, context).returns(mocks_1.dsMockUtils.createMockBool(false));
            authorizationTypeToMeshAuthorizationTypeStub
                .withArgs(filter, context)
                .returns(rawAuthorizationType);
            mocks_1.dsMockUtils
                .createRpcStub('identity', 'getFilteredAuthorizations')
                .resolves(fakeAuthorizations);
            const expectedAuthorizations = authParams.map(({ authId, target, issuer, expiry, data }) => mocks_1.entityMockUtils.getAuthorizationRequestInstance({
                authId,
                issuer,
                target,
                expiry,
                data,
            }));
            let result = yield authsNamespace.getReceived();
            expect(JSON.stringify(result)).toBe(JSON.stringify(expectedAuthorizations));
            result = yield authsNamespace.getReceived({
                type: types_1.AuthorizationType.RotatePrimaryKey,
                includeExpired: false,
            });
            expect(JSON.stringify(result)).toBe(JSON.stringify(expectedAuthorizations));
        }));
    });
    describe('method: getOne', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        beforeAll(() => {
            sinon_1.default.stub(utilsConversionModule, 'signerValueToSignatory');
            sinon_1.default.stub(utilsConversionModule, 'bigNumberToU64');
        });
        test('should return the requested Authorization Request', () => __awaiter(void 0, void 0, void 0, function* () {
            const did = 'someDid';
            const issuerDid = 'alice';
            const context = mocks_1.dsMockUtils.getContextInstance({ did });
            const identity = mocks_1.entityMockUtils.getIdentityInstance({ did });
            const authsNamespace = new Authorizations_1.Authorizations(identity, context);
            const id = new bignumber_js_1.default(1);
            /* eslint-disable @typescript-eslint/naming-convention */
            const authId = new bignumber_js_1.default(1);
            const data = { type: types_1.AuthorizationType.TransferAssetOwnership, value: 'myTicker' };
            mocks_1.dsMockUtils.createQueryStub('identity', 'authorizations', {
                returnValue: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockAuthorization({
                    auth_id: mocks_1.dsMockUtils.createMockU64(authId),
                    authorization_data: mocks_1.dsMockUtils.createMockAuthorizationData({
                        TransferAssetOwnership: mocks_1.dsMockUtils.createMockTicker(data.value),
                    }),
                    expiry: mocks_1.dsMockUtils.createMockOption(),
                    authorized_by: mocks_1.dsMockUtils.createMockIdentityId(issuerDid),
                })),
            });
            const result = yield authsNamespace.getOne({ id });
            expect(result.authId).toEqual(authId);
            expect(result.expiry).toBeNull();
            expect(result.data).toEqual(data);
            expect(result.target.did).toEqual(did);
            expect(result.issuer.did).toEqual(issuerDid);
        }));
        test('should throw an error if the Authorization Request does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
            const did = 'someDid';
            const context = mocks_1.dsMockUtils.getContextInstance({ did });
            const identity = mocks_1.entityMockUtils.getIdentityInstance({ did });
            const authsNamespace = new Authorizations_1.Authorizations(identity, context);
            const id = new bignumber_js_1.default(1);
            /* eslint-disable @typescript-eslint/naming-convention */
            const authId = new bignumber_js_1.default(1);
            const data = { type: types_1.AuthorizationType.TransferAssetOwnership, value: 'myTicker' };
            const target = identity;
            const issuer = mocks_1.entityMockUtils.getIdentityInstance({ did: 'alice' });
            const authParams = {
                authId,
                expiry: null,
                data,
                target,
                issuer,
            };
            mocks_1.dsMockUtils.createQueryStub('identity', 'authorizations', {
                returnValue: mocks_1.dsMockUtils.createMockOption(),
            });
            mocks_1.entityMockUtils.getAuthorizationRequestInstance(authParams);
            return expect(authsNamespace.getOne({ id })).rejects.toThrow('The Authorization Request does not exist');
        }));
    });
});
//# sourceMappingURL=Authorizations.js.map