Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Diane Huxley committed May 16, 2024
1 parent 04999ee commit c778e09
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
36 changes: 34 additions & 2 deletions test/helpers/test_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class TestData {
pfiDid = await DidJwk.create(keyManager: _pfiKeyManager);
}

static Offering getOffering() {
static Offering getOffering({
PresentationDefinition? requiredClaims,
}) {
return Offering.create(
pfiDid.uri,
OfferingData(
Expand All @@ -57,17 +59,47 @@ class TestData {
),
],
),
requiredClaims: requiredClaims,
),
);
}

static PresentationDefinition getRequiredClaims() {
// From web5-spec test vectors
const json = r'''
{
"id": "7ce4004c-3c38-4853-968b-e411bafcd945",
"input_descriptors": [
{
"id": "bbdb9b7c-5754-4f46-b63b-590bada959e0",
"constraints": {
"fields": [
{
"path": ["$.vc.type[*]"],
"filter": {
"type": "string",
"pattern": "^YoloCredential$"
}
}
]
}
}
]
}
''';

return PresentationDefinition.fromJson(jsonDecode(json));
}

static Rfq getRfq({
String? offeringId,
String? amount,
String? payinKind,
String? payoutKind,
String? to,
List<String>? claims,
}) {
claims ??= [];
return Rfq.create(
to ?? pfiDid.uri,
aliceDid.uri,
Expand All @@ -92,7 +124,7 @@ class TestData {
'cvv': '123',
}),
),
claims: [],
claims: claims,
),
);
}
Expand Down
51 changes: 47 additions & 4 deletions test/protocol/models/rfq_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:tbdex/src/protocol/models/resource.dart';
import 'package:tbdex/src/protocol/models/rfq.dart';
import 'package:test/test.dart';
import 'package:typeid/typeid.dart';
import 'package:web5/web5.dart';

import '../../helpers/test_data.dart';

Expand Down Expand Up @@ -352,10 +353,52 @@ void main() async {
});
});

test('can verify offering requirements', () {
final offering = TestData.getOffering();
final rfq = TestData.getRfq(offeringId: offering.metadata.id);
expect(() => rfq.verifyOfferingRequirements(offering), returnsNormally);
group('verifyOfferingRequirements', () {
test('can verify offering requirements', () {
final offering = TestData.getOffering();
final rfq = TestData.getRfq(offeringId: offering.metadata.id);
expect(() => rfq.verifyOfferingRequirements(offering), returnsNormally);
});

test('succeeds if claims satisfy offering required claims', () async {
final vc = VerifiableCredential.create(
// this credential fulfills the offering's required claims
type: ['YoloCredential', 'VerifiableCredential',],
issuer: TestData.aliceDid.uri,
subject: TestData.aliceDid.uri,
data: {
'beep': 'boop',
},
);
final vcJwt = await vc.sign(TestData.aliceDid);

final offering =
TestData.getOffering(requiredClaims: TestData.getRequiredClaims());
final rfq =
TestData.getRfq(offeringId: offering.metadata.id, claims: [vcJwt]);

expect(() => rfq.verifyOfferingRequirements(offering), returnsNormally);
});

test('fails if offering has no required claims', () async {
final vc = VerifiableCredential.create(
// this credential fulfills the offering's required claims
type: ['NotYoloCredential', 'VerifiableCredential',],
issuer: TestData.aliceDid.uri,
subject: TestData.aliceDid.uri,
data: {
'beep': 'boop',
},
);
final vcJwt = await vc.sign(TestData.aliceDid);

final offering =
TestData.getOffering(requiredClaims: TestData.getRequiredClaims());
final rfq =
TestData.getRfq(offeringId: offering.metadata.id, claims: [vcJwt]);

expect(() => rfq.verifyOfferingRequirements(offering), throwsException);
});
});

test('should throw exception if rfq offeringId differs from offering id',
Expand Down

0 comments on commit c778e09

Please sign in to comment.