Skip to content

Commit

Permalink
feat: 完善单元测试;
Browse files Browse the repository at this point in the history
完善单元测试;
  • Loading branch information
Imfdj committed May 10, 2021
1 parent 225abb7 commit 5246fce
Show file tree
Hide file tree
Showing 17 changed files with 286 additions and 56 deletions.
8 changes: 4 additions & 4 deletions app/controller/v1/invites.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ class RoleController extends Controller {

/**
* @apikey
* @summary 获取某个 邀请
* @description 获取某个 邀请
* @router get /api/v1/invites
* @request query number *id eg:1 inviteID
* @summary 获取某个 邀请 by UUID
* @description 获取某个 邀请 by UUID
* @router get /api/v1/invites/uuid
* @request query number *uuid eg:1 uuid
*/
async findOneByUUID() {
const { ctx, service } = this;
Expand Down
18 changes: 1 addition & 17 deletions app/controller/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class RoleController extends Controller {
ctx.validate(beforeParams, ctx.request.body);

// 如果不是开发环境 获取配置中的rsa私钥对密码解密
if (app.config.env !== 'local') {
if (app.config.env === 'prod') {
try {
const { rsa_private_key } = await ctx.model.Configurations.findOne({
where: { id: 1 },
Expand Down Expand Up @@ -402,22 +402,6 @@ class RoleController extends Controller {
}
}

/**
* @summary 创建 用户
* @description 创建 用户
* @router put /api/v1/users/minus
* @request body userBodyReq
*/
async minus() {
const ctx = this.ctx;
const state = await this.app.redis.decr('goodsCount');
if (state > 0) {
ctx.helper.body.SUCCESS({ ctx, res: { state } });
} else {
ctx.helper.body.INVALID_REQUEST({ ctx });
}
}

/**
* @summary github授权登录
* @description github授权登录
Expand Down
1 change: 0 additions & 1 deletion app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module.exports = app => {
router.put('/api/v1/users/password', controller.v1.users.updateUserPassword);
router.put('/api/v1/users/department', controller.v1.users.updateUserDepartment);
router.post('/api/v1/users/refreshToken', controller.v1.users.refreshToken);
router.put('/api/v1/users/department', controller.v1.users.minus);
router.post('/api/v1/users/github/login', controller.v1.users.githubLogin);

/**
Expand Down
1 change: 0 additions & 1 deletion app/service/configurations.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const Service = require('egg').Service;
const { Op } = require('sequelize');

class _objectName_Service extends Service {
async update(payload) {
Expand Down
11 changes: 7 additions & 4 deletions app/service/invites.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,13 @@ class _objectName_Service extends Service {
},
],
});
// 有效时间expires是否大于当前时间,是则是可用的
res.dataValues.valid = app.dayjs(res.expires)
.isAfter(app.dayjs());
return res;
if (res) {
// 有效时间expires是否大于当前时间,是则是可用的
res.dataValues.valid = app.dayjs(res.expires)
.isAfter(app.dayjs());
return res;
}
return false;
}
}

Expand Down
38 changes: 22 additions & 16 deletions app/service/task_lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,31 @@ class _objectName_Service extends Service {
}
if (preId !== undefined) {
const pre = await ctx.model.TaskLists.findOne({ where: { id: preId } });
sort = pre.sort + 1000;
return await ctx.model.TaskLists.update(
{ sort },
{
where: { id: payload.id },
individualHooks: true,
}
);
if (pre) {
sort = pre.sort + 1000;
return await ctx.model.TaskLists.update(
{ sort },
{
where: { id: payload.id },
individualHooks: true,
}
);
}
return false;
}
if (nextId !== undefined) {
const next = await ctx.model.TaskLists.findOne({ where: { id: nextId } });
sort = next.sort / 1.1;
return await ctx.model.TaskLists.update(
{ sort },
{
where: { id: payload.id },
individualHooks: true,
}
);
if (next) {
sort = next.sort / 1.1;
return await ctx.model.TaskLists.update(
{ sort },
{
where: { id: payload.id },
individualHooks: true,
}
);
}
return false;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion database/migrations/20200110080210-create-task_priority.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module.exports = {
type: Sequelize.INTEGER(11),
allowNull: false,
defaultValue: '0',
unique: true,
comment: '排序,越大越靠前',
},
created_at: {
Expand Down
1 change: 0 additions & 1 deletion generator/config-task-priority.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ module.exports = {
allowNull: false, // 是否允许为空
defaultValue: 0, // 数据库表中字段的默认值
comment: '排序,越大越靠前', // 数据库表中字段的描述
unique: true, // 是否唯一
},
],
fields_option: {},
Expand Down
8 changes: 1 addition & 7 deletions test/.setup.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
'use strict';
const NodeRSA = require('node-rsa');
const { app } = require('egg-mock/bootstrap');
const factories = require('./factories');

before(async () => {
const { rsa_public_key } = await app.model.models.configurations.findOne({
where: { id: 1 },
});
const key = new NodeRSA(rsa_public_key);
const password = key.encrypt('123123', 'base64');
const res = await app
.httpRequest()
.post('/api/v1/users/login')
.send({
username: 'admin',
password,
password: '123123',
})
.expect(200);
if (app.config.verification_mode === 'jwt') {
Expand Down
4 changes: 1 addition & 3 deletions test/app/controller/configurations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ const { assert, app } = require('egg-mock/bootstrap');

describe('test/app/controller/configurations.test.js', () => {
const createName = 'configurationName' + Math.random();
let createMenuData = {};
before(async () => {});

describe('GET /api/v1/configurations/public_key', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
const res = await app.httpRequest()
.get('/api/v1/configurations/public_key')
.query({ id: createMenuData.id })
.query({})
.set('authorization', app.__authorization);
assert(res.status === 200);
assert(res.body.data);
createMenuData = res.body.data;
assert(res.body.code === 0);
});
});
Expand Down
27 changes: 27 additions & 0 deletions test/app/controller/invites.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('test/app/controller/invites.test.js', () => {
.send({
group: 'Projects',
group_id: 999999,
receiver_id: 1,
});
assert(res.status === 201);
assert(res.body.code === 0);
Expand Down Expand Up @@ -68,6 +69,32 @@ describe('test/app/controller/invites.test.js', () => {
});
});

describe('GET /api/v1/invites/valid', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
const res = await app.httpRequest()
.get('/api/v1/invites/valid')
.query({ group: 'Projects', group_id: 999999 })
.set('authorization', app.__authorization);
assert(res.status === 200);
assert(res.body.data);
assert(res.body.code === 0);
});
});

describe('GET /api/v1/invites/uuid', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
const res = await app.httpRequest()
.get('/api/v1/invites/uuid')
.query({ uuid: createMenuData.uuid })
.set('authorization', app.__authorization);
assert(res.status === 200);
assert(res.body.data);
assert(res.body.code === 0);
});
});

describe('DELETE /api/v1/invites', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
Expand Down
13 changes: 13 additions & 0 deletions test/app/controller/menus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ describe('test/app/controller/menus.test.js', () => {
});
});

describe('GET /api/v1/menus/user_menus', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
const res = await app.httpRequest()
.get('/api/v1/menus/user_menus')
.query({})
.set('authorization', app.__authorization);
assert(res.status === 200);
assert(res.body.data);
assert(res.body.code === 0);
});
});

describe('DELETE /api/v1/menus', () => {
it('should work', async () => {
app.mockCsrf();
Expand Down
22 changes: 22 additions & 0 deletions test/app/controller/task_lists.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ describe('test/app/controller/task_lists.test.js', () => {
});
});

describe('PUT /api/v1/task_lists/sort', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
const next = await app.httpRequest()
.get('/api/v1/task_lists/list')
.query({ limit: 1 })
.set('authorization', app.__authorization);
assert(next.status === 200);
assert(next.body.data);
const nextData = next.body.data.rows[0];
const res = await app.httpRequest()
.put('/api/v1/task_lists/sort')
.set('authorization', app.__authorization)
.send({
id: createMenuData.id,
nextId: nextData.id,
});
assert(res.status === 201);
assert(res.body.code === 0);
});
});

describe('DELETE /api/v1/task_lists', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
Expand Down
15 changes: 15 additions & 0 deletions test/app/controller/task_task_tags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,19 @@ describe('test/app/controller/task_task_tags.test.js', () => {
assert(res.body.code === 0);
});
});

describe('POST /api/v1/task_task_tags/change', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
const res = await app.httpRequest()
.post('/api/v1/task_task_tags/change')
.set('authorization', app.__authorization)
.send({
task_id: 999999,
task_tag_id: 999999,
});
assert(res.status === 201);
assert(res.body.code === 0);
});
});
});
31 changes: 30 additions & 1 deletion test/app/controller/tasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('test/app/controller/tasks.test.js', () => {
.send({
name: createName,
project_id: 999999,
task_list_id: 98,
task_list_id: 999999,
task_state_id: 2,
task_type_id: 2,
task_priority_id: 2,
Expand Down Expand Up @@ -76,6 +76,35 @@ describe('test/app/controller/tasks.test.js', () => {
});
});

describe('PUT /api/v1/tasks/sort', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
const res = await app.httpRequest()
.put('/api/v1/tasks/sort')
.set('authorization', app.__authorization)
.send({
id: createMenuData.id,
task_list_id: 999999,
});
assert(res.status === 201);
assert(res.body.code === 0);
});
});

describe('PUT /api/v1/tasks/recycle_all_task_of_taskList', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
const res = await app.httpRequest()
.put('/api/v1/tasks/recycle_all_task_of_taskList')
.set('authorization', app.__authorization)
.send({
task_list_id: 999999,
});
assert(res.status === 201);
assert(res.body.code === 0);
});
});

describe('DELETE /api/v1/tasks', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
Expand Down
15 changes: 15 additions & 0 deletions test/app/controller/user_tasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ describe('test/app/controller/user_tasks.test.js', () => {
app.mockCsrf();
});

describe('POST /api/v1/user_tasks/change', () => {
it('should work', async () => {
app.mockCookies({ EGG_SESS: app.__cookies });
const res = await app.httpRequest()
.post('/api/v1/user_tasks/change')
.set('authorization', app.__authorization)
.send({
user_id: 1,
task_id: 999999,
});
assert(res.status === 201);
assert(res.body.code === 0);
});
});

// describe('POST /api/v1/user_tasks', () => {
// it('should work', async () => {
// app.mockCookies({ EGG_SESS: app.__cookies });
Expand Down
Loading

0 comments on commit 5246fce

Please sign in to comment.