From 1d2d5e145ea02e08645bd5f62dbbe3f88c53f689 Mon Sep 17 00:00:00 2001 From: joelchu Date: Sun, 10 Nov 2019 17:39:40 +0800 Subject: [PATCH 1/3] add a login method for testing --- packages/@jsonql/koa/package.json | 2 +- packages/@jsonql/koa/tests/auth.test.js | 24 +++++++++++++++---- .../tests/fixtures/resolvers/auth/login.js | 8 +++++++ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 packages/@jsonql/koa/tests/fixtures/resolvers/auth/login.js diff --git a/packages/@jsonql/koa/package.json b/packages/@jsonql/koa/package.json index 52f22d1d..d5556857 100644 --- a/packages/@jsonql/koa/package.json +++ b/packages/@jsonql/koa/package.json @@ -53,7 +53,7 @@ "debug": "^4.1.1", "fs-extra": "^8.1.0", "jsonql-constants": "^1.8.10", - "jsonql-koa": "^1.4.6", + "jsonql-koa": "^1.4.7", "jsonql-params-validator": "^1.4.11", "koa": "^2.11.0", "koa-bodyparser": "^4.2.1", diff --git a/packages/@jsonql/koa/tests/auth.test.js b/packages/@jsonql/koa/tests/auth.test.js index f39c3841..50694ca8 100644 --- a/packages/@jsonql/koa/tests/auth.test.js +++ b/packages/@jsonql/koa/tests/auth.test.js @@ -1,6 +1,7 @@ const test = require('ava') const fsx = require('fs-extra') const { join } = require('path') +const debug = require('debug')('jsonql-koa:test:auth') const jsonqlNodeClient = require('jsonql-node-client') const { JS_WS_NAME, HELLO } = require('jsonql-constants') const jsonqlKoaServer = require('./fixtures/test-server') @@ -22,22 +23,35 @@ test.before(t => { test.after( t => { t.context.stop() fsx.removeSync(contractDir) + fsx.removeSync(join(__dirname, 'fixtures', 'keys')) }) -test.serial(`It should able to setup a auth server`, async t => { +test.beforeEach(async t => { t.context.client = await jsonqlNodeClient({ hostname: `http://localhost:${port}`, enableAuth: true, contractDir }) +}) - const result = await t.context.client.query.helloWorld() - +test.serial(`It should able to setup a auth server`, async t => { + const client = t.context.client; + const result = await client.query.helloWorld() t.is(result, HELLO) }) -test.todo(`It should not able to connect to a private method`) +test.serial(`It should not able to connect to a private method`, async t => { + const client = t.context.client + const error = await t.throwsAsync(client.query.getSecretMsg()) + t.true(error.message.indexOf('JsonqlAuthorisationError') > -1) +}) -test.todo(`It should able to login`) +test.serial.cb(`It should able to login`, async t=> { + const client = t.context.client + client.login() + client.onLogin = function() { + + } +}) test.todo(`It should able to connect to a private method after login`) diff --git a/packages/@jsonql/koa/tests/fixtures/resolvers/auth/login.js b/packages/@jsonql/koa/tests/fixtures/resolvers/auth/login.js new file mode 100644 index 00000000..5a389505 --- /dev/null +++ b/packages/@jsonql/koa/tests/fixtures/resolvers/auth/login.js @@ -0,0 +1,8 @@ + +/** + * login method + * + */ +module.exports = function login(name) { + return { name, loginTime: Date.now() } +} -- Gitee From 7f1ef59c717cbc0ff3a2b6ccf0a5919bf5358137 Mon Sep 17 00:00:00 2001 From: joelchu Date: Sun, 10 Nov 2019 19:15:55 +0800 Subject: [PATCH 2/3] Login test passede --- packages/@jsonql/koa/tests/auth.test.js | 18 ++++++++++++------ .../koa/tests/fixtures/resolvers/auth/login.js | 8 +++++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/@jsonql/koa/tests/auth.test.js b/packages/@jsonql/koa/tests/auth.test.js index 50694ca8..c5c4d506 100644 --- a/packages/@jsonql/koa/tests/auth.test.js +++ b/packages/@jsonql/koa/tests/auth.test.js @@ -14,7 +14,6 @@ test.before(t => { port: port, autoStart: true, enableAuth: true - // socketType: JS_WS_NAME }) t.context.stop = stop; @@ -46,12 +45,19 @@ test.serial(`It should not able to connect to a private method`, async t => { t.true(error.message.indexOf('JsonqlAuthorisationError') > -1) }) -test.serial.cb(`It should able to login`, async t=> { +test.serial.cb(`It should able to login`, t => { + t.plan(1) const client = t.context.client - client.login() - client.onLogin = function() { - - } + const name = 'Joel' + client.login(name) + .then(result => { + const userdata = client.userdata() + t.is(userdata.name, name) + t.end() + }) + .catch(err => { + debug('login error', err) + }) }) test.todo(`It should able to connect to a private method after login`) diff --git a/packages/@jsonql/koa/tests/fixtures/resolvers/auth/login.js b/packages/@jsonql/koa/tests/fixtures/resolvers/auth/login.js index 5a389505..2db3d326 100644 --- a/packages/@jsonql/koa/tests/fixtures/resolvers/auth/login.js +++ b/packages/@jsonql/koa/tests/fixtures/resolvers/auth/login.js @@ -1,8 +1,10 @@ - +const debug = require('debug')('jsonql-koa:resolvers:auth:login') /** * login method - * + * @param {string} name give it a name + * @return {object} a userdata object with timestamp */ module.exports = function login(name) { - return { name, loginTime: Date.now() } + debug(`Login got call with ${name}`) + return { name, timestamp: Date.now() } } -- Gitee From 64d82d2cc8400b906ff8cb3ff3e30c4e5579e85c Mon Sep 17 00:00:00 2001 From: joelchu Date: Sun, 10 Nov 2019 21:16:11 +0800 Subject: [PATCH 3/3] fix a search resolver error in the jsonql-koa --- packages/@jsonql/koa/src/index.js | 5 ++--- packages/@jsonql/koa/tests/auth.test.js | 10 ++++++++-- packages/koa/package.json | 2 +- packages/koa/src/middlewares/auth-middleware.js | 17 ++++++++++------- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/@jsonql/koa/src/index.js b/packages/@jsonql/koa/src/index.js index bb8f5bd2..62f6e3b6 100644 --- a/packages/@jsonql/koa/src/index.js +++ b/packages/@jsonql/koa/src/index.js @@ -3,18 +3,17 @@ const http = require('http') const Koa = require('koa') const bodyparser = require('koa-bodyparser') const cors = require('koa-cors') -// const contractApi = require('jsonql-koa/contract') +/* require('../../../koa/main') */ const { jsonqlKoa } = require('jsonql-koa') const { getSocketServer } = require('./get-socket-server') - /** * @param {object} config configuration * @param {array} middlewares what it said * @return {object} see below for detail */ function initServer(config, middlewares) { - + const app = new Koa() // apply default middlewares app.use(bodyparser()) diff --git a/packages/@jsonql/koa/tests/auth.test.js b/packages/@jsonql/koa/tests/auth.test.js index c5c4d506..c9fd2e64 100644 --- a/packages/@jsonql/koa/tests/auth.test.js +++ b/packages/@jsonql/koa/tests/auth.test.js @@ -46,14 +46,20 @@ test.serial(`It should not able to connect to a private method`, async t => { }) test.serial.cb(`It should able to login`, t => { - t.plan(1) + t.plan(2) const client = t.context.client const name = 'Joel' client.login(name) .then(result => { const userdata = client.userdata() t.is(userdata.name, name) - t.end() + + client.query.getSecretMsg() + .then(result => { + t.is(result, 'Let me tell ya a secret ...') + t.end() + }) + }) .catch(err => { debug('login error', err) diff --git a/packages/koa/package.json b/packages/koa/package.json index 5ff8c1d5..4cd16f03 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-koa", - "version": "1.4.7", + "version": "1.4.8", "description": "jsonql Koa middleware", "main": "main.js", "module": "index.js", diff --git a/packages/koa/src/middlewares/auth-middleware.js b/packages/koa/src/middlewares/auth-middleware.js index 3ba9164d..52829346 100644 --- a/packages/koa/src/middlewares/auth-middleware.js +++ b/packages/koa/src/middlewares/auth-middleware.js @@ -76,6 +76,7 @@ const createJwtValidatorChain = (config, validator = false) => { if (!validator || typeof validator !== 'function') { return jwtFn; } + return chainFns(jwtFn, validator) } @@ -95,17 +96,19 @@ const getValidator = (config, type, contract) => { try { localValidator = getLocalValidator(config, type, contract) } catch(e) { + const checkErr = e instanceof JsonqlResolverNotFoundError + // debug('checkErr', checkErr) // we ignore this error becasue they might not have one? - if (!(e instanceof JsonqlResolverNotFoundError)) { + if (!checkErr) { return finalCatch(e) } } - if (config.useJwt) { - debug(`return the jwt validation chain`) - return createJwtValidatorChain(config, localValidator) - } - debug(`return a local validator`) - return localValidator; + // if (config.useJwt) { // we always use the jwt opiton now + ///debug(`return the jwt validation chain`) + return createJwtValidatorChain(config, localValidator) + //} + // debug(`return a local validator`) + // return localValidator; } /** -- Gitee