|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { AuthenticatedAdapter } from '../AuthenticatedAdapter'; |
| 3 | +import { Adapter } from '../types/adapters'; |
| 4 | + |
| 5 | +describe('AuthenticatedAdapter', () => { |
| 6 | + let mockBaseAdapter: Adapter; |
| 7 | + let adapter: AuthenticatedAdapter; |
| 8 | + const credentials = { appId: 'test-app-id', appSecret: 'test-app-secret' }; |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + mockBaseAdapter = { |
| 12 | + setHeader: vi.fn(), |
| 13 | + get: vi.fn().mockResolvedValue({}), |
| 14 | + post: vi.fn().mockResolvedValue({ |
| 15 | + mage_id: 'MAG123', |
| 16 | + ust: 'test-token-xyz', |
| 17 | + expires_in: 360 |
| 18 | + }), |
| 19 | + put: vi.fn().mockResolvedValue({}), |
| 20 | + delete: vi.fn().mockResolvedValue({}) |
| 21 | + }; |
| 22 | + adapter = new AuthenticatedAdapter(mockBaseAdapter, credentials); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('authenticate', () => { |
| 26 | + it('should POST to /app/session/token with Basic auth', async () => { |
| 27 | + await adapter.get('/test'); |
| 28 | + |
| 29 | + expect(mockBaseAdapter.post).toHaveBeenCalledWith( |
| 30 | + '/app/session/token', |
| 31 | + { grant_type: 'session', expires_in: 360 }, |
| 32 | + { |
| 33 | + auth: { |
| 34 | + username: 'test-app-id', |
| 35 | + password: 'test-app-secret' |
| 36 | + } |
| 37 | + } |
| 38 | + ); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('Bearer token', () => { |
| 43 | + it('should pass Bearer token in authorization header', async () => { |
| 44 | + await adapter.get('/test'); |
| 45 | + |
| 46 | + expect(mockBaseAdapter.get).toHaveBeenCalledWith('/test', { headers: { authorization: 'Bearer test-token-xyz' } }); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('|MAGE_ID| replacement', () => { |
| 51 | + it('should replace |MAGE_ID| in URLs', async () => { |
| 52 | + await adapter.get('/users/|MAGE_ID|/profile'); |
| 53 | + |
| 54 | + expect(mockBaseAdapter.get).toHaveBeenCalledWith('/users/MAG123/profile', expect.any(Object)); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should replace |MAGE_ID| for POST requests', async () => { |
| 58 | + await adapter.post('/users/|MAGE_ID|', { data: 'test' }); |
| 59 | + |
| 60 | + expect(mockBaseAdapter.post).toHaveBeenCalledTimes(2); // 1 auth + 1 actual |
| 61 | + const actualCall = (mockBaseAdapter.post as ReturnType<typeof vi.fn>).mock.calls[1]; |
| 62 | + expect(actualCall[0]).toBe('/users/MAG123'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should replace |MAGE_ID| for PUT requests', async () => { |
| 66 | + await adapter.put('/users/|MAGE_ID|', { data: 'test' }); |
| 67 | + |
| 68 | + expect(mockBaseAdapter.put).toHaveBeenCalledWith('/users/MAG123', { data: 'test' }, expect.any(Object)); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should replace |MAGE_ID| for DELETE requests', async () => { |
| 72 | + await adapter.delete('/users/|MAGE_ID|/key'); |
| 73 | + |
| 74 | + expect(mockBaseAdapter.delete).toHaveBeenCalledWith('/users/MAG123/key', expect.any(Object)); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('addHeaders', () => { |
| 79 | + it('should merge auth headers with existing config headers', async () => { |
| 80 | + await adapter.get('/test', { headers: { 'x-custom': 'value' } }); |
| 81 | + |
| 82 | + expect(mockBaseAdapter.get).toHaveBeenCalledWith('/test', { |
| 83 | + headers: { |
| 84 | + 'x-custom': 'value', |
| 85 | + authorization: 'Bearer test-token-xyz' |
| 86 | + } |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should work when config is undefined', async () => { |
| 91 | + await adapter.get('/test'); |
| 92 | + |
| 93 | + expect(mockBaseAdapter.get).toHaveBeenCalledWith('/test', { headers: { authorization: 'Bearer test-token-xyz' } }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('HTTP method delegation', () => { |
| 98 | + it('should delegate get', async () => { |
| 99 | + await adapter.get('/path', { params: { a: '1' } }); |
| 100 | + |
| 101 | + expect(mockBaseAdapter.get).toHaveBeenCalledWith( |
| 102 | + '/path', |
| 103 | + expect.objectContaining({ |
| 104 | + params: { a: '1' }, |
| 105 | + headers: expect.objectContaining({ authorization: 'Bearer test-token-xyz' }) |
| 106 | + }) |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should delegate post', async () => { |
| 111 | + await adapter.post('/path', { body: true }); |
| 112 | + |
| 113 | + const calls = (mockBaseAdapter.post as ReturnType<typeof vi.fn>).mock.calls; |
| 114 | + const actualCall = calls[calls.length - 1]; |
| 115 | + expect(actualCall[0]).toBe('/path'); |
| 116 | + expect(actualCall[1]).toEqual({ body: true }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should delegate put', async () => { |
| 120 | + await adapter.put('/path', { body: true }); |
| 121 | + |
| 122 | + expect(mockBaseAdapter.put).toHaveBeenCalledWith( |
| 123 | + '/path', |
| 124 | + { body: true }, |
| 125 | + expect.objectContaining({ |
| 126 | + headers: expect.objectContaining({ authorization: 'Bearer test-token-xyz' }) |
| 127 | + }) |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should delegate delete', async () => { |
| 132 | + await adapter.delete('/path'); |
| 133 | + |
| 134 | + expect(mockBaseAdapter.delete).toHaveBeenCalledWith( |
| 135 | + '/path', |
| 136 | + expect.objectContaining({ |
| 137 | + headers: expect.objectContaining({ authorization: 'Bearer test-token-xyz' }) |
| 138 | + }) |
| 139 | + ); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('getMageId', () => { |
| 144 | + it('should return the mage_id from authentication', async () => { |
| 145 | + const mageId = await adapter.getMageId(); |
| 146 | + |
| 147 | + expect(mageId).toBe('MAG123'); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments