Repository navigation
Expand file tree
/
Copy pathverify-proof.test.ts
More file actions
78 lines (64 loc) · 2.05 KB
/
Copy pathverify-proof.test.ts
File metadata and controls
78 lines (64 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { Resolvable } from "@agentcommercekit/did"
import { verifyCredential, type VerifiedCredential } from "did-jwt-vc"
import { describe, expect, it, vi } from "vitest"
import { InvalidProofError, UnsupportedProofTypeError } from "./errors"
import { verifyProof } from "./verify-proof"
vi.mock("did-jwt-vc", async () => {
const actual = await vi.importActual("did-jwt-vc")
return {
...actual,
verifyCredential: vi.fn(),
}
})
vi.mock("./verify-credential-jwt", () => ({
verifyCredentialJwt: vi.fn(),
}))
describe("verifyProof", () => {
const mockResolver = {
resolve: vi.fn(),
} as unknown as Resolvable
it("throws for invalid proof payload", async () => {
const invalidProof = {
type: "JwtProof2020",
// Missing jwt field
}
await expect(verifyProof(invalidProof, mockResolver)).rejects.toThrow(
InvalidProofError,
)
})
it("throws for unsupported proof type", async () => {
const unsupportedProof = {
type: "UnsupportedProofType",
jwt: "some.jwt.token",
}
await expect(verifyProof(unsupportedProof, mockResolver)).rejects.toThrow(
UnsupportedProofTypeError,
)
})
it("handles verification errors from verifyCredentialJwt", async () => {
const proofWithInvalidJwt = {
type: "JwtProof2020",
jwt: "invalid.jwt.token",
}
vi.mocked(verifyCredential).mockRejectedValueOnce(new Error("invalid_jwt"))
await expect(
verifyProof(proofWithInvalidJwt, mockResolver),
).rejects.toThrow(InvalidProofError)
})
it("returns the credential decoded from the verified proof", async () => {
const validProof = {
type: "JwtProof2020",
jwt: "valid.jwt.token",
}
const verifiableCredential = {
issuer: { id: "did:example:issuer" },
credentialSubject: { id: "did:example:subject" },
}
vi.mocked(verifyCredential).mockResolvedValueOnce({
verifiableCredential,
} as unknown as VerifiedCredential)
await expect(verifyProof(validProof, mockResolver)).resolves.toBe(
verifiableCredential,
)
})
})