Przeglądaj źródła

1.新增登录错误提示
2.新增部分模块单元测试

olym 7 lat temu
rodzic
commit
5841f5ea28

+ 10 - 0
app/base/base_service.js

@@ -52,6 +52,16 @@ class BaseService extends Service {
     }
 
     /**
+     * 根据条件查找单条数据
+     *
+     * @param {Object} condition - 筛选条件
+     * @return {Object} - 返回单条数据
+     */
+    async getDataByCondition(condition) {
+        return await this.db.get(this.tableName, condition);
+    }
+
+    /**
      * 根据id删除数据
      *
      * @param {Number} id - 数据库中的id

+ 10 - 3
app/controller/login_controller.js

@@ -20,7 +20,13 @@ module.exports = app => {
          * @return {void}
          */
         async index(ctx) {
-            const renderData = {};
+            const errorMessage = ctx.session.loginError;
+            // 显示完删除
+            ctx.session.loginError = null;
+
+            const renderData = {
+                errorMessage,
+            };
             await ctx.render('login/login.ejs', renderData);
         }
 
@@ -50,11 +56,12 @@ module.exports = app => {
                 if (!result) {
                     throw '登录失败';
                 }
+                ctx.body = 'success';
             } catch (error) {
                 console.log(error);
+                ctx.session.loginError = '用户名或密码错误';
+                ctx.redirect('/login');
             }
-
-            ctx.body = 'success';
         }
     }
 

+ 12 - 5
app/view/login/login.ejs

@@ -20,24 +20,31 @@
         </nav>
         <div class="tab-content">
             <div class="tab-pane active" id="preview" role="tabpanel">
-                <div class="form-group">
+                <div class="form-group <% if (errorMessage !== undefined && errorMessage !== null) { %>has-danger<% } %>">
                     <input id="username" name="username" class="form-control " placeholder="通行账号 邮箱/手机" value="laiku123@qq.com" autofocus="">
                 </div>
-                <div class="form-group">
+                <div class="form-group <% if (errorMessage !== undefined && errorMessage !== null) { %>has-danger<% } %>">
                     <input id="password" name="password" class="form-control " placeholder="输入密码" value="19930523" type="password">
                 </div>
             </div>
             <div class="tab-pane" id="paid" role="tabpanel">
-                <div class="form-group">
+                <div class="form-group <% if (errorMessage !== undefined && errorMessage !== null) { %>has-danger<% } %>">
                     <input id="project" class="form-control" name="project" placeholder="项目编号" autofocus="" />
                 </div>
-                <div class="form-group">
+                <div class="form-group <% if (errorMessage !== undefined && errorMessage !== null) { %>has-danger<% } %>">
                     <input id="account" class="form-control" name="account" placeholder="输入账号" autofocus="" />
                 </div>
-                <div class="form-group">
+                <div class="form-group <% if (errorMessage !== undefined && errorMessage !== null) { %>has-danger<% } %>">
                     <input id="project-password" name="project_password" class="form-control" placeholder="输入密码" type="password" />
                 </div>
             </div>
+            <% if(errorMessage !== undefined && errorMessage !== null) { %>
+            <div class="form-group">
+                <div class="alert alert-danger" role="alert">
+                    <strong>登录失败</strong> <%= errorMessage %>
+                </div>
+            </div>
+            <% } %>
         </div>
         <div class="form-group">
             <button class="btn btn-primary btn-block" type="submit">登录</button>

+ 0 - 30
test/app/controller/manager_controller.test.js

@@ -1,30 +0,0 @@
-/**
- * 后台管理员控制器单元测试
- *
- * @author CaiAoLin
- * @date 2017/10/23
- * @version
- */
-'use strict';
-
-const { app, assert } = require('egg-mock/bootstrap');
-
-describe('test/app/service/manager_controller.test.js', () => {
-    it('test delete manager', function* () {
-        // 模拟后台用户
-        const session = {};
-        /*
-        session.managerSession = { username: 'admin',
-            loginTime: 1508743639337,
-            sessionToken: 'fdlRuYBTCON1FoKvdJOn+JbCPc0=',
-            userID: 1,
-            permission: 'all',
-        };
-        const ctx = app.mockContext({
-            session,
-        });
-        const result = yield app.httpRequest().get('/manager/delete/1022');
-        assert(result.status === 302);
-        */
-    });
-});

+ 0 - 27
test/app/lib/cld.test.js

@@ -1,27 +0,0 @@
-/**
- * cld类单元测试
- *
- * @author CaiAoLin
- * @date 2017/10/18
- * @version
- */
-'use strict';
-
-const { app, assert } = require('egg-mock/bootstrap');
-const Cld = require('../../../app/lib/sso');
-
-describe('test/app/lib/cld.test.js', () => {
-
-    it('CLD valid', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext();
-        const cld = new Cld(ctx);
-        const result = yield cld.loginValid('caiaolin', 'test');
-        assert(result);
-        // 检验数据库是否插入数据
-        if (result) {
-            const managerData = yield app.mysql.get('zh_manager', { username: 'caiaolin' });
-            assert(managerData.id > 0);
-        }
-    });
-});

+ 32 - 0
test/app/lib/sso.test.js

@@ -0,0 +1,32 @@
+/**
+ * sso类单元测试
+ *
+ * @author CaiAoLin
+ * @date 2017/11/17
+ * @version
+ */
+
+'use strict';
+
+const { app, assert } = require('egg-mock/bootstrap');
+const SSO = require('../../../app/lib/sso');
+
+describe('test/app/lib/sso.test.js', () => {
+
+    it('login success valid', function* () {
+        const ctx = app.mockContext();
+        const sso = new SSO(ctx);
+
+        const username = 'laiku123@qq.com';
+        const password = '19930523';
+        const result = yield sso.loginValid(username, password);
+
+        // 验证返回
+        assert(result);
+
+        // 验证数据库中是否新增了账号
+        const customer = yield ctx.service.customer.getDataByCondition({ email: username });
+        assert(customer !== null);
+    });
+
+});

+ 0 - 40
test/app/service/group.test.js

@@ -1,40 +0,0 @@
-/**
- * 用户组单元测试
- *
- * @author CaiAoLin
- * @date 2017/10/13
- * @version
- */
-'use strict';
-
-const { app, assert } = require('egg-mock/bootstrap');
-
-describe('test/app/service/group.test.js', () => {
-
-    it('getList', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext({
-            sort: [],
-            page: 1,
-        });
-
-        const group = yield ctx.service.group.getList({});
-        assert(group);
-    });
-
-    it('save group', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext();
-        const insertData = {
-            name: 'test',
-            permission: ['11', '12', '13'],
-            remark: '111',
-            _csrf: '',
-            create_time: new Date().getTime() / 1000,
-        };
-        // const result = yield ctx.service.group.save(insertData);
-        // assert(result);
-    });
-
-
-});

+ 0 - 58
test/app/service/log.test.js

@@ -1,58 +0,0 @@
-'use strict';
-
-/**
- * 日志模块单元测试
- *
- * @author CaiAoLin
- * @date 2017/10/31
- * @version
- */
-
-const { app, assert } = require('egg-mock/bootstrap');
-
-describe('test/app/service/log.test.js', () => {
-
-    it('log list test', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext({
-            sort: [],
-            page: 1,
-        });
-
-        ctx.service.log.searchFilter(ctx.request.query);
-        const logList = yield ctx.service.log.getListWithBuilder();
-
-        assert(logList instanceof Array);
-    });
-
-    it('add log test', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext({
-            session: {
-                managerSession: {
-                    username: 'tester',
-                },
-            },
-        });
-        // 错误插入
-        const errInsertData = {
-            controller: '',
-            action: 'test',
-            operation: '',
-        };
-        const errInsertResult = yield ctx.service.log.addLog(errInsertData);
-        assert(!errInsertResult);
-
-
-        // 成功插入模拟
-        const successInsertData = {
-            controller: 'test',
-            action: 'test',
-            operation: '单元测试',
-            type: 1,
-            target_id: 0,
-        };
-        const result = yield ctx.service.log.addLog(successInsertData);
-        assert(result);
-    });
-});

+ 0 - 22
test/app/service/manager.test.js

@@ -1,22 +0,0 @@
-'use strict';
-
-/**
- * 后台用户管理单元测试
- *
- * @author CaiAoLin
- * @date 2017/10/20
- * @version
- */
-
-const { app, assert } = require('egg-mock/bootstrap');
-
-describe('test/app/service/manager.test.js', () => {
-    it('login valid', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext({
-            session: {},
-        });
-        const failResult = yield ctx.service.manager.validManager('admin', 'test');
-        assert(!failResult);
-    });
-});

+ 0 - 69
test/app/service/message.test.js

@@ -1,69 +0,0 @@
-'use strict';
-
-/**
- * 消息模块单元测试
- *
- * @author CaiAoLin
- * @date 2017/11/1
- * @version
- */
-
-const { app, assert } = require('egg-mock/bootstrap');
-
-describe('test/app/service/message.test.js', () => {
-
-    it('release message', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext();
-        const result = yield ctx.service.message.releaseSchedule();
-        assert(typeof result === 'boolean');
-    });
-
-    it('save message without release time', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext({
-            session: {
-                managerSession: {
-                    username: 'test',
-                },
-            },
-        });
-        try {
-            // 没有填写发布时间
-            const insertErrorData = {
-                title: 'test',
-                content: 'content',
-                release_type: 2,
-            };
-            yield ctx.service.message.save(insertErrorData);
-        } catch (error) {
-            assert(error === '请选择发布时间');
-        }
-
-    });
-
-    it('save message release less than create time', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext({
-            session: {
-                managerSession: {
-                    username: 'test',
-                },
-            },
-        });
-        try {
-            // 没有填写发布时间
-            const insertErrorData = {
-                title: 'test',
-                content: 'content',
-                release_type: 2,
-                release_time: '2017-11-1 17:19:00',
-                create_time: '2017-11-1 18:00:00',
-            };
-            yield ctx.service.message.save(insertErrorData);
-        } catch (error) {
-            assert(error === '发布时间不能小于当前时间');
-        }
-
-    });
-});

+ 0 - 36
test/app/service/permission.test.js

@@ -1,36 +0,0 @@
-/**
- * 权限业务流程单元测试
- *
- * @author CaiAoLin
- * @date 2017/10/20
- * @version
- */
-'use strict';
-
-const { app, assert } = require('egg-mock/bootstrap');
-
-describe('test/app/service/permission.test.js', () => {
-
-    it('get parent id', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext({
-            permissionRootId: 0,
-        });
-        const allPermission = yield ctx.service.permission.getAllData(true, true);
-        ctx.service.permission.getTopPid(15, allPermission);
-
-        assert(ctx.permissionRootId === 11);
-    });
-
-    it('save permission', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext();
-        const insertData = {
-            name: 'test',
-            pid: 0,
-            create_time: new Date().getTime() / 1000,
-        };
-        // const result = yield ctx.service.permission.save(insertData);
-        // assert(result);
-    });
-});

+ 14 - 26
test/app/service/project.test.js

@@ -1,39 +1,27 @@
 /**
- * 项目相关业务流程单元测试
+ * 项目数据模型单元测试
  *
  * @author CaiAoLin
- * @date 2017/11/15
+ * @date 2017/11/17
  * @version
  */
 
 'use strict';
-const { app, assert } = require('egg-mock/bootstrap');
 
-describe('test/app/service/project.test.js', () => {
+const { app, assert } = require('egg-mock/bootstrap');
 
-    it('insert data test', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext({
-            session: {
-                managerSession: {
-                    office: 1,
-                    username: '单元测试',
-                },
-            },
-        });
+describe('test/app/lib/project.test.js', () => {
 
-        const insertData = {
-            name: '单元测试',
-            user_account: 'test@test.com',
-            office: 1,
-            manager_id: 1,
-            max_user: 10,
-            remark: '备注测试',
-            create_time: new Date().getTime() / 1000,
-        };
-        // const result = yield ctx.service.project.save(insertData, 0);
+    it('not exist code', function* () {
+        const ctx = app.mockContext();
+        const projectData = yield ctx.service.project.getProjectByCode('1111');
+        assert(projectData === null);
+    });
 
-        // assert(result);
+    it('exist code', function* () {
+        const ctx = app.mockContext();
+        const projectData = yield ctx.service.project.getProjectByCode('J201711163164');
+        assert(projectData.id === 16);
     });
-});
 
+});

+ 0 - 33
test/app/service/project_account.test.js

@@ -1,33 +0,0 @@
-/**
- * 项目账号业务逻辑单元测试
- *
- * @author CaiAoLin
- * @date 2017/11/15
- * @version
- */
-
-'use strict';
-
-const { app, assert } = require('egg-mock/bootstrap');
-
-describe('test/app/service/project_account.test.js', () => {
-
-    it('get account in not exist project', function* () {
-        const ctx = app.mockContext();
-
-        ctx.service.projectAccount.searchFilter({});
-        const accountList = yield ctx.service.projectAccount.getAccountByProjectId(1000);
-
-        assert(accountList.length === 0);
-    });
-
-    it('get account in project', function* () {
-        const ctx = app.mockContext();
-
-        ctx.service.projectAccount.searchFilter({});
-        const accountList = yield ctx.service.projectAccount.getAccountByProjectId(5);
-
-        assert(accountList.length > 0);
-    });
-
-});

+ 0 - 35
test/app/service/sql_execute.test.js

@@ -1,35 +0,0 @@
-/**
- * sql执行数据模型单元测试
- *
- * @author CaiAoLin
- * @date 2017/10/23
- * @version
- */
-'use strict';
-
-const { app, assert } = require('egg-mock/bootstrap');
-
-describe('test/app/service/sql_execute.test.js', () => {
-
-    it('select test', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext();
-        const sql = 'select * from zh_manager';
-        const [filedList, result] = yield ctx.service.sqlExecute.execute(sql);
-        assert(result.length > 0 && filedList.length > 0);
-    });
-
-    it('other operate test', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext();
-        const sql = 'UPDATE zh_manager SET token = 111 WHERE id = 10;';
-        let message = '';
-        try {
-            yield ctx.service.sqlExecute.execute(sql);
-        } catch (error) {
-            message = error.toString();
-        }
-
-        assert(message === '只能执行select相关代码');
-    });
-});

+ 0 - 20
test/app/service/white_list.test.js

@@ -1,20 +0,0 @@
-'use strict';
-
-/**
- * 白名单数据模型单元测试
- *
- * @author CaiAoLin
- * @date 2017/11/1
- * @version
- */
-
-const { app, assert } = require('egg-mock/bootstrap');
-
-describe('test/app/service/white_list.test.js', () => {
-
-    it('get data test', function* () {
-        const ctx = app.mockContext();
-        const data = yield ctx.service.whiteList.getAllData(false);
-        assert(typeof data === 'object');
-    });
-});