From 4c384cc8de3216ba18992126c7a1855ec45b1c41 Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Fri, 19 May 2023 13:38:27 +0200 Subject: [PATCH] test: add test for get note interceptor Signed-off-by: Tilman Vatteroth --- .../api/utils/get-note.interceptor.spec.ts | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 backend/src/api/utils/get-note.interceptor.spec.ts diff --git a/backend/src/api/utils/get-note.interceptor.spec.ts b/backend/src/api/utils/get-note.interceptor.spec.ts new file mode 100644 index 000000000..380be9218 --- /dev/null +++ b/backend/src/api/utils/get-note.interceptor.spec.ts @@ -0,0 +1,85 @@ +/* + * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { CallHandler, ExecutionContext } from '@nestjs/common'; +import { HttpArgumentsHost } from '@nestjs/common/interfaces/features/arguments-host.interface'; +import { Observable } from 'rxjs'; +import { Mock } from 'ts-mockery'; + +import { Note } from '../../notes/note.entity'; +import { NotesService } from '../../notes/notes.service'; +import { GetNoteInterceptor } from './get-note.interceptor'; +import { CompleteRequest } from './request.type'; + +describe('get note interceptor', () => { + const mockNote = Mock.of({}); + const mockNoteId = 'noteIdOrAlias'; + const mockObservable = Mock.of>({}); + const nextCallHandler = Mock.of({ + handle: () => mockObservable, + }); + + let notesService: NotesService; + let noteFetchSpy: jest.SpyInstance; + + beforeEach(() => { + notesService = Mock.of({ + getNoteByIdOrAlias: (id) => + id === mockNoteId ? Promise.resolve(mockNote) : Promise.reject(), + }); + noteFetchSpy = jest.spyOn(notesService, 'getNoteByIdOrAlias'); + }); + + function mockExecutionContext(request: CompleteRequest) { + return Mock.of({ + switchToHttp: () => + Mock.of({ + getRequest: () => request, + }), + }); + } + + it('extracts the note from the request headers', async () => { + const request = Mock.of({ + params: {}, + headers: { ['hedgedoc-note']: mockNoteId }, + }); + const context = mockExecutionContext(request); + const sut: GetNoteInterceptor = new GetNoteInterceptor(notesService); + const result = await sut.intercept(context, nextCallHandler); + + expect(result).toBe(mockObservable); + expect(request.note).toBe(mockNote); + expect(noteFetchSpy).toHaveBeenCalledTimes(1); + }); + + it('extracts the note from the request parameters', async () => { + const request = Mock.of({ + params: { noteIdOrAlias: mockNoteId }, + }); + const context = mockExecutionContext(request); + const sut: GetNoteInterceptor = new GetNoteInterceptor(notesService); + const result = await sut.intercept(context, nextCallHandler); + + expect(result).toBe(mockObservable); + expect(request.note).toBe(mockNote); + expect(noteFetchSpy).toHaveBeenCalledTimes(1); + }); + + it('works if no note id is in the request', async () => { + const request = Mock.of({ + params: {}, + headers: {}, + }); + + const context = mockExecutionContext(request); + const sut: GetNoteInterceptor = new GetNoteInterceptor(notesService); + const result = await sut.intercept(context, nextCallHandler); + + expect(result).toBe(mockObservable); + expect(request.note).toBe(undefined); + expect(noteFetchSpy).toHaveBeenCalledTimes(0); + }); +});