From c8e9a623851c5e421857dbe59c7f87bcedb261e1 Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Sat, 27 Aug 2022 15:23:59 +0530 Subject: [PATCH] refactor/Added some unit tests for project api --- .../unit/rest/tests/helpers/project.ts | 4 +- .../__tests__/unit/rest/tests/helpers/user.ts | 16 +++-- .../__tests__/unit/rest/tests/project.test.ts | 66 ++++++++++++++++++- 3 files changed, 75 insertions(+), 11 deletions(-) diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts b/packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts index dd26483d83..4ba479903b 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts @@ -4,11 +4,11 @@ const defaultProjectValue = { title: 'Title', }; -const createProject = async (app, token) => { +const createProject = async (app, token, projectArgs = defaultProjectValue) => { const response = await request(app) .post('/api/v1/db/meta/projects/') .set('xc-auth', token) - .send(defaultProjectValue); + .send(projectArgs); const project = response.body; return project; diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/helpers/user.ts b/packages/nocodb/src/__tests__/unit/rest/tests/helpers/user.ts index 7b52a04a01..d6b3f32921 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/helpers/user.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/helpers/user.ts @@ -1,15 +1,17 @@ import request from 'supertest'; import User from '../../../../../lib/models/User'; -const createUser = async ( - app, - email = 'test@example.com', - password = 'A1234abh2@dsad' -) => { +const defaultUserArgs = { + email: 'test@example.com', + password: 'A1234abh2@dsad', +}; + +const createUser = async (app, userArgs = {}) => { + const args = { ...defaultUserArgs, ...userArgs }; const response = await request(app) .post('/api/v1/auth/user/signup') - .send({ email, password }); - const user = User.getByEmail(email); + .send(args); + const user = User.getByEmail(args.email); return { token: response.body.token, user }; }; diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts b/packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts index 2e9a279102..a5bc5dcf51 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts +++ b/packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts @@ -12,11 +12,32 @@ function projectTest() { before(async function () { app = await server(); - const response = await createUser(app); + const response = await createUser(app, { roles: 'editor' }); token = response.token; project = await createProject(app, token); }); + it('Get project info', function (done) { + request(app) + .get(`/api/v1/db/meta/projects/${project.id}/info`) + .set('xc-auth', token) + .send({}) + .expect(200, done); + }); + + // todo: Test by creating models under project and check if the UCL is working + it('UI ACL', (done) => { + request(app) + .get(`/api/v1/db/meta/projects/${project.id}/visibility-rules`) + .set('xc-auth', token) + .send({}) + .expect(200, (_, res) => { + console.log('UI ACL Respinse:', res.body); + done(); + }); + }); + // todo: Test creating visibility set + it('List projects', function (done) { request(app) .get('/api/v1/db/meta/projects/') @@ -25,12 +46,54 @@ function projectTest() { .expect(200, (err, res) => { if (err) done(err); else if (res.body.list.length !== 1) done('Should list only 1 project'); + else if (!res.body.pageInfo) done('Should have pagination info'); else { done(); } }); }); + it('Create project', function (done) { + request(app) + .post('/api/v1/db/meta/projects/') + .set('xc-auth', token) + .send({ + title: 'Title1', + }) + .expect(200, async (err, res) => { + if (err) return done(err); + + const newProject = await Project.getByTitleOrId(res.body.id); + if (!newProject) return done('Project not created'); + + done(); + }); + }); + + // todo: fix passport user role popluation bug + // it('Delete project', async (done) => { + // const toBeDeletedProject = await createProject(app, token, { + // title: 'deletedTitle', + // }); + // request(app) + // .delete('/api/v1/db/meta/projects/${toBeDeletedProject.id}') + // .set('xc-auth', token) + // .send({ + // title: 'Title1', + // }) + // .expect(200, async (err) => { + // // console.log(res); + // if (err) return done(err); + + // const deletedProject = await Project.getByTitleOrId( + // toBeDeletedProject.id + // ); + // if (deletedProject) return done('Project not delete'); + + // done(); + // }); + // }); + it('Update projects', function (done) { request(app) .patch(`/api/v1/db/meta/projects/${project.id}`) @@ -44,7 +107,6 @@ function projectTest() { return; } const newProject = await Project.getByTitleOrId(project.id); - console.log(newProject); if (newProject.title !== 'NewTitle') { done('Project not updated'); return;