Skip to content

Commit

Permalink
Add test for EIP4361AuthProvider storage expiry.
Browse files Browse the repository at this point in the history
  • Loading branch information
derekpierre committed Aug 19, 2024
1 parent fa82134 commit f997940
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion packages/taco-auth/test/auth-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
TEST_SIWE_PARAMS,
} from '@nucypher/test-utils';
import { SiweMessage } from 'siwe';
import { describe, expect, it } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { EIP4361AuthProvider } from '../src/providers';
import { EIP4361TypedDataSchema } from '../src/providers/eip4361/common';
Expand Down Expand Up @@ -52,3 +52,50 @@ describe('auth provider', () => {
).toThrow();
});
});

describe('auth provider caching', () => {
beforeEach(() => {
// tell vitest we use mocked time
vi.useFakeTimers();
});

afterEach(() => {
// restoring date after each test run
vi.useRealTimers();
});

const provider = fakeProvider(bobSecretKeyBytes);
const signer = fakeSigner(bobSecretKeyBytes);
const eip4361Provider = new EIP4361AuthProvider(
provider,
signer,
TEST_SIWE_PARAMS,
);

it('caches auth signature, but regenerates when expired', async () => {
const createAuthSignatureSpy = vi.spyOn(
eip4361Provider,
'createSIWEAuthMessage',
);

const typedSignature = await eip4361Provider.getOrCreateAuthSignature();
expect(createAuthSignatureSpy).toHaveBeenCalledTimes(1);

const typedSignatureSecondCall =
await eip4361Provider.getOrCreateAuthSignature();
// auth signature not expired, so spy is not called a 2nd time
expect(createAuthSignatureSpy).toHaveBeenCalledTimes(1);
expect(typedSignatureSecondCall).toEqual(typedSignature);

// time travel to 2h:1m in the future; auth signature is now expired
const now = new Date();
now.setHours(now.getHours() + 2, now.getMinutes() + 1);
vi.setSystemTime(now);

const typedSignatureThirdCall =
await eip4361Provider.getOrCreateAuthSignature();
// auth signature is now expired, so spy is called a 2nd time
expect(createAuthSignatureSpy).toHaveBeenCalledTimes(2);
expect(typedSignatureThirdCall).not.toEqual(typedSignature);
});
});

0 comments on commit f997940

Please sign in to comment.