fix(upload): add populate query to findOne method

This commit is contained in:
nathan-pichon 2023-01-18 17:23:14 +01:00
parent ea3f7fa37e
commit 2f1b44db8a
No known key found for this signature in database
2 changed files with 33 additions and 1 deletions

View File

@ -26,9 +26,10 @@ module.exports = {
async findOne(ctx) {
const {
params: { id },
query: { populate },
} = ctx;
const file = await getService('upload').findOne(id);
const file = await getService('upload').findOne(id, populate);
if (!file) {
return ctx.notFound('file.notFound');

View File

@ -2,6 +2,7 @@
const fs = require('fs');
const path = require('path');
const get = require('lodash/get');
// Helpers.
const { createTestBuilder } = require('../../../../../test/helpers/builder');
@ -163,6 +164,17 @@ describe('Upload plugin', () => {
])
);
});
test('Get one file', async () => {
const getRes = await rq({ method: 'GET', url: '/upload/files/1' });
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(
expect.objectContaining({
id: expect.anything(),
url: expect.any(String),
})
);
});
});
describe('Create an entity with a file', () => {
@ -224,6 +236,25 @@ describe('Upload plugin', () => {
});
data.dogs.push(res.body);
});
test('File should have related field', async () => {
const fileId = get(data, 'dogs[0].data.attributes.profilePicture.data.id');
expect(fileId).toBeDefined();
const getRes = await rq({
method: 'GET',
url: `/upload/files/${fileId}`,
qs: { populate: '*' },
});
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual(
expect.objectContaining({
id: fileId,
related: [expect.any(Object)],
})
);
});
});
describe('Create an entity with a component with a file', () => {