From bb036333a2070aa44200411eed0f78498b71bce5 Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Mon, 5 Aug 2019 09:10:00 +0200 Subject: [PATCH] Init upload plugin e2e tests --- CONTRIBUTING.md | 8 +- .../controllers/Upload.js | 13 +- packages/strapi-plugin-upload/test/rec.jpg | Bin 0 -> 787 bytes .../test/upload.test.e2e.js | 227 ++++++++++++++++++ test/e2e.js | 22 +- 5 files changed, 250 insertions(+), 20 deletions(-) create mode 100644 packages/strapi-plugin-upload/test/rec.jpg create mode 100644 packages/strapi-plugin-upload/test/upload.test.e2e.js diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fcb5960cf3..ca7f46b27d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 ``` --- diff --git a/packages/strapi-plugin-upload/controllers/Upload.js b/packages/strapi-plugin-upload/controllers/Upload.js index 19e7144084..a3040d9a14 100644 --- a/packages/strapi-plugin-upload/controllers/Upload.js +++ b/packages/strapi-plugin-upload/controllers/Upload.js @@ -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 }); diff --git a/packages/strapi-plugin-upload/test/rec.jpg b/packages/strapi-plugin-upload/test/rec.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e402224d6d04ed5bee05c9af55d754c490037d65 GIT binary patch literal 787 zcmex=LK$;OGwtxvPE3$wY!3HV(|CYfbAS1sdzc?emK*3ngfWgAa)0YKg8W4cl zs$izT71^Gf{S2E}UN&&fc=N-l?*9P>K@J8H1`%dPB?cxzMrJ|A|3?_)fp)Sof&o|? zkYHqDW?^Mx=iubx1}fMpz`(@F%*@2X%*qO~hOrhX&%h$cDx_%W$R-?^$gWfnAuRebI{N?Mn?>~P20{IIVo)B*V zNr=zT{3QtV7ZVE$GYdP&UyMxUAdd^Ouqqm|2{{I`Cl(4T88vc8f2KE_o9%~}YXK;@p{B@hbnL!2&3}j>sTnr2hTw+{+Af_Y+1WI~J zz@P^TR)E0~5V!*e|AB@uDygVgm{=s3m?S)#(=g{5$>9H8V6tTTe~W>KnGu+nm<1W^ S8J@jZ)Wx6y#Q!fKm^T3@zAuIV literal 0 HcmV?d00001 diff --git a/packages/strapi-plugin-upload/test/upload.test.e2e.js b/packages/strapi-plugin-upload/test/upload.test.e2e.js new file mode 100644 index 0000000000..579c180996 --- /dev/null +++ b/packages/strapi-plugin-upload/test/upload.test.e2e.js @@ -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 ', () => {}); +}); diff --git a/test/e2e.js b/test/e2e.js index 006a9fb379..6c53e186d7 100644 --- a/test/e2e.js +++ b/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;