Browse Source

refactor/Added some unit tests for project api

pull/3358/head
Muhammed Mustafa 2 years ago
parent
commit
c8e9a62385
  1. 4
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts
  2. 16
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/user.ts
  3. 66
      packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts

4
packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts

@ -4,11 +4,11 @@ const defaultProjectValue = {
title: 'Title', title: 'Title',
}; };
const createProject = async (app, token) => { const createProject = async (app, token, projectArgs = defaultProjectValue) => {
const response = await request(app) const response = await request(app)
.post('/api/v1/db/meta/projects/') .post('/api/v1/db/meta/projects/')
.set('xc-auth', token) .set('xc-auth', token)
.send(defaultProjectValue); .send(projectArgs);
const project = response.body; const project = response.body;
return project; return project;

16
packages/nocodb/src/__tests__/unit/rest/tests/helpers/user.ts

@ -1,15 +1,17 @@
import request from 'supertest'; import request from 'supertest';
import User from '../../../../../lib/models/User'; import User from '../../../../../lib/models/User';
const createUser = async ( const defaultUserArgs = {
app, email: 'test@example.com',
email = 'test@example.com', password: 'A1234abh2@dsad',
password = 'A1234abh2@dsad' };
) => {
const createUser = async (app, userArgs = {}) => {
const args = { ...defaultUserArgs, ...userArgs };
const response = await request(app) const response = await request(app)
.post('/api/v1/auth/user/signup') .post('/api/v1/auth/user/signup')
.send({ email, password }); .send(args);
const user = User.getByEmail(email); const user = User.getByEmail(args.email);
return { token: response.body.token, user }; return { token: response.body.token, user };
}; };

66
packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts

@ -12,11 +12,32 @@ function projectTest() {
before(async function () { before(async function () {
app = await server(); app = await server();
const response = await createUser(app); const response = await createUser(app, { roles: 'editor' });
token = response.token; token = response.token;
project = await createProject(app, 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) { it('List projects', function (done) {
request(app) request(app)
.get('/api/v1/db/meta/projects/') .get('/api/v1/db/meta/projects/')
@ -25,12 +46,54 @@ function projectTest() {
.expect(200, (err, res) => { .expect(200, (err, res) => {
if (err) done(err); if (err) done(err);
else if (res.body.list.length !== 1) done('Should list only 1 project'); else if (res.body.list.length !== 1) done('Should list only 1 project');
else if (!res.body.pageInfo) done('Should have pagination info');
else { else {
done(); 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) { it('Update projects', function (done) {
request(app) request(app)
.patch(`/api/v1/db/meta/projects/${project.id}`) .patch(`/api/v1/db/meta/projects/${project.id}`)
@ -44,7 +107,6 @@ function projectTest() {
return; return;
} }
const newProject = await Project.getByTitleOrId(project.id); const newProject = await Project.getByTitleOrId(project.id);
console.log(newProject);
if (newProject.title !== 'NewTitle') { if (newProject.title !== 'NewTitle') {
done('Project not updated'); done('Project not updated');
return; return;

Loading…
Cancel
Save