|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +describe('getUser redirect protection', () => { |
| 4 | + it('should not loop infinitely on repeated redirects', async () => { |
| 5 | + // Monkey-patch axios in the require cache to simulate redirect loop |
| 6 | + let callCount = 0; |
| 7 | + const axiosMock = { |
| 8 | + get: async (url: string) => { |
| 9 | + callCount += 1; |
| 10 | + const isA = url === 'https://www.tradingview.com/'; |
| 11 | + return { |
| 12 | + data: '<html>no auth here</html>', |
| 13 | + headers: { |
| 14 | + location: isA |
| 15 | + ? 'https://www.tradingview.com/accounts/signin/' |
| 16 | + : 'https://www.tradingview.com/', |
| 17 | + }, |
| 18 | + }; |
| 19 | + }, |
| 20 | + }; |
| 21 | + |
| 22 | + const axiosPath = require.resolve('axios'); |
| 23 | + const originalModule = require.cache[axiosPath]; |
| 24 | + require.cache[axiosPath] = { |
| 25 | + id: axiosPath, |
| 26 | + filename: axiosPath, |
| 27 | + loaded: true, |
| 28 | + exports: axiosMock, |
| 29 | + } as any; |
| 30 | + |
| 31 | + const miscPath = require.resolve('../src/miscRequests'); |
| 32 | + delete require.cache[miscPath]; |
| 33 | + |
| 34 | + try { |
| 35 | + // eslint-disable-next-line global-require |
| 36 | + const misc = require('../src/miscRequests'); |
| 37 | + await expect( |
| 38 | + misc.getUser('fake_session', 'fake_signature'), |
| 39 | + ).rejects.toThrow('Too many redirects'); |
| 40 | + |
| 41 | + expect(callCount).toBeGreaterThan(0); |
| 42 | + expect(callCount).toBeLessThanOrEqual(10); |
| 43 | + } finally { |
| 44 | + if (originalModule) require.cache[axiosPath] = originalModule; |
| 45 | + else delete require.cache[axiosPath]; |
| 46 | + delete require.cache[miscPath]; |
| 47 | + } |
| 48 | + }, 5000); |
| 49 | +}); |
0 commit comments