-
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.
Add tests for getPartySecretSignedValidator
- Loading branch information
1 parent
7b137ea
commit 0b2cd2c
Showing
3 changed files
with
94 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
80 changes: 80 additions & 0 deletions
80
.../src/Escrow/validators/common/SecretValidators/spec/getPartySecretSignedValidator.spec.ts
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,80 @@ | ||
/* eslint-disable max-nested-callbacks */ | ||
import { HDWallet } from '@xyo-network/account' | ||
import type { AccountInstance } from '@xyo-network/account-model' | ||
import type { BoundWitness } from '@xyo-network/boundwitness-model' | ||
import type { Id } from '@xyo-network/id-payload-plugin' | ||
import { IdSchema } from '@xyo-network/id-payload-plugin' | ||
import { PayloadBuilder } from '@xyo-network/payload-builder' | ||
|
||
import type { EscrowParty, EscrowTerms } from '../../../../Terms/index.ts' | ||
import { EscrowTermsSchema } from '../../../../Terms/index.ts' | ||
import { createEscrowIntentWithSecret } from '../../../../util/index.ts' | ||
import { getPartySecretSignedValidator } from '../getPartySecretSignedValidator.ts' | ||
|
||
describe('RegistrarSentinel', () => { | ||
let buyer: AccountInstance | ||
let seller: AccountInstance | ||
const baseTerms: EscrowTerms = { schema: EscrowTermsSchema } | ||
const buyerSecret: Id = { schema: IdSchema, salt: `${Date.now() - 10}` } | ||
let buyerSecretSignature: BoundWitness | ||
const sellerSecret: Id = { schema: IdSchema, salt: `${Date.now() + 10}` } | ||
let sellerSecretSignature: BoundWitness | ||
|
||
describe('getPartySecretSignedValidator', () => { | ||
beforeAll(async () => { | ||
buyer = await HDWallet.random() | ||
seller = await HDWallet.random() | ||
|
||
baseTerms.buyer = [buyer.address] | ||
baseTerms.buyerSecret = await PayloadBuilder.dataHash(buyerSecret) | ||
baseTerms.seller = [seller.address] | ||
baseTerms.sellerSecret = await PayloadBuilder.dataHash(sellerSecret) | ||
|
||
const buyerIntent = await createEscrowIntentWithSecret(baseTerms, buyerSecret, buyer) | ||
const sellerIntent = await createEscrowIntentWithSecret(baseTerms, sellerSecret, seller) | ||
|
||
buyerSecretSignature = buyerIntent[0] | ||
sellerSecretSignature = sellerIntent[0] | ||
}) | ||
const cases: EscrowParty[] = ['buyer', 'seller'] | ||
describe.each(cases)('for %s', (party) => { | ||
describe('returns true', () => { | ||
it('with valid escrow terms and values supplied', async () => { | ||
const payloads = party === 'buyer' ? [buyerSecret, buyerSecretSignature] : [sellerSecret, sellerSecretSignature] | ||
const dictionary = await PayloadBuilder.toAllHashMap(payloads) | ||
const partySecretValidator = getPartySecretSignedValidator(dictionary, party) | ||
const result = await partySecretValidator(baseTerms) | ||
expect(result).toBeTrue() | ||
}) | ||
}) | ||
describe('throws', () => { | ||
describe('with invalid terms for', () => { | ||
it('secret undefined', async () => { | ||
const payloads = party === 'buyer' ? [buyerSecret, buyerSecretSignature] : [sellerSecret, sellerSecretSignature] | ||
const terms = { ...baseTerms, [`${party}Secret`]: undefined } | ||
const dictionary = await PayloadBuilder.toAllHashMap(payloads) | ||
const partySecretValidator = getPartySecretSignedValidator(dictionary, party) | ||
const result = await partySecretValidator(terms) | ||
expect(result).toBeFalse() | ||
}) | ||
describe('with missing value for', () => { | ||
it('without boundwitness for buyerSecret', async () => { | ||
const payloads = party === 'buyer' ? [buyerSecret, buyerSecretSignature] : [sellerSecret, sellerSecretSignature] | ||
const dictionary = await PayloadBuilder.toAllHashMap(payloads) | ||
const partySecretValidator = getPartySecretSignedValidator(dictionary, party) | ||
const result = await partySecretValidator(baseTerms) | ||
expect(result).toBeTrue() | ||
}) | ||
it('without secret for buyerSecret', async () => { | ||
const payloads = party === 'buyer' ? [buyerSecret, buyerSecretSignature] : [sellerSecret, sellerSecretSignature] | ||
const dictionary = await PayloadBuilder.toAllHashMap(payloads) | ||
const partySecretValidator = getPartySecretSignedValidator(dictionary, party) | ||
const result = await partySecretValidator(baseTerms) | ||
expect(result).toBeTrue() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
13 changes: 13 additions & 0 deletions
13
...ayload/packages/payments/src/Escrow/validators/common/SecretValidators/spec/tsconfig.json
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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"moduleResolution": "NodeNext", | ||
"module": "NodeNext", | ||
"sourceMap": true, | ||
"inlineSources": true | ||
}, | ||
"extends": "@xylabs/tsconfig-jest" | ||
} |