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-02-17 07:20:54 -05:00
|
|
|
import { UsersService } from '../../src/users/users.service';
|
|
|
|
import { User } from '../../src/users/user.entity';
|
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;
|
2021-02-17 07:20:54 -05:00
|
|
|
let user: User;
|
|
|
|
let content: string;
|
2020-08-21 15:16:20 -04:00
|
|
|
|
|
|
|
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);
|
2021-02-17 07:20:54 -05:00
|
|
|
const userService = moduleRef.get(UsersService);
|
|
|
|
user = await userService.createUser('hardcoded', 'Testy');
|
|
|
|
content = 'This is a test note.';
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
2021-02-17 07:20:54 -05:00
|
|
|
it('POST /notes', async () => {
|
2020-08-21 15:16:20 -04:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.post('/notes')
|
2020-09-22 12:30:22 -04:00
|
|
|
.set('Content-Type', 'text/markdown')
|
2021-02-17 07:20:54 -05:00
|
|
|
.send(content)
|
2020-08-21 15:16:20 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
expect(response.body.metadata?.id).toBeDefined();
|
|
|
|
expect(
|
2021-02-19 07:36:01 -05:00
|
|
|
await notesService.getNoteContentByNote(
|
2021-01-30 06:47:31 -05:00
|
|
|
await notesService.getNoteByIdOrAlias(response.body.metadata.id),
|
|
|
|
),
|
2021-02-17 07:20:54 -05:00
|
|
|
).toEqual(content);
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
2021-02-17 07:20:54 -05:00
|
|
|
describe('GET /notes/{note}', () => {
|
|
|
|
it('works with an existing note', async () => {
|
|
|
|
// check if we can succefully get a note that exists
|
|
|
|
await notesService.createNote(content, 'test1', user);
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.get('/notes/test1')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body.content).toEqual(content);
|
|
|
|
});
|
|
|
|
it('fails with an non-existing note', async () => {
|
|
|
|
// 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
|
|
|
});
|
|
|
|
|
2021-02-17 07:20:54 -05:00
|
|
|
describe('POST /notes/{note}', () => {
|
|
|
|
it('works with a non-existing alias', async () => {
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.post('/notes/test2')
|
|
|
|
.set('Content-Type', 'text/markdown')
|
|
|
|
.send(content)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
expect(response.body.metadata?.id).toBeDefined();
|
|
|
|
return expect(
|
2021-02-19 07:36:01 -05:00
|
|
|
await notesService.getNoteContentByNote(
|
2021-02-17 07:20:54 -05:00
|
|
|
await notesService.getNoteByIdOrAlias(response.body.metadata?.id),
|
|
|
|
),
|
|
|
|
).toEqual(content);
|
|
|
|
});
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
2021-02-17 07:20:54 -05:00
|
|
|
describe('DELETE /notes/{note}', () => {
|
|
|
|
it('works with an existing alias', async () => {
|
|
|
|
await notesService.createNote(content, 'test3', user);
|
|
|
|
await request(app.getHttpServer()).delete('/notes/test3').expect(200);
|
|
|
|
await expect(notesService.getNoteByIdOrAlias('test3')).rejects.toEqual(
|
|
|
|
new NotInDBError("Note with id/alias 'test3' not found."),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('fails with a non-existing alias', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.delete('/notes/i_dont_exist')
|
|
|
|
.expect(404);
|
|
|
|
});
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
2021-02-17 07:20:54 -05:00
|
|
|
describe('PUT /notes/{note}', () => {
|
|
|
|
const changedContent = 'New note text';
|
|
|
|
it('works with existing alias', async () => {
|
|
|
|
await notesService.createNote(content, 'test4', user);
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.put('/notes/test4')
|
|
|
|
.set('Content-Type', 'text/markdown')
|
|
|
|
.send(changedContent)
|
|
|
|
.expect(200);
|
|
|
|
await expect(
|
2021-02-19 07:36:01 -05:00
|
|
|
await notesService.getNoteContentByNote(
|
2021-02-17 07:20:54 -05:00
|
|
|
await notesService.getNoteByIdOrAlias('test4'),
|
|
|
|
),
|
|
|
|
).toEqual(changedContent);
|
|
|
|
expect(response.body.content).toEqual(changedContent);
|
|
|
|
});
|
|
|
|
it('fails with a non-existing alias', async () => {
|
|
|
|
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', () => {
|
2021-02-17 07:20:54 -05:00
|
|
|
it('returns complete metadata object', async () => {
|
|
|
|
await notesService.createNote(content, 'test5', user);
|
2021-01-10 14:12:21 -05:00
|
|
|
const metadata = await request(app.getHttpServer())
|
2021-02-17 07:20:54 -05:00
|
|
|
.get('/notes/test5/metadata')
|
2021-01-10 14:12:21 -05:00
|
|
|
.expect(200);
|
|
|
|
expect(typeof metadata.body.id).toEqual('string');
|
2021-02-17 07:20:54 -05:00
|
|
|
expect(metadata.body.alias).toEqual('test5');
|
2021-01-10 14:12:21 -05:00
|
|
|
expect(metadata.body.title).toBeNull();
|
|
|
|
expect(metadata.body.description).toBeNull();
|
|
|
|
expect(typeof metadata.body.createTime).toEqual('string');
|
|
|
|
expect(metadata.body.editedBy).toEqual([]);
|
2021-02-17 07:20:54 -05:00
|
|
|
expect(metadata.body.permissions.owner.userName).toEqual('hardcoded');
|
2021-01-10 14:12:21 -05:00
|
|
|
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([]);
|
2021-02-17 07:20:54 -05:00
|
|
|
});
|
2021-01-10 14:12:21 -05:00
|
|
|
|
2021-02-17 07:20:54 -05:00
|
|
|
it('fails with non-existing alias', async () => {
|
2021-01-10 14:12:21 -05:00
|
|
|
// 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
|
2021-02-17 07:20:54 -05:00
|
|
|
const note = await notesService.createNote(content, 'test5a', user);
|
2021-01-10 14:12:21 -05:00
|
|
|
// save the creation time
|
|
|
|
const createDate = (await note.revisions)[0].createdAt;
|
|
|
|
// wait one second
|
|
|
|
await new Promise((r) => setTimeout(r, 1000));
|
|
|
|
// update the note
|
2021-02-17 07:20:54 -05:00
|
|
|
await notesService.updateNoteByIdOrAlias('test5a', 'More test content');
|
2021-01-10 14:12:21 -05:00
|
|
|
const metadata = await request(app.getHttpServer())
|
2021-02-17 07:20:54 -05:00
|
|
|
.get('/notes/test5a/metadata')
|
2021-01-10 14:12:21 -05:00
|
|
|
.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
|
|
|
});
|
|
|
|
|
2021-02-17 07:20:54 -05:00
|
|
|
describe('GET /notes/{note}/revisions', () => {
|
|
|
|
it('works with existing alias', async () => {
|
|
|
|
await notesService.createNote(content, 'test6', user);
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.get('/notes/test6/revisions')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body).toHaveLength(1);
|
|
|
|
});
|
2021-01-09 16:38:10 -05:00
|
|
|
|
2021-02-17 07:20:54 -05:00
|
|
|
it('fails with non-existing alias', async () => {
|
|
|
|
// 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
|
|
|
});
|
|
|
|
|
2021-02-17 07:20:54 -05:00
|
|
|
describe('GET /notes/{note}/revisions/{revision-id}', () => {
|
|
|
|
it('works with an existing alias', async () => {
|
|
|
|
const note = await notesService.createNote(content, 'test7', user);
|
|
|
|
const revision = await notesService.getLatestRevision(note);
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.get('/notes/test7/revisions/' + revision.id)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body.content).toEqual(content);
|
|
|
|
});
|
|
|
|
it('fails with non-existing alias', async () => {
|
|
|
|
// 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
|
|
|
});
|
|
|
|
|
2021-02-17 07:20:54 -05:00
|
|
|
describe('GET /notes/{note}/content', () => {
|
|
|
|
it('works with an existing alias', async () => {
|
|
|
|
await notesService.createNote(content, 'test8', user);
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.get('/notes/test8/content')
|
|
|
|
.expect(200);
|
|
|
|
expect(response.text).toEqual(content);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fails with non-existing alias', async () => {
|
|
|
|
// check if a missing note correctly returns 404
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get('/notes/i_dont_exist/content')
|
|
|
|
.expect('Content-Type', /text\/markdown/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
2020-08-21 15:16:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
});
|