test: increase coverage for get-server-auth-session
This commit is contained in:
parent
5dcc089865
commit
75f13c8b1d
3 changed files with 91 additions and 40 deletions
|
@ -3,6 +3,7 @@ import React from 'react';
|
|||
import { render, screen, waitFor, act, fireEvent, renderHook } from '../../../../../../tests/test-utils';
|
||||
import { getTRPCMockError } from '../../../../mocks/getTrpcMock';
|
||||
import { server } from '../../../../mocks/server';
|
||||
import { useSystemStore } from '../../../../state/systemStore';
|
||||
import { useToastStore } from '../../../../state/toastStore';
|
||||
import { SettingsContainer } from './SettingsContainer';
|
||||
|
||||
|
@ -41,11 +42,57 @@ describe('Test: SettingsContainer', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('Restart', () => {
|
||||
it('should remove token from local storage on success', async () => {
|
||||
const { result, unmount } = renderHook(() => useSystemStore());
|
||||
const current = faker.system.semver();
|
||||
const removeItem = jest.spyOn(localStorage, 'removeItem');
|
||||
|
||||
render(<SettingsContainer data={{ current, latest: current }} />);
|
||||
expect(result.current.pollStatus).toBe(false);
|
||||
|
||||
const restartButton = screen.getByTestId('settings-modal-restart-button');
|
||||
|
||||
act(() => {
|
||||
fireEvent.click(restartButton);
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(removeItem).toBeCalledWith('token');
|
||||
expect(result.current.pollStatus).toBe(true);
|
||||
});
|
||||
|
||||
removeItem.mockRestore();
|
||||
unmount();
|
||||
});
|
||||
|
||||
it('should display error toast on error', async () => {
|
||||
const { result, unmount } = renderHook(() => useToastStore());
|
||||
const current = faker.system.semver();
|
||||
const error = faker.lorem.sentence();
|
||||
server.use(getTRPCMockError({ path: ['system', 'restart'], type: 'mutation', message: error }));
|
||||
render(<SettingsContainer data={{ current }} />);
|
||||
|
||||
const restartButton = screen.getByTestId('settings-modal-restart-button');
|
||||
act(() => {
|
||||
fireEvent.click(restartButton);
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.toasts[0].description).toBe(error);
|
||||
});
|
||||
|
||||
unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Update', () => {
|
||||
it('should remove token from local storage on success', async () => {
|
||||
const { result, unmount } = renderHook(() => useSystemStore());
|
||||
const current = '0.0.1';
|
||||
const latest = faker.system.semver();
|
||||
const removeItem = jest.spyOn(localStorage, 'removeItem');
|
||||
|
||||
render(<SettingsContainer data={{ current, latest }} />);
|
||||
|
||||
const updateButton = screen.getByText('Update');
|
||||
|
@ -55,7 +102,10 @@ describe('Test: SettingsContainer', () => {
|
|||
|
||||
await waitFor(() => {
|
||||
expect(removeItem).toBeCalledWith('token');
|
||||
expect(result.current.pollStatus).toBe(true);
|
||||
});
|
||||
|
||||
unmount();
|
||||
});
|
||||
|
||||
it('should display error toast on error', async () => {
|
||||
|
@ -78,40 +128,4 @@ describe('Test: SettingsContainer', () => {
|
|||
unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Restart', () => {
|
||||
it('should remove token from local storage on success', async () => {
|
||||
const current = faker.system.semver();
|
||||
const removeItem = jest.spyOn(localStorage, 'removeItem');
|
||||
|
||||
render(<SettingsContainer data={{ current }} />);
|
||||
const restartButton = screen.getByTestId('settings-modal-restart-button');
|
||||
act(() => {
|
||||
fireEvent.click(restartButton);
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(removeItem).toBeCalledWith('token');
|
||||
});
|
||||
});
|
||||
|
||||
it('should display error toast on error', async () => {
|
||||
const { result, unmount } = renderHook(() => useToastStore());
|
||||
const current = faker.system.semver();
|
||||
const error = faker.lorem.sentence();
|
||||
server.use(getTRPCMockError({ path: ['system', 'restart'], type: 'mutation', message: error }));
|
||||
render(<SettingsContainer data={{ current }} />);
|
||||
|
||||
const restartButton = screen.getByTestId('settings-modal-restart-button');
|
||||
act(() => {
|
||||
fireEvent.click(restartButton);
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.toasts[0].description).toBe(error);
|
||||
});
|
||||
|
||||
unmount();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -27,15 +27,16 @@ export const SettingsContainer: React.FC<IProps> = ({ data }) => {
|
|||
setLoading(true);
|
||||
},
|
||||
onSuccess: async () => {
|
||||
setLoading(false);
|
||||
setPollStatus(true);
|
||||
localStorage.removeItem('token');
|
||||
},
|
||||
onError: (error) => {
|
||||
setLoading(false);
|
||||
updateDisclosure.close();
|
||||
addToast({ title: 'Error', description: error.message, status: 'error' });
|
||||
},
|
||||
onSettled: () => {
|
||||
setLoading(false);
|
||||
},
|
||||
});
|
||||
|
||||
const restart = trpc.system.restart.useMutation({
|
||||
|
@ -43,15 +44,16 @@ export const SettingsContainer: React.FC<IProps> = ({ data }) => {
|
|||
setLoading(true);
|
||||
},
|
||||
onSuccess: async () => {
|
||||
setLoading(false);
|
||||
setPollStatus(true);
|
||||
localStorage.removeItem('token');
|
||||
},
|
||||
onError: (error) => {
|
||||
setLoading(false);
|
||||
restartDisclosure.close();
|
||||
addToast({ title: 'Error', description: error.message, status: 'error' });
|
||||
},
|
||||
onSettled: () => {
|
||||
setLoading(false);
|
||||
},
|
||||
});
|
||||
|
||||
const renderUpdate = () => {
|
||||
|
|
35
src/server/common/__tests__/get-server-auth-session.test.ts
Normal file
35
src/server/common/__tests__/get-server-auth-session.test.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import jwt from 'jsonwebtoken';
|
||||
import TipiCache from '../../core/TipiCache';
|
||||
import { getConfig } from '../../core/TipiConfig';
|
||||
import { getServerAuthSession } from '../get-server-auth-session';
|
||||
|
||||
jest.mock('redis');
|
||||
|
||||
describe('getServerAuthSession', () => {
|
||||
it('should return null if no token is provided', async () => {
|
||||
// @ts-expect-error - wrong res
|
||||
const result = await getServerAuthSession({ req: { headers: { authorization: null } }, res: {} });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null if an invalid token is provided', async () => {
|
||||
// @ts-expect-error - wrong res
|
||||
const result = await getServerAuthSession({ req: { headers: { authorization: 'Bearer invalid_token' } }, res: {} });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null if there is no session id in the cache', async () => {
|
||||
const validToken = jwt.sign('12', getConfig().jwtSecret);
|
||||
// @ts-expect-error - wrong res
|
||||
const result = await getServerAuthSession({ req: { headers: { authorization: `Bearer ${validToken}` } }, res: {} });
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return the user id and session id if a valid token is provided', async () => {
|
||||
const validToken = jwt.sign({ id: 12, session: 'session_id' }, getConfig().jwtSecret);
|
||||
TipiCache.set('session_id', '12');
|
||||
// @ts-expect-error - wrong res
|
||||
const result = await getServerAuthSession({ req: { headers: { authorization: `Bearer ${validToken}` } }, res: {} });
|
||||
expect(result).toEqual({ userId: 12, id: 'session_id' });
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue