Skip to content

Commit

Permalink
refactor: PresentationRequestSchema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martonmoro committed Nov 4, 2024
1 parent 5db5990 commit 4634545
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 71 deletions.
21 changes: 21 additions & 0 deletions tests/deserialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
} from '../src/presentation.ts';
import { zkAppAddress } from './test-utils.ts';
import { computeContext, generateContext } from '../src/context.ts';
import { PresentationRequestSchema } from '../src/validation.ts';

test('Deserialize Spec', async (t) => {
await t.test('deserializeProvable', async (t) => {
Expand Down Expand Up @@ -808,6 +809,16 @@ test('deserializePresentationRequest with context', async (t) => {
});

const serialized = PresentationRequest.toJSON(originalRequest);

const parsed = JSON.parse(serialized);

const result = PresentationRequestSchema.safeParse(parsed);
assert(
result.success,
'ZkApp presentation request should be valid: ' +
(result.success ? '' : JSON.stringify(result.error.issues, null, 2))
);

const deserialized = PresentationRequest.fromJSON<typeof originalRequest>(
'zk-app',
serialized
Expand Down Expand Up @@ -861,6 +872,16 @@ test('deserializePresentationRequest with context', async (t) => {
});

const serialized = PresentationRequest.toJSON(originalRequest);

const parsed = JSON.parse(serialized);

const result = PresentationRequestSchema.safeParse(parsed);
assert(
result.success,
'HTTPS presentation request should be valid: ' +
(result.success ? '' : JSON.stringify(result.error.issues, null, 2))
);

const deserialized = PresentationRequest.fromJSON<typeof originalRequest>(
'https',
serialized
Expand Down
10 changes: 10 additions & 0 deletions tests/presentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from './test-utils.ts';
import { Credential } from '../src/credential-index.ts';
import { Presentation, PresentationRequest } from '../src/presentation.ts';
import { PresentationRequestSchema } from '../src/validation.ts';

test('program with simple spec and signature credential', async (t) => {
const Bytes32 = Bytes(32);
Expand All @@ -36,6 +37,15 @@ test('program with simple spec and signature credential', async (t) => {
});
let json = PresentationRequest.toJSON(requestInitial);

let serialized = JSON.parse(json);

const result = PresentationRequestSchema.safeParse(serialized);
assert(
result.success,
'No-context presentation request should be valid: ' +
(result.success ? '' : JSON.stringify(result.error.issues, null, 2))
);

// wallet: deserialize and compile request
let deserialized = PresentationRequest.fromJSON('no-context', json);
let request = await Presentation.compile(deserialized);
Expand Down
71 changes: 0 additions & 71 deletions tests/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,77 +80,6 @@ test('StoredCredentialSchema validation', async (t) => {
});

test('PresentationRequestSchema validation', async (t) => {
const spec = Spec(
{
signedData: Credential.Simple({ age: Field, name: Bytes32 }),
targetAge: Claim(Field),
targetName: Constant(Bytes32, Bytes32.fromString('Alice')),
},
({ signedData, targetAge, targetName }) => ({
assert: Operation.and(
Operation.equals(Operation.property(signedData, 'age'), targetAge),
Operation.equals(Operation.property(signedData, 'name'), targetName)
),
outputClaim: Operation.property(signedData, 'age'),
})
);

await t.test('validates no-context presentation request', () => {
let requestInitial = PresentationRequest.noContext(spec, {
targetAge: Field(18),
});
let serialized = JSON.parse(PresentationRequest.toJSON(requestInitial));

const result = PresentationRequestSchema.safeParse(serialized);
assert(
result.success,
'No-context presentation request should be valid: ' +
(result.success ? '' : JSON.stringify(result.error.issues, null, 2))
);
});

await t.test('validates zk-app presentation request', () => {
const request = PresentationRequest.zkApp(
spec,
{
targetAge: Field(18),
},
{
action: Field(123), // Mock method ID + args hash
}
);

const serialized = JSON.parse(PresentationRequest.toJSON(request));

const result = PresentationRequestSchema.safeParse(serialized);
assert(
result.success,
'ZkApp presentation request should be valid: ' +
(result.success ? '' : JSON.stringify(result.error.issues, null, 2))
);
});

await t.test('validates https presentation request', () => {
const request = PresentationRequest.https(
spec,
{
targetAge: Field(18),
},
{
action: 'POST /api/verify',
}
);

const serialized = JSON.parse(PresentationRequest.toJSON(request));

const result = PresentationRequestSchema.safeParse(serialized);
assert(
result.success,
'HTTPS presentation request should be valid: ' +
(result.success ? '' : JSON.stringify(result.error.issues, null, 2))
);
});

await t.test('validates complex presentation request', () => {
const InputData = { age: Field, name: Bytes32 };
const NestedInputData = { person: InputData, points: Field };
Expand Down

0 comments on commit 4634545

Please sign in to comment.