mirror of
https://github.com/jellyfin/jellyfin-vue.git
synced 2024-11-27 08:10:26 +00:00
Merge pull request #748 from jellyfin/test-admin-middleware1
test(admin middleware): add tests for admin middleware
This commit is contained in:
commit
a2c4649db9
46
middleware/__tests__/adminMiddleware.spec.ts
Normal file
46
middleware/__tests__/adminMiddleware.spec.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { Context } from '@nuxt/types';
|
||||
import adminMiddleware from '../adminMiddleware';
|
||||
|
||||
const mockRedirect = jest.fn();
|
||||
|
||||
const BASE_INPUT = ({
|
||||
$auth: {
|
||||
user: {
|
||||
Policy: {
|
||||
IsAdministrator: false
|
||||
}
|
||||
}
|
||||
},
|
||||
redirect: mockRedirect
|
||||
} as unknown) as Context;
|
||||
|
||||
const INPUT_NOT_ADMIN = {
|
||||
...BASE_INPUT
|
||||
};
|
||||
|
||||
const INPUT_ADMIN = ({
|
||||
...BASE_INPUT,
|
||||
$auth: {
|
||||
user: {
|
||||
Policy: {
|
||||
IsAdministrator: true
|
||||
}
|
||||
}
|
||||
}
|
||||
} as unknown) as Context;
|
||||
|
||||
afterEach(() => mockRedirect.mockReset());
|
||||
|
||||
describe('adminMiddleware', () => {
|
||||
it('redirects to "/" when user is not an administrator', (): void => {
|
||||
adminMiddleware(INPUT_NOT_ADMIN);
|
||||
|
||||
expect(mockRedirect.mock.calls[0][0]).toBe('/');
|
||||
});
|
||||
|
||||
it('does not redirect when user is not an administrator', (): void => {
|
||||
adminMiddleware(INPUT_ADMIN);
|
||||
|
||||
expect(mockRedirect.mock.calls).toHaveLength(0);
|
||||
});
|
||||
});
|
@ -8,7 +8,7 @@ import { Context } from '@nuxt/types';
|
||||
* @returns {void}
|
||||
*/
|
||||
export default function (context: Context): void {
|
||||
if (!context.$auth.user.Policy.IsAdministrator) {
|
||||
if (!context.$auth.user?.Policy?.IsAdministrator) {
|
||||
return context.redirect('/');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user