High performance self-hosted photo and video management solution.
TypeScript backup-toolfluttergoogle-photosgoogle-photos-alternativejavascriptmobile-appnestjsnodejsphoto-galleryphotosphotos-managementself-hostedsveltesveltekittypescriptvideos
import { ExecutionContext } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { AuthGuard, Authenticated } from 'src/middleware/auth.guard.js';
import { LoggingRepository } from 'src/repositories/logging.repository.js';
import { AuthService } from 'src/services/auth.service.js';
import { mockEnvData } from 'test/repositories/config.repository.mock.js';
import { ServiceMocks, newTestService } from 'test/utils.js';
class TestController {
@Authenticated({ public: true, setup: true })
setupRoute() {}
@Authenticated({ public: true })
publicRoute() {}
undecoratedRoute() {}
}
@Authenticated({ admin: true })
class AdminTestController {
route() {}
}
const contextFor = (handler: () => void, controller: object = TestController) =>
({
getHandler: () => handler,
getClass: () => controller,
switchToHttp: () => ({ getRequest: () => ({ headers: {}, query: {}, path: '/' }) }),
}) as unknown as ExecutionContext;
describe(AuthGuard.name, () => {
let sut: AuthGuard;
let authService: AuthService;
let mocks: ServiceMocks;
beforeEach(() => {
({ sut: authService, mocks } = newTestService(AuthService));
sut = new AuthGuard(mocks.logger as unknown as LoggingRepository, new Reflector(), authService);
});
describe('setup routes', () => {
it('should allow access while the server is awaiting its first admin', async () => {
mocks.user.hasAdmin.mockResolvedValue(false);
const authenticate = vitest.spyOn(authService, 'authenticate');
await expect(sut.canActivate(contextFor(TestController.prototype.setupRoute))).resolves.toBe(true);
expect(authenticate).not.toHaveBeenCalled();
});
it('should reject when setup is disabled', async () => {
mocks.config.getEnv.mockReturnValue(mockEnvData({ setup: { allow: false } }));
mocks.user.hasAdmin.mockResolvedValue(false);
await expect(sut.canActivate(contextFor(TestController.prototype.setupRoute))).rejects.toThrowError(
'Admin setup is not available',
);
});
it('should reject when the server already has an admin', async () => {
mocks.user.hasAdmin.mockResolvedValue(true);
await expect(sut.canActivate(contextFor(TestController.prototype.setupRoute))).rejects.toThrowError(
'Admin setup is not available',
);
});
});
describe('public routes', () => {
it('should not require setup availability', async () => {
mocks.user.hasAdmin.mockResolvedValue(true);
await expect(sut.canActivate(contextFor(TestController.prototype.publicRoute))).resolves.toBe(true);
});
});
describe('undecorated routes', () => {
it('should be rejected', async () => {
const authenticate = vitest.spyOn(authService, 'authenticate');
await expect(sut.canActivate(contextFor(TestController.prototype.undecoratedRoute))).rejects.toThrowError(
'does not declare @Authenticated()',
);
expect(authenticate).not.toHaveBeenCalled();
});
});
describe('class-level @Authenticated()', () => {
it('should apply the controller options to its undecorated routes', async () => {
const authenticate = vitest.spyOn(authService, 'authenticate').mockResolvedValue({} as never);
await expect(sut.canActivate(contextFor(AdminTestController.prototype.route, AdminTestController))).resolves.toBe(
true,
);
expect(authenticate).toHaveBeenCalledWith(
expect.objectContaining({ metadata: expect.objectContaining({ adminRoute: true }) }),
);
});
});
});