Skip to content

Commit

Permalink
chore: fix tests for core usage
Browse files Browse the repository at this point in the history
Signed-off-by: Mirko Mollik <[email protected]>
  • Loading branch information
cre8 committed Feb 28, 2024
1 parent 1257094 commit 9de7902
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 40 deletions.
45 changes: 17 additions & 28 deletions packages/core/src/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import { SDJwtInstance, SdJwtPayload } from '../index';
import { DisclosureFrame, Signer, Verifier } from '@sd-jwt/types';
import { Signer, Verifier } from '@sd-jwt/types';
import Crypto from 'node:crypto';
import { describe, expect, test } from 'vitest';
import { digest, generateSalt } from '@sd-jwt/crypto-nodejs';
import exp from 'node:constants';

export class TestInstance extends SDJwtInstance<SdJwtPayload> {
protected type = 'sd-jwt';

protected validateReservedFields(
disclosureFrame: DisclosureFrame<SdJwtPayload>,
): void {
return;
}
}

export const createSignerVerifier = () => {
const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
Expand All @@ -34,13 +23,13 @@ export const createSignerVerifier = () => {

describe('index', () => {
test('create', async () => {
const sdjwt = new TestInstance();
const sdjwt = new SDJwtInstance<SdJwtPayload>();
expect(sdjwt).toBeDefined();
});

test('kbJwt', async () => {
const { signer, verifier } = createSignerVerifier();
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
signAlg: 'EdDSA',
verifier,
Expand Down Expand Up @@ -78,7 +67,7 @@ describe('index', () => {

test('issue', async () => {
const { signer, verifier } = createSignerVerifier();
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
signAlg: 'EdDSA',
verifier,
Expand Down Expand Up @@ -112,7 +101,7 @@ describe('index', () => {
);
};

const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
signAlg: 'EdDSA',
verifier: failedverifier,
Expand Down Expand Up @@ -150,7 +139,7 @@ describe('index', () => {
Buffer.from(sig, 'base64url'),
);
};
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
signAlg: 'EdDSA',
verifier,
Expand Down Expand Up @@ -192,7 +181,7 @@ describe('index', () => {

test('verify with kbJwt', async () => {
const { signer, verifier } = createSignerVerifier();
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
signAlg: 'EdDSA',
verifier,
Expand Down Expand Up @@ -230,7 +219,7 @@ describe('index', () => {
});

test('Hasher not found', async () => {
const sdjwt = new TestInstance({});
const sdjwt = new SDJwtInstance<SdJwtPayload>({});
try {
const credential = await sdjwt.issue(
{
Expand All @@ -251,7 +240,7 @@ describe('index', () => {
});

test('SaltGenerator not found', async () => {
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
hasher: digest,
});
try {
Expand All @@ -274,7 +263,7 @@ describe('index', () => {
});

test('Signer not found', async () => {
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
hasher: digest,
saltGenerator: generateSalt,
});
Expand All @@ -299,7 +288,7 @@ describe('index', () => {

test('Verifier not found', async () => {
const { signer, verifier } = createSignerVerifier();
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
hasher: digest,
saltGenerator: generateSalt,
Expand Down Expand Up @@ -339,7 +328,7 @@ describe('index', () => {

test('kbSigner not found', async () => {
const { signer, verifier } = createSignerVerifier();
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
verifier,
hasher: digest,
Expand Down Expand Up @@ -377,7 +366,7 @@ describe('index', () => {

test('kbVerifier not found', async () => {
const { signer, verifier } = createSignerVerifier();
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
verifier,
hasher: digest,
Expand Down Expand Up @@ -417,7 +406,7 @@ describe('index', () => {

test('kbSignAlg not found', async () => {
const { signer, verifier } = createSignerVerifier();
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
verifier,
hasher: digest,
Expand Down Expand Up @@ -454,7 +443,7 @@ describe('index', () => {

test('hasher is not found', async () => {
const { signer } = createSignerVerifier();
const sdjwt_create = new TestInstance({
const sdjwt_create = new SDJwtInstance<SdJwtPayload>({
signer,
hasher: digest,
saltGenerator: generateSalt,
Expand All @@ -471,7 +460,7 @@ describe('index', () => {
_sd: ['foo'],
},
);
const sdjwt = new TestInstance({});
const sdjwt = new SDJwtInstance<SdJwtPayload>({});
expect(sdjwt.keys('')).rejects.toThrow('Hasher not found');
expect(sdjwt.presentableKeys('')).rejects.toThrow('Hasher not found');
expect(sdjwt.getClaims('')).rejects.toThrow('Hasher not found');
Expand All @@ -483,7 +472,7 @@ describe('index', () => {

test('presentableKeys', async () => {
const { signer } = createSignerVerifier();
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
hasher: digest,
saltGenerator: generateSalt,
Expand Down
14 changes: 2 additions & 12 deletions packages/core/test/app-e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ import path from 'path';
import { describe, expect, test } from 'vitest';
import { digest, generateSalt } from '@sd-jwt/crypto-nodejs';

export class TestInstance extends SDJwtInstance<SdJwtPayload> {
protected type = 'sd-jwt';

protected validateReservedFields(
disclosureFrame: DisclosureFrame<SdJwtPayload>,
): void {
return;
}
}

export const createSignerVerifier = () => {
const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519');
const signer: Signer = async (data: string) => {
Expand All @@ -36,7 +26,7 @@ export const createSignerVerifier = () => {
describe('App', () => {
test('Example', async () => {
const { signer, verifier } = createSignerVerifier();
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
signAlg: 'EdDSA',
verifier,
Expand Down Expand Up @@ -202,7 +192,7 @@ describe('App', () => {
async function JSONtest(filename: string) {
const test = loadTestJsonFile(filename);
const { signer, verifier } = createSignerVerifier();
const sdjwt = new TestInstance({
const sdjwt = new SDJwtInstance<SdJwtPayload>({
signer,
signAlg: 'EdDSA',
verifier,
Expand Down

0 comments on commit 9de7902

Please sign in to comment.