Browse Source

refactor/Added some unit tests for project api

pull/3358/head
Muhammed Mustafa 2 years ago
parent
commit
0a6c889e22
  1. 4
      packages/nocodb/src/__tests__/unit/rest/index.test.ts
  2. 17
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts
  3. 10
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/user.ts
  4. 60
      packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts

4
packages/nocodb/src/__tests__/unit/rest/index.test.ts

@ -1,5 +1,6 @@
import 'mocha';
import authTests from './tests/auth.test';
import projectTests from './tests/project.test';
import NcConfigFactory from '../../../lib/utils/NcConfigFactory';
const dbName = `test_meta`;
@ -11,6 +12,7 @@ process.env[`DATABASE_URL`] = `mysql2://root:password@localhost:3306/${dbName}`;
const dbConfig = NcConfigFactory.urlToDbConfig(
NcConfigFactory.extractXcUrlFromJdbc(process.env[`DATABASE_URL`])
);
dbConfig.connection.database = 'sakila';
dbConfig.connection.database = 'test_meta';
authTests();
projectTests();

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

@ -0,0 +1,17 @@
import request from 'supertest';
const defaultProjectValue = {
title: 'Title',
};
const createProject = async (app, token) => {
const response = await request(app)
.post('/api/v1/db/meta/projects/')
.set('xc-auth', token)
.send(defaultProjectValue);
const project = response.body;
return project;
};
export { createProject };

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

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

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

@ -0,0 +1,60 @@
import 'mocha';
import request from 'supertest';
import server from '../server';
import Project from '../../../../lib/models/Project';
import { createProject } from './helpers/project';
import { createUser } from './helpers/user';
function projectTest() {
let app;
let token;
let project;
before(async function () {
app = await server();
const response = await createUser(app);
token = response.token;
project = await createProject(app, token);
});
it('List projects', function (done) {
request(app)
.get('/api/v1/db/meta/projects/')
.set('xc-auth', token)
.send({})
.expect(200, (err, res) => {
if (err) done(err);
else if (res.body.list.length !== 1) done('Should list only 1 project');
else {
done();
}
});
});
it('Update projects', function (done) {
request(app)
.patch(`/api/v1/db/meta/projects/${project.id}`)
.set('xc-auth', token)
.send({
title: 'NewTitle',
})
.expect(200, async (err) => {
if (err) {
done(err);
return;
}
const newProject = await Project.getByTitleOrId(project.id);
console.log(newProject);
if (newProject.title !== 'NewTitle') {
done('Project not updated');
return;
}
done();
});
});
}
export default function () {
describe('Project', projectTest);
}
Loading…
Cancel
Save