forked from nucypher/taco-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fee055d
commit 23a026e
Showing
6 changed files
with
254 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { | ||
JsonRpcCondition, | ||
JsonRpcConditionProps, | ||
jsonRpcConditionSchema, | ||
JsonRpcConditionType, | ||
} from '../../../src/conditions/base/json-rpc'; | ||
import { testJsonRpcConditionObj } from '../../test-utils'; | ||
|
||
describe('JsonRpcCondition', () => { | ||
describe('validation', () => { | ||
it('accepts a valid schema', () => { | ||
const result = JsonRpcCondition.validate( | ||
jsonRpcConditionSchema, | ||
testJsonRpcConditionObj, | ||
); | ||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual(testJsonRpcConditionObj); | ||
}); | ||
|
||
it.each(['unsafe-url', 'http://http-url.com'])( | ||
'rejects an invalid schema', | ||
() => { | ||
const badJsonRpcObj = { | ||
...testJsonRpcConditionObj, | ||
endpoint: 'unsafe-url', | ||
}; | ||
|
||
const result = JsonRpcCondition.validate( | ||
jsonRpcConditionSchema, | ||
badJsonRpcObj, | ||
); | ||
|
||
expect(result.error).toBeDefined(); | ||
expect(result.data).toBeUndefined(); | ||
const errorMessages = result.error?.errors.map((err) => err.message); | ||
expect( | ||
errorMessages?.includes('Invalid url - must start with https://'), | ||
).toBeTruthy(); | ||
}, | ||
); | ||
|
||
describe('authorizationToken', () => { | ||
it('accepts context variable', () => { | ||
const result = JsonRpcCondition.validate(jsonRpcConditionSchema, { | ||
...testJsonRpcConditionObj, | ||
authorizationToken: ':authToken', | ||
}); | ||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual({ | ||
...testJsonRpcConditionObj, | ||
authorizationToken: ':authToken', | ||
}); | ||
}); | ||
it.each([ | ||
'authToken', | ||
'ABCDEF1234567890', | ||
':authToken?', | ||
'$:authToken', | ||
':auth-Token', | ||
])('rejects invalid context variable', (contextVar) => { | ||
const result = JsonRpcCondition.validate(jsonRpcConditionSchema, { | ||
...testJsonRpcConditionObj, | ||
authorizationToken: `${contextVar}`, | ||
}); | ||
expect(result.error).toBeDefined(); | ||
expect(result.data).toBeUndefined(); | ||
expect(result.error?.format()).toMatchObject({ | ||
authorizationToken: { | ||
_errors: ['Invalid'], | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('properties', () => { | ||
it('accepts conditions without query path', () => { | ||
const { query, ...noQueryObj } = testJsonRpcConditionObj; | ||
const result = JsonRpcCondition.validate( | ||
jsonRpcConditionSchema, | ||
noQueryObj, | ||
); | ||
|
||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual(noQueryObj); | ||
}); | ||
|
||
it('accepts conditions without params', () => { | ||
const { params, ...noParamsObj } = testJsonRpcConditionObj; | ||
const result = JsonRpcCondition.validate( | ||
jsonRpcConditionSchema, | ||
noParamsObj, | ||
); | ||
|
||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual(noParamsObj); | ||
}); | ||
|
||
it('accepts conditions with params as dictionary', () => { | ||
const result = JsonRpcCondition.validate(jsonRpcConditionSchema, { | ||
...testJsonRpcConditionObj, | ||
params: { | ||
value1: 42, | ||
value2: 23, | ||
}, | ||
}); | ||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual({ | ||
...testJsonRpcConditionObj, | ||
params: { | ||
value1: 42, | ||
value2: 23, | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('context variables', () => { | ||
it('allow context variables for various values including as substring', () => { | ||
const testJsonRpcConditionObjWithContextVars: JsonRpcConditionProps = { | ||
conditionType: JsonRpcConditionType, | ||
endpoint: 'https://math.example.com/:version/simple', | ||
method: ':methodContextVar', | ||
params: { | ||
value1: 42, | ||
value2: ':value2', | ||
}, | ||
query: '$.:queryKey', | ||
authorizationToken: ':authToken', | ||
returnValueTest: { | ||
comparator: '==', | ||
value: ':expectedResult', | ||
}, | ||
}; | ||
|
||
const result = JsonRpcCondition.validate( | ||
jsonRpcConditionSchema, | ||
testJsonRpcConditionObjWithContextVars, | ||
); | ||
expect(result.error).toBeUndefined(); | ||
expect(result.data).toEqual(testJsonRpcConditionObjWithContextVars); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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