Skip to content

Commit

Permalink
test: handle new provider options
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas committed Aug 22, 2023
1 parent d1ff3eb commit f49c839
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
8 changes: 4 additions & 4 deletions test/keystore/providers/KeyGeneratorKeystoreProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('KeyGeneratorKeystoreProvider', () => {
it('creates a key when wallet supplied', async () => {
const provider = new KeyGeneratorKeystoreProvider()
const keystore = await provider.newKeystore(
testProviderOptions(),
testProviderOptions({}),
apiClient,
wallet
)
Expand All @@ -28,7 +28,7 @@ describe('KeyGeneratorKeystoreProvider', () => {
it('throws KeystoreProviderUnavailableError when no wallet supplied', async () => {
const provider = new KeyGeneratorKeystoreProvider()
const prom = provider.newKeystore(
testProviderOptions(),
testProviderOptions({}),
apiClient,
undefined
)
Expand All @@ -39,7 +39,7 @@ describe('KeyGeneratorKeystoreProvider', () => {
const provider = new KeyGeneratorKeystoreProvider()
const preCreateIdentityCallback = jest.fn()
const keystore = await provider.newKeystore(
{ ...testProviderOptions(), preCreateIdentityCallback },
{ ...testProviderOptions({}), preCreateIdentityCallback },
apiClient,
wallet
)
Expand All @@ -51,7 +51,7 @@ describe('KeyGeneratorKeystoreProvider', () => {
const provider = new KeyGeneratorKeystoreProvider()
const preEnableIdentityCallback = jest.fn()
const keystore = await provider.newKeystore(
{ ...testProviderOptions(), preEnableIdentityCallback },
{ ...testProviderOptions({}), preEnableIdentityCallback },
apiClient,
wallet
)
Expand Down
5 changes: 3 additions & 2 deletions test/keystore/providers/NetworkKeyManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { buildPersistenceFromOptions } from '../../../src/keystore/providers/hel
import NetworkKeyManager from '../../../src/keystore/providers/NetworkKeyManager'
import { Signer } from '../../../src/types/Signer'
import { newWallet, pollFor, sleep, wrapAsLedgerWallet } from '../../helpers'
import { testProviderOptions } from './helpers'

describe('NetworkKeyManager', () => {
let wallet: Signer
Expand Down Expand Up @@ -100,13 +101,13 @@ describe('NetworkKeyManager', () => {
it('respects the options provided', async () => {
const bundle = await PrivateKeyBundleV1.generate(wallet)
const shouldBeUndefined = await buildPersistenceFromOptions(
{ persistConversations: false, env: 'local' },
testProviderOptions({ persistConversations: false }),
bundle
)
expect(shouldBeUndefined).toBeUndefined()

const shouldBeDefined = await buildPersistenceFromOptions(
{ persistConversations: true, env: 'local' },
testProviderOptions({ persistConversations: true }),
bundle
)
expect(shouldBeDefined).toBeInstanceOf(PrefixedPersistence)
Expand Down
8 changes: 4 additions & 4 deletions test/keystore/providers/NetworkKeystoreProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('NetworkKeystoreProvider', () => {
it('fails gracefully when no keys are found', async () => {
const provider = new NetworkKeystoreProvider()
expect(
provider.newKeystore(testProviderOptions(), apiClient, wallet)
provider.newKeystore(testProviderOptions({}), apiClient, wallet)
).rejects.toThrow(KeystoreProviderUnavailableError)
})

Expand All @@ -41,7 +41,7 @@ describe('NetworkKeystoreProvider', () => {

const provider = new NetworkKeystoreProvider()
const keystore = await provider.newKeystore(
testProviderOptions(),
testProviderOptions({}),
apiClient,
wallet
)
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('NetworkKeystoreProvider', () => {
// Now try and load it
const provider = new NetworkKeystoreProvider()
const keystore = await provider.newKeystore(
testProviderOptions(),
testProviderOptions({}),
apiClient,
wallet
)
Expand All @@ -91,7 +91,7 @@ describe('NetworkKeystoreProvider', () => {
const provider = new NetworkKeystoreProvider()
const mockNotifier = jest.fn()
await provider.newKeystore(
{ ...testProviderOptions(), preEnableIdentityCallback: mockNotifier },
{ ...testProviderOptions({}), preEnableIdentityCallback: mockNotifier },
apiClient,
wallet
)
Expand Down
35 changes: 21 additions & 14 deletions test/keystore/providers/StaticKeystoreProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { privateKey } from '@xmtp/proto'
import { PrivateKeyBundleV1 } from '../../../src/crypto'
import { newWallet } from '../../helpers'
import { testProviderOptions } from './helpers'
import StaticKeystoreProvider from '../../../src/keystore/providers/StaticKeystoreProvider'
import { KeystoreProviderUnavailableError } from '../../../src/keystore/providers/errors'

Expand All @@ -14,31 +15,37 @@ describe('StaticKeystoreProvider', () => {
v2: undefined,
}).finish()
const provider = new StaticKeystoreProvider()
const keystore = await provider.newKeystore({
privateKeyOverride: keyBytes,
env: ENV,
persistConversations: false,
})
const keystore = await provider.newKeystore(
testProviderOptions({
privateKeyOverride: keyBytes,
env: ENV,
persistConversations: false,
})
)

expect(keystore).not.toBeNull()
})

it('throws with an unset key', async () => {
expect(
new StaticKeystoreProvider().newKeystore({
env: ENV,
persistConversations: false,
})
new StaticKeystoreProvider().newKeystore(
testProviderOptions({
env: ENV,
persistConversations: false,
})
)
).rejects.toThrow(KeystoreProviderUnavailableError)
})

it('fails with an invalid key', async () => {
expect(
new StaticKeystoreProvider().newKeystore({
privateKeyOverride: Uint8Array.from([1, 2, 3]),
env: ENV,
persistConversations: false,
})
new StaticKeystoreProvider().newKeystore(
testProviderOptions({
privateKeyOverride: Uint8Array.from([1, 2, 3]),
env: ENV,
persistConversations: false,
})
)
).rejects.toThrow()
})
})
10 changes: 6 additions & 4 deletions test/keystore/providers/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { LocalStoragePersistence } from '../../../src'
import { KeystoreProviderOptions } from '../../../src/keystore/providers'

export const testProviderOptions = (
export const testProviderOptions = ({
privateKeyOverride = undefined,
persistConversations = false,
basePersistence = new LocalStoragePersistence()
) => ({
env: 'local' as const,
basePersistence = new LocalStoragePersistence(),
env = 'local' as const,
}: Partial<KeystoreProviderOptions>) => ({
env,
persistConversations,
privateKeyOverride,
basePersistence,
Expand Down

0 comments on commit f49c839

Please sign in to comment.