Add GET /me/media

Returns all media files uploaded by the authenticated user.

Signed-off-by: Yannick Bungers <git@innay.de>
This commit is contained in:
Yannick Bungers 2021-03-14 17:47:16 +01:00 committed by David Mehren
parent 7a7b3d3a50
commit f47d85b301
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
7 changed files with 174 additions and 10 deletions

View file

@ -33,6 +33,9 @@ import { ConfigModule } from '@nestjs/config';
import mediaConfigMock from '../../src/config/media.config.mock';
import appConfigMock from '../../src/config/app.config.mock';
import { User } from '../../src/users/user.entity';
import { MediaService } from '../../src/media/media.service';
import { MediaModule } from '../../src/media/media.module';
import { promises as fs } from 'fs';
import { NoteMetadataDto } from '../../src/notes/note-metadata.dto';
// TODO Tests have to be reworked using UserService functions
@ -42,6 +45,7 @@ describe('Notes', () => {
let historyService: HistoryService;
let notesService: NotesService;
let userService: UsersService;
let mediaService: MediaService;
let user: User;
beforeAll(async () => {
@ -66,6 +70,7 @@ describe('Notes', () => {
AuthModule,
UsersModule,
HistoryModule,
MediaModule,
],
})
.overrideGuard(TokenAuthGuard)
@ -75,6 +80,7 @@ describe('Notes', () => {
notesService = moduleRef.get(NotesService);
historyService = moduleRef.get(HistoryService);
userService = moduleRef.get(UsersService);
mediaService = moduleRef.get(MediaService);
user = await userService.createUser('hardcoded', 'Testy');
await app.init();
});
@ -222,6 +228,41 @@ describe('Notes', () => {
expect(noteMetaDtos[0].updateUser.userName).toEqual(user.userName);
});
it('GET /me/media', async () => {
const note1 = await notesService.createNote(
'This is a test note.',
'test8',
await userService.getUserByUsername('hardcoded'),
);
const note2 = await notesService.createNote(
'This is a test note.',
'test9',
await userService.getUserByUsername('hardcoded'),
);
const httpServer = app.getHttpServer();
const response1 = await request(httpServer)
.get('/me/media/')
.expect('Content-Type', /json/)
.expect(200);
expect(response1.body).toHaveLength(0);
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
const url0 = await mediaService.saveFile(testImage, 'hardcoded', note1.id);
const url1 = await mediaService.saveFile(testImage, 'hardcoded', note1.id);
const url2 = await mediaService.saveFile(testImage, 'hardcoded', note2.id);
const url3 = await mediaService.saveFile(testImage, 'hardcoded', note2.id);
const response = await request(httpServer)
.get('/me/media/')
.expect('Content-Type', /json/)
.expect(200);
expect(response.body).toHaveLength(4);
expect(response.body[0].url).toEqual(url0);
expect(response.body[1].url).toEqual(url1);
expect(response.body[2].url).toEqual(url2);
expect(response.body[3].url).toEqual(url3);
});
afterAll(async () => {
await app.close();
});