109 lines
3.7 KiB
TypeScript
109 lines
3.7 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
/**
|
|
* Catalogue section 17.2 — tRPC client (`tests/lib/trpc-client.test.ts`).
|
|
*
|
|
* `@/src/trpc-client` is module-mapped to the procedure-registry mock for every
|
|
* other suite. Here the real module is loaded with an explicit relative path
|
|
* (which does not match the mapper) so the production `httpBatchLink` fetch
|
|
* wrapper, bearer-header builder and 401 force-logout handler run for real.
|
|
*/
|
|
import { DeviceEventEmitter } from 'react-native'
|
|
|
|
import { resetAllMocks } from '../helpers/reset'
|
|
import { __set } from '../mocks/secure-store-mock'
|
|
import { TEST_JWT } from '../fixtures'
|
|
import { FORCE_LOGOUT_EVENT } from 'common-ui/src/lib/const-strs'
|
|
|
|
function loadRealClient() {
|
|
// The explicit `.ts` suffix keeps this request out of the moduleNameMapper.
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
return require('../../src/trpc-client.ts') as typeof import('@/src/trpc-client')
|
|
}
|
|
|
|
function jsonResponse(status: number, body: any) {
|
|
const make = (): any => ({
|
|
ok: status >= 200 && status < 300,
|
|
status,
|
|
statusText: String(status),
|
|
headers: { get: () => 'application/json' },
|
|
json: async () => body,
|
|
text: async () => JSON.stringify(body),
|
|
})
|
|
const response = make()
|
|
response.clone = () => make()
|
|
return response
|
|
}
|
|
|
|
describe('trpc client (ATRPC-01..03)', () => {
|
|
beforeEach(() => {
|
|
resetAllMocks()
|
|
jest.spyOn(DeviceEventEmitter, 'emit')
|
|
})
|
|
|
|
afterEach(() => {
|
|
jest.restoreAllMocks()
|
|
})
|
|
|
|
it('ATRPC-01: a 401 UNAUTHORIZED response emits the force-logout event', async () => {
|
|
const { trpcClient } = loadRealClient()
|
|
__set('jwt_token', TEST_JWT)
|
|
;(global as any).fetch = jest.fn(async () =>
|
|
jsonResponse(401, {
|
|
error: {
|
|
message: 'UNAUTHORIZED',
|
|
code: -32001,
|
|
data: { code: 'UNAUTHORIZED', httpStatus: 401 },
|
|
},
|
|
})
|
|
)
|
|
|
|
await expect(trpcClient.admin.staffUser.me.query()).rejects.toBeTruthy()
|
|
|
|
expect(DeviceEventEmitter.emit).toHaveBeenCalledWith(FORCE_LOGOUT_EVENT)
|
|
expect((global as any).fetch).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('ATRPC-02: a 500 / non-UNAUTHORIZED failure does not force logout', async () => {
|
|
const { trpcClient } = loadRealClient()
|
|
|
|
;(global as any).fetch = jest.fn(async () =>
|
|
jsonResponse(500, {
|
|
error: {
|
|
message: 'Something exploded',
|
|
code: -32603,
|
|
data: { code: 'INTERNAL_SERVER_ERROR', httpStatus: 500 },
|
|
},
|
|
})
|
|
)
|
|
await expect(trpcClient.admin.staffUser.me.query()).rejects.toBeTruthy()
|
|
expect(DeviceEventEmitter.emit).not.toHaveBeenCalledWith(FORCE_LOGOUT_EVENT)
|
|
|
|
// A 401 that is not the UNAUTHORIZED code must also stay signed in.
|
|
;(global as any).fetch = jest.fn(async () =>
|
|
jsonResponse(401, {
|
|
error: {
|
|
message: 'Session is not the right shape',
|
|
code: -32001,
|
|
data: { code: 'BAD_REQUEST', httpStatus: 401 },
|
|
},
|
|
})
|
|
)
|
|
await expect(trpcClient.admin.staffUser.me.query()).rejects.toBeTruthy()
|
|
expect(DeviceEventEmitter.emit).not.toHaveBeenCalledWith(FORCE_LOGOUT_EVENT)
|
|
})
|
|
|
|
it('ATRPC-03: attaches the stored JWT as a bearer Authorization header', async () => {
|
|
const { trpcClient } = loadRealClient()
|
|
__set('jwt_token', TEST_JWT)
|
|
;(global as any).fetch = jest.fn(async () =>
|
|
jsonResponse(200, { result: { data: { id: 3, name: 'Admin One' } } })
|
|
)
|
|
|
|
await trpcClient.admin.staffUser.me.query()
|
|
|
|
const [url, options] = ((global as any).fetch as jest.Mock).mock.calls[0]
|
|
expect(String(url)).toContain('/api/trpc')
|
|
expect(options.method).toBe('GET')
|
|
expect(options.headers).toMatchObject({ Authorization: `Bearer ${TEST_JWT}` })
|
|
})
|
|
})
|