2021-01-05 21:12:38 +00:00
|
|
|
/*
|
2021-01-06 20:36:07 +00:00
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
2021-01-05 21:12:38 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2020-08-21 19:16:20 +00:00
|
|
|
import { INestApplication } from '@nestjs/common';
|
2021-02-20 21:24:17 +00:00
|
|
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
2020-08-21 19:16:20 +00:00
|
|
|
import { Test } from '@nestjs/testing';
|
2020-09-22 16:27:35 +00:00
|
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
import { join } from 'path';
|
2021-04-29 20:47:18 +00:00
|
|
|
import request from 'supertest';
|
2021-08-29 16:45:46 +00:00
|
|
|
|
2020-09-22 16:27:35 +00:00
|
|
|
import { PublicApiModule } from '../../src/api/public/public-api.module';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { AuthModule } from '../../src/auth/auth.module';
|
|
|
|
import { MockAuthGuard } from '../../src/auth/mock-auth.guard';
|
2021-09-04 16:03:41 +00:00
|
|
|
import { TokenAuthGuard } from '../../src/auth/token.strategy';
|
2021-03-01 20:12:01 +00:00
|
|
|
import appConfigMock from '../../src/config/mock/app.config.mock';
|
2021-08-29 16:45:46 +00:00
|
|
|
import mediaConfigMock from '../../src/config/mock/media.config.mock';
|
2020-10-17 16:47:10 +00:00
|
|
|
import { NotInDBError } from '../../src/errors/errors';
|
2020-09-22 16:27:35 +00:00
|
|
|
import { GroupsModule } from '../../src/groups/groups.module';
|
2020-09-27 19:48:42 +00:00
|
|
|
import { LoggerModule } from '../../src/logger/logger.module';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { MediaService } from '../../src/media/media.service';
|
|
|
|
import { NotePermissionsUpdateDto } from '../../src/notes/note-permissions.dto';
|
2020-09-22 16:27:35 +00:00
|
|
|
import { NotesModule } from '../../src/notes/notes.module';
|
2020-08-21 19:16:20 +00:00
|
|
|
import { NotesService } from '../../src/notes/notes.service';
|
2020-09-22 16:27:35 +00:00
|
|
|
import { PermissionsModule } from '../../src/permissions/permissions.module';
|
2021-02-17 12:20:54 +00:00
|
|
|
import { User } from '../../src/users/user.entity';
|
2021-01-30 11:47:31 +00:00
|
|
|
import { UsersModule } from '../../src/users/users.module';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { UsersService } from '../../src/users/users.service';
|
2020-08-21 19:16:20 +00:00
|
|
|
|
|
|
|
describe('Notes', () => {
|
|
|
|
let app: INestApplication;
|
|
|
|
let notesService: NotesService;
|
2021-03-19 15:54:24 +00:00
|
|
|
let mediaService: MediaService;
|
2021-02-17 12:20:54 +00:00
|
|
|
let user: User;
|
2021-03-19 15:54:24 +00:00
|
|
|
let user2: User;
|
2021-02-17 12:20:54 +00:00
|
|
|
let content: string;
|
2021-02-20 21:24:17 +00:00
|
|
|
let forbiddenNoteId: string;
|
2021-03-24 10:18:26 +00:00
|
|
|
let uploadPath: string;
|
2021-03-31 23:23:12 +00:00
|
|
|
let testImage: Buffer;
|
2020-08-21 19:16:20 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const moduleRef = await Test.createTestingModule({
|
2020-09-22 16:27:35 +00:00
|
|
|
imports: [
|
2021-01-08 11:53:16 +00:00
|
|
|
ConfigModule.forRoot({
|
|
|
|
isGlobal: true,
|
2021-02-20 21:15:04 +00:00
|
|
|
load: [mediaConfigMock, appConfigMock],
|
2021-01-08 11:53:16 +00:00
|
|
|
}),
|
2020-09-22 16:27:35 +00:00
|
|
|
PublicApiModule,
|
|
|
|
NotesModule,
|
|
|
|
PermissionsModule,
|
|
|
|
GroupsModule,
|
|
|
|
TypeOrmModule.forRoot({
|
|
|
|
type: 'sqlite',
|
2020-10-24 10:30:23 +00:00
|
|
|
database: './hedgedoc-e2e-notes.sqlite',
|
2020-09-22 16:27:35 +00:00
|
|
|
autoLoadEntities: true,
|
|
|
|
synchronize: true,
|
2020-10-24 10:30:23 +00:00
|
|
|
dropSchema: true,
|
2020-09-22 16:27:35 +00:00
|
|
|
}),
|
2020-09-27 19:48:42 +00:00
|
|
|
LoggerModule,
|
2021-01-16 18:33:09 +00:00
|
|
|
AuthModule,
|
2021-01-30 11:47:31 +00:00
|
|
|
UsersModule,
|
2020-09-22 16:27:35 +00:00
|
|
|
],
|
2021-01-16 18:33:09 +00:00
|
|
|
})
|
|
|
|
.overrideGuard(TokenAuthGuard)
|
|
|
|
.useClass(MockAuthGuard)
|
|
|
|
.compile();
|
2020-08-21 19:16:20 +00:00
|
|
|
|
2021-02-20 21:24:17 +00:00
|
|
|
const config = moduleRef.get<ConfigService>(ConfigService);
|
|
|
|
forbiddenNoteId = config.get('appConfig').forbiddenNoteIds[0];
|
2021-03-24 10:18:26 +00:00
|
|
|
uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath;
|
2020-08-21 19:16:20 +00:00
|
|
|
app = moduleRef.createNestApplication();
|
|
|
|
await app.init();
|
2020-09-22 16:27:35 +00:00
|
|
|
notesService = moduleRef.get(NotesService);
|
2021-03-19 15:54:24 +00:00
|
|
|
mediaService = moduleRef.get(MediaService);
|
2021-02-17 12:20:54 +00:00
|
|
|
const userService = moduleRef.get(UsersService);
|
|
|
|
user = await userService.createUser('hardcoded', 'Testy');
|
2021-03-19 15:54:24 +00:00
|
|
|
user2 = await userService.createUser('hardcoded2', 'Max Mustermann');
|
2021-02-17 12:20:54 +00:00
|
|
|
content = 'This is a test note.';
|
2021-03-31 23:23:12 +00:00
|
|
|
testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
2020-08-21 19:16:20 +00:00
|
|
|
});
|
|
|
|
|
2021-02-17 12:20:54 +00:00
|
|
|
it('POST /notes', async () => {
|
2020-08-21 19:16:20 +00:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.post('/notes')
|
2020-09-22 16:30:22 +00:00
|
|
|
.set('Content-Type', 'text/markdown')
|
2021-02-17 12:20:54 +00:00
|
|
|
.send(content)
|
2020-08-21 19:16:20 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
expect(response.body.metadata?.id).toBeDefined();
|
|
|
|
expect(
|
2021-02-22 21:34:18 +00:00
|
|
|
await notesService.getNoteContent(
|
2021-01-30 11:47:31 +00:00
|
|
|
await notesService.getNoteByIdOrAlias(response.body.metadata.id),
|
|
|
|
),
|
2021-02-17 12:20:54 +00:00
|
|
|
).toEqual(content);
|
2020-08-21 19:16:20 +00:00
|
|
|
});
|
|
|
|
|
2021-02-17 12:20:54 +00: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);
|
|
|
|
});
|
2021-08-29 15:36:03 +00:00
|
|
|
it('fails with a forbidden note id', async () => {
|
|
|
|
// check if a forbidden note correctly returns 400
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get('/notes/forbiddenNoteId')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(400);
|
|
|
|
});
|
2020-08-21 19:16:20 +00:00
|
|
|
});
|
|
|
|
|
2021-02-17 12:20:54 +00: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-22 21:34:18 +00:00
|
|
|
await notesService.getNoteContent(
|
2021-02-17 12:20:54 +00:00
|
|
|
await notesService.getNoteByIdOrAlias(response.body.metadata?.id),
|
|
|
|
),
|
|
|
|
).toEqual(content);
|
|
|
|
});
|
2021-02-20 15:10:30 +00:00
|
|
|
|
2021-02-20 21:24:17 +00:00
|
|
|
it('fails with a forbidden alias', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.post(`/notes/${forbiddenNoteId}`)
|
|
|
|
.set('Content-Type', 'text/markdown')
|
|
|
|
.send(content)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
2021-02-20 15:10:30 +00:00
|
|
|
it('fails with a existing alias', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.post('/notes/test2')
|
|
|
|
.set('Content-Type', 'text/markdown')
|
|
|
|
.send(content)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(400);
|
|
|
|
});
|
2020-08-21 19:16:20 +00:00
|
|
|
});
|
|
|
|
|
2021-02-17 12:20:54 +00:00
|
|
|
describe('DELETE /notes/{note}', () => {
|
2021-03-31 23:23:12 +00:00
|
|
|
describe('works', () => {
|
|
|
|
it('with an existing alias and keepMedia false', async () => {
|
|
|
|
const noteId = 'test3';
|
2021-08-29 20:28:21 +00:00
|
|
|
const note = await notesService.createNote(content, noteId, user);
|
|
|
|
await mediaService.saveFile(testImage, user, note);
|
2021-03-31 23:23:12 +00:00
|
|
|
await request(app.getHttpServer())
|
|
|
|
.delete(`/notes/${noteId}`)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send({
|
|
|
|
keepMedia: false,
|
|
|
|
})
|
|
|
|
.expect(204);
|
|
|
|
await expect(notesService.getNoteByIdOrAlias(noteId)).rejects.toEqual(
|
|
|
|
new NotInDBError(`Note with id/alias '${noteId}' not found.`),
|
|
|
|
);
|
|
|
|
expect(await mediaService.listUploadsByUser(user)).toHaveLength(0);
|
|
|
|
});
|
|
|
|
it('with an existing alias and keepMedia true', async () => {
|
|
|
|
const noteId = 'test3a';
|
2021-08-29 20:28:21 +00:00
|
|
|
const note = await notesService.createNote(content, noteId, user);
|
|
|
|
const url = await mediaService.saveFile(testImage, user, note);
|
2021-03-31 23:23:12 +00:00
|
|
|
await request(app.getHttpServer())
|
|
|
|
.delete(`/notes/${noteId}`)
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send({
|
|
|
|
keepMedia: true,
|
|
|
|
})
|
|
|
|
.expect(204);
|
|
|
|
await expect(notesService.getNoteByIdOrAlias(noteId)).rejects.toEqual(
|
|
|
|
new NotInDBError(`Note with id/alias '${noteId}' not found.`),
|
|
|
|
);
|
|
|
|
expect(await mediaService.listUploadsByUser(user)).toHaveLength(1);
|
|
|
|
// Remove /upload/ from path as we just need the filename.
|
|
|
|
const fileName = url.replace('/uploads/', '');
|
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
|
|
|
});
|
2021-02-17 12:20:54 +00:00
|
|
|
});
|
2021-03-17 09:30:37 +00:00
|
|
|
it('works with an existing alias with permissions', async () => {
|
|
|
|
const note = await notesService.createNote(content, 'test3', user);
|
|
|
|
const updateNotePermission = new NotePermissionsUpdateDto();
|
|
|
|
updateNotePermission.sharedToUsers = [
|
|
|
|
{
|
2021-10-13 20:28:10 +00:00
|
|
|
username: user.username,
|
2021-03-17 09:30:37 +00:00
|
|
|
canEdit: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
updateNotePermission.sharedToGroups = [];
|
|
|
|
await notesService.updateNotePermissions(note, updateNotePermission);
|
2021-05-16 20:46:02 +00:00
|
|
|
const updatedNote = await notesService.getNoteByIdOrAlias(
|
2021-06-06 15:55:41 +00:00
|
|
|
note.aliases.filter((alias) => alias.primary)[0].name,
|
2021-05-16 20:46:02 +00:00
|
|
|
);
|
2021-03-17 09:30:37 +00:00
|
|
|
expect(updatedNote.userPermissions).toHaveLength(1);
|
|
|
|
expect(updatedNote.userPermissions[0].canEdit).toEqual(
|
|
|
|
updateNotePermission.sharedToUsers[0].canEdit,
|
|
|
|
);
|
2021-10-13 20:28:10 +00:00
|
|
|
expect(updatedNote.userPermissions[0].user.username).toEqual(
|
|
|
|
user.username,
|
2021-03-17 09:30:37 +00:00
|
|
|
);
|
|
|
|
expect(updatedNote.groupPermissions).toHaveLength(0);
|
|
|
|
await request(app.getHttpServer()).delete('/notes/test3').expect(204);
|
|
|
|
await expect(notesService.getNoteByIdOrAlias('test3')).rejects.toEqual(
|
|
|
|
new NotInDBError("Note with id/alias 'test3' not found."),
|
|
|
|
);
|
|
|
|
});
|
2021-02-20 21:24:17 +00:00
|
|
|
it('fails with a forbidden alias', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.delete(`/notes/${forbiddenNoteId}`)
|
|
|
|
.expect(400);
|
|
|
|
});
|
2021-02-17 12:20:54 +00:00
|
|
|
it('fails with a non-existing alias', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.delete('/notes/i_dont_exist')
|
|
|
|
.expect(404);
|
|
|
|
});
|
2020-08-21 19:16:20 +00:00
|
|
|
});
|
|
|
|
|
2021-02-17 12:20:54 +00: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);
|
2021-02-24 20:14:16 +00:00
|
|
|
expect(
|
2021-02-22 21:34:18 +00:00
|
|
|
await notesService.getNoteContent(
|
2021-02-17 12:20:54 +00:00
|
|
|
await notesService.getNoteByIdOrAlias('test4'),
|
|
|
|
),
|
|
|
|
).toEqual(changedContent);
|
|
|
|
expect(response.body.content).toEqual(changedContent);
|
|
|
|
});
|
2021-02-20 21:24:17 +00:00
|
|
|
it('fails with a forbidden alias', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.put(`/notes/${forbiddenNoteId}`)
|
|
|
|
.set('Content-Type', 'text/markdown')
|
|
|
|
.send(changedContent)
|
|
|
|
.expect(400);
|
|
|
|
});
|
2021-02-17 12:20:54 +00:00
|
|
|
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 19:16:20 +00:00
|
|
|
});
|
|
|
|
|
2021-01-10 19:12:21 +00:00
|
|
|
describe('GET /notes/{note}/metadata', () => {
|
2021-02-17 12:20:54 +00:00
|
|
|
it('returns complete metadata object', async () => {
|
|
|
|
await notesService.createNote(content, 'test5', user);
|
2021-01-10 19:12:21 +00:00
|
|
|
const metadata = await request(app.getHttpServer())
|
2021-02-17 12:20:54 +00:00
|
|
|
.get('/notes/test5/metadata')
|
2021-01-10 19:12:21 +00:00
|
|
|
.expect(200);
|
|
|
|
expect(typeof metadata.body.id).toEqual('string');
|
2021-06-06 15:55:41 +00:00
|
|
|
expect(metadata.body.aliases).toEqual(['test5']);
|
|
|
|
expect(metadata.body.primaryAlias).toEqual('test5');
|
2021-04-24 19:33:47 +00:00
|
|
|
expect(metadata.body.title).toEqual('');
|
|
|
|
expect(metadata.body.description).toEqual('');
|
2021-01-10 19:12:21 +00:00
|
|
|
expect(typeof metadata.body.createTime).toEqual('string');
|
|
|
|
expect(metadata.body.editedBy).toEqual([]);
|
2021-10-13 20:28:10 +00:00
|
|
|
expect(metadata.body.permissions.owner.username).toEqual('hardcoded');
|
2021-01-10 19:12:21 +00: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');
|
2021-10-13 20:28:10 +00:00
|
|
|
expect(typeof metadata.body.updateUser.username).toEqual('string');
|
2021-01-10 19:12:21 +00:00
|
|
|
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 12:20:54 +00:00
|
|
|
});
|
2021-01-10 19:12:21 +00:00
|
|
|
|
2021-02-20 21:24:17 +00:00
|
|
|
it('fails with a forbidden alias', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get(`/notes/${forbiddenNoteId}/metadata`)
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
2021-02-17 12:20:54 +00:00
|
|
|
it('fails with non-existing alias', async () => {
|
2021-01-10 19:12:21 +00: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 12:20:54 +00:00
|
|
|
const note = await notesService.createNote(content, 'test5a', user);
|
2021-01-10 19:12:21 +00: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-20 15:50:11 +00:00
|
|
|
await notesService.updateNote(note, 'More test content');
|
2021-01-10 19:12:21 +00:00
|
|
|
const metadata = await request(app.getHttpServer())
|
2021-02-17 12:20:54 +00:00
|
|
|
.get('/notes/test5a/metadata')
|
2021-01-10 19:12:21 +00:00
|
|
|
.expect(200);
|
|
|
|
expect(metadata.body.createTime).toEqual(createDate.toISOString());
|
|
|
|
expect(metadata.body.updateTime).not.toEqual(createDate.toISOString());
|
|
|
|
});
|
2020-08-21 19:16:20 +00:00
|
|
|
});
|
|
|
|
|
2021-02-17 12:20:54 +00: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 21:38:10 +00:00
|
|
|
|
2021-02-20 21:24:17 +00:00
|
|
|
it('fails with a forbidden alias', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get(`/notes/${forbiddenNoteId}/revisions`)
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
2021-02-17 12:20:54 +00: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 19:16:20 +00:00
|
|
|
});
|
|
|
|
|
2021-02-17 12:20:54 +00: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())
|
2021-02-24 20:14:16 +00:00
|
|
|
.get(`/notes/test7/revisions/${revision.id}`)
|
2021-02-17 12:20:54 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body.content).toEqual(content);
|
|
|
|
});
|
2021-02-20 21:24:17 +00:00
|
|
|
it('fails with a forbidden alias', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get(`/notes/${forbiddenNoteId}/revisions/1`)
|
|
|
|
.expect(400);
|
|
|
|
});
|
2021-02-17 12:20:54 +00: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/1')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
2020-08-21 19:16:20 +00:00
|
|
|
});
|
|
|
|
|
2021-02-17 12:20:54 +00: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);
|
|
|
|
});
|
2021-02-20 21:24:17 +00:00
|
|
|
it('fails with a forbidden alias', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get(`/notes/${forbiddenNoteId}/content`)
|
|
|
|
.expect(400);
|
|
|
|
});
|
2021-02-17 12:20:54 +00: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/content')
|
|
|
|
.expect('Content-Type', /text\/markdown/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
2020-08-21 19:16:20 +00:00
|
|
|
});
|
|
|
|
|
2021-03-19 15:54:24 +00:00
|
|
|
describe('GET /notes/{note}/media', () => {
|
|
|
|
it('works', async () => {
|
2021-05-16 20:46:02 +00:00
|
|
|
const alias = 'test9';
|
|
|
|
const extraAlias = 'test10';
|
2021-08-29 20:28:21 +00:00
|
|
|
const note1 = await notesService.createNote(content, alias, user);
|
|
|
|
const note2 = await notesService.createNote(content, extraAlias, user);
|
2021-03-19 15:54:24 +00:00
|
|
|
const httpServer = app.getHttpServer();
|
|
|
|
const response = await request(httpServer)
|
2021-05-16 20:46:02 +00:00
|
|
|
.get(`/notes/${alias}/media/`)
|
2021-03-19 15:54:24 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body).toHaveLength(0);
|
|
|
|
|
|
|
|
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
2021-08-29 20:28:21 +00:00
|
|
|
const url0 = await mediaService.saveFile(testImage, user, note1);
|
|
|
|
const url1 = await mediaService.saveFile(testImage, user, note2);
|
2021-03-19 15:54:24 +00:00
|
|
|
|
|
|
|
const responseAfter = await request(httpServer)
|
2021-05-16 20:46:02 +00:00
|
|
|
.get(`/notes/${alias}/media/`)
|
2021-03-19 15:54:24 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(responseAfter.body).toHaveLength(1);
|
|
|
|
expect(responseAfter.body[0].url).toEqual(url0);
|
|
|
|
expect(responseAfter.body[0].url).not.toEqual(url1);
|
2021-03-24 10:18:26 +00:00
|
|
|
for (const fileUrl of [url0, url1]) {
|
|
|
|
const fileName = fileUrl.replace('/uploads/', '');
|
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
|
|
|
}
|
2021-03-31 20:42:56 +00:00
|
|
|
await fs.rmdir(uploadPath, { recursive: true });
|
2021-03-19 15:54:24 +00:00
|
|
|
});
|
|
|
|
it('fails, when note does not exist', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get(`/notes/i_dont_exist/media/`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
it("fails, when user can't read note", async () => {
|
2021-05-16 20:46:02 +00:00
|
|
|
const alias = 'test11';
|
|
|
|
await notesService.createNote('This is a test note.', alias, user2);
|
2021-03-19 15:54:24 +00:00
|
|
|
await request(app.getHttpServer())
|
2021-05-16 20:46:02 +00:00
|
|
|
.get(`/notes/${alias}/media/`)
|
2021-03-19 15:54:24 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(401);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-21 19:16:20 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
});
|