Merge pull request #748 from jellyfin/test-admin-middleware1

test(admin middleware): add tests for admin middleware
This commit is contained in:
Julien Machiels 2021-03-07 13:15:45 +01:00 committed by GitHub
commit a2c4649db9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View 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);
});
});

View File

@ -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('/');
}
}