From 2ba1be6ea377e6c4d8dc1b485a2697d62f82174e Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Mon, 12 Apr 2021 10:55:16 +0200 Subject: [PATCH] PrivateHistoryE2E: Extend POST /me/history Test the correct behaviour in error cases Signed-off-by: Philip Molares --- test/private-api/history.e2e-spec.ts | 95 ++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 20 deletions(-) diff --git a/test/private-api/history.e2e-spec.ts b/test/private-api/history.e2e-spec.ts index 192c8964d..4b79be03e 100644 --- a/test/private-api/history.e2e-spec.ts +++ b/test/private-api/history.e2e-spec.ts @@ -5,7 +5,7 @@ */ import { INestApplication } from '@nestjs/common'; -import { ConfigModule } from '@nestjs/config'; +import { ConfigModule, ConfigService } from '@nestjs/config'; import { Test } from '@nestjs/testing'; import { TypeOrmModule } from '@nestjs/typeorm'; import * as request from 'supertest'; @@ -27,6 +27,7 @@ import { PrivateApiModule } from '../../src/api/private/private-api.module'; import { HistoryService } from '../../src/history/history.service'; import { Note } from '../../src/notes/note.entity'; import { HistoryEntryImportDto } from '../../src/history/history-entry-import.dto'; +import { HistoryEntry } from '../../src/history/history-entry.entity'; describe('History', () => { let app: INestApplication; @@ -34,6 +35,7 @@ describe('History', () => { let user: User; let note: Note; let note2: Note; + let forbiddenNoteId: string; let content: string; beforeAll(async () => { @@ -66,6 +68,8 @@ describe('History', () => { ], }).compile(); + const config = moduleRef.get(ConfigService); + forbiddenNoteId = config.get('appConfig').forbiddenNoteIds[0]; app = moduleRef.createNestApplication(); await app.init(); content = 'This is a test note.'; @@ -99,25 +103,76 @@ describe('History', () => { ); }); - it('POST /me/history', async () => { - expect((await historyService.getEntriesByUser(user)).length).toEqual(1); - const pinStatus = true; - const lastVisited = new Date('2020-12-01 12:23:34'); - const postEntryDto = new HistoryEntryImportDto(); - postEntryDto.note = note2.alias; - postEntryDto.pinStatus = pinStatus; - postEntryDto.lastVisited = lastVisited; - await request(app.getHttpServer()) - .post('/me/history') - .set('Content-Type', 'application/json') - .send(JSON.stringify({ history: [postEntryDto] })) - .expect(201); - const userEntries = await historyService.getEntriesByUser(user); - expect(userEntries.length).toEqual(1); - expect(userEntries[0].note.alias).toEqual(note2.alias); - expect(userEntries[0].user.userName).toEqual(user.userName); - expect(userEntries[0].pinStatus).toEqual(pinStatus); - expect(userEntries[0].updatedAt).toEqual(lastVisited); + describe('POST /me/history', () => { + it('works', async () => { + expect(await historyService.getEntriesByUser(user)).toHaveLength(1); + const pinStatus = true; + const lastVisited = new Date('2020-12-01 12:23:34'); + const postEntryDto = new HistoryEntryImportDto(); + postEntryDto.note = note2.alias; + postEntryDto.pinStatus = pinStatus; + postEntryDto.lastVisited = lastVisited; + await request(app.getHttpServer()) + .post('/me/history') + .set('Content-Type', 'application/json') + .send(JSON.stringify({ history: [postEntryDto] })) + .expect(201); + const userEntries = await historyService.getEntriesByUser(user); + expect(userEntries.length).toEqual(1); + expect(userEntries[0].note.alias).toEqual(note2.alias); + expect(userEntries[0].user.userName).toEqual(user.userName); + expect(userEntries[0].pinStatus).toEqual(pinStatus); + expect(userEntries[0].updatedAt).toEqual(lastVisited); + }); + describe('fails', () => { + let pinStatus: boolean; + let lastVisited: Date; + let postEntryDto: HistoryEntryImportDto; + let prevEntry: HistoryEntry; + beforeAll(async () => { + const previousHistory = await historyService.getEntriesByUser(user); + expect(previousHistory).toHaveLength(1); + prevEntry = previousHistory[0]; + pinStatus = !previousHistory[0].pinStatus; + lastVisited = new Date('2020-12-01 23:34:45'); + postEntryDto = new HistoryEntryImportDto(); + postEntryDto.note = note2.alias; + postEntryDto.pinStatus = pinStatus; + postEntryDto.lastVisited = lastVisited; + }); + it('with forbiddenId', async () => { + const brokenEntryDto = new HistoryEntryImportDto(); + brokenEntryDto.note = forbiddenNoteId; + brokenEntryDto.pinStatus = pinStatus; + brokenEntryDto.lastVisited = lastVisited; + await request(app.getHttpServer()) + .post('/me/history') + .set('Content-Type', 'application/json') + .send(JSON.stringify({ history: [brokenEntryDto] })) + .expect(400); + }); + it('with non-existing note', async () => { + const brokenEntryDto = new HistoryEntryImportDto(); + brokenEntryDto.note = 'i_dont_exist'; + brokenEntryDto.pinStatus = pinStatus; + brokenEntryDto.lastVisited = lastVisited; + await request(app.getHttpServer()) + .post('/me/history') + .set('Content-Type', 'application/json') + .send(JSON.stringify({ history: [brokenEntryDto] })) + .expect(400); + }); + afterEach(async () => { + const historyEntries = await historyService.getEntriesByUser(user); + expect(historyEntries).toHaveLength(1); + expect(historyEntries[0].note.alias).toEqual(prevEntry.note.alias); + expect(historyEntries[0].user.userName).toEqual( + prevEntry.user.userName, + ); + expect(historyEntries[0].pinStatus).toEqual(prevEntry.pinStatus); + expect(historyEntries[0].updatedAt).toEqual(prevEntry.updatedAt); + }); + }); }); it('DELETE /me/history', async () => {