From 119d1d0b4471beee11777c7f353900f475445aa6 Mon Sep 17 00:00:00 2001 From: joelchu Date: Wed, 6 Nov 2019 16:19:05 +0800 Subject: [PATCH 1/6] disable the jwt test for now --- packages/node-client/package.json | 4 ++-- packages/node-client/tests/{jwt.test.js => jwt._test_.js} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename packages/node-client/tests/{jwt.test.js => jwt._test_.js} (100%) diff --git a/packages/node-client/package.json b/packages/node-client/package.json index fd53c816..aa76224f 100755 --- a/packages/node-client/package.json +++ b/packages/node-client/package.json @@ -55,8 +55,8 @@ }, "devDependencies": { "ava": "^2.4.0", - "jsonql-contract": "^1.8.2", - "jsonql-koa": "^1.4.0", + "jsonql-contract": "^1.8.3", + "jsonql-koa": "^1.4.1", "jsonql-ws-server": "^1.3.5", "nyc": "^14.1.1", "server-io-core": "^1.2.0", diff --git a/packages/node-client/tests/jwt.test.js b/packages/node-client/tests/jwt._test_.js similarity index 100% rename from packages/node-client/tests/jwt.test.js rename to packages/node-client/tests/jwt._test_.js -- Gitee From e4cb9736c0629a4a660224265c6124fdf6f772d2 Mon Sep 17 00:00:00 2001 From: joelchu Date: Wed, 6 Nov 2019 16:28:47 +0800 Subject: [PATCH 2/6] pre generate a contract for the node-client jwt test --- packages/contract-cli/tests/debug-contract.test.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/contract-cli/tests/debug-contract.test.js b/packages/contract-cli/tests/debug-contract.test.js index 7b73c1de..24b397f1 100644 --- a/packages/contract-cli/tests/debug-contract.test.js +++ b/packages/contract-cli/tests/debug-contract.test.js @@ -8,15 +8,15 @@ const fsx = require('fs-extra') const baseDir = join(__dirname, '..', '..', 'node-client', 'tests', 'fixtures') const resolverDir = join(baseDir, 'resolvers') const keysDir = join(baseDir, 'keys') - -const contractDir = join(__dirname, 'fixtures', 'tmp', 'debug-contract') +// we use this test to create a contract.json file for the node-client jwt test to use +const contractDir = join(baseDir, 'jwt') const debug = require('debug')('jsonql-contract:test:debug-contract') const generator = require('../index') test.after(t => { - fsx.removeSync(contractDir) + // fsx.removeSync(contractDir) }) test(`It should able to create contract with this resolverDir when using custom methods and enableAuth`, async t => { @@ -25,7 +25,6 @@ test(`It should able to create contract with this resolverDir when using custom resolverDir, contractDir, keysDir, - // enableSplitTask: true, returnAs: 'json', enableAuth: true, loginHandlerName: 'customLogin', -- Gitee From 3116ac1fff5228c84cc9a54ff27aa785dcefcf21 Mon Sep 17 00:00:00 2001 From: joelchu Date: Wed, 6 Nov 2019 16:52:40 +0800 Subject: [PATCH 3/6] reenable the jwt test with static contract.json --- .../tests/fixtures/jwt/contract.json | 154 ++++++++++++++++++ .../tests/{jwt._test_.js => jwt.test.js} | 9 +- 2 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 packages/node-client/tests/fixtures/jwt/contract.json rename packages/node-client/tests/{jwt._test_.js => jwt.test.js} (89%) diff --git a/packages/node-client/tests/fixtures/jwt/contract.json b/packages/node-client/tests/fixtures/jwt/contract.json new file mode 100644 index 00000000..94270e3a --- /dev/null +++ b/packages/node-client/tests/fixtures/jwt/contract.json @@ -0,0 +1,154 @@ +{ + "query": { + "getUser": { + "file": "/home/joel/projects/open-source/jsonql/packages/node-client/tests/fixtures/resolvers/query/get-user.js", + "description": "get user call", + "params": [ + { + "type": [ + "number" + ], + "name": "id", + "description": "user id" + } + ], + "returns": [ + { + "type": [ + "object", + "string" + ], + "description": "user object on ok" + } + ] + } + }, + "mutation": { + "sendUser": { + "file": "/home/joel/projects/open-source/jsonql/packages/node-client/tests/fixtures/resolvers/mutation/send-user.js", + "description": false, + "params": [ + { + "type": [ + "object" + ], + "name": "payload" + }, + { + "type": [ + "object" + ], + "name": "condition" + } + ], + "returns": [ + { + "type": [ + "object" + ], + "description": "two merge together" + } + ] + } + }, + "auth": { + "customLogin": { + "file": "/home/joel/projects/open-source/jsonql/packages/node-client/tests/fixtures/resolvers/auth/custom-login.js", + "description": "custom login method for testing with jwt modules", + "params": [ + { + "type": [ + "string" + ], + "name": "username", + "description": "username" + }, + { + "type": [ + "string" + ], + "name": "password", + "description": "password" + } + ], + "returns": [ + { + "type": [ + "object" + ], + "description": "payload or throw" + } + ] + }, + "customValidator": { + "file": "/home/joel/projects/open-source/jsonql/packages/node-client/tests/fixtures/resolvers/auth/custom-validator.js", + "description": "result", + "params": [ + { + "type": [ + "any" + ], + "name": "userdata", + "description": "from last processor" + } + ], + "returns": [ + { + "type": [ + "any" + ], + "description": "userdata" + } + ] + } + }, + "timestamp": 1573028878, + "sourceType": "script", + "socket": { + "gateway": { + "namespace": "jsonql/public", + "public": true, + "file": "/home/joel/projects/open-source/jsonql/packages/node-client/tests/fixtures/resolvers/socket/public/gateway.js", + "description": "tell the gateway what you want and wait for the broadcast", + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "a message" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "the first will just say, hang on" + } + ] + }, + "secretChat": { + "namespace": "jsonql/private", + "file": "/home/joel/projects/open-source/jsonql/packages/node-client/tests/fixtures/resolvers/socket/secret-chat.js", + "description": "for testing the private socket interface", + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "something you want to say" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "my reply" + } + ] + } + } +} diff --git a/packages/node-client/tests/jwt._test_.js b/packages/node-client/tests/jwt.test.js similarity index 89% rename from packages/node-client/tests/jwt._test_.js rename to packages/node-client/tests/jwt.test.js index 1ed43f4a..55a3b79b 100644 --- a/packages/node-client/tests/jwt._test_.js +++ b/packages/node-client/tests/jwt.test.js @@ -7,16 +7,17 @@ const server = require('./fixtures/server-with-auth') const nodeClient = require('../index') const { contractKey, users } = require('./fixtures/options') -const contractDir = join(__dirname, 'fixtures', 'tmp', 'jwt') +const contractDir = join(__dirname, 'fixtures', 'jwt') const keysDir = join(__dirname, 'fixtures', 'keys') const port = 8890; const username = 'joel'; test.before(async t => { // make sure the folder is clean - fsx.removeSync(contractDir) - + // fsx.removeSync(contractDir) + const contract = fsx.readJsonSync(join(contractDir, 'public.json')) const { stop } = await server(port, { + contract, contractDir, keysDir, // enableAuth: true, @@ -38,7 +39,7 @@ test.before(async t => { }) test.after(() => { - fsx.removeSync(contractDir) + // fsx.removeSync(contractDir) }) test.serial("It should able to login and received a token", async t => { -- Gitee From 8d4ebe3b0893a9a1d1fde53b994a7f47f3f038fa Mon Sep 17 00:00:00 2001 From: joelchu Date: Wed, 6 Nov 2019 16:53:44 +0800 Subject: [PATCH 4/6] jwt test if we don't need to call the generator just leave it for now --- .../tests/fixtures/jwt/public-contract.json | 131 ++++++++++++++++++ packages/node-client/tests/jwt.test.js | 2 +- 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 packages/node-client/tests/fixtures/jwt/public-contract.json diff --git a/packages/node-client/tests/fixtures/jwt/public-contract.json b/packages/node-client/tests/fixtures/jwt/public-contract.json new file mode 100644 index 00000000..e2c06280 --- /dev/null +++ b/packages/node-client/tests/fixtures/jwt/public-contract.json @@ -0,0 +1,131 @@ +{ + "query": { + "helloWorld": { + "params": [], + "returns": [ + { + "type": "string", + "description": "stock message" + } + ] + }, + "getUser": { + "params": [ + { + "type": [ + "number" + ], + "name": "id", + "description": "user id" + } + ], + "returns": [ + { + "type": [ + "object", + "string" + ], + "description": "user object on ok" + } + ] + } + }, + "mutation": { + "sendUser": { + "params": [ + { + "type": [ + "object" + ], + "name": "payload" + }, + { + "type": [ + "object" + ], + "name": "condition" + } + ], + "returns": [ + { + "type": [ + "object" + ], + "description": "two merge together" + } + ] + } + }, + "auth": { + "customLogin": { + "params": [ + { + "type": [ + "string" + ], + "name": "username", + "description": "username" + }, + { + "type": [ + "string" + ], + "name": "password", + "description": "password" + } + ], + "returns": [ + { + "type": [ + "object" + ], + "description": "payload or throw" + } + ] + } + }, + "timestamp": 1573028878, + "socket": { + "gateway": { + "namespace": "jsonql/public", + "public": true, + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "a message" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "the first will just say, hang on" + } + ] + }, + "secretChat": { + "namespace": "jsonql/private", + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "something you want to say" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "my reply" + } + ] + } + } +} diff --git a/packages/node-client/tests/jwt.test.js b/packages/node-client/tests/jwt.test.js index 55a3b79b..a85710e1 100644 --- a/packages/node-client/tests/jwt.test.js +++ b/packages/node-client/tests/jwt.test.js @@ -15,7 +15,7 @@ const username = 'joel'; test.before(async t => { // make sure the folder is clean // fsx.removeSync(contractDir) - const contract = fsx.readJsonSync(join(contractDir, 'public.json')) + const contract = fsx.readJsonSync(join(contractDir, 'contract.json')) const { stop } = await server(port, { contract, contractDir, -- Gitee From 87158138b4cf3d48a5ec936e18dccd5de80ff1a6 Mon Sep 17 00:00:00 2001 From: joelchu Date: Wed, 6 Nov 2019 21:20:32 +0800 Subject: [PATCH 5/6] socket test completed --- packages/node-client/tests/socket.test.js | 38 ++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/node-client/tests/socket.test.js b/packages/node-client/tests/socket.test.js index b97bca66..c7a14b34 100644 --- a/packages/node-client/tests/socket.test.js +++ b/packages/node-client/tests/socket.test.js @@ -4,6 +4,9 @@ const debug = require('debug')('jsonql-node-client:test:socket') const serverWithSocket = require('./fixtures/server-with-socket') const nodeClient = require('../index') +const { loginToken, token } = require('./fixtures/options') + + test.before(async t => { const { stop, config, port } = await serverWithSocket() const opts = Object.assign({ @@ -42,4 +45,37 @@ test.cb(`It should able to connect to the public socket interface`, t => { }) -test.todo(`It should able to connect to the private interface after login`) +test.cb.only(`It should able to connect to the private interface after login`, t => { + t.plan(4) + const client = t.context.client; + const socket = client.socket + // first try to connect to the private interface and should not be able to connect + socket.secretChat('calling') + .catch(err => { + debug(err) + t.pass() + // now try to call the auth + client.login(loginToken) + .then(result => { + debug('login result', result) + }) + }) + // this event will get fire first + socket.secretChat.onError = function(err) { + debug(err) + t.pass() + } + + socket.onLogin = function(result) { + t.pass() + // try again + socket.secretChat('try again') + + socket.secretChat.onResult = function(result) { + t.is(result, 'got it') + t.end() + } + } + + +}) -- Gitee From f19626e319ccf25d7078ee8fc36f44ec88b4b925 Mon Sep 17 00:00:00 2001 From: joelchu Date: Wed, 6 Nov 2019 21:23:05 +0800 Subject: [PATCH 6/6] jsonql-node-client to v1.2.0 --- packages/koa/package.json | 2 +- packages/node-client/tests/socket.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/koa/package.json b/packages/koa/package.json index 51644ebc..9b50b3e2 100644 --- a/packages/koa/package.json +++ b/packages/koa/package.json @@ -42,7 +42,7 @@ "WebSocket", "API" ], - "author": "Joel Chu ", + "author": "to1source ", "license": "ISC", "repository": { "type": "git", diff --git a/packages/node-client/tests/socket.test.js b/packages/node-client/tests/socket.test.js index c7a14b34..e12fea04 100644 --- a/packages/node-client/tests/socket.test.js +++ b/packages/node-client/tests/socket.test.js @@ -70,7 +70,7 @@ test.cb.only(`It should able to connect to the private interface after login`, t t.pass() // try again socket.secretChat('try again') - + // @TODO need to check why the promise is not resolved socket.secretChat.onResult = function(result) { t.is(result, 'got it') t.end() -- Gitee