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());
    });
};
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import { expect } from 'chai';
import { GeneralJwsSigner } from '../../../../src/jose/jws/general/signer.js';
import { Secp256k1 } from '../../../../src/utils/secp256k1.js';
import { DEFAULT_CONDITIONS, PermissionsRequest } from '../../../../src/interfaces/permissions/messages/permissions-request.js';
chai.use(chaiAsPromised);
describe('PermissionsRequest', () => {
    if (process.env.TESTSOFF === 'true') {
        return;
    }
    describe('constructor', () => {
        it('throws an exception if payload is not valid JSON', () => __awaiter(void 0, void 0, void 0, function* () {
            const jsonMessage = {
                descriptor: {
                    conditions: {
                        attestation: 'optional',
                        delegation: false,
                        encryption: 'optional',
                        publication: false,
                        sharedAccess: false
                    },
                    description: 'perms',
                    grantedTo: 'did:dwn:alice',
                    grantedBy: 'did:dwn:bob',
                    method: 'PermissionsRequest',
                    objectId: '331806c4-ce15-4759-b1c3-0f742312aae9',
                    scope: { method: 'RecordsWrite' }
                },
                authorization: {} // to be assigned in loop
            };
            const testVectors = [
                { input: 'bah', expectedError: 'payload is not a JSON object' },
                { input: JSON.stringify([]), expectedError: 'signed payload must be a plain object' }
            ];
            const { privateJwk } = yield Secp256k1.generateKeyPair();
            for (const vector of testVectors) {
                const payloadBytes = new TextEncoder().encode(vector.input);
                const protectedHeader = { alg: privateJwk.alg, kid: 'did:dwn:alice#key1' };
                const signer = yield GeneralJwsSigner.create(payloadBytes, [{ privateJwk, protectedHeader }]);
                const jws = signer.getJws();
                // jsonMessage['authorization'] = jws;
                jsonMessage.authorization = jws;
                yield expect(PermissionsRequest.parse(jsonMessage))
                    .to.be.rejectedWith(vector.expectedError);
            }
        }));
    });
    describe('create', () => {
        it('creates a PermissionsRequest message', () => __awaiter(void 0, void 0, void 0, function* () {
            const { privateJwk } = yield Secp256k1.generateKeyPair();
            const authorizationSignatureInput = {
                privateJwk,
                protectedHeader: {
                    alg: privateJwk.alg,
                    kid: 'did:dwn:bob'
                }
            };
            const message = yield PermissionsRequest.create({
                description: 'perms',
                grantedBy: 'did:dwn:bob',
                grantedTo: 'did:dwn:alice',
                scope: { method: 'RecordsWrite' },
                authorizationSignatureInput
            });
            expect(message.grantedTo).to.equal('did:dwn:alice');
            expect(message.grantedBy).to.equal('did:dwn:bob');
            expect(message.scope).to.eql({ method: 'RecordsWrite' });
            expect(message.conditions).to.eql(DEFAULT_CONDITIONS);
            expect(message.description).to.eql(message.description);
        }));
        it('uses default conditions if none are provided', () => __awaiter(void 0, void 0, void 0, function* () {
            const { privateJwk } = yield Secp256k1.generateKeyPair();
            const authorizationSignatureInput = {
                privateJwk,
                protectedHeader: {
                    alg: privateJwk.alg,
                    kid: 'did:dwn:bob'
                }
            };
            const message = yield PermissionsRequest.create({
                description: 'perms',
                grantedBy: 'did:dwn:bob',
                grantedTo: 'did:dwn:alice',
                scope: { method: 'RecordsWrite' },
                authorizationSignatureInput
            });
            const { conditions } = message;
            for (const conditionName in DEFAULT_CONDITIONS) {
                expect(conditions[conditionName]).to.equal(DEFAULT_CONDITIONS[conditionName]);
            }
            const numConditions = Object.keys(conditions).length;
            const numExpectedConditions = Object.keys(DEFAULT_CONDITIONS).length;
            expect(numConditions).to.equal(numExpectedConditions);
        }));
        describe('verifyAuth', () => {
            afterEach(() => {
                // restores all fakes, stubs, spies etc. not restoring causes a memory leak.
                // more info here: https://sinonjs.org/releases/v13/general-setup/
                sinon.restore();
            });
        });
    });
});
//# sourceMappingURL=permissions-request.spec.js.map