Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
shamilovtim committed Oct 23, 2024
1 parent 30b2109 commit e0a16cf
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions packages/api/tests/web5.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,6 @@ describe('web5 api', () => {
});

it('should request all permissions for a protocol if no specific permissions are provided', async () => {

sinon.stub(Web5UserAgent, 'create').resolves(testHarness.agent as Web5UserAgent);

const createPermissionRequestForProtocolSpy = sinon.spy(WalletConnect, 'createPermissionRequestForProtocol');
Expand All @@ -818,7 +817,6 @@ describe('web5 api', () => {
};

try {

await Web5.connect({
walletConnectOptions: {
displayName : 'Sample App',
Expand All @@ -836,21 +834,23 @@ describe('web5 api', () => {
expect(error.message).to.include('Failed to connect to wallet');

expect(createPermissionRequestForProtocolSpy.callCount).to.equal(1);
const call = createPermissionRequestForProtocolSpy.getCall(0);

// since no explicit permissions were provided, all permissions should be requested
expect(call.args[0].permissions).to.have.members([
'read', 'write', 'delete', 'query', 'subscribe'
const result = createPermissionRequestForProtocolSpy.getCall(0).returnValue;

// Check if all permissions are included in the result
expect(result.permissionScopes).to.deep.include.members([
{ protocol: protocolDefinition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Read },
{ protocol: protocolDefinition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Write },
{ protocol: protocolDefinition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Delete },
{ protocol: protocolDefinition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Query },
{ protocol: protocolDefinition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Subscribe }
]);
}
});

it('should only request the specified permissions for a protocol', async () => {

sinon.stub(Web5UserAgent, 'create').resolves(testHarness.agent as Web5UserAgent);

// spy on the WalletConnect createPermissionRequestForProtocol method
const requestPermissionsSpy = sinon.spy(WalletConnect, 'createPermissionRequestForProtocol');
const createPermissionRequestForProtocolSpy = sinon.spy(WalletConnect, 'createPermissionRequestForProtocol');

// stub the cleanUpIdentity method to avoid actual cleanup
sinon.stub(Web5 as any, 'cleanUpIdentity').resolves();
Expand Down Expand Up @@ -883,9 +883,7 @@ describe('web5 api', () => {
}
};


try {

await Web5.connect({
walletConnectOptions: {
displayName : 'Sample App',
Expand All @@ -894,8 +892,8 @@ describe('web5 api', () => {
validatePin : async () => { return '1234'; },
onWalletUriReady : (_walletUri: string) => {},
permissionRequests : [
{ protocolDefinition: protocol1Definition }, // no permissions provided, expect all permissions to be requested
{ protocolDefinition: protocol2Definition, permissions: ['read', 'write'] } // only read and write permissions provided
{ protocolDefinition: protocol1Definition },
{ protocolDefinition: protocol2Definition, permissions: ['read', 'write'] }
]
}
});
Expand All @@ -905,20 +903,28 @@ describe('web5 api', () => {
// we expect an error because we aren't testing the whole e2e flow
expect(error.message).to.include('Failed to connect to wallet');

// The `createPermissionRequestForProtocol` method should have been called once for each provided request
expect(requestPermissionsSpy.callCount).to.equal(2);
const call1 = requestPermissionsSpy.getCall(0);

// since no explicit permissions were provided for the first protocol, all permissions should be requested
expect(call1.args[0].permissions).to.have.members([
'read', 'write', 'delete', 'query', 'subscribe'
expect(createPermissionRequestForProtocolSpy.callCount).to.equal(2);
const result1 = createPermissionRequestForProtocolSpy.getCall(0).returnValue;
const result2 = createPermissionRequestForProtocolSpy.getCall(1).returnValue;

// Check if all permissions are included for the first protocol
expect(result1.permissionScopes).to.deep.include.members([
{ protocol: protocol1Definition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Read },
{ protocol: protocol1Definition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Write },
{ protocol: protocol1Definition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Delete },
{ protocol: protocol1Definition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Query },
{ protocol: protocol1Definition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Subscribe }
]);

const call2 = requestPermissionsSpy.getCall(1);

// only the provided permissions should be requested for the second protocol
expect(call2.args[0].permissions).to.have.members([
'read', 'write'
// Check if only read and write permissions are included for the second protocol
expect(result2.permissionScopes).to.deep.include.members([
{ protocol: protocol2Definition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Read },
{ protocol: protocol2Definition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Write }
]);
expect(result2.permissionScopes).to.not.deep.include.members([
{ protocol: protocol2Definition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Delete },
{ protocol: protocol2Definition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Query },
{ protocol: protocol2Definition.protocol, interface: DwnInterfaceName.Records, method: DwnMethodName.Subscribe }
]);
}
});
Expand Down

0 comments on commit e0a16cf

Please sign in to comment.