diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 01696fe..1301745 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -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 }} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b402299..0000000 --- a/.travis.yml +++ /dev/null @@ -1,18 +0,0 @@ - -language: node_js -node_js: - - '8' - - '9' - - '10' - - '11' - - '12' - - '13' - - '14' -before_install: - - npm i npminstall -g -install: - - npminstall -script: - - npm run ci -after_script: - - npminstall codecov && codecov diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 7ff26cf..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,20 +0,0 @@ -environment: - matrix: - - nodejs_version: '8' - - nodejs_version: '9' - - nodejs_version: '10' - - nodejs_version: '11' - - nodejs_version: '12' - - nodejs_version: '13' - - nodejs_version: '14' - -install: - - ps: Install-Product node $env:nodejs_version - - npm i npminstall && node_modules\.bin\npminstall - -test_script: - - node --version - - npm --version - - npm run test - -build: off diff --git a/config/config.unittest.js b/config/config.unittest.js new file mode 100644 index 0000000..9860b15 --- /dev/null +++ b/config/config.unittest.js @@ -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; +}; diff --git a/db/init_unittest_db.sh b/db/init_unittest_db.sh new file mode 100755 index 0000000..c656251 --- /dev/null +++ b/db/init_unittest_db.sh @@ -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`;' diff --git a/lib/plugin/egg-xprofiler-auth/app/extend/application.unittest.js b/lib/plugin/egg-xprofiler-auth/app/extend/application.unittest.js new file mode 100644 index 0000000..fa01107 --- /dev/null +++ b/lib/plugin/egg-xprofiler-auth/app/extend/application.unittest.js @@ -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, + }, + }); + }, +}; diff --git a/lib/plugin/egg-xprofiler-auth/app/middleware/basicAuth.js b/lib/plugin/egg-xprofiler-auth/app/middleware/basicAuth.js index 9d31da3..b1f07a4 100644 --- a/lib/plugin/egg-xprofiler-auth/app/middleware/basicAuth.js +++ b/lib/plugin/egg-xprofiler-auth/app/middleware/basicAuth.js @@ -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); diff --git a/package.json b/package.json index b91bcdd..204a75f 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" diff --git a/test/app/controller/home.test.js b/test/app/controller/home.test.js index 00741c3..8d52f7b 100644 --- a/test/app/controller/home.test.js +++ b/test/app/controller/home.test.js @@ -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('