diff --git a/packages/resolver/index.js b/packages/resolver/index.js index 4cd1a01b973f17409ee051c025c7ca5f05c0f6ea..f68d16dcf18d2e1f28c7a79ee70a4b6c7a3a9f72 100644 --- a/packages/resolver/index.js +++ b/packages/resolver/index.js @@ -8,9 +8,11 @@ const { } = require('./src/resolve-methods') const { getLocalValidator, - handleAuthMethods, - getSocketAuthInterceptor + handleAuthMethods } = require('./src/handle-auth-methods') +// added in 1.2.0 +const { getSocketInterceptor } = require('./src/get-socket-interceptor') + // for 1.0.0 release (first on 0.9.8) // we also export the node client generator parts // when the middleware is use with socket server @@ -28,16 +30,16 @@ const { ctxErrorHandler } = require('./src/utils') - // @TODO use the same for the jsonql-ws-server as well module.exports = { searchResolvers, validateAndCall, getResolver, - + handleAuthMethods, getLocalValidator, - getSocketAuthInterceptor, + + getSocketInterceptor, executeResolver, resolverRenderHandler, diff --git a/packages/resolver/package.json b/packages/resolver/package.json index 8193f95c0d2e7344395f87ad8da003042442c840..ee439f186709b9e19c905af7b4fc3c0acf69f655 100644 --- a/packages/resolver/package.json +++ b/packages/resolver/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-resolver", - "version": "1.1.3", + "version": "1.2.0", "description": "This is NOT for general use, please do not install it directly. This module is part of the jsonql tools supporting modules.", "main": "index.js", "files": [ @@ -15,7 +15,12 @@ "test:clients": "DEBUG=jsonql* ava ./tests/clients.test.js", "test:throw": "DEBUG=jsonql-resolver* ava ./tests/throw.test.js", "test:es": "DEBUG=jsonql-resolver* ava ./tests/es.test.js", - "contract": "DEBUG=jsonql-contract* jsonql-contract create ./tests/fixtures/resolvers ./tests/fixtures/contract" + "contract:socket": "npm run contract:socket:standard && npm run contract:socket:public", + "contract:socket:standard": "IS_PUBLIC=false jsonql-contract config ./tests/fixtures/socket-auth-config.js", + "contract:socket:public": "IS_PUBLIC=true jsonql-contract config ./tests/fixtures/socket-auth-config.js", + "contract": "npm run contract:standard && npm run contract:public", + "contract:standard": "DEBUG=jsonql-contract* jsonql-contract create ./tests/fixtures/resolvers ./tests/fixtures/contract", + "contract:public": "DEBUG=jsonql-contract* jsonql-contract create --public=1 ./tests/fixtures/resolvers ./tests/fixtures/contract" }, "keywords": [ "jsonql", @@ -29,17 +34,17 @@ }, "dependencies": { "debug": "^4.1.1", - "jsonql-constants": "^2.0.2", + "jsonql-constants": "^2.0.6", "jsonql-errors": "^1.2.1", "jsonql-jwt": "^1.3.10", "jsonql-node-client": "^1.3.1", "jsonql-params-validator": "^1.6.1", - "jsonql-utils": "^1.2.0", + "jsonql-utils": "^1.2.4", "lodash.merge": "^4.6.2" }, "devDependencies": { "ava": "^3.5.0", - "jsonql-contract": "^1.8.10", + "jsonql-contract": "^1.9.0", "jsonql-koa": "^1.6.2", "server-io-core": "^1.3.3" }, diff --git a/packages/resolver/src/get-socket-interceptor.js b/packages/resolver/src/get-socket-interceptor.js new file mode 100644 index 0000000000000000000000000000000000000000..c14ece39f111011573a38e5e6374c9f63b131ecb --- /dev/null +++ b/packages/resolver/src/get-socket-interceptor.js @@ -0,0 +1,148 @@ +// this will be a separate extened methods for the socket to get the interceptors +// login, logout, disconnect, validator etc +//////////////////////////////////////////// +// SOCKET AUTH // +//////////////////////////////////////////// + +const fsx = require('fs-extra') +const { + SOCKET_AUTH_NAME, + LOGIN_EVENT_NAME, + SA_LOGIN_EVENT_NAME, + LOGOUT_EVENT_NAME, + DISCONNECT_EVENT_NAME, + VALIDATOR_FN_NAME, + LOGIN_FN_NAME_PROP_KEY, + LOGOUT_FN_NAME_PROP_KEY, + DISCONNECT_FN_NAME_PROP_KEY +} = require('jsonql-constants') +const { validateAsync } = require('jsonql-params-validator') +const debug = require('debug')('jsonql-resolver:get-socket-interceptor') +const { getFnBySourceType } = require('./search-resolvers') + +/** + * This is the first place to look to find the path to the interceptor + * @param {object} contract + * @return {object} fail will be an empty object see above + */ +function extractSocketAuthPart(contract) { + try { + return contract[SOCKET_AUTH_NAME] + } catch(e) { + return {} + } +} + +/** + * Another wrapper method to make less duplication + * to find the file part from the contract + * @param {object} searchPart + * @param {object} opts + * @return {string} the file part + */ +function getInterceptorFileFn(searchPart, opts) { + /** + * @param {string} fnName + * @return {string} the file part + */ + return function(fnName) { + const name = opts[fnName] + debug('funtion name', fnName, name) + return searchPart[name] && searchPart[name].file ? searchPart[name] : null + } +} + +/** + * Construct the search path to find interceptors + * This is actually a fall back option if the contract didn't provide it + * @param {string} evtName type of event + * @param {object} opts configuration + * @return {object} the interceptor info from contract + */ +function getInterceptorInfo(evtName, opts) { + const { contract } = opts + const searchPart = extractSocketAuthPart(contract) + const getFileFn = getInterceptorFileFn(searchPart, opts) + + debug('searchPart', searchPart) + + switch(evtName) { + case LOGIN_EVENT_NAME: + case SA_LOGIN_EVENT_NAME: + return getFileFn(LOGIN_FN_NAME_PROP_KEY) + case LOGOUT_EVENT_NAME: + return getFileFn(LOGOUT_FN_NAME_PROP_KEY) + /* // not implement at the moment @1.2.0 + case CONNECTED_EVENT_NAME: + return getFileFn(INIT_CONNECTION_FN_NAME_PROP_KEY) + case SWITCH_USER_EVENT_NAME: + return getFileFn(SWITCH_USER_FN_NAME_PROP_KEY) + */ + // @TBC this should be an internal event handler + case DISCONNECT_EVENT_NAME: + return getFileFn(DISCONNECT_FN_NAME_PROP_KEY) + + // this one is a special case because it's an internal method + case VALIDATOR_FN_NAME: + return getFileFn(VALIDATOR_FN_NAME) + default: + // @TODO this feature is not going to implement until a few versions later + throw new Error(`"${evtName}" is not implement!`) + } +} + + +/** + * For socket to add auth interceptors + * @param {string} evtName of the interceptor + * @param {object} opts configuration + * @return {function} the interceptor method to handle the callback + */ +function getSocketAuthInterceptorFn(evtName, opts) { + let info = getInterceptorInfo(evtName, opts) + if (info) { + const pathToResolver = info.file + + if (pathToResolver && fsx.existsSync(pathToResolver)) { + const { params } = info + const { sourceType } = opts.contract + const fn = getFnBySourceType(pathToResolver, sourceType) + + return [ fn, params ] + } + } + return false +} + +/** + * Putting all the socket interceptor method together in one + * @param {string} evtName event name + * @param {object} opts configuration + * @return {function} to execute and get validate when pass arguments + */ +function getSocketInterceptor(evtName, opts) { + const result = getSocketAuthInterceptorFn(evtName, opts) + if (result) { + const [ fn, params ] = result + /** + * @param {array} args + */ + return (...args) => { + + return validateAsync(args, params) + .then(_args => { + try { + return Reflect.apply(fn, null, _args) + } catch(e) { + throw new JsonqlResolverAppError(evtName, e) + } + }) + } + } +} + + +module.exports = { + getSocketInterceptor, + getSocketAuthInterceptorFn +} diff --git a/packages/resolver/src/handle-auth-methods.js b/packages/resolver/src/handle-auth-methods.js index 0fa14a69cedf36369c5107c8584bdb275b7b9d8f..e110c664146a516ca9c34076bbbc8bce9113ee13 100644 --- a/packages/resolver/src/handle-auth-methods.js +++ b/packages/resolver/src/handle-auth-methods.js @@ -9,7 +9,7 @@ const { SA_LOGIN_EVENT_NAME, CONNECTED_EVENT_NAME, DISCONNECT_EVENT_NAME, - + LOGIN_FN_NAME_PROP_KEY, LOGOUT_FN_NAME_PROP_KEY, INIT_CONNECTION_FN_NAME_PROP_KEY, @@ -102,126 +102,10 @@ const handleAuthMethods = async function(ctx, payload, opts, contract) { } } -//////////////////////////////////////////// -// SOCKET AUTH // -//////////////////////////////////////////// - -/** - * This is the first place to look to find the path to the interceptor - * @param {object} contract - * @return {object} fail will be an empty object see above - */ -function extractSocketAuthPart(contract) { - try { - return contract[SOCKET_NAME][AUTH_TYPE] - } catch(e) { - return {} - } -} - -/** - * Another wrapper method to make less duplication - * to find the file part from the contract - * @param {object} searchPart - * @param {object} opts - * @return {string} the file part - */ -function getInterceptorFileFn(searchPart, opts) { - /** - * @param {string} evtName - * @return {string} the file part - */ - return function(evtName) { - const name = opts[evtName] - return searchPart[name] && searchPart[name].file ? searchPart[name] : null - } -} - -/** - * Construct the search path to find interceptors - * This is actually a fall back option if the contract didn't provide it - * @param {string} evtName type of event - * @param {object} opts configuration - * @return {object} the interceptor info from contract - */ -function getInterceptorInfo(evtName, opts) { - const { contract } = opts - const searchPart = extractSocketAuthPart(contract) - const getFileFn = getInterceptorFileFn(searchPart, opts) - - switch(evtName) { - case LOGIN_EVENT_NAME: - case SA_LOGIN_EVENT_NAME: - return getFileFn(LOGIN_FN_NAME_PROP_KEY) - case LOGOUT_EVENT_NAME: - return getFileFn(LOGOUT_FN_NAME_PROP_KEY) - case CONNECTED_EVENT_NAME: - return getFileFn(INIT_CONNECTION_FN_NAME_PROP_KEY) - // @TBC this should be an internal event handler - case DISCONNECT_EVENT_NAME: - return getFileFn(DISCONNECT_FN_NAME_PROP_KEY) - case SWITCH_USER_EVENT_NAME: - return getFileFn(SWITCH_USER_FN_NAME_PROP_KEY) - default: - // @TODO this feature is not going to implement until a few versions later - throw new Error(`"${evtName}" is not implement!`) - } -} - -/** - * For socket to add auth interceptors - * @param {string} evtName of the interceptor - * @param {object} opts configuration - * @return {function} the interceptor method to handle the callback - */ -function getSocketAuthInterceptorFn(evtName, opts) { - let info = getInterceptorInfo(evtName, opts) - if (info) { - const pathToResolver = info.file - - if (pathToResolver && fsx.existsSync(pathToResolver)) { - const { params } = info - const { sourceType } = opts.contract - const fn = getFnBySourceType(pathToResolver, sourceType) - - return [ fn, params ] - } - } - return false -} - -/** - * Putting all the socket interceptor method together in one - * @param {string} evtName event name - * @param {object} opts configuration - * @return {function} to execute and get validate when pass arguments - */ -function getSocketAuthInterceptor(evtName, opts) { - const result = getSocketAuthInterceptorFn(evtName, opts) - if (result) { - const [ fn, params ] = result - /** - * @param {array} args - */ - return (...args) => { - - return validateAsync(args, params) - .then(_args => { - try { - return Reflect.apply(fn, null, _args) - } catch(e) { - throw new JsonqlResolverAppError(evtName, e) - } - }) - } - } -} module.exports = { getLocalValidator, - handleAuthMethods, - - getSocketAuthInterceptor + handleAuthMethods } diff --git a/packages/resolver/tests/es.test.js b/packages/resolver/tests/es.test.js index de8da991b54aabc247c3f6020470eb21da71118c..9cc86c36e44da4d9a1ff8bb3e1dc230e72e7b40c 100644 --- a/packages/resolver/tests/es.test.js +++ b/packages/resolver/tests/es.test.js @@ -1,6 +1,7 @@ // test the ES module import const test = require('ava') const { join } = require('path') +const fsx = require('fs-extra') const debug = require('debug')('jsonql-resolver:test:es') const contractApi = require('jsonql-contract') // method to test @@ -21,7 +22,7 @@ test.before(async t => { }) // after test test.after(t => { - // todo + fsx.removeSync(contractDir) }) // start testing diff --git a/packages/resolver/tests/fixtures/contract/contract.json b/packages/resolver/tests/fixtures/contract/contract.json index bcc5f3dbb48f559d43616b99a6575d7f26167cc0..9682401eb4ca367466ae7339f00860c4a218d313 100644 --- a/packages/resolver/tests/fixtures/contract/contract.json +++ b/packages/resolver/tests/fixtures/contract/contract.json @@ -99,6 +99,28 @@ }, "mutation": {}, "auth": {}, - "timestamp": 1583286535, - "sourceType": "script" + "timestamp": 1584618687, + "sourceType": "script", + "socket": { + "sendSomething": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/socket/send-something.js", + "description": "Just a dummy socket interface", + "params": [ + { + "type": [ + "string" + ], + "name": "msg" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "msg" + } + ] + } + } } diff --git a/packages/resolver/tests/fixtures/contract/es/contract.json b/packages/resolver/tests/fixtures/contract/es/contract.json deleted file mode 100644 index 9a8c8055e4901787fd6fb84b7b1b75fb0743a305..0000000000000000000000000000000000000000 --- a/packages/resolver/tests/fixtures/contract/es/contract.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "query": { - "getSomething": { - "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/es/query/get-something.js", - "description": false, - "params": [], - "returns": [ - { - "type": [ - "array" - ], - "description": "list of something" - } - ] - } - }, - "mutation": { - "saveSomething": { - "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/es/mutation/save-something.js", - "description": false, - "params": [ - { - "type": [ - "object" - ], - "name": "payload" - }, - { - "type": [ - "object" - ], - "name": "condition" - } - ], - "returns": [ - { - "type": [ - "boolean" - ], - "description": "true on OK" - } - ] - } - }, - "auth": {}, - "timestamp": 1584287161, - "sourceType": "module" -} diff --git a/packages/resolver/tests/fixtures/contract/public-contract.json b/packages/resolver/tests/fixtures/contract/public-contract.json new file mode 100644 index 0000000000000000000000000000000000000000..9996eafbfb1008de734832da7106694994747762 --- /dev/null +++ b/packages/resolver/tests/fixtures/contract/public-contract.json @@ -0,0 +1,143 @@ +{ + "query": { + "helloWorld": { + "description": "This is the stock resolver for testing purpose", + "params": [], + "returns": [ + { + "type": [ + "string" + ], + "description": "stock message" + } + ] + }, + "anotherService": { + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "input" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "msg output" + } + ] + }, + "callMsService": { + "description": "pass a txt value then process else where", + "params": [ + { + "type": [ + "string" + ], + "name": "txt" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "transformted txt" + } + ] + }, + "fakeLogin": { + "description": "Fake the login and throw a JsonqlAuthorisationError error to see if it catches it", + "params": [ + { + "type": [ + "string" + ], + "name": "name", + "description": "username" + } + ], + "returns": [ + { + "type": [ + "object" + ], + "description": "userdata" + } + ] + }, + "getSomething": { + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "name", + "description": "pass a name" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "name with prefix" + } + ] + }, + "toFail": { + "description": "This resolver will fail and throw an error", + "params": [], + "returns": [ + { + "type": [ + "any" + ], + "description": "a variable that didn't exist" + } + ] + } + }, + "mutation": {}, + "auth": {}, + "timestamp": 1584618687, + "socket": { + "sendSomething": { + "description": "Just a dummy socket interface", + "params": [ + { + "type": [ + "string" + ], + "name": "msg" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "msg" + } + ] + }, + "helloWorld": { + "description": "This is the stock resolver for testing purpose", + "params": [], + "returns": [ + { + "type": [ + "string" + ], + "description": "stock message" + } + ] + } + } +} diff --git a/packages/resolver/tests/fixtures/contract/socket/contract.json b/packages/resolver/tests/fixtures/contract/socket/contract.json new file mode 100644 index 0000000000000000000000000000000000000000..fdd392ce6536e1416efc780b957f9bd2d5b023f0 --- /dev/null +++ b/packages/resolver/tests/fixtures/contract/socket/contract.json @@ -0,0 +1,167 @@ +{ + "query": { + "anotherService": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/query/another-service.js", + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "input" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "msg output" + } + ] + }, + "callMsService": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/query/call-ms-service.js", + "description": "pass a txt value then process else where", + "params": [ + { + "type": [ + "string" + ], + "name": "txt" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "transformted txt" + } + ] + }, + "fakeLogin": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/query/fake-login.js", + "description": "Fake the login and throw a JsonqlAuthorisationError error to see if it catches it", + "params": [ + { + "type": [ + "string" + ], + "name": "name", + "description": "username" + } + ], + "returns": [ + { + "type": [ + "object" + ], + "description": "userdata" + } + ] + }, + "getSomething": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/query/get-something.js", + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "name", + "description": "pass a name" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "name with prefix" + } + ] + }, + "toFail": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/query/to-fail.js", + "description": "This resolver will fail and throw an error", + "params": [], + "returns": [ + { + "type": [ + "any" + ], + "description": "a variable that didn't exist" + } + ] + } + }, + "mutation": {}, + "auth": {}, + "timestamp": 1584619274, + "sourceType": "script", + "socket": { + "sendSomething": { + "namespace": "jsonql/private", + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/socket/send-something.js", + "description": "Just a dummy socket interface", + "params": [ + { + "type": [ + "string" + ], + "name": "msg" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "msg" + } + ] + } + }, + "socket-auth": { + "login": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/socket/auth/login.js", + "description": "login interceptor", + "returns": [ + { + "type": [ + "boolean" + ] + } + ], + "params": [ + { + "type": [ + "array." + ], + "name": "args" + } + ] + }, + "validator": { + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/socket/auth/validator/index.js", + "description": "A dummy validator interceptor", + "returns": false, + "params": [ + { + "type": [ + "object" + ], + "name": "req" + }, + { + "type": [ + "boolean" + ], + "name": "" + } + ] + } + } +} diff --git a/packages/resolver/tests/fixtures/contract/socket/public-contract.json b/packages/resolver/tests/fixtures/contract/socket/public-contract.json new file mode 100644 index 0000000000000000000000000000000000000000..fd61fef473ce629f7d98476c8db325affc9da18f --- /dev/null +++ b/packages/resolver/tests/fixtures/contract/socket/public-contract.json @@ -0,0 +1,165 @@ +{ + "query": { + "helloWorld": { + "description": "This is the stock resolver for testing purpose", + "params": [], + "returns": [ + { + "type": [ + "string" + ], + "description": "stock message" + } + ] + }, + "anotherService": { + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "msg", + "description": "input" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "msg output" + } + ] + }, + "callMsService": { + "description": "pass a txt value then process else where", + "params": [ + { + "type": [ + "string" + ], + "name": "txt" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "transformted txt" + } + ] + }, + "fakeLogin": { + "description": "Fake the login and throw a JsonqlAuthorisationError error to see if it catches it", + "params": [ + { + "type": [ + "string" + ], + "name": "name", + "description": "username" + } + ], + "returns": [ + { + "type": [ + "object" + ], + "description": "userdata" + } + ] + }, + "getSomething": { + "description": false, + "params": [ + { + "type": [ + "string" + ], + "name": "name", + "description": "pass a name" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "name with prefix" + } + ] + }, + "toFail": { + "description": "This resolver will fail and throw an error", + "params": [], + "returns": [ + { + "type": [ + "any" + ], + "description": "a variable that didn't exist" + } + ] + } + }, + "mutation": {}, + "auth": {}, + "timestamp": 1584619274, + "socket": { + "sendSomething": { + "namespace": "jsonql/private", + "description": "Just a dummy socket interface", + "params": [ + { + "type": [ + "string" + ], + "name": "msg" + } + ], + "returns": [ + { + "type": [ + "string" + ], + "description": "msg" + } + ] + }, + "helloWorld": { + "description": "This is the stock resolver for testing purpose", + "params": [], + "returns": [ + { + "type": [ + "string" + ], + "description": "stock message" + } + ] + } + }, + "socket-auth": { + "login": { + "description": "login interceptor", + "returns": [ + { + "type": [ + "boolean" + ] + } + ], + "params": [ + { + "type": [ + "array." + ], + "name": "args" + } + ], + "file": "/home/joel/projects/open-source/jsonql/packages/resolver/tests/fixtures/resolvers/socket/auth/login.js" + } + } +} diff --git a/packages/resolver/tests/fixtures/resolvers/socket/auth/login.js b/packages/resolver/tests/fixtures/resolvers/socket/auth/login.js new file mode 100644 index 0000000000000000000000000000000000000000..37f11db04522d7bfe4aa5eea09c17709bd01fb23 --- /dev/null +++ b/packages/resolver/tests/fixtures/resolvers/socket/auth/login.js @@ -0,0 +1,8 @@ +/** + * login interceptor + * @param {array.} args + * @return {boolean} + */ +module.exports = function login(...args) { + return !!args.length +} diff --git a/packages/resolver/tests/fixtures/resolvers/socket/auth/validator/index.js b/packages/resolver/tests/fixtures/resolvers/socket/auth/validator/index.js new file mode 100644 index 0000000000000000000000000000000000000000..f328df060887f774f2b5e53c7fa2721be1282397 --- /dev/null +++ b/packages/resolver/tests/fixtures/resolvers/socket/auth/validator/index.js @@ -0,0 +1,8 @@ +/** + * A dummy validator interceptor + * @param {object} req + * @param {boolean} + */ +module.exports = function validator(req) { + return req.headers ? true : false +} diff --git a/packages/resolver/tests/fixtures/resolvers/socket/send-something.js b/packages/resolver/tests/fixtures/resolvers/socket/send-something.js new file mode 100644 index 0000000000000000000000000000000000000000..cb0c51ad9111ad6cf63690cb5973c712327ad28f --- /dev/null +++ b/packages/resolver/tests/fixtures/resolvers/socket/send-something.js @@ -0,0 +1,8 @@ +/** + * Just a dummy socket interface + * @param {string} msg + * @return {string} msg + */ +module.exports = function(msg) { + return 'whatever you said is: ' + msg +} diff --git a/packages/resolver/tests/fixtures/socket-auth-config.js b/packages/resolver/tests/fixtures/socket-auth-config.js new file mode 100644 index 0000000000000000000000000000000000000000..0c36e949bd3ab41fd30ea0c38091538c89f36e28 --- /dev/null +++ b/packages/resolver/tests/fixtures/socket-auth-config.js @@ -0,0 +1,10 @@ +// create config file to generate the auth contract +const { join } = require('path') +const baseDir = join(__dirname) + +module.exports = { + inDir: join(baseDir, 'resolvers'), + outDir: join(baseDir, 'contract', 'socket'), + enableAuth: true, + public: process.env.IS_PUBLIC === 'true' +} diff --git a/packages/resolver/tests/socket.test.js b/packages/resolver/tests/socket.test.js index c408ea7d1269756af283d10495fce2a1058cb3d4..668916a8da54e39c52a642e85b57b8d4f4abc221 100644 --- a/packages/resolver/tests/socket.test.js +++ b/packages/resolver/tests/socket.test.js @@ -1,11 +1,31 @@ +// testing the get socket auth interceptor const test = require('ava') -const { - getSocketAuthInterceptor -} = require('../index') +const fsx = require('fs-extra') +const { join } = require('path') +const debug = require('debug')('jsonql-resolver:test:socket') +const { LOGIN_EVENT_NAME, LOGIN_FN_NAME_PROP_KEY } = require('jsonql-constants') +const { getSocketInterceptor } = require('../index') +const baseDir = join(__dirname, 'fixtures', 'contract', 'socket') + +test.before(t => { + const contract = fsx.readJsonSync(join(baseDir, 'contract.json')) + t.context.config = { + contract, + [LOGIN_FN_NAME_PROP_KEY]: 'login' + } +}) test(`Just stub it here for the time being`, t => { - - t.true(typeof getSocketAuthInterceptor === 'function') + + t.true(typeof getSocketInterceptor === 'function') }) -test.todo(`It should able to find socket auth interceptors`) \ No newline at end of file +test(`It should able to find socket auth interceptors`, t => { + + const fn = getSocketInterceptor(LOGIN_EVENT_NAME, t.context.config) + + debug('auth interceptor', fn) + + t.true(typeof fn === 'function') + +})