2021-03-23 17:41:57 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
|
|
import { Test } from '@nestjs/testing';
|
|
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
|
|
import { promises as fs } from 'fs';
|
2021-08-29 12:45:46 -04:00
|
|
|
import { join } from 'path';
|
2021-04-29 16:47:18 -04:00
|
|
|
import request from 'supertest';
|
2021-08-29 12:45:46 -04:00
|
|
|
|
|
|
|
import { PrivateApiModule } from '../../src/api/private/private-api.module';
|
|
|
|
import { AuthModule } from '../../src/auth/auth.module';
|
2021-10-13 14:43:56 -04:00
|
|
|
import { AuthConfig } from '../../src/config/auth.config';
|
2021-03-23 17:41:57 -04:00
|
|
|
import appConfigMock from '../../src/config/mock/app.config.mock';
|
|
|
|
import authConfigMock from '../../src/config/mock/auth.config.mock';
|
|
|
|
import customizationConfigMock from '../../src/config/mock/customization.config.mock';
|
|
|
|
import externalConfigMock from '../../src/config/mock/external-services.config.mock';
|
2021-08-29 12:45:46 -04:00
|
|
|
import mediaConfigMock from '../../src/config/mock/media.config.mock';
|
2021-03-23 17:41:57 -04:00
|
|
|
import { GroupsModule } from '../../src/groups/groups.module';
|
2021-10-13 14:43:56 -04:00
|
|
|
import { IdentityService } from '../../src/identity/identity.service';
|
2021-08-29 12:45:46 -04:00
|
|
|
import { ConsoleLoggerService } from '../../src/logger/console-logger.service';
|
2021-03-23 17:41:57 -04:00
|
|
|
import { LoggerModule } from '../../src/logger/logger.module';
|
|
|
|
import { MediaModule } from '../../src/media/media.module';
|
|
|
|
import { NotesModule } from '../../src/notes/notes.module';
|
|
|
|
import { NotesService } from '../../src/notes/notes.service';
|
|
|
|
import { PermissionsModule } from '../../src/permissions/permissions.module';
|
|
|
|
import { UsersService } from '../../src/users/users.service';
|
2021-10-13 14:43:56 -04:00
|
|
|
import { setupSessionMiddleware } from '../../src/utils/session';
|
2021-04-22 15:29:23 -04:00
|
|
|
import { ensureDeleted } from '../utils';
|
2021-03-23 17:41:57 -04:00
|
|
|
|
|
|
|
describe('Media', () => {
|
2021-10-13 14:43:56 -04:00
|
|
|
let identityService: IdentityService;
|
2021-03-23 17:41:57 -04:00
|
|
|
let app: NestExpressApplication;
|
|
|
|
let uploadPath: string;
|
2021-10-13 14:43:56 -04:00
|
|
|
let agent: request.SuperAgentTest;
|
2021-03-23 17:41:57 -04:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const moduleRef = await Test.createTestingModule({
|
|
|
|
imports: [
|
|
|
|
ConfigModule.forRoot({
|
|
|
|
isGlobal: true,
|
|
|
|
load: [
|
|
|
|
mediaConfigMock,
|
|
|
|
appConfigMock,
|
|
|
|
authConfigMock,
|
|
|
|
customizationConfigMock,
|
|
|
|
externalConfigMock,
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
PrivateApiModule,
|
|
|
|
MediaModule,
|
|
|
|
TypeOrmModule.forRoot({
|
|
|
|
type: 'sqlite',
|
|
|
|
database: './hedgedoc-e2e-private-media.sqlite',
|
|
|
|
autoLoadEntities: true,
|
|
|
|
dropSchema: true,
|
|
|
|
synchronize: true,
|
|
|
|
}),
|
|
|
|
NotesModule,
|
|
|
|
PermissionsModule,
|
|
|
|
GroupsModule,
|
|
|
|
LoggerModule,
|
|
|
|
AuthModule,
|
|
|
|
],
|
|
|
|
}).compile();
|
|
|
|
const config = moduleRef.get<ConfigService>(ConfigService);
|
|
|
|
uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath;
|
|
|
|
app = moduleRef.createNestApplication<NestExpressApplication>();
|
|
|
|
app.useStaticAssets(uploadPath, {
|
|
|
|
prefix: '/uploads',
|
|
|
|
});
|
2021-10-13 14:43:56 -04:00
|
|
|
const authConfig = config.get('authConfig') as AuthConfig;
|
|
|
|
setupSessionMiddleware(app, authConfig);
|
2021-03-23 17:41:57 -04:00
|
|
|
await app.init();
|
2021-04-02 13:08:30 -04:00
|
|
|
const logger = await app.resolve(ConsoleLoggerService);
|
2021-03-23 17:41:57 -04:00
|
|
|
logger.log('Switching logger', 'AppBootstrap');
|
|
|
|
app.useLogger(logger);
|
2021-10-13 14:43:56 -04:00
|
|
|
identityService = moduleRef.get(IdentityService);
|
2021-07-15 14:30:51 -04:00
|
|
|
const notesService: NotesService = moduleRef.get(NotesService);
|
2021-03-23 17:41:57 -04:00
|
|
|
await notesService.createNote('test content', 'test_upload_media');
|
2021-07-15 14:30:51 -04:00
|
|
|
const userService: UsersService = moduleRef.get(UsersService);
|
2021-10-13 14:43:56 -04:00
|
|
|
const user = await userService.createUser('hardcoded', 'Testy');
|
|
|
|
await identityService.createLocalIdentity(user, 'test');
|
|
|
|
agent = request.agent(app.getHttpServer());
|
|
|
|
await agent
|
|
|
|
.post('/auth/local/login')
|
|
|
|
.send({ username: 'hardcoded', password: 'test' })
|
|
|
|
.expect(201);
|
2021-03-23 17:41:57 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /media', () => {
|
|
|
|
it('works', async () => {
|
2021-10-13 14:43:56 -04:00
|
|
|
const uploadResponse = await agent
|
2021-03-23 17:41:57 -04:00
|
|
|
.post('/media')
|
|
|
|
.attach('file', 'test/private-api/fixtures/test.png')
|
|
|
|
.set('HedgeDoc-Note', 'test_upload_media')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(201);
|
|
|
|
const path: string = uploadResponse.body.link;
|
|
|
|
const testImage = await fs.readFile('test/private-api/fixtures/test.png');
|
2021-10-13 14:43:56 -04:00
|
|
|
const downloadResponse = await agent.get(path);
|
2021-03-23 17:41:57 -04:00
|
|
|
expect(downloadResponse.body).toEqual(testImage);
|
2021-03-31 16:43:13 -04:00
|
|
|
// Remove /uploads/ from path as we just need the filename.
|
2021-03-23 17:41:57 -04:00
|
|
|
const fileName = path.replace('/uploads/', '');
|
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
|
|
|
});
|
|
|
|
describe('fails:', () => {
|
2021-03-31 16:41:38 -04:00
|
|
|
beforeEach(async () => {
|
2021-04-22 15:29:23 -04:00
|
|
|
await ensureDeleted(uploadPath);
|
2021-03-31 16:41:38 -04:00
|
|
|
});
|
2021-03-23 17:41:57 -04:00
|
|
|
it('MIME type not supported', async () => {
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-03-23 17:41:57 -04:00
|
|
|
.post('/media')
|
|
|
|
.attach('file', 'test/private-api/fixtures/test.zip')
|
|
|
|
.set('HedgeDoc-Note', 'test_upload_media')
|
|
|
|
.expect(400);
|
2021-03-31 16:41:38 -04:00
|
|
|
await expect(fs.access(uploadPath)).rejects.toBeDefined();
|
2021-03-23 17:41:57 -04:00
|
|
|
});
|
|
|
|
it('note does not exist', async () => {
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-03-23 17:41:57 -04:00
|
|
|
.post('/media')
|
|
|
|
.attach('file', 'test/private-api/fixtures/test.zip')
|
|
|
|
.set('HedgeDoc-Note', 'i_dont_exist')
|
|
|
|
.expect(400);
|
2021-03-31 16:41:38 -04:00
|
|
|
await expect(fs.access(uploadPath)).rejects.toBeDefined();
|
2021-03-23 17:41:57 -04:00
|
|
|
});
|
|
|
|
it('mediaBackend error', async () => {
|
|
|
|
await fs.mkdir(uploadPath, {
|
|
|
|
mode: '444',
|
|
|
|
});
|
2021-10-13 14:43:56 -04:00
|
|
|
await agent
|
2021-03-23 17:41:57 -04:00
|
|
|
.post('/media')
|
|
|
|
.attach('file', 'test/private-api/fixtures/test.png')
|
|
|
|
.set('HedgeDoc-Note', 'test_upload_media')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(500);
|
2021-03-31 16:41:38 -04:00
|
|
|
});
|
|
|
|
afterEach(async () => {
|
2021-04-22 15:29:23 -04:00
|
|
|
await ensureDeleted(uploadPath);
|
2021-03-23 17:41:57 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
// Delete the upload folder
|
2021-04-22 15:29:23 -04:00
|
|
|
await ensureDeleted(uploadPath);
|
2021-03-31 16:36:14 -04:00
|
|
|
await app.close();
|
2021-03-23 17:41:57 -04:00
|
|
|
});
|
|
|
|
});
|