diff --git a/packages/nocodb/src/__tests__/unit/rest/index.test.ts b/packages/nocodb/src/__tests__/unit/rest/index.test.ts index 84efc00b31..428ec250a0 100644 --- a/packages/nocodb/src/__tests__/unit/rest/index.test.ts +++ b/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(); diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts b/packages/nocodb/src/__tests__/unit/rest/tests/helpers/project.ts new file mode 100644 index 0000000000..dd26483d83 --- /dev/null +++ b/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 }; 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 10296c2049..7b52a04a01 100644 --- a/packages/nocodb/src/__tests__/unit/rest/tests/helpers/user.ts +++ b/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 }; diff --git a/packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts b/packages/nocodb/src/__tests__/unit/rest/tests/project.test.ts new file mode 100644 index 0000000000..2e9a279102 --- /dev/null +++ b/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); +}