Skip to content

Commit

Permalink
test: init unittest script and run the first test case
Browse files Browse the repository at this point in the history
PR-URL: #10
Reviewed-BY: hyj1991 <yeekwanvong@gmail.com>
  • Loading branch information
fengmk2 authored Dec 23, 2020
1 parent ea96c4c commit e3816f5
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 83 deletions.
80 changes: 52 additions & 28 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

# name: Node.js CI

# on:
# push:
# branches: [ master ]
# pull_request:
# branches: [ master ]

# jobs:
# build:

# runs-on: ${{ matrix.os }}

# strategy:
# matrix:
# node-version: [8.x, 9.x, 10.x, 11.x, 12.x, 13.x, 14.x]
# os: [ubuntu-latest, windows-latest, macos-latest]

# steps:
# - uses: actions/checkout@v2
# - name: Use Node.js ${{ matrix.node-version }}
# uses: actions/setup-node@v1
# with:
# node-version: ${{ matrix.node-version }}
# - run: npm i -g npminstall && npminstall
# - run: npm run ci
# env:
# CI: true
name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: '0 2 * * *'

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version: [10, 12, 14]
os: [ubuntu-latest]

steps:
# https://github.com/marketplace/actions/setup-mysql
- name: Setup MySQL
uses: mirromutth/mysql-action@v1.1
with:
mysql version: '5.7'
mysql root password: 'root'

# https://github.com/marketplace/actions/redis-server-in-github-actions
- name: Redis Server in GitHub Actions
uses: supercharge/redis-github-action@1.1.0
with:
redis-version: 5

- name: Checkout Git Source
uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install Dependencies
run: npm i -g npminstall && npminstall

- name: Continuous Integration
run: npm run ci

- name: Code Coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

20 changes: 0 additions & 20 deletions appveyor.yml

This file was deleted.

44 changes: 44 additions & 0 deletions config/config.unittest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

module.exports = () => {
const config = {};

const password = process.env.MYSQL_ROOT_PASSWORD || 'root';

config.mysql = {
app: true,
agent: false,
clients: {
xprofiler_console: {
host: '127.0.0.1',
port: 3306,
user: 'root',
password,
database: 'xprofiler_console_unittest',
},
xprofiler_logs: {
host: '127.0.0.1',
port: 3306,
user: 'root',
password,
database: 'xprofiler_logs_unittest',
},
},
};

config.redis = {
client: {
sentinels: null,
port: 6379,
host: '127.0.0.1',
password: '',
db: 0,
},
};

config.xprofilerConsole = 'http://127.0.0.1:8443';

config.xtransitManager = 'http://127.0.0.1:8543';

return config;
};
10 changes: 10 additions & 0 deletions db/init_unittest_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

# init unit test database script

PASSWORD=${MYSQL_ROOT_PASSWORD:-root}

mysql -uroot -p${PASSWORD} -h127.0.0.1 -e 'DROP DATABASE IF EXISTS `xprofiler_console_unittest`; CREATE DATABASE `xprofiler_console_unittest`;'
mysql -uroot -p${PASSWORD} -h127.0.0.1 -D 'xprofiler_console_unittest' < ${PWD}/db/init.sql
mysql -uroot -p${PASSWORD} -h127.0.0.1 -D 'xprofiler_console_unittest' -e 'SHOW tables;'

mysql -uroot -p${PASSWORD} -h127.0.0.1 -e 'DROP DATABASE IF EXISTS `xprofiler_logs_unittest`; CREATE DATABASE `xprofiler_logs_unittest`;'
15 changes: 15 additions & 0 deletions lib/plugin/egg-xprofiler-auth/app/extend/application.unittest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports = {
async mockUser(user) {
this.mockContext({
user: {
userId: 1,
name: 'mockuser',
nick: 'mocknick',
mail: 'mockuser@mock.com',
...user,
},
});
},
};
2 changes: 2 additions & 0 deletions lib/plugin/egg-xprofiler-auth/app/middleware/basicAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const auth = require('basic-auth');

module.exports = () => {
return async function(ctx, next) {
if (ctx.user) return await next();

const { service: { mysql } } = ctx;

const credentials = auth(ctx.req);
Expand Down
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"autod": "^3.1.0",
"autod-egg": "^1.1.0",
"egg-bin": "^4.14.1",
"egg-ci": "^1.15.0",
"egg-mock": "^4.0.0",
"eslint": "^7.0.0",
"eslint-config-egg": "^8.0.1"
Expand All @@ -35,19 +34,17 @@
"stop": "egg-scripts stop --title=egg-server-xprofiler-console",
"dev": "egg-bin dev --port=8443",
"debug": "egg-bin debug",
"init-test-db": "db/init_unittest_db.sh",
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"test-local": "npm run init-test-db && egg-bin test",
"cov": "npm run init-test-db && egg-bin cov",
"lint": "eslint app config lib test --fix",
"ci": "npm run lint && npm run cov",
"autod": "autod"
},
"ci": {
"version": "10"
},
"repository": {
"type": "git",
"url": ""
"url": "https://github.com/X-Profiler/xprofiler-console.git"
},
"author": "yeekwanvong@gmail.com",
"license": "BSD-2-Clause"
Expand Down
22 changes: 12 additions & 10 deletions test/app/controller/home.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
const { app, assert } = require('egg-mock/bootstrap');

describe('test/app/controller/home.test.js', () => {
it('should assert', () => {
const pkg = require('../../../package.json');
assert(app.config.keys.startsWith(pkg.name));

// const ctx = app.mockContext({});
// yield ctx.service.xx();
it('should block anonymous user', async () => {
const res = await app.httpRequest()
.get('/');
assert(res.status === 401);
assert(res.text === 'access denied');
});

it('should GET /', () => {
return app.httpRequest()
.get('/')
.expect(200);
it('should GET /', async () => {
await app.mockUser();
const res = await app.httpRequest()
.get('/');
assert(res.status === 200);
assert(res.text.includes('<title>Easy-Monitor</title>'));
assert(res.headers['content-type'] === 'text/html; charset=utf-8');
});
});

0 comments on commit e3816f5

Please sign in to comment.