mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 15:13:21 +00:00
Init upload plugin e2e tests
This commit is contained in:
parent
d7649292c3
commit
bb036333a2
@ -123,10 +123,10 @@ The administration panel is available at http://localhost:4000/admin
|
||||
You can run the test suites using different databases:
|
||||
|
||||
```bash
|
||||
$ node test/e2e.js sqlite
|
||||
$ node test/e2e.js mongo
|
||||
$ node test/e2e.js postgres
|
||||
$ node test/e2e.js mysql
|
||||
$ node test/e2e.js --db=sqlite
|
||||
$ node test/e2e.js --db=mongo
|
||||
$ node test/e2e.js --db=postgres
|
||||
$ node test/e2e.js --db=mysql
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@ -115,14 +115,11 @@ module.exports = {
|
||||
},
|
||||
|
||||
async getEnvironments(ctx) {
|
||||
const environments = _.map(
|
||||
_.keys(strapi.config.environments),
|
||||
environment => {
|
||||
return {
|
||||
name: environment,
|
||||
active: strapi.config.environment === environment,
|
||||
};
|
||||
}
|
||||
const environments = Object.keys(strapi.config.environments).map(
|
||||
environment => ({
|
||||
name: environment,
|
||||
active: strapi.config.environment === environment,
|
||||
})
|
||||
);
|
||||
|
||||
ctx.send({ environments });
|
||||
|
||||
BIN
packages/strapi-plugin-upload/test/rec.jpg
Normal file
BIN
packages/strapi-plugin-upload/test/rec.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 787 B |
227
packages/strapi-plugin-upload/test/upload.test.e2e.js
Normal file
227
packages/strapi-plugin-upload/test/upload.test.e2e.js
Normal file
@ -0,0 +1,227 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
// Helpers.
|
||||
const { registerAndLogin } = require('../../../test/helpers/auth');
|
||||
// const createModelsUtils = require('../../../test/helpers/models');
|
||||
// const form = require('../../../test/helpers/generators');
|
||||
const { createAuthRequest } = require('../../../test/helpers/request');
|
||||
|
||||
// const cleanDate = entry => {
|
||||
// delete entry.updatedAt;
|
||||
// delete entry.createdAt;
|
||||
// delete entry.created_at;
|
||||
// delete entry.updated_at;
|
||||
// };
|
||||
|
||||
// let data;
|
||||
// let modelsUtils;
|
||||
let rq;
|
||||
|
||||
const defaultProviderConfig = {
|
||||
provider: 'local',
|
||||
name: 'Local server',
|
||||
enabled: true,
|
||||
sizeLimit: 1000000,
|
||||
};
|
||||
|
||||
const resetProviderConfigToDefault = () => {
|
||||
return setConfigOptions(defaultProviderConfig);
|
||||
};
|
||||
|
||||
const setConfigOptions = assign => {
|
||||
return rq.put('/upload/settings/development', {
|
||||
body: {
|
||||
...defaultProviderConfig,
|
||||
...assign,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
describe('Upload plugin end to end tests', () => {
|
||||
beforeAll(async () => {
|
||||
const token = await registerAndLogin();
|
||||
rq = createAuthRequest(token);
|
||||
|
||||
// modelsUtils = createModelsUtils({ rq });
|
||||
|
||||
// await modelsUtils.createModels([
|
||||
// form.article,
|
||||
// form.tag,
|
||||
// form.category,
|
||||
// form.reference,
|
||||
// form.product,
|
||||
// form.articlewithtag,
|
||||
// ]);
|
||||
}, 60000);
|
||||
|
||||
afterAll(() => {
|
||||
// modelsUtils.deleteModels([
|
||||
// 'article',
|
||||
// 'tag',
|
||||
// 'category',
|
||||
// 'reference',
|
||||
// 'product',
|
||||
// 'articlewithtag',
|
||||
// ]),
|
||||
}, 60000);
|
||||
|
||||
afterEach(async () => {
|
||||
await resetProviderConfigToDefault();
|
||||
});
|
||||
|
||||
describe('GET /upload/environments => List available environments', () => {
|
||||
test('Returns the list of envrionments and which one is currently active', async () => {
|
||||
const res = await rq.get('/upload/environments');
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body).toEqual({
|
||||
environments: expect.arrayContaining([
|
||||
{
|
||||
name: 'development',
|
||||
active: true,
|
||||
},
|
||||
{
|
||||
name: 'staging',
|
||||
active: false,
|
||||
},
|
||||
{
|
||||
name: 'production',
|
||||
active: false,
|
||||
},
|
||||
]),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /upload/settings/:environment => Get settings for an environment', () => {
|
||||
test('Lists the available providers', async () => {
|
||||
const res = await rq.get('/upload/settings/development');
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body).toMatchObject({
|
||||
providers: [
|
||||
{
|
||||
provider: 'local',
|
||||
name: 'Local server',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('Return the default provider config', async () => {
|
||||
const res = await rq.get('/upload/settings/development');
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body).toMatchObject({
|
||||
config: {
|
||||
provider: 'local',
|
||||
name: 'Local server',
|
||||
enabled: true,
|
||||
sizeLimit: 1000000,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('PUT /upload/settings/:environment', () => {
|
||||
test('Updates an envrionment config correctly', async () => {
|
||||
const updateRes = await rq.put('/upload/settings/development', {
|
||||
body: {
|
||||
provider: 'test',
|
||||
enabled: false,
|
||||
sizeLimit: 1000,
|
||||
},
|
||||
});
|
||||
|
||||
expect(updateRes.statusCode).toBe(200);
|
||||
expect(updateRes.body).toEqual({ ok: true });
|
||||
|
||||
const getRes = await rq.get('/upload/settings/development');
|
||||
|
||||
expect(getRes.statusCode).toBe(200);
|
||||
expect(getRes.body.config).toEqual({
|
||||
provider: 'test',
|
||||
enabled: false,
|
||||
sizeLimit: 1000,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /upload => Upload a file', () => {
|
||||
test('Simple image upload', async () => {
|
||||
const res = await rq.post('/upload', {
|
||||
formData: {
|
||||
files: fs.createReadStream(__dirname + '/rec.jpg'),
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(Array.isArray(res.body)).toBe(true);
|
||||
expect(res.body.length).toBe(1);
|
||||
expect(res.body[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: expect.anything(),
|
||||
sha256: expect.any(String),
|
||||
hash: expect.any(String),
|
||||
size: expect.any(String),
|
||||
url: expect.any(String),
|
||||
provider: 'local',
|
||||
name: 'rec.jpg',
|
||||
ext: '.jpg',
|
||||
mime: 'image/jpeg',
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
test('Rejects when provider is not enabled', async () => {
|
||||
await setConfigOptions({ enabled: false });
|
||||
|
||||
const res = await rq.post('/upload', {
|
||||
formData: {
|
||||
files: fs.createReadStream(__dirname + '/rec.jpg'),
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(res.body).toMatchObject({
|
||||
message: 'File upload is disabled',
|
||||
});
|
||||
});
|
||||
|
||||
test('Rejects when no files are provided', async () => {
|
||||
const res = await rq.post('/upload', {
|
||||
formData: {},
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(res.body).toMatchObject({
|
||||
message: 'Files are empty',
|
||||
});
|
||||
});
|
||||
|
||||
test('Rejects when any file if over the configured size limit', async () => {
|
||||
await setConfigOptions({
|
||||
sizeLimit: 0,
|
||||
});
|
||||
|
||||
const res = await rq.post('/upload', {
|
||||
formData: {
|
||||
files: fs.createReadStream(__dirname + '/rec.jpg'),
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
expect(res.body).toMatchObject({
|
||||
message: 'rec.jpg file is bigger than limit size!',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /upload/files => Find files', () => {});
|
||||
describe('GET /upload/files/count => Count available files', () => {});
|
||||
describe('GET /upload/files/:id => Find one file', () => {});
|
||||
describe('GET /upload/search/:id => Search files', () => {});
|
||||
describe('DELETE /upload/files/:id => Delete a file ', () => {});
|
||||
});
|
||||
22
test/e2e.js
22
test/e2e.js
@ -41,8 +41,8 @@ const databases = {
|
||||
},
|
||||
};
|
||||
|
||||
const test = async () => {
|
||||
return execa.shell('npm run -s test:e2e', {
|
||||
const test = async args => {
|
||||
return execa.shell(`yarn -s test:e2e ${args}`, {
|
||||
stdio: 'inherit',
|
||||
cwd: path.resolve(__dirname, '..'),
|
||||
env: {
|
||||
@ -52,7 +52,7 @@ const test = async () => {
|
||||
});
|
||||
};
|
||||
|
||||
const main = async database => {
|
||||
const main = async (database, args) => {
|
||||
try {
|
||||
await cleanTestApp(appName);
|
||||
await generateTestApp({ appName, database });
|
||||
@ -60,7 +60,7 @@ const main = async database => {
|
||||
|
||||
await waitOn({ resources: ['http://localhost:1337'] });
|
||||
|
||||
await test().catch(() => {
|
||||
await test(args).catch(() => {
|
||||
testAppProcess.kill();
|
||||
process.stdout.write('Tests failed\n', () => {
|
||||
process.exit(1);
|
||||
@ -79,14 +79,20 @@ const main = async database => {
|
||||
|
||||
yargs
|
||||
.command(
|
||||
'$0 [databaseName]',
|
||||
'$0',
|
||||
'run end to end tests',
|
||||
yargs => {
|
||||
yargs.positional('databaseName', {
|
||||
default: 'sqlite',
|
||||
yargs.option('database', {
|
||||
alias: 'db',
|
||||
describe: 'choose a database',
|
||||
choices: Object.keys(databases),
|
||||
default: 'sqlite',
|
||||
});
|
||||
},
|
||||
({ databaseName }) => main(databases[databaseName])
|
||||
argv => {
|
||||
const { database, _: args } = argv;
|
||||
|
||||
main(databases[database], args.join(' '));
|
||||
}
|
||||
)
|
||||
.help().argv;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user