UNPKG

@sync-in/server

Version:

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

121 lines (120 loc) 4.69 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 _common = require("@nestjs/common"); const _testing = require("@nestjs/testing"); const _linkscontroller = require("./links.controller"); const _linksmanagerservice = require("./services/links-manager.service"); describe(_linkscontroller.LinksController.name, ()=>{ let controller; let linksManager; beforeAll(async ()=>{ linksManager = { linkValidation: jest.fn(), linkAccess: jest.fn(), linkAuthentication: jest.fn() }; const module = await _testing.Test.createTestingModule({ controllers: [ _linkscontroller.LinksController ], providers: [ { provide: _linksmanagerservice.LinksManager, useValue: linksManager } ] }).compile(); controller = module.get(_linkscontroller.LinksController); }); beforeEach(()=>{ jest.clearAllMocks(); }); it('should be defined', ()=>{ expect(controller).toBeDefined(); }); describe('linkValidation', ()=>{ it('should call LinksManager.linkValidation with provided user and uuid and return its result', async ()=>{ const user = { id: 123, login: 'john' }; const uuid = 'test-uuid-1'; const expected = { error: true, ok: true, link: { id: 1, uuid, name: 'My Link' } }; linksManager.linkValidation.mockResolvedValue(expected); const result = await controller.linkValidation(user, uuid); expect(linksManager.linkValidation).toHaveBeenCalledTimes(1); expect(linksManager.linkValidation).toHaveBeenCalledWith(user, uuid); expect(result).toBe(expected); }); }); describe('linkAccess', ()=>{ it('should forward to LinksManager.linkAccess and return a StreamableFile', async ()=>{ const user = { id: 42 }; const uuid = 'test-uuid-2'; const req = {}; const res = {}; const file = new _common.StreamableFile(Buffer.from('data')); linksManager.linkAccess.mockResolvedValue(file); const result = await controller.linkAccess(user, uuid, req, res); expect(linksManager.linkAccess).toHaveBeenCalledTimes(1); expect(linksManager.linkAccess).toHaveBeenCalledWith(user, uuid, req, res); expect(result).toBe(file); }); it('should forward to LinksManager.linkAccess and return a LoginResponseDto', async ()=>{ const user = { id: 43 }; const uuid = 'test-uuid-3'; const req = {}; const res = {}; const loginResponse = { accessToken: 'token', refreshToken: 'refresh' }; linksManager.linkAccess.mockResolvedValue(loginResponse); const result = await controller.linkAccess(user, uuid, req, res); expect(linksManager.linkAccess).toHaveBeenCalledTimes(1); expect(linksManager.linkAccess).toHaveBeenCalledWith(user, uuid, req, res); expect(result).toBe(loginResponse); }); }); describe('linkAuthentication', ()=>{ it('should call LinksManager.linkAuthentication with provided params and return its result', async ()=>{ const user = { id: 7 }; const uuid = 'test-uuid-4'; const body = { password: 'secret' }; const req = {}; const res = {}; const expectedLogin = { accessToken: 'acc', refreshToken: 'ref' }; linksManager.linkAuthentication.mockResolvedValue(expectedLogin); const result = await controller.linkAuthentication(user, uuid, body, req, res); expect(linksManager.linkAuthentication).toHaveBeenCalledTimes(1); expect(linksManager.linkAuthentication).toHaveBeenCalledWith(user, uuid, body, req, res); expect(result).toBe(expectedLogin); }); }); }); //# sourceMappingURL=links.controller.spec.js.map