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 { expect } from 'chai';
import { DidKeyResolver } from '../../src/index.js';
import { PermissionsGrant } from '../../src/interfaces/permissions-grant.js';
import { Secp256k1 } from '../../src/utils/secp256k1.js';
import { TestDataGenerator } from '../utils/test-data-generator.js';
import { DwnInterfaceName, DwnMethodName, Message } from '../../src/core/message.js';
describe('PermissionsGrant', () => {
    if (process.env.TESTSOFF === 'true') {
        return;
    }
    describe('create()', () => __awaiter(void 0, void 0, void 0, function* () {
        it('creates a PermissionsGrant message', () => __awaiter(void 0, void 0, void 0, function* () {
            const { privateJwk } = yield Secp256k1.generateKeyPair();
            const authorizationSignatureInput = {
                privateJwk,
                protectedHeader: {
                    alg: privateJwk.alg,
                    kid: 'did:key:bob'
                }
            };
            const { message } = yield PermissionsGrant.create({
                description: 'perms',
                grantedBy: 'did:key:bob',
                grantedTo: 'did:key:alice',
                grantedFor: 'did:key:bob',
                scope: { interface: DwnInterfaceName.Records, method: DwnMethodName.Write },
                authorizationSignatureInput
            });
            expect(message.descriptor.grantedTo).to.equal('did:key:alice');
            expect(message.descriptor.grantedBy).to.equal('did:key:bob');
            expect(message.descriptor.scope).to.eql({ interface: DwnInterfaceName.Records, method: DwnMethodName.Write });
            expect(message.descriptor.conditions).to.be.undefined;
            expect(message.descriptor.description).to.eql('perms');
        }));
    }));
    describe('createFromPermissionsRequest()', () => __awaiter(void 0, void 0, void 0, function* () {
        it('should create a PermissionsGrant from a PermissionsRequest with the same properties', () => __awaiter(void 0, void 0, void 0, function* () {
            const alice = yield TestDataGenerator.generatePersona();
            const bob = yield TestDataGenerator.generatePersona();
            const { privateJwk } = yield Secp256k1.generateKeyPair();
            const authorizationSignatureInput = {
                privateJwk,
                protectedHeader: {
                    alg: privateJwk.alg,
                    kid: alice.did
                }
            };
            const { permissionsRequest } = yield TestDataGenerator.generatePermissionsRequest({
                author: bob,
                description: 'friendship',
                grantedBy: alice.did,
                grantedFor: alice.did,
                grantedTo: bob.did,
            });
            const permissionsGrant = yield PermissionsGrant.createFromPermissionsRequest(permissionsRequest, authorizationSignatureInput);
            expect(permissionsGrant.author).to.eq(alice.did);
            expect(permissionsGrant.message.descriptor.description).to.eq(permissionsRequest.message.descriptor.description);
            expect(permissionsGrant.message.descriptor.grantedBy).to.eq(permissionsRequest.message.descriptor.grantedBy);
            expect(permissionsGrant.message.descriptor.grantedTo).to.eq(permissionsRequest.message.descriptor.grantedTo);
            expect(permissionsGrant.message.descriptor.grantedFor).to.eq(permissionsRequest.message.descriptor.grantedFor);
            expect(permissionsGrant.message.descriptor.scope).to.eq(permissionsRequest.message.descriptor.scope);
            expect(permissionsGrant.message.descriptor.conditions).to.eq(permissionsRequest.message.descriptor.conditions);
            expect(permissionsGrant.message.descriptor.permissionsRequestId).to.eq(yield Message.getCid(permissionsRequest.message));
        }));
        it('should create a PermissionsGrant from a PerimssionsRequest and overrides', () => __awaiter(void 0, void 0, void 0, function* () {
            const alice = yield DidKeyResolver.generate();
            const bob = yield DidKeyResolver.generate();
            const carol = yield DidKeyResolver.generate();
            const { privateJwk } = yield Secp256k1.generateKeyPair();
            const authorizationSignatureInput = {
                privateJwk,
                protectedHeader: {
                    alg: privateJwk.alg,
                    kid: alice.did
                }
            };
            const { permissionsRequest } = yield TestDataGenerator.generatePermissionsRequest();
            const description = 'friendship';
            const overrides = {
                description,
                grantedBy: alice.did,
                grantedTo: bob.did,
                grantedFor: carol.did,
                scope: {
                    interface: DwnInterfaceName.Protocols,
                    method: DwnMethodName.Configure,
                },
                conditions: {
                    publication: true,
                }
            };
            const permissionsGrant = yield PermissionsGrant.createFromPermissionsRequest(permissionsRequest, authorizationSignatureInput, overrides);
            expect(permissionsGrant.author).to.eq(alice.did);
            expect(permissionsGrant.message.descriptor.description).to.eq(description);
            expect(permissionsGrant.message.descriptor.grantedBy).to.eq(overrides.grantedBy);
            expect(permissionsGrant.message.descriptor.grantedTo).to.eq(overrides.grantedTo);
            expect(permissionsGrant.message.descriptor.grantedFor).to.eq(overrides.grantedFor);
            expect(permissionsGrant.message.descriptor.scope).to.eq(overrides.scope);
            expect(permissionsGrant.message.descriptor.conditions).to.eq(overrides.conditions);
            expect(permissionsGrant.message.descriptor.permissionsRequestId).to.eq(yield Message.getCid(permissionsRequest.message));
        }));
    }));
});
//# sourceMappingURL=permissions-grant.spec.js.map