Browse Source

refactor/Added some unit tests for table api and integrated new test scheme with auth tests

pull/3358/head
Muhammed Mustafa 2 years ago
parent
commit
ba2dd1900b
  1. 37
      packages/nocodb/src/__tests__/unit/rest/tests/auth.test.ts
  2. 2
      packages/nocodb/src/__tests__/unit/rest/tests/helpers/user.ts
  3. 162
      packages/nocodb/src/__tests__/unit/rest/tests/table.test.ts

37
packages/nocodb/src/__tests__/unit/rest/tests/auth.test.ts

@ -2,22 +2,22 @@ import { expect } from 'chai';
import 'mocha';
import request from 'supertest';
import server from '../server';
const EMAIL_ID = 'abc@g.com';
const VALID_PASSWORD = 'Abc@1234';
import { createUser, defaultUserArgs } from './helpers/user';
function authTests() {
let app;
let token;
before(async function () {
beforeEach(async function () {
app = await server();
const response = await createUser(app);
token = response.token;
});
it('Signup with valid email', function (done) {
request(app)
.post('/api/v1/auth/user/signup')
.send({ email: EMAIL_ID, password: VALID_PASSWORD })
.send({ email: 'new@example.com', password: defaultUserArgs.password })
.expect(200, (err, res) => {
if (err) {
expect(res.status).to.equal(400);
@ -32,21 +32,24 @@ function authTests() {
it('Signup with invalid email', (done) => {
request(app)
.post('/api/v1/auth/user/signup')
.send({ email: 'test', password: VALID_PASSWORD })
.send({ email: 'test', password: defaultUserArgs.password })
.expect(400, done);
});
it('Signup with invalid passsword', (done) => {
request(app)
.post('/api/v1/auth/user/signup')
.send({ email: EMAIL_ID, password: 'weakpass' })
.send({ email: defaultUserArgs.email, password: 'weakpass' })
.expect(400, done);
});
it('Signin with valid credentials', function (done) {
request(app)
.post('/api/v1/auth/user/signin')
.send({ email: EMAIL_ID, password: VALID_PASSWORD })
.send({
email: defaultUserArgs.email,
password: defaultUserArgs.password,
})
.expect(200, async function (err, res) {
if (err) {
console.log(res.error);
@ -70,14 +73,14 @@ function authTests() {
it('Signin with invalid credentials', function (done) {
request(app)
.post('/api/v1/auth/user/signin')
.send({ email: 'abc@abc.com', password: VALID_PASSWORD })
.send({ email: 'abc@abc.com', password: defaultUserArgs.password })
.expect(400, done);
});
it('Signin with invalid password', function (done) {
request(app)
.post('/api/v1/auth/user/signin')
.send({ email: EMAIL_ID, password: 'wrongPassword' })
.send({ email: defaultUserArgs.email, password: 'wrongPassword' })
.expect(400, done);
});
@ -110,7 +113,7 @@ function authTests() {
return done(err);
}
const email = res.body.email;
expect(email).to.equal(EMAIL_ID);
expect(email).to.equal(defaultUserArgs.email);
done();
});
});
@ -130,8 +133,8 @@ function authTests() {
.post('/api/v1/auth/password/change')
.set('xc-auth', token)
.send({
currentPassword: VALID_PASSWORD,
newPassword: 'NEW' + VALID_PASSWORD,
currentPassword: defaultUserArgs.password,
newPassword: 'NEW' + defaultUserArgs.password,
})
.expect(200, done);
});
@ -141,8 +144,8 @@ function authTests() {
.post('/api/v1/auth/password/change')
.unset('xc-auth')
.send({
currentPassword: VALID_PASSWORD,
newPassword: 'NEW' + VALID_PASSWORD,
currentPassword: defaultUserArgs.password,
newPassword: 'NEW' + defaultUserArgs.password,
})
.expect(500, function (_err, _res) {
done();
@ -153,14 +156,14 @@ function authTests() {
it('Reset Password with an invalid token', function (done) {
request(app)
.post('/api/v1/auth/password/reset/someRandomValue')
.send({ email: EMAIL_ID })
.send({ email: defaultUserArgs.email })
.expect(400, done);
});
it('Email validate with an invalid token', function (done) {
request(app)
.post('/api/v1/auth/email/validate/someRandomValue')
.send({ email: EMAIL_ID })
.send({ email: defaultUserArgs.email })
.expect(400, done);
});

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

@ -15,4 +15,4 @@ const createUser = async (app, userArgs = {}) => {
return { token: response.body.token, user };
};
export { createUser };
export { createUser, defaultUserArgs };

162
packages/nocodb/src/__tests__/unit/rest/tests/table.test.ts

@ -6,6 +6,7 @@ import { createUser } from './helpers/user';
import { createTable } from './helpers/table';
import { createProject } from './helpers/project';
import Model from '../../../../lib/models/Model';
import { defaultColumns } from './helpers/column';
function tableTest() {
let app;
@ -13,7 +14,7 @@ function tableTest() {
let project;
let table;
before(async function () {
beforeEach(async function () {
app = await server();
const response = await createUser(app, { roles: 'editor' });
token = response.token;
@ -36,6 +37,165 @@ function tableTest() {
});
});
it('Create table', function (done) {
request(app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.send({
table_name: 'table2',
title: 'new title 2',
columns: defaultColumns,
})
.expect(200, async (err, res) => {
if (err) return done(err);
const tables = await Model.list({
project_id: project.id,
base_id: project.bases[0].id,
});
if (tables.length !== 2) {
return done('Tables is not be created');
}
if (res.body.columns.length !== defaultColumns.length) {
done('Columns not saved properly');
}
if (
!(
res.body.table_name.startsWith(project.prefix) &&
res.body.table_name.endsWith('table2')
)
) {
done('table name not configured properly');
}
done();
});
});
it('Create table with no table name', function (done) {
request(app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.send({
table_name: undefined,
title: 'new title',
columns: defaultColumns,
})
.expect(400, async (err, res) => {
if (err) return done(err);
if (
!res.text.includes(
'Missing table name `table_name` property in request body'
)
) {
console.error(res.text);
return done('Wrong api response');
}
const tables = await Model.list({
project_id: project.id,
base_id: project.bases[0].id,
});
if (tables.length !== 1) {
console.log(tables);
return done(
`Tables should not be created, tables.length:${tables.length}`
);
}
done();
});
});
it('Create table with same table name', function (done) {
request(app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.send({
table_name: table.table_name,
title: 'New title',
columns: defaultColumns,
})
.expect(400, async (err, res) => {
if (err) return done(err);
if (!res.text.includes('Duplicate table name')) {
console.error(res.text);
return done('Wrong api response');
}
const tables = await Model.list({
project_id: project.id,
base_id: project.bases[0].id,
});
if (tables.length !== 1) {
return done('Tables should not be created');
}
done();
});
});
it('Create table with same title', function (done) {
request(app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.send({
table_name: 'New table name',
title: table.title,
columns: defaultColumns,
})
.expect(400, async (err, res) => {
if (err) return done(err);
if (!res.text.includes('Duplicate table alias')) {
console.error(res.text);
return done('Wrong api response');
}
const tables = await Model.list({
project_id: project.id,
base_id: project.bases[0].id,
});
if (tables.length !== 1) {
return done('Tables should not be created');
}
done();
});
});
it('Create table with title length more than the limit', function (done) {
request(app)
.post(`/api/v1/db/meta/projects/${project.id}/tables`)
.set('xc-auth', token)
.send({
table_name: 'a'.repeat(256),
title: 'new title',
columns: defaultColumns,
})
.expect(400, async (err, res) => {
if (err) return done(err);
if (!res.text.includes('Table name exceeds ')) {
console.error(res.text);
return done('Wrong api response');
}
const tables = await Model.list({
project_id: project.id,
base_id: project.bases[0].id,
});
if (tables.length !== 1) {
return done('Tables should not be created');
}
done();
});
});
it('Update table', function (done) {
request(app)
.patch(`/api/v1/db/meta/tables/${table.id}`)

Loading…
Cancel
Save