|
1 |
| -import { bech32 } from 'bech32'; |
2 |
| -import { ec as EC } from 'elliptic'; |
3 |
| -import { testUserLnUrlLogin } from '../testUserLnUrl'; |
4 |
| - |
5 |
| -jest.mock('bech32'); |
6 |
| -jest.mock('elliptic'); |
7 |
| - |
8 |
| -describe('testUserLnUrl', () => { |
9 |
| - const mockDigest = jest.fn(); |
10 |
| - const originalCrypto = global.crypto; |
11 |
| - const mockConsoleLog = jest.spyOn(console, 'log').mockImplementation(); |
12 |
| - |
13 |
| - beforeEach(() => { |
14 |
| - global.TextEncoder = jest.fn().mockImplementation(() => ({ |
15 |
| - encode: jest.fn().mockReturnValue(new Uint8Array([1, 2, 3, 4])) |
16 |
| - })) as any; |
17 |
| - |
18 |
| - global.TextDecoder = jest.fn().mockImplementation(() => ({ |
19 |
| - decode: () => 'https://example.com/lnurl-auth?k1=abcdef' |
20 |
| - })) as any; |
21 |
| - |
22 |
| - global.crypto = { |
23 |
| - ...originalCrypto, |
24 |
| - subtle: { |
25 |
| - digest: mockDigest |
26 |
| - } |
27 |
| - } as unknown as Crypto; |
28 |
| - |
29 |
| - const mockHashBuffer = new Uint8Array(32).fill(1); |
30 |
| - mockDigest.mockResolvedValue(mockHashBuffer.buffer); |
31 |
| - |
32 |
| - (bech32.decode as jest.Mock).mockReturnValue({ |
33 |
| - words: [1, 2, 3, 4] |
34 |
| - }); |
35 |
| - (bech32.fromWords as jest.Mock).mockReturnValue( |
36 |
| - new Uint8Array([ |
37 |
| - 104, 116, 116, 112, 115, 58, 47, 47, 101, 120, 97, 109, 112, 108, 101, 46, 99, 111, 109, 47, |
38 |
| - 108, 110, 117, 114, 108, 45, 97, 117, 116, 104, 63, 107, 49, 61, 97, 98, 99, 100, 101, 102 |
39 |
| - ]) |
40 |
| - ); |
41 |
| - |
42 |
| - const mockSign = jest.fn().mockReturnValue({ |
43 |
| - toDER: () => new Uint8Array([1, 2, 3, 4]) |
44 |
| - }); |
45 |
| - const mockGetPublic = jest.fn().mockReturnValue({ |
46 |
| - encode: () => 'mock-pubkey' |
47 |
| - }); |
48 |
| - const mockKeyFromPrivate = jest.fn().mockReturnValue({ |
49 |
| - getPublic: mockGetPublic, |
50 |
| - sign: mockSign |
51 |
| - }); |
52 |
| - (EC as jest.Mock).mockImplementation(() => ({ |
53 |
| - keyFromPrivate: mockKeyFromPrivate, |
54 |
| - sign: mockSign |
55 |
| - })); |
56 |
| - |
57 |
| - global.fetch = jest.fn().mockResolvedValue({ |
58 |
| - ok: true, |
59 |
| - json: () => Promise.resolve({ status: 'OK' }) |
60 |
| - }); |
61 |
| - |
62 |
| - jest.spyOn(Date, 'now').mockReturnValue(1234567890); |
63 |
| - }); |
64 |
| - |
65 |
| - afterEach(() => { |
66 |
| - jest.clearAllMocks(); |
67 |
| - global.crypto = originalCrypto; |
68 |
| - global.fetch = undefined as any; |
69 |
| - global.TextEncoder = undefined as any; |
70 |
| - global.TextDecoder = undefined as any; |
71 |
| - mockConsoleLog.mockRestore(); |
72 |
| - }); |
73 |
| - |
74 |
| - it('should successfully process a valid lnurl login', async () => { |
75 |
| - await testUserLnUrlLogin('lnurl1234567'); |
76 |
| - |
77 |
| - expect(bech32.decode).toHaveBeenCalledWith('lnurl1234567', 1500); |
78 |
| - expect(bech32.fromWords).toHaveBeenCalledWith([1, 2, 3, 4]); |
79 |
| - |
80 |
| - expect(mockDigest).toHaveBeenCalled(); |
81 |
| - |
82 |
| - expect(global.fetch).toHaveBeenCalledWith( |
83 |
| - expect.stringContaining('https://example.com/lnurl-auth?k1=abcdef&sig=') |
84 |
| - ); |
85 |
| - expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining('&key=mock-pubkey')); |
86 |
| - expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining('&t=1234567890')); |
87 |
| - |
88 |
| - expect(mockConsoleLog).toHaveBeenCalledWith({ status: 'OK' }); |
89 |
| - }); |
90 |
| - |
91 |
| - it('should handle invalid bech32 input', async () => { |
92 |
| - (bech32.decode as jest.Mock).mockImplementation(() => { |
93 |
| - throw new Error('Invalid bech32'); |
94 |
| - }); |
95 |
| - |
96 |
| - await expect(testUserLnUrlLogin('invalid')).rejects.toThrow('Invalid bech32'); |
97 |
| - }); |
98 |
| - |
99 |
| - it('should handle fetch error', async () => { |
100 |
| - (global.fetch as jest.Mock).mockRejectedValue(new Error('Network error')); |
101 |
| - |
102 |
| - await expect(testUserLnUrlLogin('lnurl1234567')).rejects.toThrow('Network error'); |
103 |
| - }); |
104 |
| - |
105 |
| - it('should handle non-OK response', async () => { |
106 |
| - (global.fetch as jest.Mock).mockResolvedValue({ |
107 |
| - ok: false, |
108 |
| - status: 404, |
109 |
| - statusText: 'Not Found' |
110 |
| - }); |
111 |
| - |
112 |
| - await testUserLnUrlLogin('lnurl1234567'); |
113 |
| - |
114 |
| - expect(mockConsoleLog).not.toHaveBeenCalled(); |
115 |
| - }); |
116 |
| - |
117 |
| - it('should handle empty k1 parameter', async () => { |
118 |
| - global.TextDecoder = jest.fn().mockImplementation(() => ({ |
119 |
| - decode: () => 'https://example.com/lnurl-auth' |
120 |
| - })) as any; |
121 |
| - |
122 |
| - await testUserLnUrlLogin('lnurl1234567'); |
123 |
| - |
124 |
| - expect(global.fetch).toHaveBeenCalled(); |
125 |
| - }); |
126 |
| - |
127 |
| - it('should handle hexToBytes with odd length input', async () => { |
128 |
| - global.TextDecoder = jest.fn().mockImplementation(() => ({ |
129 |
| - decode: () => 'https://example.com/lnurl-auth?k1=abc' |
130 |
| - })) as any; |
131 |
| - |
132 |
| - await expect(testUserLnUrlLogin('lnurl1234567')).rejects.toThrow( |
133 |
| - 'Hex string must have an even length' |
134 |
| - ); |
135 |
| - }); |
136 |
| - |
137 |
| - it('should handle hexToBytes with valid input', async () => { |
138 |
| - global.TextDecoder = jest.fn().mockImplementation(() => ({ |
139 |
| - decode: () => 'https://example.com/lnurl-auth?k1=abcd' |
140 |
| - })) as any; |
141 |
| - |
142 |
| - await testUserLnUrlLogin('lnurl1234567'); |
143 |
| - |
144 |
| - expect(global.fetch).toHaveBeenCalled(); |
145 |
| - }); |
146 |
| -}); |
| 1 | +import { bech32 } from 'bech32'; |
| 2 | +import { waitFor } from '@testing-library/react'; |
| 3 | +import { ec as EC } from 'elliptic'; |
| 4 | +import { testUserLnUrlLogin } from '../testUserLnUrl'; |
| 5 | + |
| 6 | +jest.mock('bech32'); |
| 7 | +jest.mock('elliptic'); |
| 8 | + |
| 9 | +describe('testUserLnUrl', () => { |
| 10 | + const mockDigest = jest.fn(); |
| 11 | + const originalCrypto = global.crypto; |
| 12 | + const mockConsoleLog = jest.spyOn(console, 'log').mockImplementation(); |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + global.TextEncoder = jest.fn().mockImplementation(() => ({ |
| 16 | + encode: jest.fn().mockReturnValue(new Uint8Array([1, 2, 3, 4])) |
| 17 | + })) as any; |
| 18 | + |
| 19 | + global.TextDecoder = jest.fn().mockImplementation(() => ({ |
| 20 | + decode: () => 'https://example.com/lnurl-auth?k1=abcdef' |
| 21 | + })) as any; |
| 22 | + |
| 23 | + global.crypto = { |
| 24 | + ...originalCrypto, |
| 25 | + subtle: { |
| 26 | + digest: mockDigest |
| 27 | + } |
| 28 | + } as unknown as Crypto; |
| 29 | + |
| 30 | + const mockHashBuffer = new Uint8Array(32).fill(1); |
| 31 | + mockDigest.mockResolvedValue(mockHashBuffer.buffer); |
| 32 | + |
| 33 | + (bech32.decode as jest.Mock).mockReturnValue({ |
| 34 | + words: [1, 2, 3, 4] |
| 35 | + }); |
| 36 | + (bech32.fromWords as jest.Mock).mockReturnValue( |
| 37 | + new Uint8Array([ |
| 38 | + 104, 116, 116, 112, 115, 58, 47, 47, 101, 120, 97, 109, 112, 108, 101, 46, 99, 111, 109, 47, |
| 39 | + 108, 110, 117, 114, 108, 45, 97, 117, 116, 104, 63, 107, 49, 61, 97, 98, 99, 100, 101, 102 |
| 40 | + ]) |
| 41 | + ); |
| 42 | + |
| 43 | + const mockSign = jest.fn().mockReturnValue({ |
| 44 | + toDER: () => new Uint8Array([1, 2, 3, 4]) |
| 45 | + }); |
| 46 | + const mockGetPublic = jest.fn().mockReturnValue({ |
| 47 | + encode: () => 'mock-pubkey' |
| 48 | + }); |
| 49 | + const mockKeyFromPrivate = jest.fn().mockReturnValue({ |
| 50 | + getPublic: mockGetPublic, |
| 51 | + sign: mockSign |
| 52 | + }); |
| 53 | + (EC as jest.Mock).mockImplementation(() => ({ |
| 54 | + keyFromPrivate: mockKeyFromPrivate, |
| 55 | + sign: mockSign |
| 56 | + })); |
| 57 | + |
| 58 | + global.fetch = jest.fn().mockResolvedValue({ |
| 59 | + ok: true, |
| 60 | + json: () => Promise.resolve({ status: 'OK' }) |
| 61 | + }); |
| 62 | + |
| 63 | + jest.spyOn(Date, 'now').mockReturnValue(1234567890); |
| 64 | + }); |
| 65 | + |
| 66 | + afterEach(() => { |
| 67 | + jest.clearAllMocks(); |
| 68 | + global.crypto = originalCrypto; |
| 69 | + global.fetch = undefined as any; |
| 70 | + global.TextEncoder = undefined as any; |
| 71 | + global.TextDecoder = undefined as any; |
| 72 | + mockConsoleLog.mockRestore(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should successfully process a valid lnurl login', async () => { |
| 76 | + waitFor(() => { |
| 77 | + testUserLnUrlLogin('lnurl1234567'); |
| 78 | + |
| 79 | + expect(bech32.decode).toHaveBeenCalledWith('lnurl1234567', 1500); |
| 80 | + expect(bech32.fromWords).toHaveBeenCalledWith([1, 2, 3, 4]); |
| 81 | + |
| 82 | + expect(mockDigest).toHaveBeenCalled(); |
| 83 | + |
| 84 | + expect(global.fetch).toHaveBeenCalledWith( |
| 85 | + expect.stringContaining('https://example.com/lnurl-auth?k1=abcdef&sig=') |
| 86 | + ); |
| 87 | + expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining('&key=mock-pubkey')); |
| 88 | + expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining('&t=1234567890')); |
| 89 | + |
| 90 | + expect(mockConsoleLog).toHaveBeenCalledWith({ status: 'OK' }); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should handle invalid bech32 input', async () => { |
| 95 | + (bech32.decode as jest.Mock).mockImplementation(() => { |
| 96 | + throw new Error('Invalid bech32'); |
| 97 | + }); |
| 98 | + |
| 99 | + await expect(testUserLnUrlLogin('invalid')).rejects.toThrow('Invalid bech32'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should handle fetch error', async () => { |
| 103 | + (global.fetch as jest.Mock).mockRejectedValue(new Error('Network error')); |
| 104 | + |
| 105 | + await expect(testUserLnUrlLogin('lnurl1234567')).rejects.toThrow('Network error'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should handle non-OK response', async () => { |
| 109 | + (global.fetch as jest.Mock).mockResolvedValue({ |
| 110 | + ok: false, |
| 111 | + status: 404, |
| 112 | + statusText: 'Not Found' |
| 113 | + }); |
| 114 | + |
| 115 | + await testUserLnUrlLogin('lnurl1234567'); |
| 116 | + |
| 117 | + expect(mockConsoleLog).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should handle empty k1 parameter', async () => { |
| 121 | + global.TextDecoder = jest.fn().mockImplementation(() => ({ |
| 122 | + decode: () => 'https://example.com/lnurl-auth' |
| 123 | + })) as any; |
| 124 | + |
| 125 | + await testUserLnUrlLogin('lnurl1234567'); |
| 126 | + |
| 127 | + expect(global.fetch).toHaveBeenCalled(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should handle hexToBytes with odd length input', async () => { |
| 131 | + global.TextDecoder = jest.fn().mockImplementation(() => ({ |
| 132 | + decode: () => 'https://example.com/lnurl-auth?k1=abc' |
| 133 | + })) as any; |
| 134 | + |
| 135 | + await expect(testUserLnUrlLogin('lnurl1234567')).rejects.toThrow( |
| 136 | + 'Hex string must have an even length' |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should handle hexToBytes with valid input', async () => { |
| 141 | + global.TextDecoder = jest.fn().mockImplementation(() => ({ |
| 142 | + decode: () => 'https://example.com/lnurl-auth?k1=abcd' |
| 143 | + })) as any; |
| 144 | + |
| 145 | + await testUserLnUrlLogin('lnurl1234567'); |
| 146 | + |
| 147 | + expect(global.fetch).toHaveBeenCalled(); |
| 148 | + }); |
| 149 | +}); |
0 commit comments