UNPKG

@sync-in/server

Version:

The secure, open-source platform for file storage, sharing, collaboration, and sync

89 lines (88 loc) 3.31 kB
/* * Copyright (C) 2012-2025 Johan Legrand <johan.legrand@sync-in.com> * This file is part of Sync-in | The open source file sync and share solution * See the LICENSE file for licensing details */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const _tsjest = require("@golevelup/ts-jest"); const _testing = require("@nestjs/testing"); const _nestjspino = require("nestjs-pino"); const _usermodel = require("../../applications/users/models/user.model"); const _test = require("../../applications/users/utils/test"); const _authmethod = require("../models/auth-method"); const _authlocalguard = require("./auth-local.guard"); const _authlocalstrategy = require("./auth-local.strategy"); describe(_authlocalguard.AuthLocalGuard.name, ()=>{ let authLocalGuard; let authMethod; let userTest; let context; beforeAll(async ()=>{ const module = await _testing.Test.createTestingModule({ providers: [ _authlocalguard.AuthLocalGuard, _authlocalstrategy.AuthLocalStrategy, { provide: _authmethod.AuthMethod, useValue: {} }, { provide: _nestjspino.PinoLogger, useValue: { assign: ()=>undefined } } ] }).compile(); authLocalGuard = module.get(_authlocalguard.AuthLocalGuard); authMethod = module.get(_authmethod.AuthMethod); userTest = new _usermodel.UserModel((0, _test.generateUserTest)(), false); context = (0, _tsjest.createMock)(); }); it('should be defined', ()=>{ expect(authLocalGuard).toBeDefined(); expect(authMethod).toBeDefined(); expect(userTest).toBeDefined(); }); it('should validate the user authentication', async ()=>{ authMethod.validateUser = jest.fn().mockReturnValueOnce(userTest); context.switchToHttp().getRequest.mockReturnValue({ raw: { user: '' }, body: { login: userTest.login, password: userTest.password } }); expect(await authLocalGuard.canActivate(context)).toBe(true); expect(userTest.password).toBeUndefined(); }); it('should not validate the user authentication', async ()=>{ userTest.password = 'password'; authMethod.validateUser = jest.fn().mockReturnValueOnce(null); context.switchToHttp().getRequest.mockReturnValue({ raw: { user: '' }, body: { login: userTest.login, password: userTest.password } }); await expect(authLocalGuard.canActivate(context)).rejects.toThrow(/password/i); }); it('should throw error due to malformed body', async ()=>{ authMethod.validateUser = jest.fn().mockReturnValueOnce(null); context.switchToHttp().getRequest.mockReturnValue({ raw: { user: '' }, body: null }); await expect(authLocalGuard.canActivate(context)).rejects.toThrow(); }); }); //# sourceMappingURL=auth-local.guard.spec.js.map