|
| 1 | +import assert from 'assert' |
| 2 | +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' |
| 3 | +import { testBuilders, EnhancedTestSetup } from './helpers/setup' |
| 4 | +import { utils } from './helpers/auth' |
| 5 | +import { CHANNEL_STATES } from '../src/lib/constants' |
| 6 | + |
| 7 | +let testSetup: EnhancedTestSetup |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + testSetup = testBuilders.standardClient() |
| 11 | +}) |
| 12 | + |
| 13 | +afterEach(() => { |
| 14 | + testSetup.cleanup() |
| 15 | + testSetup.socket.removeAllChannels() |
| 16 | +}) |
| 17 | + |
| 18 | +describe('Custom JWT token preservation', () => { |
| 19 | + test('preserves access token when resubscribing after removeChannel', async () => { |
| 20 | + // Test scenario: |
| 21 | + // 1. Set custom JWT via setAuth (not using accessToken callback) |
| 22 | + // 2. Subscribe to private channel |
| 23 | + // 3. removeChannel |
| 24 | + // 4. Create new channel with same topic and subscribe |
| 25 | + |
| 26 | + const customToken = utils.generateJWT('1h') |
| 27 | + |
| 28 | + // Step 1: Set auth with custom token (mimics user's setup) |
| 29 | + await testSetup.socket.setAuth(customToken) |
| 30 | + |
| 31 | + // Verify token was set |
| 32 | + assert.strictEqual(testSetup.socket.accessTokenValue, customToken) |
| 33 | + |
| 34 | + // Step 2: Create and subscribe to private channel (first time) |
| 35 | + const channel1 = testSetup.socket.channel('conversation:dc3fb8c1-ceef-4c00-9f92-e496acd03593', { |
| 36 | + config: { private: true }, |
| 37 | + }) |
| 38 | + |
| 39 | + // Spy on the push to verify join payload |
| 40 | + const pushSpy = vi.spyOn(testSetup.socket, 'push') |
| 41 | + |
| 42 | + // Simulate successful subscription |
| 43 | + channel1.state = CHANNEL_STATES.closed // Start from closed |
| 44 | + await channel1.subscribe() |
| 45 | + |
| 46 | + // Verify first join includes access_token |
| 47 | + const firstJoinCall = pushSpy.mock.calls.find((call) => call[0]?.event === 'phx_join') |
| 48 | + expect(firstJoinCall).toBeDefined() |
| 49 | + expect(firstJoinCall![0].payload).toHaveProperty('access_token', customToken) |
| 50 | + |
| 51 | + // Step 3: Remove channel (mimics user cleanup) |
| 52 | + await testSetup.socket.removeChannel(channel1) |
| 53 | + |
| 54 | + // Verify channel was removed |
| 55 | + expect(testSetup.socket.getChannels()).not.toContain(channel1) |
| 56 | + |
| 57 | + // Step 4: Create NEW channel with SAME topic and subscribe |
| 58 | + pushSpy.mockClear() |
| 59 | + const channel2 = testSetup.socket.channel('conversation:dc3fb8c1-ceef-4c00-9f92-e496acd03593', { |
| 60 | + config: { private: true }, |
| 61 | + }) |
| 62 | + |
| 63 | + // This should be a different channel instance |
| 64 | + expect(channel2).not.toBe(channel1) |
| 65 | + |
| 66 | + // Subscribe to the new channel |
| 67 | + channel2.state = CHANNEL_STATES.closed |
| 68 | + await channel2.subscribe() |
| 69 | + |
| 70 | + // Verify second join also includes access token |
| 71 | + const secondJoinCall = pushSpy.mock.calls.find((call) => call[0]?.event === 'phx_join') |
| 72 | + |
| 73 | + expect(secondJoinCall).toBeDefined() |
| 74 | + expect(secondJoinCall![0].payload).toHaveProperty('access_token', customToken) |
| 75 | + }) |
| 76 | + |
| 77 | + test('supports accessToken callback for token rotation', async () => { |
| 78 | + // Verify that callback-based token fetching works correctly |
| 79 | + const customToken = utils.generateJWT('1h') |
| 80 | + let callCount = 0 |
| 81 | + |
| 82 | + const clientWithCallback = testBuilders.standardClient({ |
| 83 | + accessToken: async () => { |
| 84 | + callCount++ |
| 85 | + return customToken |
| 86 | + }, |
| 87 | + }) |
| 88 | + |
| 89 | + // Set initial auth |
| 90 | + await clientWithCallback.socket.setAuth() |
| 91 | + |
| 92 | + // Create and subscribe to first channel |
| 93 | + const channel1 = clientWithCallback.socket.channel('conversation:test', { |
| 94 | + config: { private: true }, |
| 95 | + }) |
| 96 | + |
| 97 | + const pushSpy = vi.spyOn(clientWithCallback.socket, 'push') |
| 98 | + channel1.state = CHANNEL_STATES.closed |
| 99 | + await channel1.subscribe() |
| 100 | + |
| 101 | + const firstJoin = pushSpy.mock.calls.find((call) => call[0]?.event === 'phx_join') |
| 102 | + expect(firstJoin![0].payload).toHaveProperty('access_token', customToken) |
| 103 | + |
| 104 | + // Remove and recreate |
| 105 | + await clientWithCallback.socket.removeChannel(channel1) |
| 106 | + pushSpy.mockClear() |
| 107 | + |
| 108 | + const channel2 = clientWithCallback.socket.channel('conversation:test', { |
| 109 | + config: { private: true }, |
| 110 | + }) |
| 111 | + |
| 112 | + channel2.state = CHANNEL_STATES.closed |
| 113 | + await channel2.subscribe() |
| 114 | + |
| 115 | + const secondJoin = pushSpy.mock.calls.find((call) => call[0]?.event === 'phx_join') |
| 116 | + |
| 117 | + // Callback should provide token for both subscriptions |
| 118 | + expect(secondJoin![0].payload).toHaveProperty('access_token', customToken) |
| 119 | + |
| 120 | + clientWithCallback.cleanup() |
| 121 | + }) |
| 122 | + |
| 123 | + test('preserves token when subscribing to different topics', async () => { |
| 124 | + const customToken = utils.generateJWT('1h') |
| 125 | + await testSetup.socket.setAuth(customToken) |
| 126 | + |
| 127 | + // Subscribe to first topic |
| 128 | + const channel1 = testSetup.socket.channel('topic1', { config: { private: true } }) |
| 129 | + channel1.state = CHANNEL_STATES.closed |
| 130 | + await channel1.subscribe() |
| 131 | + |
| 132 | + await testSetup.socket.removeChannel(channel1) |
| 133 | + |
| 134 | + // Subscribe to DIFFERENT topic |
| 135 | + const pushSpy = vi.spyOn(testSetup.socket, 'push') |
| 136 | + const channel2 = testSetup.socket.channel('topic2', { config: { private: true } }) |
| 137 | + channel2.state = CHANNEL_STATES.closed |
| 138 | + await channel2.subscribe() |
| 139 | + |
| 140 | + const joinCall = pushSpy.mock.calls.find((call) => call[0]?.event === 'phx_join') |
| 141 | + expect(joinCall![0].payload).toHaveProperty('access_token', customToken) |
| 142 | + }) |
| 143 | + |
| 144 | + test('handles accessToken callback errors gracefully during subscribe', async () => { |
| 145 | + const errorMessage = 'Token fetch failed during subscribe' |
| 146 | + let callCount = 0 |
| 147 | + const tokens = ['initial-token', null] // Second call will throw |
| 148 | + |
| 149 | + const accessToken = vi.fn(() => { |
| 150 | + if (callCount++ === 0) { |
| 151 | + return Promise.resolve(tokens[0]) |
| 152 | + } |
| 153 | + return Promise.reject(new Error(errorMessage)) |
| 154 | + }) |
| 155 | + |
| 156 | + const logSpy = vi.fn() |
| 157 | + |
| 158 | + const client = testBuilders.standardClient({ |
| 159 | + accessToken, |
| 160 | + logger: logSpy, |
| 161 | + }) |
| 162 | + |
| 163 | + // First subscribe should work |
| 164 | + await client.socket.setAuth() |
| 165 | + const channel1 = client.socket.channel('test', { config: { private: true } }) |
| 166 | + channel1.state = CHANNEL_STATES.closed |
| 167 | + await channel1.subscribe() |
| 168 | + |
| 169 | + expect(client.socket.accessTokenValue).toBe(tokens[0]) |
| 170 | + |
| 171 | + // Remove and resubscribe - callback will fail but should fall back |
| 172 | + await client.socket.removeChannel(channel1) |
| 173 | + |
| 174 | + const channel2 = client.socket.channel('test', { config: { private: true } }) |
| 175 | + channel2.state = CHANNEL_STATES.closed |
| 176 | + await channel2.subscribe() |
| 177 | + |
| 178 | + // Verify error was logged |
| 179 | + expect(logSpy).toHaveBeenCalledWith( |
| 180 | + 'error', |
| 181 | + 'Error fetching access token from callback', |
| 182 | + expect.any(Error) |
| 183 | + ) |
| 184 | + |
| 185 | + // Verify subscription still succeeded with cached token |
| 186 | + expect(client.socket.accessTokenValue).toBe(tokens[0]) |
| 187 | + |
| 188 | + client.cleanup() |
| 189 | + }) |
| 190 | +}) |
0 commit comments