2021-01-05 16:12:38 -05:00
|
|
|
/*
|
2021-01-06 15:36:07 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
2021-01-05 16:12:38 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2020-08-21 15:16:20 -04:00
|
|
|
import { INestApplication } from '@nestjs/common';
|
2021-01-15 10:57:04 -05:00
|
|
|
import { ConfigModule } from '@nestjs/config';
|
2020-08-21 15:16:20 -04:00
|
|
|
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';
|
2021-01-15 10:57:04 -05:00
|
|
|
import mediaConfigMock from '../../src/config/media.config.mock';
|
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';
|
2021-01-16 13:33:09 -05:00
|
|
|
import { AuthModule } from '../../src/auth/auth.module';
|
|
|
|
import { TokenAuthGuard } from '../../src/auth/token-auth.guard';
|
|
|
|
import { MockAuthGuard } from '../../src/auth/mock-auth.guard';
|
2021-01-30 06:47:31 -05:00
|
|
|
import { UsersModule } from '../../src/users/users.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: [
|
2021-01-08 06:53:16 -05:00
|
|
|
ConfigModule.forRoot({
|
|
|
|
isGlobal: true,
|
2021-01-19 06:45:36 -05:00
|
|
|
load: [mediaConfigMock],
|
2021-01-08 06:53:16 -05:00
|
|
|
}),
|
2020-09-22 12:27:35 -04:00
|
|
|
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,
|
2021-01-16 13:33:09 -05:00
|
|
|
AuthModule,
|
2021-01-30 06:47:31 -05:00
|
|
|
UsersModule,
|
2020-09-22 12:27:35 -04:00
|
|
|
],
|
2021-01-16 13:33:09 -05:00
|
|
|
})
|
|
|
|
.overrideGuard(TokenAuthGuard)
|
|
|
|
.useClass(MockAuthGuard)
|
|
|
|
.compile();
|
2020-08-21 15:16:20 -04:00
|
|
|
|
|
|
|
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(
|
2021-01-30 06:47:31 -05:00
|
|
|
await notesService.getCurrentContent(
|
|
|
|
await notesService.getNoteByIdOrAlias(response.body.metadata.id),
|
|
|
|
),
|
2020-08-21 15:16:20 -04:00
|
|
|
).toEqual(newNote);
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`GET /notes/{note}`, async () => {
|
2021-01-09 16:38:10 -05:00
|
|
|
// check if we can succefully get a note that exists
|
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.');
|
2021-01-09 16:38:10 -05:00
|
|
|
|
|
|
|
// check if a missing note correctly returns 404
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get('/notes/i_dont_exist')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
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(
|
2021-01-30 06:47:31 -05:00
|
|
|
await notesService.getCurrentContent(
|
|
|
|
await notesService.getNoteByIdOrAlias(response.body.metadata?.id),
|
|
|
|
),
|
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');
|
2021-01-06 17:48:53 -05:00
|
|
|
await request(app.getHttpServer()).delete('/notes/test3').expect(200);
|
2021-01-09 16:38:10 -05:00
|
|
|
await 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
|
|
|
);
|
2021-01-09 16:38:10 -05:00
|
|
|
// check if a missing note correctly returns 404
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.delete('/notes/i_dont_exist')
|
|
|
|
.expect(404);
|
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');
|
2021-01-10 14:30:45 -05:00
|
|
|
const response = await request(app.getHttpServer())
|
2020-08-21 15:16:20 -04:00
|
|
|
.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);
|
2021-01-09 16:38:10 -05:00
|
|
|
await expect(
|
2021-01-30 06:47:31 -05:00
|
|
|
await notesService.getCurrentContent(
|
|
|
|
await notesService.getNoteByIdOrAlias('test4'),
|
|
|
|
),
|
2020-09-22 11:53:17 -04:00
|
|
|
).toEqual('New note text');
|
2021-01-10 14:30:45 -05:00
|
|
|
expect(response.body.content).toEqual('New note text');
|
2021-01-09 16:38:10 -05:00
|
|
|
|
|
|
|
// check if a missing note correctly returns 404
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.put('/notes/i_dont_exist')
|
|
|
|
.set('Content-Type', 'text/markdown')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
2021-01-10 14:12:21 -05:00
|
|
|
describe('GET /notes/{note}/metadata', () => {
|
|
|
|
it(`returns complete metadata object`, async () => {
|
|
|
|
await notesService.createNote('This is a test note.', 'test6');
|
|
|
|
const metadata = await request(app.getHttpServer())
|
|
|
|
.get('/notes/test6/metadata')
|
|
|
|
.expect(200);
|
|
|
|
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([]);
|
|
|
|
|
|
|
|
// check if a missing note correctly returns 404
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get('/notes/i_dont_exist/metadata')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('has the correct update/create dates', async () => {
|
|
|
|
// create a note
|
|
|
|
const note = await notesService.createNote(
|
|
|
|
'This is a test note.',
|
|
|
|
'test6a',
|
|
|
|
);
|
|
|
|
// save the creation time
|
|
|
|
const createDate = (await note.revisions)[0].createdAt;
|
|
|
|
// wait one second
|
|
|
|
await new Promise((r) => setTimeout(r, 1000));
|
|
|
|
// update the note
|
|
|
|
await notesService.updateNoteByIdOrAlias('test6a', 'More test content');
|
|
|
|
const metadata = await request(app.getHttpServer())
|
|
|
|
.get('/notes/test6a/metadata')
|
|
|
|
.expect(200);
|
|
|
|
expect(metadata.body.createTime).toEqual(createDate.toISOString());
|
|
|
|
expect(metadata.body.updateTime).not.toEqual(createDate.toISOString());
|
|
|
|
});
|
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);
|
2021-01-09 16:38:10 -05:00
|
|
|
|
|
|
|
// check if a missing note correctly returns 404
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get('/notes/i_dont_exist/revisions')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
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');
|
2021-01-10 12:20:28 -05:00
|
|
|
const revision = await notesService.getLatestRevision(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.');
|
2021-01-09 16:38:10 -05:00
|
|
|
|
|
|
|
// check if a missing note correctly returns 404
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get('/notes/i_dont_exist/revisions/1')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
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.');
|
2021-01-09 16:38:10 -05:00
|
|
|
|
|
|
|
// check if a missing note correctly returns 404
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get('/notes/i_dont_exist/content')
|
|
|
|
.expect(404);
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
});
|