Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
test(backend:files): improve coverage of files-only-office.guard.spec.ts
johavend4e2cba21787
diff --git a/backend/src/applications/files/guards/files-only-office.guard.spec.ts b/backend/src/applications/files/guards/files-only-office.guard.spec.tsindex 78763491..8a582466 100644--- a/backend/src/applications/files/guards/files-only-office.guard.spec.ts+++ b/backend/src/applications/files/guards/files-only-office.guard.spec.ts@@ -4,10 +4,78 @@* See the LICENSE file for licensing details*/+import { createMock, DeepMocked } from '@golevelup/ts-jest'+import { ExecutionContext } from '@nestjs/common'+import { JwtModule, JwtService } from '@nestjs/jwt'+import { Test, TestingModule } from '@nestjs/testing'+import { PinoLogger } from 'nestjs-pino'+import { JwtPayload } from '../../../authentication/interfaces/jwt-payload.interface'+import { configuration } from '../../../configuration/config.environment'+import { ONLY_OFFICE_TOKEN_QUERY_PARAM_NAME } from '../constants/only-office'+import { API_FILES_ONLY_OFFICE_CALLBACK } from '../constants/routes'import { FilesOnlyOfficeGuard } from './files-only-office.guard'+import { FilesOnlyOfficeStrategy } from './files-only-office.strategy'describe(FilesOnlyOfficeGuard.name, () => {+ let jwtService: JwtService+ let filesOnlyOfficeGuard: FilesOnlyOfficeGuard+ let context: DeepMocked<ExecutionContext>+ let accessToken: string++ beforeAll(async () => {+ const module: TestingModule = await Test.createTestingModule({+ imports: [JwtModule.register({ global: true })],+ providers: [+ FilesOnlyOfficeGuard,+ FilesOnlyOfficeStrategy,+ {+ provide: PinoLogger,+ useValue: {+ assign: () => undefined+ }+ }+ ]+ }).compile()++ jwtService = module.get<JwtService>(JwtService)+ filesOnlyOfficeGuard = module.get<FilesOnlyOfficeGuard>(FilesOnlyOfficeGuard)+ context = createMock<ExecutionContext>()+ accessToken = await jwtService.signAsync({ identity: { id: 1, login: 'foo' } } as JwtPayload, {+ secret: configuration.auth.token.access.secret,+ expiresIn: 30+ })+ })+it('should be defined', () => {- expect(new FilesOnlyOfficeGuard()).toBeDefined()+ expect(jwtService).toBeDefined()+ expect(filesOnlyOfficeGuard).toBeDefined()+ expect(accessToken).toBeDefined()+ })++ it('should not pass if enabled (or not) without a valid token', async () => {+ configuration.applications.files.onlyoffice.enabled = false+ context.switchToHttp().getRequest.mockReturnValue({+ url: `${API_FILES_ONLY_OFFICE_CALLBACK}`,+ raw: { user: '' }+ })+ expect(() => filesOnlyOfficeGuard.canActivate(context)).toThrow(/feature not enabled/i)+ configuration.applications.files.onlyoffice.enabled = true+ await expect(filesOnlyOfficeGuard.canActivate(context)).rejects.toThrow('Unauthorized')+ })++ it('should pass if enabled (or not) with a valid token', async () => {+ configuration.applications.files.onlyoffice.enabled = false+ context.switchToHttp().getRequest.mockReturnValue({+ url: `${API_FILES_ONLY_OFFICE_CALLBACK}?${ONLY_OFFICE_TOKEN_QUERY_PARAM_NAME}=${accessToken}`,+ raw: { user: '' }+ })+ expect(() => filesOnlyOfficeGuard.canActivate(context)).toThrow(/feature not enabled/i)+ configuration.applications.files.onlyoffice.enabled = true+ expect(await filesOnlyOfficeGuard.canActivate(context)).toBe(true)+ context.switchToHttp().getRequest.mockReturnValue({+ url: `${API_FILES_ONLY_OFFICE_CALLBACK}?${ONLY_OFFICE_TOKEN_QUERY_PARAM_NAME}=unvalidToken`,+ raw: { user: '' }+ })+ await expect(filesOnlyOfficeGuard.canActivate(context)).rejects.toThrow('Unauthorized')})})diff --git a/backend/src/authentication/guards/auth-basic.guard.ts b/backend/src/authentication/guards/auth-basic.guard.tsindex f6dcc814..2db8ed47 100644--- a/backend/src/authentication/guards/auth-basic.guard.ts+++ b/backend/src/authentication/guards/auth-basic.guard.ts@@ -5,8 +5,7 @@*/import { ExecutionContext, Injectable, Logger } from '@nestjs/common'-import { AuthGuard } from '@nestjs/passport'-import { IAuthGuard } from '@nestjs/passport/dist/auth.guard'+import { AuthGuard, IAuthGuard } from '@nestjs/passport'import { FastifyRequest } from 'fastify'import { HTTP_METHOD } from '../../applications/applications.constants'import { WEBDAV_BASE_PATH } from '../../applications/webdav/constants/routes'diff --git a/backend/src/authentication/guards/auth-digest.guard.ts b/backend/src/authentication/guards/auth-digest.guard.tsindex 1fd6df84..b8bfa837 100644--- a/backend/src/authentication/guards/auth-digest.guard.ts+++ b/backend/src/authentication/guards/auth-digest.guard.ts@@ -5,8 +5,7 @@*/// import { ExecutionContext, Injectable, Logger } from '@nestjs/common'-// import { AuthGuard } from '@nestjs/passport'-// import { IAuthGuard } from '@nestjs/passport/dist/auth.guard'+// import { AuthGuard, IAuthGuard } from '@nestjs/passport'//// @Injectable()// export class AuthDigestGuard extends AuthGuard('digest') implements IAuthGuard {diff --git a/backend/src/authentication/guards/auth-local.guard.ts b/backend/src/authentication/guards/auth-local.guard.tsindex 421bea85..a7a6988b 100755--- a/backend/src/authentication/guards/auth-local.guard.ts+++ b/backend/src/authentication/guards/auth-local.guard.ts@@ -5,8 +5,7 @@*/import { ExecutionContext, Injectable, Logger } from '@nestjs/common'-import { AuthGuard } from '@nestjs/passport'-import { IAuthGuard } from '@nestjs/passport/dist/auth.guard'+import { AuthGuard, IAuthGuard } from '@nestjs/passport'@Injectable()export class AuthLocalGuard extends AuthGuard('local') implements IAuthGuard {