From bf1081bcf6dc3955f33464346798a15f60455cad Mon Sep 17 00:00:00 2001 From: Yannick Bungers Date: Sat, 26 Sep 2020 23:27:24 +0200 Subject: [PATCH 1/3] Added tests test functions for /me routes Note that they don't contain the functions from the services yet. Signed-off-by: Yannick Bungers --- test/public-api/users.e2e-spec.ts | 135 ++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 test/public-api/users.e2e-spec.ts diff --git a/test/public-api/users.e2e-spec.ts b/test/public-api/users.e2e-spec.ts new file mode 100644 index 000000000..966ada3b2 --- /dev/null +++ b/test/public-api/users.e2e-spec.ts @@ -0,0 +1,135 @@ +import { INestApplication } from '@nestjs/common'; +import { Test } from '@nestjs/testing'; +import * as request from 'supertest'; +import { AppModule } from '../../src/app.module'; +import { UsersService } from '../../src/users/users.service'; +import { UserInfoDto } from '../../src/users/user-info.dto'; +import { HistoryService } from '../../src/history/history.service'; +import { NotesService } from '../../src/notes/notes.service'; +import { HistoryEntryUpdateDto } from '../../src/history/history-entry-update.dto'; +import { HistoryEntryDto } from '../../src/history/history-entry.dto'; + +// TODO Tests have to be reworked using UserService functions + +describe('Notes', () => { + let app: INestApplication; + let usersService: UsersService; + let historyService: HistoryService; + let notesService: NotesService; + + beforeAll(async () => { + const moduleRef = await Test.createTestingModule({ + imports: [AppModule], + }).compile(); + // TODO Create User and generateAPI Token or other Auth + app = moduleRef.createNestApplication(); + usersService = moduleRef.get(UsersService); + await app.init(); + }); + + it.skip(`GET /me`, async () => { + // TODO Get user from beforeAll + let userInfo = new UserInfoDto(); + const response = await request(app.getHttpServer()) + .post('/me') + .expect('Content-Type', /json/) + .expect(200); + expect(response.body.content).toEqual(userInfo); + }); + + it.skip(`GET /me/history`, async () => { + // TODO user has to be chosen + /* TODO Note maybe not added to history by createNote, + use function from HistoryService instead + */ + notesService.createNote('', 'testGetHistory'); + const response = await request(app.getHttpServer()) + .get('/me/history') + .expect('Content-Type', /json/) + .expect(200); + let historyEntry: HistoryEntryDto; + for (let e of response.body.content) { + if ((e).metadata.alias === 'testGetHistory') { + historyEntry = e; + } + } + expect(historyEntry).toEqual(history); + }); + + it.skip(`GET /me/history/{note}`, async () => { + const noteName = 'testGetNoteHistory'; + /* TODO Note maybe not added to history by createNote, + use function from HistoryService instead + */ + notesService.createNote('', noteName); + const response = await request(app.getHttpServer()) + .get('/me/history/' + noteName) + .expect('Content-Type', /json/) + .expect(200); + expect(response.body.metadata?.id).toBeDefined(); + return expect(response.body.metadata.alias).toEqual(noteName); + }); + + it.skip(`DELETE /me/history/{note}`, async () => { + const noteName = 'testDeleteNoteHistory'; + /* TODO Note maybe not added to history by createNote, + use function from HistoryService instead + */ + notesService.createNote('This is a test note.', noteName); + const response = await request(app.getHttpServer()) + .delete('/me/history/test3') + .expect(204); + expect(response.body.content).toBeNull(); + const history = historyService.getUserHistory('testuser'); + let historyEntry: HistoryEntryDto = null; + for (let e of history) { + if (e.metadata.alias === noteName) { + historyEntry = e; + } + } + return expect(historyEntry).toBeNull(); + }); + + it.skip(`PUT /me/history/{note}`, async () => { + const noteName = 'testPutNoteHistory'; + // TODO use function from HistoryService to add an History Entry + notesService.createNote('', noteName); + let historyEntryUpdateDto = new HistoryEntryUpdateDto(); + historyEntryUpdateDto.pinStatus = true; + let response = await request(app.getHttpServer()) + .put('/me/history/' + noteName) + .send(historyEntryUpdateDto) + .expect(200); + // TODO parameter is not used for now + const history = historyService.getUserHistory('testuser'); + let historyEntry: HistoryEntryDto; + for (let e of response.body.content) { + if ((e).metadata.alias === noteName) { + historyEntry = e; + } + } + expect(historyEntry.pinStatus).toEqual(true); + historyEntry = null; + for (let e of history) { + if (e.metadata.alias === noteName) { + historyEntry = e; + } + } + expect(historyEntry.pinStatus).toEqual(true); + }); + + + it.skip(`GET /me/notes/`, async () => { + notesService.createNote('This is a test note.', 'test7'); + // usersService.getALLNotesOwnedByUser() TODO Implement function + const response = await request(app.getHttpServer()) + .get('/me/notes/') + .expect('Content-Type', /json/) + .expect(200); + expect(response.body.revisions).toHaveLength(1); + }); + + afterAll(async () => { + await app.close(); + }); +}); From 64a064f9b6153c5822f5adf101eba4d6cb890b87 Mon Sep 17 00:00:00 2001 From: Yannick Bungers Date: Sat, 26 Sep 2020 23:40:15 +0200 Subject: [PATCH 2/3] Added await to wait for completion of note creation in tests Signed-off-by: Yannick Bungers --- test/public-api/users.e2e-spec.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/public-api/users.e2e-spec.ts b/test/public-api/users.e2e-spec.ts index 966ada3b2..fc43b2e67 100644 --- a/test/public-api/users.e2e-spec.ts +++ b/test/public-api/users.e2e-spec.ts @@ -42,7 +42,7 @@ describe('Notes', () => { /* TODO Note maybe not added to history by createNote, use function from HistoryService instead */ - notesService.createNote('', 'testGetHistory'); + await notesService.createNote('', 'testGetHistory'); const response = await request(app.getHttpServer()) .get('/me/history') .expect('Content-Type', /json/) @@ -61,7 +61,7 @@ describe('Notes', () => { /* TODO Note maybe not added to history by createNote, use function from HistoryService instead */ - notesService.createNote('', noteName); + await notesService.createNote('', noteName); const response = await request(app.getHttpServer()) .get('/me/history/' + noteName) .expect('Content-Type', /json/) @@ -75,7 +75,7 @@ describe('Notes', () => { /* TODO Note maybe not added to history by createNote, use function from HistoryService instead */ - notesService.createNote('This is a test note.', noteName); + await notesService.createNote('This is a test note.', noteName); const response = await request(app.getHttpServer()) .delete('/me/history/test3') .expect(204); @@ -93,7 +93,7 @@ describe('Notes', () => { it.skip(`PUT /me/history/{note}`, async () => { const noteName = 'testPutNoteHistory'; // TODO use function from HistoryService to add an History Entry - notesService.createNote('', noteName); + await notesService.createNote('', noteName); let historyEntryUpdateDto = new HistoryEntryUpdateDto(); historyEntryUpdateDto.pinStatus = true; let response = await request(app.getHttpServer()) @@ -120,7 +120,8 @@ describe('Notes', () => { it.skip(`GET /me/notes/`, async () => { - notesService.createNote('This is a test note.', 'test7'); + // TODO use function from HistoryService to add an History Entry + await notesService.createNote('This is a test note.', 'test7'); // usersService.getALLNotesOwnedByUser() TODO Implement function const response = await request(app.getHttpServer()) .get('/me/notes/') From fe66add8e09f2471d2b4dec36a5aa7dcda59ac1a Mon Sep 17 00:00:00 2001 From: Yannick Bungers Date: Sat, 26 Sep 2020 23:47:51 +0200 Subject: [PATCH 3/3] Changed let to const to make eslint happy Signed-off-by: Yannick Bungers --- test/public-api/users.e2e-spec.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/public-api/users.e2e-spec.ts b/test/public-api/users.e2e-spec.ts index fc43b2e67..da9128f87 100644 --- a/test/public-api/users.e2e-spec.ts +++ b/test/public-api/users.e2e-spec.ts @@ -2,7 +2,7 @@ import { INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; import * as request from 'supertest'; import { AppModule } from '../../src/app.module'; -import { UsersService } from '../../src/users/users.service'; +//import { UsersService } from '../../src/users/users.service'; import { UserInfoDto } from '../../src/users/user-info.dto'; import { HistoryService } from '../../src/history/history.service'; import { NotesService } from '../../src/notes/notes.service'; @@ -13,7 +13,7 @@ import { HistoryEntryDto } from '../../src/history/history-entry.dto'; describe('Notes', () => { let app: INestApplication; - let usersService: UsersService; + //let usersService: UsersService; let historyService: HistoryService; let notesService: NotesService; @@ -23,13 +23,13 @@ describe('Notes', () => { }).compile(); // TODO Create User and generateAPI Token or other Auth app = moduleRef.createNestApplication(); - usersService = moduleRef.get(UsersService); + //usersService = moduleRef.get(UsersService); await app.init(); }); it.skip(`GET /me`, async () => { // TODO Get user from beforeAll - let userInfo = new UserInfoDto(); + const userInfo = new UserInfoDto(); const response = await request(app.getHttpServer()) .post('/me') .expect('Content-Type', /json/) @@ -48,7 +48,7 @@ describe('Notes', () => { .expect('Content-Type', /json/) .expect(200); let historyEntry: HistoryEntryDto; - for (let e of response.body.content) { + for (const e of response.body.content) { if ((e).metadata.alias === 'testGetHistory') { historyEntry = e; } @@ -82,7 +82,7 @@ describe('Notes', () => { expect(response.body.content).toBeNull(); const history = historyService.getUserHistory('testuser'); let historyEntry: HistoryEntryDto = null; - for (let e of history) { + for (const e of history) { if (e.metadata.alias === noteName) { historyEntry = e; } @@ -94,23 +94,23 @@ describe('Notes', () => { const noteName = 'testPutNoteHistory'; // TODO use function from HistoryService to add an History Entry await notesService.createNote('', noteName); - let historyEntryUpdateDto = new HistoryEntryUpdateDto(); + const historyEntryUpdateDto = new HistoryEntryUpdateDto(); historyEntryUpdateDto.pinStatus = true; - let response = await request(app.getHttpServer()) + const response = await request(app.getHttpServer()) .put('/me/history/' + noteName) .send(historyEntryUpdateDto) .expect(200); // TODO parameter is not used for now const history = historyService.getUserHistory('testuser'); let historyEntry: HistoryEntryDto; - for (let e of response.body.content) { + for (const e of response.body.content) { if ((e).metadata.alias === noteName) { historyEntry = e; } } expect(historyEntry.pinStatus).toEqual(true); historyEntry = null; - for (let e of history) { + for (const e of history) { if (e.metadata.alias === noteName) { historyEntry = e; }