-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate PermissionController and SubjectMetadataController (#692)
* Initial copy-paste * Fix a bunch of imports etc * Fix some typing issues * Fix another type issue * Fix linting issues + add missing doc strings * Run Prettier on README * Fix some test typing issues + ignore others * Revert a recent change * Remove unused function * Remove remains of bold formatting * Fix usage of messengers to improve test typing * Fix some issues with JSDocs * Add util function tests * Use @metamask/types for a bunch of shared types * Fix a few import issues * Remove unnecessary ts-expect-error comments
- Loading branch information
1 parent
792d86c
commit 89c0050
Showing
29 changed files
with
9,688 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import * as errors from './errors'; | ||
import { decorateWithCaveats, PermissionConstraint } from '.'; | ||
|
||
describe('decorateWithCaveats', () => { | ||
it('decorates a method with caveat', async () => { | ||
const methodImplementation = () => [1, 2, 3]; | ||
|
||
const caveatSpecifications = { | ||
reverse: { | ||
type: 'reverse', | ||
decorator: (method: any, _caveat: any) => async () => { | ||
return (await method()).reverse(); | ||
}, | ||
}, | ||
}; | ||
|
||
const permission: PermissionConstraint = { | ||
id: 'foo', | ||
parentCapability: 'arbitraryMethod', | ||
invoker: 'arbitraryInvoker', | ||
date: Date.now(), | ||
caveats: [{ type: 'reverse', value: null }], | ||
}; | ||
|
||
const decorated = decorateWithCaveats( | ||
methodImplementation, | ||
permission, | ||
caveatSpecifications, | ||
); | ||
|
||
expect(methodImplementation()).toStrictEqual([1, 2, 3]); | ||
expect( | ||
await decorated({ | ||
method: 'arbitraryMethod', | ||
context: { origin: 'metamask.io' }, | ||
}), | ||
).toStrictEqual([3, 2, 1]); | ||
}); | ||
|
||
it('decorates a method with multiple caveats', async () => { | ||
const methodImplementation = () => [1, 2, 3]; | ||
|
||
const caveatSpecifications = { | ||
reverse: { | ||
type: 'reverse', | ||
decorator: (method: any, _caveat: any) => async () => { | ||
return (await method()).reverse(); | ||
}, | ||
}, | ||
slice: { | ||
type: 'slice', | ||
decorator: (method: any, caveat: any) => async () => { | ||
return (await method()).slice(0, caveat.value); | ||
}, | ||
}, | ||
}; | ||
|
||
const permission: PermissionConstraint = { | ||
id: 'foo', | ||
parentCapability: 'arbitraryMethod', | ||
invoker: 'arbitraryInvoker', | ||
date: Date.now(), | ||
caveats: [ | ||
{ type: 'reverse', value: null }, | ||
{ type: 'slice', value: 1 }, | ||
], | ||
}; | ||
|
||
const decorated = decorateWithCaveats( | ||
methodImplementation, | ||
permission, | ||
caveatSpecifications, | ||
); | ||
|
||
expect(methodImplementation()).toStrictEqual([1, 2, 3]); | ||
expect( | ||
await decorated({ | ||
method: 'arbitraryMethod', | ||
context: { origin: 'metamask.io' }, | ||
}), | ||
).toStrictEqual([3]); | ||
}); | ||
|
||
it('returns the unmodified method implementation if there are no caveats', () => { | ||
const methodImplementation = () => [1, 2, 3]; | ||
|
||
const permission: PermissionConstraint = { | ||
id: 'foo', | ||
parentCapability: 'arbitraryMethod', | ||
invoker: 'arbitraryInvoker', | ||
date: Date.now(), | ||
caveats: null, | ||
}; | ||
|
||
const decorated = decorateWithCaveats(methodImplementation, permission, {}); | ||
expect(methodImplementation()).toStrictEqual( | ||
decorated({ | ||
method: 'arbitraryMethod', | ||
context: { origin: 'metamask.io' }, | ||
}), | ||
); | ||
}); | ||
|
||
it('throws an error if the caveat type is unrecognized', () => { | ||
const methodImplementation = () => [1, 2, 3]; | ||
|
||
const caveatSpecifications = { | ||
reverse: { | ||
type: 'reverse', | ||
decorator: (method: any, _caveat: any) => async () => { | ||
return (await method()).reverse(); | ||
}, | ||
}, | ||
}; | ||
|
||
const permission: PermissionConstraint = { | ||
id: 'foo', | ||
parentCapability: 'arbitraryMethod', | ||
invoker: 'arbitraryInvoker', | ||
date: Date.now(), | ||
// This type doesn't exist | ||
caveats: [{ type: 'kaplar', value: null }], | ||
}; | ||
|
||
expect(() => | ||
decorateWithCaveats( | ||
methodImplementation, | ||
permission, | ||
caveatSpecifications, | ||
)({ | ||
method: 'arbitraryMethod', | ||
context: { origin: 'metamask.io' }, | ||
}), | ||
).toThrow(new errors.UnrecognizedCaveatTypeError('kaplar')); | ||
}); | ||
}); |
Oops, something went wrong.