2021-01-05 16:12:38 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2020-08-21 15:16:20 -04:00
|
|
|
import { INestApplication } from '@nestjs/common';
|
|
|
|
import { Test } from '@nestjs/testing';
|
2020-09-22 12:27:35 -04:00
|
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
2020-08-21 15:16:20 -04:00
|
|
|
import * as request from 'supertest';
|
2020-09-22 12:27:35 -04:00
|
|
|
import { PublicApiModule } from '../../src/api/public/public-api.module';
|
2020-10-17 12:47:10 -04:00
|
|
|
import { NotInDBError } from '../../src/errors/errors';
|
2020-09-22 12:27:35 -04:00
|
|
|
import { GroupsModule } from '../../src/groups/groups.module';
|
2020-09-27 15:48:42 -04:00
|
|
|
import { LoggerModule } from '../../src/logger/logger.module';
|
2020-09-22 12:27:35 -04:00
|
|
|
import { NotesModule } from '../../src/notes/notes.module';
|
2020-08-21 15:16:20 -04:00
|
|
|
import { NotesService } from '../../src/notes/notes.service';
|
2020-09-22 12:27:35 -04:00
|
|
|
import { PermissionsModule } from '../../src/permissions/permissions.module';
|
2020-08-21 15:16:20 -04:00
|
|
|
|
|
|
|
describe('Notes', () => {
|
|
|
|
let app: INestApplication;
|
|
|
|
let notesService: NotesService;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const moduleRef = await Test.createTestingModule({
|
2020-09-22 12:27:35 -04:00
|
|
|
imports: [
|
|
|
|
PublicApiModule,
|
|
|
|
NotesModule,
|
|
|
|
PermissionsModule,
|
|
|
|
GroupsModule,
|
|
|
|
TypeOrmModule.forRoot({
|
|
|
|
type: 'sqlite',
|
2020-10-24 06:30:23 -04:00
|
|
|
database: './hedgedoc-e2e-notes.sqlite',
|
2020-09-22 12:27:35 -04:00
|
|
|
autoLoadEntities: true,
|
|
|
|
synchronize: true,
|
2020-10-24 06:30:23 -04:00
|
|
|
dropSchema: true,
|
2020-09-22 12:27:35 -04:00
|
|
|
}),
|
2020-09-27 15:48:42 -04:00
|
|
|
LoggerModule,
|
2020-09-22 12:27:35 -04:00
|
|
|
],
|
2020-08-21 15:16:20 -04:00
|
|
|
}).compile();
|
|
|
|
|
|
|
|
app = moduleRef.createNestApplication();
|
|
|
|
await app.init();
|
2020-09-22 12:27:35 -04:00
|
|
|
notesService = moduleRef.get(NotesService);
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`POST /notes`, async () => {
|
|
|
|
const newNote = 'This is a test note.';
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.post('/notes')
|
2020-09-22 12:30:22 -04:00
|
|
|
.set('Content-Type', 'text/markdown')
|
2020-08-21 15:16:20 -04:00
|
|
|
.send(newNote)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
expect(response.body.metadata?.id).toBeDefined();
|
|
|
|
expect(
|
2020-09-22 11:53:17 -04:00
|
|
|
(await notesService.getNoteDtoByIdOrAlias(response.body.metadata.id))
|
|
|
|
.content,
|
2020-08-21 15:16:20 -04:00
|
|
|
).toEqual(newNote);
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`GET /notes/{note}`, async () => {
|
2020-09-22 15:10:09 -04:00
|
|
|
await notesService.createNote('This is a test note.', 'test1');
|
2020-08-21 15:16:20 -04:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.get('/notes/test1')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body.content).toEqual('This is a test note.');
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`POST /notes/{note}`, async () => {
|
|
|
|
const newNote = 'This is a test note.';
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.post('/notes/test2')
|
2020-09-22 12:30:22 -04:00
|
|
|
.set('Content-Type', 'text/markdown')
|
2020-08-21 15:16:20 -04:00
|
|
|
.send(newNote)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
expect(response.body.metadata?.id).toBeDefined();
|
|
|
|
return expect(
|
2020-09-22 11:53:17 -04:00
|
|
|
(await notesService.getNoteDtoByIdOrAlias(response.body.metadata.id))
|
|
|
|
.content,
|
2020-08-21 15:16:20 -04:00
|
|
|
).toEqual(newNote);
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`DELETE /notes/{note}`, async () => {
|
2020-09-22 15:10:09 -04:00
|
|
|
await notesService.createNote('This is a test note.', 'test3');
|
2020-08-21 15:16:20 -04:00
|
|
|
await request(app.getHttpServer())
|
|
|
|
.delete('/notes/test3')
|
|
|
|
.expect(200);
|
2020-09-22 15:10:09 -04:00
|
|
|
return expect(notesService.getNoteByIdOrAlias('test3')).rejects.toEqual(
|
2020-10-17 12:47:10 -04:00
|
|
|
new NotInDBError("Note with id/alias 'test3' not found."),
|
2020-09-22 15:10:09 -04:00
|
|
|
);
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`PUT /notes/{note}`, async () => {
|
2020-09-22 15:10:09 -04:00
|
|
|
await notesService.createNote('This is a test note.', 'test4');
|
2020-08-21 15:16:20 -04:00
|
|
|
await request(app.getHttpServer())
|
|
|
|
.put('/notes/test4')
|
2020-09-22 12:30:22 -04:00
|
|
|
.set('Content-Type', 'text/markdown')
|
2020-08-21 15:16:20 -04:00
|
|
|
.send('New note text')
|
|
|
|
.expect(200);
|
2020-09-22 11:53:17 -04:00
|
|
|
return expect(
|
|
|
|
(await notesService.getNoteDtoByIdOrAlias('test4')).content,
|
|
|
|
).toEqual('New note text');
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
2020-10-03 10:45:04 -04:00
|
|
|
it(`GET /notes/{note}/metadata`, async () => {
|
|
|
|
await notesService.createNote('This is a test note.', 'test6');
|
|
|
|
const metadata = await request(app.getHttpServer())
|
2020-08-21 15:16:20 -04:00
|
|
|
.get('/notes/test6/metadata')
|
|
|
|
.expect(200);
|
2020-10-03 10:45:04 -04:00
|
|
|
expect(typeof metadata.body.id).toEqual('string');
|
|
|
|
expect(metadata.body.alias).toEqual('test6');
|
|
|
|
expect(metadata.body.title).toBeNull();
|
|
|
|
expect(metadata.body.description).toBeNull();
|
|
|
|
expect(typeof metadata.body.createTime).toEqual('string');
|
|
|
|
expect(metadata.body.editedBy).toEqual([]);
|
|
|
|
expect(metadata.body.permissions.owner).toBeNull();
|
|
|
|
expect(metadata.body.permissions.sharedToUsers).toEqual([]);
|
|
|
|
expect(metadata.body.permissions.sharedToUsers).toEqual([]);
|
|
|
|
expect(metadata.body.tags).toEqual([]);
|
|
|
|
expect(typeof metadata.body.updateTime).toEqual('string');
|
|
|
|
expect(typeof metadata.body.updateUser.displayName).toEqual('string');
|
|
|
|
expect(typeof metadata.body.updateUser.userName).toEqual('string');
|
|
|
|
expect(typeof metadata.body.updateUser.email).toEqual('string');
|
|
|
|
expect(typeof metadata.body.updateUser.photo).toEqual('string');
|
|
|
|
expect(typeof metadata.body.viewCount).toEqual('number');
|
|
|
|
expect(metadata.body.editedBy).toEqual([]);
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`GET /notes/{note}/revisions`, async () => {
|
2020-09-22 15:10:09 -04:00
|
|
|
await notesService.createNote('This is a test note.', 'test7');
|
2020-08-21 15:16:20 -04:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.get('/notes/test7/revisions')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
2020-09-22 15:11:56 -04:00
|
|
|
expect(response.body).toHaveLength(1);
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`GET /notes/{note}/revisions/{revision-id}`, async () => {
|
2020-09-22 15:45:56 -04:00
|
|
|
const note = await notesService.createNote('This is a test note.', 'test8');
|
|
|
|
const revision = await notesService.getLastRevision(note);
|
2020-08-21 15:16:20 -04:00
|
|
|
const response = await request(app.getHttpServer())
|
2020-09-22 15:45:56 -04:00
|
|
|
.get('/notes/test8/revisions/' + revision.id)
|
2020-08-21 15:16:20 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body.content).toEqual('This is a test note.');
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`GET /notes/{note}/content`, async () => {
|
2020-09-22 15:10:09 -04:00
|
|
|
await notesService.createNote('This is a test note.', 'test9');
|
2020-08-21 15:16:20 -04:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.get('/notes/test9/content')
|
|
|
|
.expect(200);
|
2020-09-22 15:46:32 -04:00
|
|
|
expect(response.text).toEqual('This is a test note.');
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
});
|