|
| 1 | +/** |
| 2 | + * Tests for setUserJWT API specifically |
| 3 | + */ |
| 4 | + |
| 5 | +describe('Intercom setUserJWT API', () => { |
| 6 | + let mockIntercomModule; |
| 7 | + let Intercom; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + jest.resetModules(); |
| 11 | + |
| 12 | + // Mock React Native |
| 13 | + jest.doMock('react-native', () => ({ |
| 14 | + NativeModules: { |
| 15 | + IntercomModule: { |
| 16 | + setUserJWT: jest.fn(), |
| 17 | + setUserHash: jest.fn(), |
| 18 | + loginUserWithUserAttributes: jest.fn(), |
| 19 | + logout: jest.fn(), |
| 20 | + updateUser: jest.fn(), |
| 21 | + isUserLoggedIn: jest.fn(), |
| 22 | + }, |
| 23 | + IntercomEventEmitter: { |
| 24 | + UNREAD_COUNT_CHANGE_NOTIFICATION: 'UNREAD_COUNT_CHANGE_NOTIFICATION', |
| 25 | + WINDOW_DID_HIDE_NOTIFICATION: 'WINDOW_DID_HIDE_NOTIFICATION', |
| 26 | + WINDOW_DID_SHOW_NOTIFICATION: 'WINDOW_DID_SHOW_NOTIFICATION', |
| 27 | + HELP_CENTER_WINDOW_DID_SHOW_NOTIFICATION: |
| 28 | + 'HELP_CENTER_WINDOW_DID_SHOW_NOTIFICATION', |
| 29 | + HELP_CENTER_WINDOW_DID_HIDE_NOTIFICATION: |
| 30 | + 'HELP_CENTER_WINDOW_DID_HIDE_NOTIFICATION', |
| 31 | + startEventListener: jest.fn(), |
| 32 | + removeEventListener: jest.fn(), |
| 33 | + }, |
| 34 | + }, |
| 35 | + NativeEventEmitter: jest.fn().mockImplementation(() => ({ |
| 36 | + addListener: jest.fn().mockReturnValue({ |
| 37 | + remove: jest.fn(), |
| 38 | + }), |
| 39 | + })), |
| 40 | + Platform: { |
| 41 | + OS: 'ios', |
| 42 | + select: jest.fn((obj) => obj.ios || obj.default), |
| 43 | + }, |
| 44 | + })); |
| 45 | + |
| 46 | + const { NativeModules } = require('react-native'); |
| 47 | + mockIntercomModule = NativeModules.IntercomModule; |
| 48 | + |
| 49 | + // Import Intercom after mocking |
| 50 | + Intercom = require('../src/index.tsx').default; |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + jest.clearAllMocks(); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('setUserJWT method', () => { |
| 58 | + test('should call native setUserJWT with valid JWT', async () => { |
| 59 | + const testJWT = |
| 60 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.test'; |
| 61 | + mockIntercomModule.setUserJWT.mockResolvedValue(true); |
| 62 | + |
| 63 | + const result = await Intercom.setUserJWT(testJWT); |
| 64 | + |
| 65 | + expect(mockIntercomModule.setUserJWT).toHaveBeenCalledWith(testJWT); |
| 66 | + expect(result).toBe(true); |
| 67 | + }); |
| 68 | + |
| 69 | + test('should handle JWT authentication errors', async () => { |
| 70 | + const invalidJWT = 'invalid.jwt'; |
| 71 | + const error = new Error('JWT validation failed'); |
| 72 | + mockIntercomModule.setUserJWT.mockRejectedValue(error); |
| 73 | + |
| 74 | + await expect(Intercom.setUserJWT(invalidJWT)).rejects.toThrow( |
| 75 | + 'JWT validation failed' |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + test('should work with empty JWT string', async () => { |
| 80 | + const emptyJWT = ''; |
| 81 | + mockIntercomModule.setUserJWT.mockResolvedValue(true); |
| 82 | + |
| 83 | + const result = await Intercom.setUserJWT(emptyJWT); |
| 84 | + |
| 85 | + expect(mockIntercomModule.setUserJWT).toHaveBeenCalledWith(emptyJWT); |
| 86 | + expect(result).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + test('should handle long JWT tokens', async () => { |
| 90 | + const longJWT = |
| 91 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMTIzNDU2Nzg5MCIsImVtYWlsIjoidGVzdEBleGFtcGxlLmNvbSIsIm5hbWUiOiJKb2huIERvZSIsImN1c3RvbV9hdHRyaWJ1dGVzIjp7InBsYW4iOiJwcmVtaXVtIiwiY29tcGFueSI6IkFjbWUgSW5jIn19.very_long_signature'; |
| 92 | + mockIntercomModule.setUserJWT.mockResolvedValue(true); |
| 93 | + |
| 94 | + const result = await Intercom.setUserJWT(longJWT); |
| 95 | + |
| 96 | + expect(mockIntercomModule.setUserJWT).toHaveBeenCalledWith(longJWT); |
| 97 | + expect(result).toBe(true); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('JWT authentication workflow', () => { |
| 102 | + test('should set JWT before user login', async () => { |
| 103 | + const jwt = |
| 104 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMTIzIn0.test'; |
| 105 | + const userAttributes = { email: '[email protected]' }; |
| 106 | + |
| 107 | + mockIntercomModule.setUserJWT.mockResolvedValue(true); |
| 108 | + mockIntercomModule.loginUserWithUserAttributes.mockResolvedValue(true); |
| 109 | + |
| 110 | + await Intercom.setUserJWT(jwt); |
| 111 | + await Intercom.loginUserWithUserAttributes(userAttributes); |
| 112 | + |
| 113 | + expect(mockIntercomModule.setUserJWT).toHaveBeenCalledWith(jwt); |
| 114 | + expect( |
| 115 | + mockIntercomModule.loginUserWithUserAttributes |
| 116 | + ).toHaveBeenCalledWith(userAttributes); |
| 117 | + // Verify setUserJWT was called first by checking call counts |
| 118 | + expect(mockIntercomModule.setUserJWT).toHaveBeenCalledTimes(1); |
| 119 | + expect( |
| 120 | + mockIntercomModule.loginUserWithUserAttributes |
| 121 | + ).toHaveBeenCalledTimes(1); |
| 122 | + }); |
| 123 | + |
| 124 | + test('should support both JWT and HMAC methods', async () => { |
| 125 | + const jwt = |
| 126 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMTIzIn0.test'; |
| 127 | + const hash = 'hmac_hash_123'; |
| 128 | + |
| 129 | + mockIntercomModule.setUserJWT.mockResolvedValue(true); |
| 130 | + mockIntercomModule.setUserHash.mockResolvedValue(true); |
| 131 | + |
| 132 | + const jwtResult = await Intercom.setUserJWT(jwt); |
| 133 | + const hashResult = await Intercom.setUserHash(hash); |
| 134 | + |
| 135 | + expect(mockIntercomModule.setUserJWT).toHaveBeenCalledWith(jwt); |
| 136 | + expect(mockIntercomModule.setUserHash).toHaveBeenCalledWith(hash); |
| 137 | + expect(jwtResult).toBe(true); |
| 138 | + expect(hashResult).toBe(true); |
| 139 | + }); |
| 140 | + |
| 141 | + test('should handle complete authentication flow', async () => { |
| 142 | + const jwt = |
| 143 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMTIzIn0.test'; |
| 144 | + const userAttributes = { userId: '123', email: '[email protected]' }; |
| 145 | + |
| 146 | + mockIntercomModule.setUserJWT.mockResolvedValue(true); |
| 147 | + mockIntercomModule.loginUserWithUserAttributes.mockResolvedValue(true); |
| 148 | + mockIntercomModule.isUserLoggedIn.mockResolvedValue(true); |
| 149 | + mockIntercomModule.updateUser.mockResolvedValue(true); |
| 150 | + |
| 151 | + // Set JWT first |
| 152 | + await Intercom.setUserJWT(jwt); |
| 153 | + |
| 154 | + // Login user |
| 155 | + await Intercom.loginUserWithUserAttributes(userAttributes); |
| 156 | + |
| 157 | + // Check login status |
| 158 | + const isLoggedIn = await Intercom.isUserLoggedIn(); |
| 159 | + |
| 160 | + // Update user |
| 161 | + await Intercom.updateUser({ name: 'Updated Name' }); |
| 162 | + |
| 163 | + expect(mockIntercomModule.setUserJWT).toHaveBeenCalledWith(jwt); |
| 164 | + expect( |
| 165 | + mockIntercomModule.loginUserWithUserAttributes |
| 166 | + ).toHaveBeenCalledWith(userAttributes); |
| 167 | + expect(isLoggedIn).toBe(true); |
| 168 | + expect(mockIntercomModule.updateUser).toHaveBeenCalledWith({ |
| 169 | + name: 'Updated Name', |
| 170 | + }); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('Error handling', () => { |
| 175 | + test('should handle network errors', async () => { |
| 176 | + const jwt = 'test.jwt.token'; |
| 177 | + const networkError = new Error('Network request failed'); |
| 178 | + mockIntercomModule.setUserJWT.mockRejectedValue(networkError); |
| 179 | + |
| 180 | + await expect(Intercom.setUserJWT(jwt)).rejects.toThrow( |
| 181 | + 'Network request failed' |
| 182 | + ); |
| 183 | + }); |
| 184 | + |
| 185 | + test('should handle invalid JWT format errors', async () => { |
| 186 | + const invalidJWT = 'not.a.valid.jwt'; |
| 187 | + const formatError = new Error('Invalid JWT format'); |
| 188 | + mockIntercomModule.setUserJWT.mockRejectedValue(formatError); |
| 189 | + |
| 190 | + await expect(Intercom.setUserJWT(invalidJWT)).rejects.toThrow( |
| 191 | + 'Invalid JWT format' |
| 192 | + ); |
| 193 | + }); |
| 194 | + |
| 195 | + test('should handle JWT signature verification errors', async () => { |
| 196 | + const jwtWithBadSignature = |
| 197 | + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiMTIzIn0.bad_signature'; |
| 198 | + const signatureError = new Error('JWT signature verification failed'); |
| 199 | + mockIntercomModule.setUserJWT.mockRejectedValue(signatureError); |
| 200 | + |
| 201 | + await expect(Intercom.setUserJWT(jwtWithBadSignature)).rejects.toThrow( |
| 202 | + 'JWT signature verification failed' |
| 203 | + ); |
| 204 | + }); |
| 205 | + }); |
| 206 | +}); |
0 commit comments