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(mfa): fix tests after implementation
johavencc6c9ac99e4a
diff --git a/backend/src/applications/notifications/services/notifications-manager.service.spec.ts b/backend/src/applications/notifications/services/notifications-manager.service.spec.tsindex edb63746..d253cda5 100644--- a/backend/src/applications/notifications/services/notifications-manager.service.spec.ts+++ b/backend/src/applications/notifications/services/notifications-manager.service.spec.ts@@ -8,6 +8,7 @@ import { Test, TestingModule } from '@nestjs/testing'import { Mailer } from '../../../infrastructure/mailer/mailer.service'import { USER_NOTIFICATION } from '../../users/constants/user'import { UsersManager } from '../../users/services/users-manager.service'+import { getAvatarBase64 } from '../../users/utils/avatar'import { NOTIFICATION_APP } from '../constants/notifications'import { NOTIFICATIONS_WS } from '../constants/websocket'import * as mailModels from '../mails/models'@@ -25,10 +26,13 @@ jest.mock('../mails/models', () => ({syncMail: jest.fn(() => ['sync title', 'sync html'])}))+jest.mock('../../users/utils/avatar', () => ({+ getAvatarBase64: jest.fn()+}))+describe(NotificationsManager.name, () => {let service: NotificationsManager- const usersManagerMock = { getAvatarBase64: jest.fn() }const mailerMock = { available: true, sendMails: jest.fn() }const notificationsQueriesMock = {list: jest.fn(),@@ -51,12 +55,11 @@ describe(NotificationsManager.name, () => {notificationsQueriesMock.delete.mockResolvedValue(undefined)notificationsQueriesMock.list.mockResolvedValue([])notificationsQueriesMock.usersNotifiedByEmail.mockResolvedValue([])- usersManagerMock.getAvatarBase64.mockResolvedValue('avatar-base64')const module: TestingModule = await Test.createTestingModule({providers: [NotificationsManager,- { provide: UsersManager, useValue: usersManagerMock },+ { provide: UsersManager, useValue: {} },{ provide: Mailer, useValue: mailerMock },{ provide: WebSocketNotifications, useValue: webSocketNotificationsMock },{ provide: NotificationsQueries, useValue: notificationsQueriesMock }@@ -182,12 +185,12 @@ describe(NotificationsManager.name, () => {author: { id: 1, login: 'john' }} as any)- expect(usersManagerMock.getAvatarBase64).not.toHaveBeenCalled()+ expect(getAvatarBase64).not.toHaveBeenCalled()expect(mailerMock.sendMails).not.toHaveBeenCalled()})it('enriches author avatar and sends mapped mails', async () => {- usersManagerMock.getAvatarBase64.mockResolvedValueOnce('base64-xxx')+ ;(getAvatarBase64 as jest.Mock).mockResolvedValueOnce('base64-xxx')const toUsers = [{ id: 1, email: 'a@test', language: 'en' },{ id: 2, email: 'b@test', language: 'fr' }@@ -195,7 +198,7 @@ describe(NotificationsManager.name, () => {const options: any = { author: { id: 9, login: 'jdoe' }, content: 'hello', currentUrl: 'https://app.test/path' }const content = { app: NOTIFICATION_APP.COMMENTS } as anyawait service.sendEmailNotification(toUsers as any, content, options)- expect(usersManagerMock.getAvatarBase64).toHaveBeenCalledWith('jdoe')+ expect(getAvatarBase64).toHaveBeenCalledWith('jdoe')expect(options.author.avatarBase64).toBe('base64-xxx')expect(mailerMock.sendMails).toHaveBeenCalledTimes(1)expect((mailerMock.sendMails as jest.Mock).mock.calls[0][0]).toEqual([diff --git a/backend/src/applications/sync/services/sync-clients-manager.service.ts b/backend/src/applications/sync/services/sync-clients-manager.service.tsindex 6959714e..b4d1eb7b 100644--- a/backend/src/applications/sync/services/sync-clients-manager.service.ts+++ b/backend/src/applications/sync/services/sync-clients-manager.service.ts@@ -11,7 +11,6 @@ import { FastifyReply } from 'fastify'import crypto from 'node:crypto'import fs from 'node:fs/promises'import path from 'node:path'-import { User } from 'src/applications/users/schemas/user.interface'import { AuthMethod } from '../../../authentication/models/auth-method'import { AuthManager } from '../../../authentication/services/auth-manager.service'import { convertHumanTimeToSeconds } from '../../../common/functions'@@ -23,6 +22,7 @@ import { HTTP_METHOD } from '../../applications.constants'import { isPathExists } from '../../files/utils/files'import { USER_PERMISSION } from '../../users/constants/user'import { UserModel } from '../../users/models/user.model'+import type { User } from '../../users/schemas/user.interface'import { UsersQueries } from '../../users/services/users-queries.service'import { CLIENT_AUTH_TYPE, CLIENT_TOKEN_EXPIRATION_TIME, CLIENT_TOKEN_EXPIRED_ERROR, CLIENT_TOKEN_RENEW_TIME } from '../constants/auth'import { APP_STORE_DIRNAME, APP_STORE_MANIFEST_FILE, APP_STORE_REPOSITORY, APP_STORE_URL } from '../constants/store'diff --git a/backend/src/applications/users/admin-users.controller.spec.ts b/backend/src/applications/users/admin-users.controller.spec.tsindex 7f89ab02..ac982296 100644--- a/backend/src/applications/users/admin-users.controller.spec.ts+++ b/backend/src/applications/users/admin-users.controller.spec.ts@@ -7,10 +7,13 @@import { ConfigModule } from '@nestjs/config'import { JwtService } from '@nestjs/jwt'import { Test, TestingModule } from '@nestjs/testing'+import { AuthTwoFaGuard } from '../../authentication/guards/auth-two-fa-guard'import { AuthManager } from '../../authentication/services/auth-manager.service'+import { AuthMethod2FA } from '../../authentication/services/auth-methods/auth-method-two-fa.service'import { exportConfiguration } from '../../configuration/config.environment'import { Cache } from '../../infrastructure/cache/services/cache.service'import { DB_TOKEN_PROVIDER } from '../../infrastructure/database/constants'+import { NotificationsManager } from '../notifications/services/notifications-manager.service'import { AdminUsersController } from './admin-users.controller'import { AdminUsersManager } from './services/admin-users-manager.service'import { AdminUsersQueries } from './services/admin-users-queries.service'@@ -30,6 +33,9 @@ describe(AdminUsersController.name, () => {provide: Cache,useValue: {}},+ { provide: AuthMethod2FA, useValue: {} },+ { provide: AuthTwoFaGuard, useValue: {} },+ { provide: NotificationsManager, useValue: {} },JwtService,AuthManager,AdminUsersManager,diff --git a/backend/src/applications/users/models/user.model.ts b/backend/src/applications/users/models/user.model.tsindex cd18705d..22312b6a 100644--- a/backend/src/applications/users/models/user.model.ts+++ b/backend/src/applications/users/models/user.model.ts@@ -214,7 +214,8 @@ export class UserModel implements User {private initSecrets(props: Partial<UserModel>) {// Remove the `twoFaEnabled` property to avoid conflicts with the current getter when using `plainToClass` with `class-validator`- if ('twoFaEnabled' in props) {+ // The `props` variable may be empty when the class is instantiated using `plainToClass`+ if (props && 'twoFaEnabled' in props) {// Only used when the User model is instantiated from a token payload// Set a `twoFaSecret` property (boolean) on the `secrets` property so that the `twoFaEnabled` getter returns `true`this.secrets = { twoFaSecret: popFromObject('twoFaEnabled', props) }diff --git a/backend/src/applications/users/users.controller.spec.ts b/backend/src/applications/users/users.controller.spec.tsindex 2aa5bb45..44efc52d 100755--- a/backend/src/applications/users/users.controller.spec.ts+++ b/backend/src/applications/users/users.controller.spec.ts@@ -8,9 +8,11 @@ import { ConfigModule } from '@nestjs/config'import { JwtService } from '@nestjs/jwt'import { Test, TestingModule } from '@nestjs/testing'import { AuthManager } from '../../authentication/services/auth-manager.service'+import { AuthMethod2FA } from '../../authentication/services/auth-methods/auth-method-two-fa.service'import { exportConfiguration } from '../../configuration/config.environment'import { Cache } from '../../infrastructure/cache/services/cache.service'import { DB_TOKEN_PROVIDER } from '../../infrastructure/database/constants'+import { NotificationsManager } from '../notifications/services/notifications-manager.service'import { UserModel } from './models/user.model'import { AdminUsersManager } from './services/admin-users-manager.service'import { AdminUsersQueries } from './services/admin-users-queries.service'@@ -41,7 +43,9 @@ describe(UsersController.name, () => {AdminUsersManager,AdminUsersQueries,AuthManager,- JwtService+ JwtService,+ AuthMethod2FA,+ { provide: NotificationsManager, useValue: {} }]}).compile()userController = module.get<UsersController>(UsersController)diff --git a/backend/src/authentication/auth.controller.spec.ts b/backend/src/authentication/auth.controller.spec.tsindex 0dfa8f0c..d6a679a7 100755--- a/backend/src/authentication/auth.controller.spec.ts+++ b/backend/src/authentication/auth.controller.spec.ts@@ -8,7 +8,9 @@ import { ConfigModule, ConfigService } from '@nestjs/config'import { JwtService } from '@nestjs/jwt'import { PassportModule } from '@nestjs/passport'import { Test, TestingModule } from '@nestjs/testing'+import { NotificationsManager } from '../applications/notifications/services/notifications-manager.service'import { UserModel } from '../applications/users/models/user.model'+import { UsersManager } from '../applications/users/services/users-manager.service'import { generateUserTest } from '../applications/users/utils/test'import { convertHumanTimeToSeconds } from '../common/functions'import { currentTimeStamp } from '../common/shared'@@ -17,9 +19,12 @@ import { Cache } from '../infrastructure/cache/services/cache.service'import { DB_TOKEN_PROVIDER } from '../infrastructure/database/constants'import { AuthConfig } from './auth.config'import { AuthController } from './auth.controller'+import { TOKEN_PATHS } from './constants/auth'import { LoginResponseDto } from './dto/login-response.dto'+import { AuthTwoFaGuard } from './guards/auth-two-fa-guard'import { TOKEN_TYPE } from './interfaces/token.interface'import { AuthManager } from './services/auth-manager.service'+import { AuthMethod2FA } from './services/auth-methods/auth-method-two-fa.service'describe(AuthController.name, () => {let module: TestingModule@@ -31,7 +36,17 @@ describe(AuthController.name, () => {module = await Test.createTestingModule({imports: [await ConfigModule.forRoot({ load: [exportConfiguration], isGlobal: true }), PassportModule],controllers: [AuthController],- providers: [ConfigService, AuthManager, JwtService, { provide: DB_TOKEN_PROVIDER, useValue: {} }, { provide: Cache, useValue: {} }]+ providers: [+ ConfigService,+ AuthManager,+ JwtService,+ AuthMethod2FA,+ AuthTwoFaGuard,+ { provide: DB_TOKEN_PROVIDER, useValue: {} },+ { provide: Cache, useValue: {} },+ { provide: UsersManager, useValue: {} },+ { provide: NotificationsManager, useValue: {} }+ ]}).compile()module.useLogger(['fatal'])@@ -63,7 +78,7 @@ describe(AuthController.name, () => {it('should clear JWT in cookies', async () => {const res: any = { clearCookie: jest.fn() }await expect(authController.logout(res)).resolves.not.toThrow()- expect(res.clearCookie).toHaveBeenCalledTimes(4)+ expect(res.clearCookie).toHaveBeenCalledTimes(Object.keys(TOKEN_PATHS).length)})it('should refresh JWT in cookies', async () => {diff --git a/backend/src/authentication/guards/auth-basic.strategy.ts b/backend/src/authentication/guards/auth-basic.strategy.tsindex e4a7d4a8..47a373c0 100644--- a/backend/src/authentication/guards/auth-basic.strategy.ts+++ b/backend/src/authentication/guards/auth-basic.strategy.ts@@ -37,6 +37,7 @@ export class AuthBasicStrategy extends PassportStrategy(BasicStrategy, 'basic')}if (userFromCache !== undefined) {// cached+ // warning: plainToInstance do not use constructor to instantiate classreturn plainToInstance(UserModel, userFromCache)}const userFromDB: UserModel = await this.authMethod.validateUser(loginOrEmail, password, req.ip)diff --git a/backend/src/authentication/services/auth-methods/auth-method-database.service.spec.ts b/backend/src/authentication/services/auth-methods/auth-method-database.service.spec.tsindex 16927f77..4778964d 100644--- a/backend/src/authentication/services/auth-methods/auth-method-database.service.spec.ts+++ b/backend/src/authentication/services/auth-methods/auth-method-database.service.spec.ts@@ -6,6 +6,7 @@import { Test, TestingModule } from '@nestjs/testing'import { CONNECT_ERROR_CODE } from '../../../app.constants'+import { NotificationsManager } from '../../../applications/notifications/services/notifications-manager.service'import { UserModel } from '../../../applications/users/models/user.model'import { AdminUsersManager } from '../../../applications/users/services/admin-users-manager.service'import { AdminUsersQueries } from '../../../applications/users/services/admin-users-queries.service'@@ -33,10 +34,8 @@ describe(AuthMethodDatabase.name, () => {AdminUsersQueries,{ provide: AuthManager, useValue: {} },{ provide: DB_TOKEN_PROVIDER, useValue: {} },- {- provide: Cache,- useValue: {}- }+ { provide: Cache, useValue: {} },+ { provide: NotificationsManager, useValue: {} }]}).compile()diff --git a/backend/src/authentication/services/auth-methods/auth-method-two-fa.service.spec.ts b/backend/src/authentication/services/auth-methods/auth-method-two-fa.service.spec.tsindex 955f3d62..428554b9 100644--- a/backend/src/authentication/services/auth-methods/auth-method-two-fa.service.spec.ts+++ b/backend/src/authentication/services/auth-methods/auth-method-two-fa.service.spec.ts@@ -5,14 +5,22 @@*/import { Test, TestingModule } from '@nestjs/testing'+import { NotificationsManager } from '../../../applications/notifications/services/notifications-manager.service'+import { UsersManager } from '../../../applications/users/services/users-manager.service'+import { Cache } from '../../../infrastructure/cache/services/cache.service'import { AuthMethod2FA } from './auth-method-two-fa.service'describe(AuthMethod2FA.name, () => {let service: AuthMethod2FA- beforeEach(async () => {+ beforeAll(async () => {const module: TestingModule = await Test.createTestingModule({- providers: [AuthMethod2FA]+ providers: [+ AuthMethod2FA,+ { provide: Cache, useValue: {} },+ { provide: UsersManager, useValue: {} },+ { provide: NotificationsManager, useValue: {} }+ ]}).compile()service = module.get<AuthMethod2FA>(AuthMethod2FA)diff --git a/backend/src/authentication/services/auth-methods/auth-method-two-fa.service.ts b/backend/src/authentication/services/auth-methods/auth-method-two-fa.service.tsindex 504331e7..d81e54d0 100644--- a/backend/src/authentication/services/auth-methods/auth-method-two-fa.service.ts+++ b/backend/src/authentication/services/auth-methods/auth-method-two-fa.service.ts@@ -5,7 +5,6 @@*/import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common'-import { configuration } from 'src/configuration/config.environment'import { Totp } from 'time2fa'import { NOTIFICATION_APP, NOTIFICATION_APP_EVENT } from '../../../applications/notifications/constants/notifications'import { NotificationContent } from '../../../applications/notifications/interfaces/notification-properties.interface'@@ -16,6 +15,7 @@ import { UsersManager } from '../../../applications/users/services/users-managerimport { ACTION } from '../../../common/constants'import { generateShortUUID } from '../../../common/functions'import { qrcodeToDataURL } from '../../../common/qrcode'+import { configuration } from '../../../configuration/config.environment'import { Cache } from '../../../infrastructure/cache/services/cache.service'import { TWO_FA_CODE_LENGTH } from '../../constants/auth'import { TwoFaVerifyDto } from '../../dto/two-fa-verify.dto'diff --git a/backend/src/common/functions.ts b/backend/src/common/functions.tsindex 63c349f0..fe5d814a 100755--- a/backend/src/common/functions.ts+++ b/backend/src/common/functions.ts@@ -95,6 +95,7 @@ export function transformAndValidate<T extends object>(transformOptions: ClassTransformOptions = {},validatorOptions: ValidatorOptions = {}): T {+ // warning: plainToInstance do not use constructor to instantiate classconst instance: T = plainToInstance(schema, object, transformOptions)const errors: ValidationError[] = validateSync(instance, validatorOptions)if (errors.length > 0) {