"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 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");
jest.mock('~/base/Procedure', require("../../../testUtils/mocks/procedure").mockProcedureModule('~/base/Procedure'));
describe('AuthorizationRequest class', () => {
    let context;
    beforeAll(() => {
        mocks_1.dsMockUtils.initMocks();
        mocks_1.procedureMockUtils.initMocks();
    });
    beforeEach(() => {
        context = mocks_1.dsMockUtils.getContextInstance();
    });
    afterEach(() => {
        mocks_1.dsMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.dsMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
    });
    test('should extend Entity', () => {
        expect(internal_1.AuthorizationRequest.prototype instanceof internal_1.Entity).toBe(true);
    });
    describe('constructor', () => {
        test('should assign target did, issuer did, expiry and data to instance', () => {
            const targetDid = 'someDid';
            const issuerDid = 'otherDid';
            const targetIdentity = new internal_1.Identity({ did: targetDid }, context);
            const issuerIdentity = new internal_1.Identity({ did: issuerDid }, context);
            const expiry = new Date();
            const data = 'something';
            const authRequest = new internal_1.AuthorizationRequest({ target: targetIdentity, issuer: issuerIdentity, expiry, data, authId: new bignumber_js_1.default(1) }, context);
            expect(authRequest.target).toEqual(targetIdentity);
            expect(authRequest.issuer).toEqual(issuerIdentity);
            expect(authRequest.expiry).toBe(expiry);
            expect(authRequest.data).toBe(data);
        });
    });
    describe('method: isUniqueIdentifiers', () => {
        test('should return true if the object conforms to the interface', () => {
            expect(internal_1.AuthorizationRequest.isUniqueIdentifiers({ authId: new bignumber_js_1.default(1) })).toBe(true);
            expect(internal_1.AuthorizationRequest.isUniqueIdentifiers({})).toBe(false);
            expect(internal_1.AuthorizationRequest.isUniqueIdentifiers({ authId: 3 })).toBe(false);
        });
    });
    describe('method: accept', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should prepare the consumeAuthorizationRequests procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const authorizationRequest = new internal_1.AuthorizationRequest({
                authId: new bignumber_js_1.default(1),
                expiry: null,
                target: new internal_1.Identity({ did: 'someDid' }, context),
                issuer: new internal_1.Identity({ did: 'otherDid' }, context),
                data: { type: types_1.AuthorizationType.RotatePrimaryKey },
            }, context);
            const args = {
                accept: true,
                authRequests: [authorizationRequest],
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield authorizationRequest.accept();
            expect(queue).toBe(expectedQueue);
        }));
        test('should prepare the consumeJoinIdentityAuthorization procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const authorizationRequest = new internal_1.AuthorizationRequest({
                authId: new bignumber_js_1.default(1),
                expiry: null,
                target: new internal_1.Identity({ did: 'someDid' }, context),
                issuer: new internal_1.Identity({ did: 'otherDid' }, context),
                data: {
                    type: types_1.AuthorizationType.JoinIdentity,
                    value: {
                        tokens: null,
                        transactions: null,
                        transactionGroups: [],
                        portfolios: null,
                    },
                },
            }, context);
            const args = {
                authRequest: authorizationRequest,
                accept: true,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield authorizationRequest.accept();
            expect(queue).toBe(expectedQueue);
        }));
        test('should prepare the consumeAddMultiSigSignerAuthorization procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const authorizationRequest = new internal_1.AuthorizationRequest({
                authId: new bignumber_js_1.default(1),
                expiry: null,
                target: new internal_1.Identity({ did: 'someDid' }, context),
                issuer: new internal_1.Identity({ did: 'otherDid' }, context),
                data: {
                    type: types_1.AuthorizationType.AddMultiSigSigner,
                    value: 'someAddress',
                },
            }, context);
            const args = {
                authRequest: authorizationRequest,
                accept: true,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield authorizationRequest.accept();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: remove', () => {
        afterAll(() => {
            sinon_1.default.restore();
        });
        test('should prepare the consumeAuthorizationRequest procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const authorizationRequest = new internal_1.AuthorizationRequest({
                authId: new bignumber_js_1.default(1),
                expiry: null,
                target: new internal_1.Identity({ did: 'someDid' }, context),
                issuer: new internal_1.Identity({ did: 'otherDid' }, context),
                data: { type: types_1.AuthorizationType.RotatePrimaryKey },
            }, context);
            const args = {
                accept: false,
                authRequests: [authorizationRequest],
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield authorizationRequest.remove();
            expect(queue).toBe(expectedQueue);
        }));
        test('should prepare the consumeJoinIdentityAuthorization procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const authorizationRequest = new internal_1.AuthorizationRequest({
                authId: new bignumber_js_1.default(1),
                expiry: null,
                target: new internal_1.Identity({ did: 'someDid' }, context),
                issuer: new internal_1.Identity({ did: 'otherDid' }, context),
                data: {
                    type: types_1.AuthorizationType.JoinIdentity,
                    value: {
                        tokens: null,
                        transactions: null,
                        transactionGroups: [],
                        portfolios: null,
                    },
                },
            }, context);
            const args = {
                authRequest: authorizationRequest,
                accept: false,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield authorizationRequest.remove();
            expect(queue).toBe(expectedQueue);
        }));
        test('should prepare the consumeAddMultiSigSignerAuthorization procedure with the correct arguments and context, and return the resulting transaction queue', () => __awaiter(void 0, void 0, void 0, function* () {
            const authorizationRequest = new internal_1.AuthorizationRequest({
                authId: new bignumber_js_1.default(1),
                expiry: null,
                target: new internal_1.Identity({ did: 'someDid' }, context),
                issuer: new internal_1.Identity({ did: 'otherDid' }, context),
                data: {
                    type: types_1.AuthorizationType.AddMultiSigSigner,
                    value: 'someAddress',
                },
            }, context);
            const args = {
                authRequest: authorizationRequest,
                accept: false,
            };
            const expectedQueue = 'someQueue';
            mocks_1.procedureMockUtils
                .getPrepareStub()
                .withArgs({ args, transformer: undefined }, context)
                .resolves(expectedQueue);
            const queue = yield authorizationRequest.remove();
            expect(queue).toBe(expectedQueue);
        }));
    });
    describe('method: isExpired', () => {
        test('should return whether the request has expired', () => {
            const authorizationRequest = new internal_1.AuthorizationRequest({
                authId: new bignumber_js_1.default(1),
                expiry: new Date('10/14/1987 UTC'),
                target: new internal_1.Identity({ did: 'someDid' }, context),
                issuer: new internal_1.Identity({ did: 'otherDid' }, context),
                data: { type: types_1.AuthorizationType.RotatePrimaryKey },
            }, context);
            expect(authorizationRequest.isExpired()).toBe(true);
            authorizationRequest.expiry = null;
            expect(authorizationRequest.isExpired()).toBe(false);
        });
    });
    describe('method: exists', () => {
        test('should return whether the request exists', () => __awaiter(void 0, void 0, void 0, function* () {
            const authorizationRequest = new internal_1.AuthorizationRequest({
                authId: new bignumber_js_1.default(1),
                expiry: new Date('10/14/1987 UTC'),
                target: new internal_1.Identity({ did: 'someDid' }, context),
                issuer: new internal_1.Identity({ did: 'otherDid' }, context),
                data: { type: types_1.AuthorizationType.RotatePrimaryKey },
            }, context);
            mocks_1.dsMockUtils.createQueryStub('identity', 'authorizations', {
                returnValue: mocks_1.dsMockUtils.createMockOption(),
            });
            yield expect(authorizationRequest.exists()).resolves.toBe(false);
            mocks_1.dsMockUtils.createQueryStub('identity', 'authorizations', {
                returnValue: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockAuthorization({
                    /* eslint-disable @typescript-eslint/naming-convention */
                    auth_id: 1,
                    authorization_data: mocks_1.dsMockUtils.createMockAuthorizationData('RotatePrimaryKey'),
                    authorized_by: 'someDid',
                    expiry: mocks_1.dsMockUtils.createMockOption(),
                    /* eslint-enable @typescript-eslint/naming-convention */
                })),
            });
            return expect(authorizationRequest.exists()).resolves.toBe(true);
        }));
    });
    describe('method: toJson', () => {
        test('should return a human readable version of the entity', () => {
            const authorizationRequest = new internal_1.AuthorizationRequest({
                authId: new bignumber_js_1.default(1),
                expiry: new Date('10/14/1987 UTC'),
                target: new internal_1.Identity({ did: 'someDid' }, context),
                issuer: new internal_1.Identity({ did: 'otherDid' }, context),
                data: { type: types_1.AuthorizationType.RotatePrimaryKey },
            }, context);
            expect(authorizationRequest.toJson()).toEqual({
                id: '1',
                expiry: '1987-10-14T00:00:00.000Z',
                target: {
                    type: types_1.SignerType.Identity,
                    value: 'someDid',
                },
                issuer: 'otherDid',
                data: { type: types_1.AuthorizationType.RotatePrimaryKey },
            });
        });
    });
});
//# sourceMappingURL=AuthorizationRequest.js.map