|
| 1 | +import { PublicKeySignature } from '../src/PublicKeySignature'; |
| 2 | +import type { PublicKeyChallengeConfig } from '../src/PublicKeySignature'; |
| 3 | + |
| 4 | +const defaultConfig: PublicKeyChallengeConfig = { |
| 5 | + schema: 'app_private', |
| 6 | + crypto_network: 'btc', |
| 7 | + sign_up_with_key: 'sign_up_with_key', |
| 8 | + sign_in_request_challenge: 'sign_in_request_challenge', |
| 9 | + sign_in_record_failure: 'sign_in_record_failure', |
| 10 | + sign_in_with_challenge: 'sign_in_with_challenge', |
| 11 | +}; |
| 12 | + |
| 13 | +describe('PublicKeySignature plugin factory', () => { |
| 14 | + it('returns a valid GraphileConfig.Plugin object', () => { |
| 15 | + const plugin = PublicKeySignature(defaultConfig); |
| 16 | + expect(plugin).toBeDefined(); |
| 17 | + expect(typeof plugin).toBe('object'); |
| 18 | + // extendSchema returns a plugin with a name and version |
| 19 | + expect(plugin.name).toBeDefined(); |
| 20 | + expect(typeof plugin.name).toBe('string'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('returns a plugin with schema hooks', () => { |
| 24 | + const plugin = PublicKeySignature(defaultConfig); |
| 25 | + expect(plugin.schema).toBeDefined(); |
| 26 | + expect(plugin.schema!.hooks).toBeDefined(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('accepts custom config values', () => { |
| 30 | + const customConfig: PublicKeyChallengeConfig = { |
| 31 | + schema: 'custom_schema', |
| 32 | + crypto_network: 'eth', |
| 33 | + sign_up_with_key: 'custom_signup', |
| 34 | + sign_in_request_challenge: 'custom_challenge', |
| 35 | + sign_in_record_failure: 'custom_failure', |
| 36 | + sign_in_with_challenge: 'custom_verify', |
| 37 | + }; |
| 38 | + const plugin = PublicKeySignature(customConfig); |
| 39 | + expect(plugin).toBeDefined(); |
| 40 | + expect(typeof plugin.name).toBe('string'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('default export matches named export', async () => { |
| 44 | + const mod = await import('../src/PublicKeySignature'); |
| 45 | + expect(mod.default).toBe(mod.PublicKeySignature); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe('PublicKeyChallengeConfig interface', () => { |
| 50 | + it('requires all config fields', () => { |
| 51 | + // TypeScript enforces this at compile time; this runtime check |
| 52 | + // verifies that the factory does not throw with a complete config. |
| 53 | + expect(() => PublicKeySignature(defaultConfig)).not.toThrow(); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe('PublicKeySignature config validation', () => { |
| 58 | + it('throws on invalid schema name', () => { |
| 59 | + expect(() => PublicKeySignature({ ...defaultConfig, schema: 'DROP TABLE' })).toThrow(/invalid schema/); |
| 60 | + }); |
| 61 | + |
| 62 | + it('throws on invalid function name', () => { |
| 63 | + expect(() => PublicKeySignature({ ...defaultConfig, sign_up_with_key: 'evil"; DROP' })).toThrow( |
| 64 | + /invalid sign_up_with_key/, |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('throws on invalid crypto_network value', () => { |
| 69 | + expect(() => PublicKeySignature({ ...defaultConfig, crypto_network: 'btc mainnet' })).toThrow( |
| 70 | + /invalid crypto_network/, |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('throws on function name with uppercase letters', () => { |
| 75 | + expect(() => PublicKeySignature({ ...defaultConfig, sign_in_request_challenge: 'BadName' })).toThrow( |
| 76 | + /invalid sign_in_request_challenge/, |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('throws on function name starting with a number', () => { |
| 81 | + expect(() => PublicKeySignature({ ...defaultConfig, sign_in_record_failure: '1bad' })).toThrow( |
| 82 | + /invalid sign_in_record_failure/, |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('accepts valid snake_case identifiers', () => { |
| 87 | + expect(() => PublicKeySignature(defaultConfig)).not.toThrow(); |
| 88 | + }); |
| 89 | +}); |
0 commit comments