This is an automated email from the ASF dual-hosted git repository. jamesthomas pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-client-js.git
The following commit(s) were added to refs/heads/master by this push: new 84ec1c9 style: add .editorconfig, eslint and reformat js file (#63) 84ec1c9 is described below commit 84ec1c9eed73977f174846349e0ecabcf1c0ff14 Author: RĂ³bert Kiss <ert7...@gmail.com> AuthorDate: Tue Apr 3 10:22:47 2018 +0200 style: add .editorconfig, eslint and reformat js file (#63) * style: add .editorconfig and reformat js file use 2 space indent in every file * style: add eslint and reformat code to apply style * ci: add 'npm run lint' to travis and as pre-commit hook * ci: move 'npm run lint' -> tools/travis/build.sh * doc: Add standard code style badge * style: Apply standard style rules * doc: replace eslint part in contributing.md * build: Add lint script to package.json * update: type definitions * add backward compatibility of client options * change method names * delete "deprecated" mark from main.d.ts file * fix integration test options --- .editorconfig | 13 ++ CONTRIBUTING.md | 1 + README.md | 7 +- lib/actions.js | 18 +-- lib/activations.js | 8 +- lib/base_operation.js | 6 +- lib/client.js | 131 +++++++++--------- lib/feeds.js | 30 ++--- lib/main.d.ts | 2 +- lib/names.js | 26 ++-- lib/openwhisk_error.js | 18 +-- lib/packages.js | 2 +- lib/resources.js | 42 +++--- lib/routes.js | 54 ++++---- lib/rules.js | 17 ++- package.json | 15 ++- test/integration/actions.test.js | 92 ++++++++----- test/integration/activations.test.js | 2 +- test/integration/feeds.test.js | 20 +-- test/integration/namespaces.test.js | 4 +- test/integration/packages.test.js | 14 +- test/integration/routes.test.js | 9 +- test/integration/rules.test.js | 38 ++++-- test/integration/triggers.test.js | 18 +-- test/integration/utils.js | 26 ++-- test/unit/actions.test.js | 28 ++-- test/unit/activations.test.js | 56 ++++---- test/unit/base_operation.test.js | 86 ++++++------ test/unit/client.test.js | 108 +++++++++++---- test/unit/feeds.test.js | 200 +++++++++++++++------------ test/unit/names.test.js | 40 +++--- test/unit/namespaces.test.js | 2 +- test/unit/packages.test.js | 2 +- test/unit/resources.test.js | 32 ++--- test/unit/routes.test.js | 253 +++++++++++++++++++---------------- test/unit/rules.test.js | 64 +++++---- test/unit/triggers.test.js | 6 +- tools/travis/build.sh | 1 + 38 files changed, 845 insertions(+), 646 deletions(-) diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..6e87a00 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# Editor configuration, see http://editorconfig.org +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 2 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +max_line_length = off +trim_trailing_whitespace = false diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fcb1d0e..9167b53 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -65,3 +65,4 @@ code base. Some basic rules include: - all files must have the Apache license in the header. - all PRs must have passing builds for all operating systems. + - follow the [standard](https://standardjs.com) style rules. Linter run on CI and automatically as [pre-commit hook](http://githooks.com/). For automatic fixing run `npm run standard-fix`. diff --git a/README.md b/README.md index 3ce22fe..280440f 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![Build Status](https://travis-ci.org/apache/incubator-openwhisk-client-js.svg?branch=master)](https://travis-ci.org/apache/incubator-openwhisk-client-js) [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0) [![codecov](https://codecov.io/gh/apache/incubator-openwhisk-client-js/branch/master/graph/badge.svg)](https://codecov.io/gh/apache/incubator-openwhisk-client-js) +[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) JavaScript client library for the [Apache OpenWhisk](https://github.com/apache/incubator-openwhisk) platform. Provides a wrapper around the [OpenWhisk APIs](https://github.com/apache/incubator-openwhisk/blob/fb001afa237476eda0c0f6494ee92702e5986538/core/controller/src/main/resources/apiv1swagger.json) (Swagger JSON). @@ -33,7 +34,9 @@ exports.main = action _All methods return a Promise resolved asynchronously with the results. Failures are available through the catch method._ ```javascript -ow.resource.operation().then(function () { // success! }).catch(function (err) { // failed! }) +ow.resource.operation() + .then(function () { /* success! */ }) + .catch(function (err) { /* failed! */ }) ``` Users can override default constructor parameters by passing in explicit options as shown in the example below. @@ -158,7 +161,7 @@ ow.actions.get(name).then(action => { ```javascript ow.actions.list() .then(actions => ow.actions.invoke(actions)) - .then(result => ...) + .then(result => { /* ... */ }) ``` ### list packages diff --git a/lib/actions.js b/lib/actions.js index 77a1a57..06f205d 100644 --- a/lib/actions.js +++ b/lib/actions.js @@ -22,10 +22,10 @@ class Actions extends Resources { } get (options) { - options = this.parse_options(options) + options = this.parseOptions(options) options.qs = this.qs(options, ['code']) - return this.operation_with_id('GET', options) + return this.operationWithId('GET', options) } invoke (options) { @@ -39,16 +39,16 @@ class Actions extends Resources { create (options) { options.qs = this.qs(options, ['overwrite']) - options.body = this.action_body(options) + options.body = this.actionBody(options) return super.create(options) } - action_body (options) { + actionBody (options) { if (!options.hasOwnProperty('action')) { throw new Error(messages.MISSING_ACTION_BODY_ERROR) } - const body = { exec: { kind: options.kind || 'nodejs:default', code: options.action } } + const body = {exec: {kind: options.kind || 'nodejs:default', code: options.action}} if (options.action instanceof Buffer) { body.exec.code = options.action.toString('base64') @@ -57,15 +57,15 @@ class Actions extends Resources { } if (typeof options.params === 'object') { - body.parameters = Object.keys(options.params).map(key => ({ key, value: options.params[key] })) + body.parameters = Object.keys(options.params).map(key => ({key, value: options.params[key]})) } - if(options.version){ - body.version = options.version; + if (options.version) { + body.version = options.version } if (options.limits) { - body.limits = options.limits; + body.limits = options.limits } if (typeof options.annotations === 'object') { diff --git a/lib/activations.js b/lib/activations.js index 9cd3fe1..055d479 100644 --- a/lib/activations.js +++ b/lib/activations.js @@ -27,13 +27,13 @@ class Activations extends BaseOperation { } activation (options, path) { - const id = this.get_activation(options) + const id = this.getActivation(options) const namespace = this.namespace(options) - const url_path = `namespaces/${namespace}/activations/${id}` + (path ? `/${path}` : '') - return this.client.request('GET', url_path) + const urlPath = `namespaces/${namespace}/activations/${id}` + (path ? `/${path}` : '') + return this.client.request('GET', urlPath) } - get_activation (options) { + getActivation (options) { if (typeof options === 'string') return options options = options || {} diff --git a/lib/base_operation.js b/lib/base_operation.js index d8be106..ebdb074 100644 --- a/lib/base_operation.js +++ b/lib/base_operation.js @@ -12,11 +12,11 @@ class BaseOperation { request (params) { const namespace = this.namespace(params.options) - const path = this.resource_path(namespace, params.id) + const path = this.resourcePath(namespace, params.id) return this.client.request(params.method, path, params.options) } - resource_path (namespace, id) { + resourcePath (namespace, id) { let path = `namespaces/${namespace}/${this.resource}` if (id) { @@ -35,7 +35,7 @@ class BaseOperation { return encodeURIComponent(this.client.options.namespace) } - return encodeURIComponent(names.default_namespace()) + return encodeURIComponent(names.defaultNamespace()) } qs (options, names) { diff --git a/lib/client.js b/lib/client.js index 73dfe4a..337fb22 100644 --- a/lib/client.js +++ b/lib/client.js @@ -17,75 +17,86 @@ const http = require('http') * */ const rp = opts => { - if (opts.qs) { - // we turn the qs struct into a query string over the url - let first = true - for (let key in opts.qs) { - const str = `${encodeURIComponent(key)}=${encodeURIComponent(opts.qs[key])}` - if (first) { - opts.url += `?${str}` - first = false - } else { - opts.url += `&${str}` - } - } + if (opts.qs) { + // we turn the qs struct into a query string over the url + let first = true + for (let key in opts.qs) { + const str = `${encodeURIComponent(key)}=${encodeURIComponent(opts.qs[key])}` + if (first) { + opts.url += `?${str}` + first = false + } else { + opts.url += `&${str}` + } } + } - // it appears that certain call paths from our code do not set the - // opts.json field to true; rp is apparently more resilient to - // this situation than needle - opts.json = true - - return needle(opts.method.toLowerCase(), // needle takes e.g. 'put' not 'PUT' - opts.url, - opts.body || opts.params, - opts) - .then(resp => { - if (resp.statusCode >= 400) { - // we turn >=400 statusCode responses into exceptions - const error = new Error(resp.body.error || resp.statusMessage) - error.statusCode = resp.statusCode // the http status code - error.options = opts // the code below requires access to the input opts - error.error = resp.body // the error body - throw error - } else { - // otherwise, the response body is the expected return value - return resp.body - } - }) + // it appears that certain call paths from our code do not set the + // opts.json field to true; rp is apparently more resilient to + // this situation than needle + opts.json = true + + return needle(opts.method.toLowerCase(), // needle takes e.g. 'put' not 'PUT' + opts.url, + opts.body || opts.params, + opts) + .then(resp => { + if (resp.statusCode >= 400) { + // we turn >=400 statusCode responses into exceptions + const error = new Error(resp.body.error || resp.statusMessage) + error.statusCode = resp.statusCode // the http status code + error.options = opts // the code below requires access to the input opts + error.error = resp.body // the error body + throw error + } else { + // otherwise, the response body is the expected return value + return resp.body + } + }) } class Client { + /** + * @constructor + * @param {Object} options - options of the Client + * @param {string} [options.api] + * @param {string} [options.api_key] + * @param {string} [options.apihost] + * @param {string} [options.namespace] + * @param {boolean} [options.ignore_certs] + * @param {string} [options.apigw_token] + * @param {string} [options.apigw_space_guid] + */ constructor (options) { - this.options = this.parse_options(options || {}) + this.options = this.parseOptions(options || {}) } - parse_options (options) { - const api_key = options.api_key || process.env['__OW_API_KEY'] - const ignore_certs = options.ignore_certs || false + parseOptions (options) { + const apiKey = options.api_key || process.env['__OW_API_KEY'] + const ignoreCerts = options.ignore_certs || false // if apihost is available, parse this into full API url const api = options.api || - this.url_from_apihost(options.apihost || process.env['__OW_API_HOST']) + this.urlFromApihost(options.apihost || process.env['__OW_API_HOST']) // optional tokens for API GW service - const apigw_token = options.apigw_token || process.env['__OW_APIGW_TOKEN'] - let apigw_space_guid = options.apigw_space_guid || process.env['__OW_APIGW_SPACE_GUID'] + const apigwToken = options.apigw_token || process.env['__OW_APIGW_TOKEN'] + let apigwSpaceGuid = options.apigw_space_guid || process.env['__OW_APIGW_SPACE_GUID'] // unless space is explicitly passed, default to using auth uuid. - if (apigw_token && !apigw_space_guid) { - apigw_space_guid = api_key.split(':')[0] + if (apigwToken && !apigwSpaceGuid) { + apigwSpaceGuid = apiKey.split(':')[0] } - if (!api_key) { + if (!apiKey) { throw new Error(`${messages.INVALID_OPTIONS_ERROR} Missing api_key parameter.`) } else if (!api) { throw new Error(`${messages.INVALID_OPTIONS_ERROR} Missing either api or apihost parameters.`) } - return {api_key, api, ignore_certs, namespace: options.namespace, apigw_token, apigw_space_guid} + return {apiKey: apiKey, api, ignoreCerts: ignoreCerts, namespace: options.namespace, apigwToken: apigwToken, apigwSpaceGuid: apigwSpaceGuid} } - url_from_apihost (apihost) { + urlFromApihost (apihost) { if (!apihost) return apihost let url = `${apihost}/api/v1/` @@ -99,42 +110,42 @@ class Client { request (method, path, options) { const req = this.params(method, path, options) - return rp(req).catch(err => this.handle_errors(err)) + return rp(req).catch(err => this.handleErrors(err)) } params (method, path, options) { return Object.assign({ json: true, method: method, - url: this.path_url(path), - rejectUnauthorized: !this.options.ignore_certs, + url: this.pathUrl(path), + rejectUnauthorized: !this.options.ignoreCerts, headers: { - Authorization: this.auth_header() + Authorization: this.authHeader() } }, options) } - path_url (url_path) { - const endpoint = this.api_url() - endpoint.pathname = url.resolve(endpoint.pathname, url_path) + pathUrl (urlPath) { + const endpoint = this.apiUrl() + endpoint.pathname = url.resolve(endpoint.pathname, urlPath) return url.format(endpoint) } - api_url () { + apiUrl () { return url.parse( this.options.api.endsWith('/') ? this.options.api : this.options.api + '/' ) } - auth_header () { - const api_key_base64 = Buffer.from(this.options.api_key).toString('base64') - return `Basic ${api_key_base64}` + authHeader () { + const apiKeyBase64 = Buffer.from(this.options.apiKey).toString('base64') + return `Basic ${apiKeyBase64}` } - handle_errors (reason) { + handleErrors (reason) { let message = `Unknown Error From API: ${reason.message}` if (reason.hasOwnProperty('statusCode')) { - const responseError = this.err_message(reason.error) + const responseError = this.errMessage(reason.error) message = `${reason.options.method} ${reason.options.url} Returned HTTP ${reason.statusCode} (${http.STATUS_CODES[reason.statusCode]}) --> "${responseError}"` } @@ -143,7 +154,7 @@ class Client { // Error messages might be returned from platform or using custom // invocation result response from action. - err_message (error) { + errMessage (error) { if (!error) return 'Response Missing Error Message.' if (typeof error.error === 'string') { diff --git a/lib/feeds.js b/lib/feeds.js index 1ecc151..6c79104 100644 --- a/lib/feeds.js +++ b/lib/feeds.js @@ -30,49 +30,49 @@ class Feeds { } feed (event, options) { - if (!this.feed_name(options)) { + if (!this.feedName(options)) { throw new Error(messages.MISSING_FEED_NAME_ERROR) } - if (!this.trigger_name(options)) { + if (!this.triggerName(options)) { throw new Error(messages.MISSING_FEED_TRIGGER_ERROR) } - const invoke_options = this.invoke_options(event, options) - return this.actions.invoke(invoke_options) + const invokeOptions = this.invokeOptions(event, options) + return this.actions.invoke(invokeOptions) } - feed_name (options) { + feedName (options) { return options.feedName || options.name } - trigger_name (options) { + triggerName (options) { if (!options.trigger) return if (options.trigger.startsWith('/')) return options.trigger - const ns = this.client.options.namespace || names.default_namespace() + const ns = this.client.options.namespace || names.defaultNamespace() return `/${ns}/${options.trigger}` } - invoke_options (event, options) { - const params = this.invoke_params(event, options) + invokeOptions (event, options) { + const params = this.invokeParams(event, options) - const invoke_options = { - name: this.feed_name(options), + const invokeOptions = { + name: this.feedName(options), namespace: options.namespace, blocking: true, params } - return invoke_options + return invokeOptions } - invoke_params (event, options) { + invokeParams (event, options) { const params = { lifecycleEvent: event, - authKey: this.client.options.api_key, - triggerName: this.trigger_name(options) + authKey: this.client.options.apiKey, + triggerName: this.triggerName(options) } // Add user-provided invocation parameters diff --git a/lib/main.d.ts b/lib/main.d.ts index 94ea0e3..289129e 100644 --- a/lib/main.d.ts +++ b/lib/main.d.ts @@ -294,4 +294,4 @@ declare namespace openwhisk { apidoc: Swagger.Spec; } -} \ No newline at end of file +} diff --git a/lib/names.js b/lib/names.js index 391d54a..8f69b6a 100644 --- a/lib/names.js +++ b/lib/names.js @@ -3,24 +3,24 @@ const messages = require('./messages') -const default_namespace = () => { +const defaultNamespace = () => { return process.env['__OW_NAMESPACE'] || '_' } // valid resource identifiers -// - resource_name -// - package/resource_name -// - /namespace/resource_name -// - /namespace/package/resource_name -// - namespace/package/resource_name -const parse_id_and_ns = resource => { +// - resourceName +// - package/resourceName +// - /namespace/resourceName +// - /namespace/package/resourceName +// - namespace/package/resourceName +const parseIdAndNs = resource => { const parts = (resource.match(/\//g) || []).length const names = resource.split('/') // checking for `resource_name` and `package/resource_name` if (parts === 0 || (parts === 1 && !resource.startsWith('/'))) { - return { namespace: default_namespace(), id: resource } + return { namespace: defaultNamespace(), id: resource } } // checking for `/namespace/resource_name` and `namespace/package/resource_name` @@ -40,11 +40,11 @@ const parse_id_and_ns = resource => { throw new Error(messages.INVALID_RESOURCE_ERROR) } -const parse_namespace = id => parse_id_and_ns(id).namespace -const parse_id = id => parse_id_and_ns(id).id +const parseNamespace = id => parseIdAndNs(id).namespace +const parseId = id => parseIdAndNs(id).id module.exports = { - default_namespace, - parse_namespace, - parse_id + defaultNamespace: defaultNamespace, + parseNamespace: parseNamespace, + parseId: parseId } diff --git a/lib/openwhisk_error.js b/lib/openwhisk_error.js index eb64e17..6780726 100644 --- a/lib/openwhisk_error.js +++ b/lib/openwhisk_error.js @@ -1,14 +1,14 @@ // Licensed to the Apache Software Foundation (ASF) under one or more contributor // license agreements; and to You under the Apache License, Version 2.0. -'use strict'; +'use strict' -module.exports = function OpenWhiskError(message, error, statusCode) { - Error.captureStackTrace(this, this.constructor); - this.name = this.constructor.name; - this.message = message; - this.error = error; - this.statusCode = statusCode; -}; +module.exports = function OpenWhiskError (message, error, statusCode) { + Error.captureStackTrace(this, this.constructor) + this.name = this.constructor.name + this.message = message + this.error = error + this.statusCode = statusCode +} -require('util').inherits(module.exports, Error); +require('util').inherits(module.exports, Error) diff --git a/lib/packages.js b/lib/packages.js index 27391b6..fc649e9 100644 --- a/lib/packages.js +++ b/lib/packages.js @@ -24,7 +24,7 @@ class Packages extends Resources { } create (options) { - options = this.parse_options(options) + options = this.parseOptions(options) options.qs = this.qs(options, ['overwrite']) options.body = options.package || {} diff --git a/lib/resources.js b/lib/resources.js index 15cf720..de32c26 100644 --- a/lib/resources.js +++ b/lib/resources.js @@ -18,7 +18,7 @@ class Resources extends BaseOperation { } get (options) { - return this.operation_with_id('GET', options) + return this.operationWithId('GET', options) } invoke (options) { @@ -27,64 +27,64 @@ class Resources extends BaseOperation { options.qs = this.qs(options, this.qs_options.invoke || []) options.body = this.payload(options) } - return this.operation_with_id('POST', options) + return this.operationWithId('POST', options) } create (options) { - return this.operation_with_id('PUT', options) + return this.operationWithId('PUT', options) } delete (options) { - return this.operation_with_id('DELETE', options) + return this.operationWithId('DELETE', options) } update (options) { - options = this.parse_options(options) + options = this.parseOptions(options) options.overwrite = true return this.create(options) } operation (method, options) { - options = this.parse_options(options) + options = this.parseOptions(options) const id = options.id - return this.request({ method, id, options }) + return this.request({method, id, options}) } - operation_with_id (method, options) { + operationWithId (method, options) { if (Array.isArray(options)) { - return Promise.all(options.map(i => this.operation_with_id(method, i))) + return Promise.all(options.map(i => this.operationWithId(method, i))) } - options = this.parse_options(options) - options.namespace = this.parse_namespace(options) - options.id = this.parse_id(options) + options = this.parseOptions(options) + options.namespace = this.parseNamespace(options) + options.id = this.parseId(options) return this.operation(method, options) } - parse_options (options) { + parseOptions (options) { if (typeof options === 'string') { - options = { name: options } + options = {name: options} } return options || {} } - parse_id (options) { - const id = this.retrieve_id(options) - return names.parse_id(id) + parseId (options) { + const id = this.retrieveId(options) + return names.parseId(id) } - parse_namespace (options) { - const id = this.retrieve_id(options) + parseNamespace (options) { + const id = this.retrieveId(options) if (id.startsWith('/')) { - return names.parse_namespace(id) + return names.parseNamespace(id) } return options.namespace } - retrieve_id (options) { + retrieveId (options) { options = options || {} const id = this.identifiers.find(name => options.hasOwnProperty(name)) diff --git a/lib/routes.js b/lib/routes.js index eb55026..a40e85e 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -22,8 +22,8 @@ class Routes extends BaseOperation { list (options) { options = options || {} - if (this.has_basepath(options)) { - options.basepath = this.calculate_basepath(options) + if (this.hasBasepath(options)) { + options.basepath = this.calculateBasepath(options) } const qs = this.qs(options, ['relpath', 'basepath', 'operation', 'limit', 'skip']) @@ -33,27 +33,27 @@ class Routes extends BaseOperation { qs (options, names) { const result = super.qs(options, names) - if (this.has_access_token()) { - result.accesstoken = this.client.options.apigw_token - result.spaceguid = this.client.options.apigw_space_guid + if (this.hasAccessToken()) { + result.accesstoken = this.client.options.apigwToken + result.spaceguid = this.client.options.apigwSpaceGuid } return result } - has_basepath (options) { + hasBasepath (options) { return !!(options.name || options.basepath) } basepath (options) { - if (!this.has_basepath(options)) { + if (!this.hasBasepath(options)) { throw new Error(messages.MISSING_BASEPATH_ERROR) } - return this.calculate_basepath(options) + return this.calculateBasepath(options) } - calculate_basepath (options) { + calculateBasepath (options) { if (options.name && options.basepath) { throw new Error(messages.INVALID_BASEPATH_ERROR) } @@ -61,7 +61,7 @@ class Routes extends BaseOperation { return options.basepath || options.name } - missing_basepath (options) { + missingBasepath (options) { return !(options.name || options.basepath) } @@ -75,12 +75,12 @@ class Routes extends BaseOperation { } create (options) { - const body = this.create_body(options || {}) + const body = this.createBody(options || {}) const qs = this.qs(options, ['responsetype']) return this.client.request('POST', this.routeMgmtApiPath('createApi'), { body, qs }) } - create_body (options) { + createBody (options) { if (options.swagger) { return { apidoc: { namespace: '_', swagger: options.swagger } } } @@ -91,17 +91,17 @@ class Routes extends BaseOperation { throw new Error(`Missing mandatory parameters: ${missing.join(', ')}`) } - return this.route_swagger_definition(options) + return this.routeSwaggerDefinition(options) } - route_swagger_definition (params) { + routeSwaggerDefinition (params) { const apidoc = { namespace: '_', - gatewayBasePath: this.route_base_path(params), + gatewayBasePath: this.routeBasepath(params), gatewayPath: params.relpath, gatewayMethod: params.operation, - id: `API:_:${this.route_base_path(params)}`, - action: this.route_swagger_action(params) + id: `API:_:${this.routeBasepath(params)}`, + action: this.routeSwaggerAction(params) } if (params.name) { @@ -111,37 +111,37 @@ class Routes extends BaseOperation { return { apidoc } } - route_swagger_action (params) { - const id = names.parse_id(params.action) + routeSwaggerAction (params) { + const id = names.parseId(params.action) let namespace = decodeURIComponent(this.namespace(params)) if (params.action.startsWith('/')) { - namespace = names.parse_namespace(params.action) + namespace = names.parseNamespace(params.action) } return { name: id, namespace: namespace, backendMethod: `GET`, - backendUrl: this.action_url_path(id, namespace), - authkey: this.client.options.api_key + backendUrl: this.actionUrlPath(id, namespace), + authkey: this.client.options.apiKey } } - route_base_path (params) { + routeBasepath (params) { return params.basepath || '/' } - action_url_path (id, namespace) { + actionUrlPath (id, namespace) { // web action path must contain package identifier. uses default for // non-explicit package. if (!id.includes('/')) { id = `default/${id}` } - return this.client.path_url(`web/${namespace}/${id}.http`) + return this.client.pathUrl(`web/${namespace}/${id}.http`) } - has_access_token () { - return !!this.client.options.apigw_token + hasAccessToken () { + return !!this.client.options.apigwToken } } diff --git a/lib/rules.js b/lib/rules.js index 4d09291..6bacdec 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -27,12 +27,12 @@ class Rules extends Resources { create (options) { options.qs = this.qs(options, ['overwrite']) - options.body = this.rule_body(options) + options.body = this.ruleBody(options) return super.create(options) } - rule_body (options) { + ruleBody (options) { options = options || {} if (!options.hasOwnProperty('action')) { @@ -43,25 +43,28 @@ class Rules extends Resources { throw new Error(messages.MISSING_RULE_TRIGGER_ERROR) } - return {action: this.convert_to_fqn(options.action, options.namespace), trigger: this.convert_to_fqn(options.trigger, options.namespace)} + return { + action: this.convertToFqn(options.action, options.namespace), + trigger: this.convertToFqn(options.trigger, options.namespace) + } } enable (options) { options = options || {} - options.params = { status: 'active' } + options.params = {status: 'active'} return super.invoke(options) } disable (options) { options = options || {} - options.params = { status: 'inactive' } + options.params = {status: 'inactive'} return super.invoke(options) } - convert_to_fqn (identifier, namespace) { + convertToFqn (identifier, namespace) { if (identifier.startsWith('/')) return identifier - const ns = namespace || this.client.options.namespace || names.default_namespace() + const ns = namespace || this.client.options.namespace || names.defaultNamespace() return `/${ns}/${identifier}` } } diff --git a/package.json b/package.json index cc3c540..54d84cd 100644 --- a/package.json +++ b/package.json @@ -11,12 +11,19 @@ "test": "tests" }, "scripts": { - "test": "ava test/unit", + "test": "standard && ava test/unit", "test-integration": "ava test/integration/*.test.js", "code-coverage-build": "babel --out-dir=ecma5 lib", "code-coverage-run": "./tools/merge-coverage.sh", - "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov" + "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", + "pre-commit-message": "Pre-commit tasks running ... & exit 0", + "lint": "standard", + "standard-fix": "standard --fix" }, + "pre-commit": [ + "pre-commit-message", + "standard-fix" + ], "repository": { "type": "git", "url": "git+https://github.com/openwhisk/openwhisk-client-js.git" @@ -42,7 +49,9 @@ "codecov": "^3.0.0", "jszip": "^3.1.3", "nyc": "^11.0.3", - "proxyquire": "2.0.1" + "pre-commit": "^1.2.2", + "proxyquire": "2.0.1", + "standard": "^11.0.1" }, "dependencies": { "needle": "^2.1.0" diff --git a/test/integration/actions.test.js b/test/integration/actions.test.js index 86b92f0..9533fce 100644 --- a/test/integration/actions.test.js +++ b/test/integration/actions.test.js @@ -8,7 +8,7 @@ const Actions = require('../../lib/actions.js') const Client = require('../../lib/client.js') const JSZip = require('jszip') const Utils = require('./utils.js') -const options = Utils.autoOptions(); +const options = Utils.autoOptions() const envParams = ['API_KEY', 'API_HOST', 'NAMESPACE'] @@ -53,24 +53,24 @@ test('list all actions using options namespace', t => { test('get a non-existing action, expecting 404', async t => { const actions = new Actions(new Client(options)) await actions.get({name: 'glorfindel'}).catch(err => { - t.is(err.statusCode, 404) + t.is(err.statusCode, 404) }) }) test('delete a non-existing action, expecting 404', async t => { const actions = new Actions(new Client(options)) await actions.delete({name: 'glorfindel'}).catch(err => { - t.is(err.statusCode, 404) + t.is(err.statusCode, 404) }) }) test('create with an existing action, expecting 409', async t => { const actions = new Actions(new Client(options)) - await actions.create({name: 'glorfindel2', action: 'x=>x'}) - .then(() => actions.create({name: 'glorfindel2', action: 'x=>x'})) - .catch(err => { - t.is(err.statusCode, 409) - }) + await actions.create({name: 'glorfindel2', action: 'x=>x'}) + .then(() => actions.create({name: 'glorfindel2', action: 'x=>x'})) + .catch(err => { + t.is(err.statusCode, 409) + }) }) test('create, get and delete an action', t => { @@ -80,14 +80,17 @@ test('create, get and delete an action', t => { } const actions = new Actions(new Client(options)) - return actions.create({actionName: 'random_action_test', action: 'function main() {return {payload:"testing"}}'}).then(result => { + return actions.create({ + actionName: 'random_action_test', + action: 'function main() {return {payload:"testing"}}' + }).then(result => { t.is(result.name, 'random_action_test') t.is(result.namespace, NAMESPACE) t.is(result.exec.kind, 'nodejs:6') t.is(result.exec.code, 'function main() {return {payload:"testing"}}') - return actions.get({actionName: 'random_action_test'}).then(action_result => { - t.is(action_result.name, 'random_action_test') - t.is(action_result.namespace, NAMESPACE) + return actions.get({actionName: 'random_action_test'}).then(actionResult => { + t.is(actionResult.name, 'random_action_test') + t.is(actionResult.namespace, NAMESPACE) t.pass() return actions.delete({actionName: 'random_action_test'}).catch(errors) }).catch(errors) @@ -101,13 +104,16 @@ test('create and update an action', t => { } const actions = new Actions(new Client(options)) - return actions.create({actionName: 'random_update_tested', action: 'function main() {return {payload:"testing"}}'}).then(result => { + return actions.create({ + actionName: 'random_update_tested', + action: 'function main() {return {payload:"testing"}}' + }).then(result => { t.is(result.name, 'random_update_tested') t.is(result.namespace, NAMESPACE) t.is(result.exec.kind, 'nodejs:6') t.is(result.exec.code, 'function main() {return {payload:"testing"}}') - return actions.update({actionName: 'random_update_tested', action: 'update test'}).then(update_result => { - t.is(update_result.exec.code, 'update test') + return actions.update({actionName: 'random_update_tested', action: 'update test'}).then(updateResult => { + t.is(updateResult.exec.code, 'update test') t.pass() return actions.delete({actionName: 'random_update_tested'}).catch(errors) }).catch(errors) @@ -121,16 +127,24 @@ test('create, get and delete with parameters an action', t => { } const actions = new Actions(new Client(options)) - return actions.create({name: 'random_action_params_test', params: { hello: 'world' }, action: 'function main() {return {payload:"testing"}}'}).then(result => { + return actions.create({ + name: 'random_action_params_test', + params: {hello: 'world'}, + action: 'function main() {return {payload:"testing"}}' + }).then(result => { t.is(result.name, 'random_action_params_test') t.is(result.namespace, NAMESPACE) t.deepEqual(result.parameters, [{key: 'hello', value: 'world'}]) t.is(result.exec.kind, 'nodejs:6') t.is(result.exec.code, 'function main() {return {payload:"testing"}}') - return actions.update({actionName: 'random_action_params_test', params: { foo: 'bar' }, action: 'update test'}).then(update_result => { - t.is(update_result.name, 'random_action_params_test') - t.is(update_result.namespace, NAMESPACE) - t.deepEqual(update_result.parameters, [{key: 'foo', value: 'bar'}]) + return actions.update({ + actionName: 'random_action_params_test', + params: {foo: 'bar'}, + action: 'update test' + }).then(updateResult => { + t.is(updateResult.name, 'random_action_params_test') + t.is(updateResult.namespace, NAMESPACE) + t.deepEqual(updateResult.parameters, [{key: 'foo', value: 'bar'}]) t.pass() return actions.delete({name: 'random_action_params_test'}).catch(errors) }).catch(errors) @@ -149,18 +163,18 @@ test('get an action with and without its code', t => { t.is(result.namespace, NAMESPACE) t.is(result.exec.kind, 'nodejs:6') t.is(result.exec.code, 'function main() {return {payload:"testing"}}') - return actions.get({actionName: 'random_action_get_test', code: false}).then(action_result => { - t.is(action_result.name, 'random_action_get_test') - t.is(action_result.namespace, NAMESPACE) - t.is(action_result.exec.code, undefined) - return actions.get({actionName: 'random_action_get_test', code: true}).then(action_result => { - t.is(action_result.name, 'random_action_get_test') - t.is(action_result.namespace, NAMESPACE) - t.is(action_result.exec.code, 'function main() {return {payload:"testing"}}') - return actions.get({actionName: 'random_action_get_test'}).then(action_result => { - t.is(action_result.name, 'random_action_get_test') - t.is(action_result.namespace, NAMESPACE) - t.is(action_result.exec.code, 'function main() {return {payload:"testing"}}') + return actions.get({actionName: 'random_action_get_test', code: false}).then(actionResult => { + t.is(actionResult.name, 'random_action_get_test') + t.is(actionResult.namespace, NAMESPACE) + t.is(actionResult.exec.code, undefined) + return actions.get({actionName: 'random_action_get_test', code: true}).then(actionResult => { + t.is(actionResult.name, 'random_action_get_test') + t.is(actionResult.namespace, NAMESPACE) + t.is(actionResult.exec.code, 'function main() {return {payload:"testing"}}') + return actions.get({actionName: 'random_action_get_test'}).then(actionTesult => { + t.is(actionTesult.name, 'random_action_get_test') + t.is(actionTesult.namespace, NAMESPACE) + t.is(actionTesult.exec.code, 'function main() {return {payload:"testing"}}') t.pass() return actions.delete({actionName: 'random_action_get_test'}).catch(errors) }).catch(errors) @@ -177,8 +191,8 @@ test('invoke action with fully-qualified name', t => { } const actions = new Actions(new Client(options)) - return actions.invoke({actionName: '/whisk.system/utils/sort', blocking: true}).then(invoke_result => { - t.true(invoke_result.response.success) + return actions.invoke({actionName: '/whisk.system/utils/sort', blocking: true}).then(invokeResult => { + t.true(invokeResult.response.success) t.pass() }).catch(errors) }) @@ -197,9 +211,13 @@ test('create, invoke and remove package action', t => { return zip.generateAsync({type: 'nodebuffer'}).then(content => { const actions = new Actions(new Client(options)) return actions.create({actionName: 'random_package_action_test', action: content}).then(result => { - return actions.invoke({actionName: 'random_package_action_test', params: {hello: 'world'}, blocking: true}).then(invoke_result => { - t.deepEqual(invoke_result.response.result, {hello: 'world'}) - t.true(invoke_result.response.success) + return actions.invoke({ + actionName: 'random_package_action_test', + params: {hello: 'world'}, + blocking: true + }).then(invokeResult => { + t.deepEqual(invokeResult.response.result, {hello: 'world'}) + t.true(invokeResult.response.success) t.pass() return actions.delete({actionName: 'random_package_action_test'}).catch(errors) }) diff --git a/test/integration/activations.test.js b/test/integration/activations.test.js index 5c3d87f..56ef163 100644 --- a/test/integration/activations.test.js +++ b/test/integration/activations.test.js @@ -7,7 +7,7 @@ const test = require('ava') const Activations = require('../../lib/activations.js') const Client = require('../../lib/client.js') const Utils = require('./utils.js') -const options = Utils.autoOptions(); +const options = Utils.autoOptions() const envParams = ['API_KEY', 'API_HOST', 'NAMESPACE'] diff --git a/test/integration/feeds.test.js b/test/integration/feeds.test.js index d2ea95c..63ecd79 100644 --- a/test/integration/feeds.test.js +++ b/test/integration/feeds.test.js @@ -8,7 +8,7 @@ const Feeds = require('../../lib/feeds.js') const Triggers = require('../../lib/triggers.js') const Client = require('../../lib/client.js') const Utils = require('./utils.js') -const options = Utils.autoOptions(); +const options = Utils.autoOptions() const envParams = ['API_KEY', 'API_HOST', 'NAMESPACE'] @@ -21,7 +21,7 @@ envParams.forEach(key => { }) const NAMESPACE = process.env.__OW_NAMESPACE -var tempTest = Utils.getInsecureFlag() ? test.skip : test; +const tempTest = Utils.getInsecureFlag() ? test.skip : test tempTest('create, get, update, and delete a feed', t => { const errors = err => { @@ -31,19 +31,19 @@ tempTest('create, get, update, and delete a feed', t => { const feeds = new Feeds(new Client(options)) const triggers = new Triggers(new Client(options)) - const feed_params = { + const feedParams = { feedName: '/whisk.system/alarms/alarm', trigger: `/${NAMESPACE}/sample_feed_trigger`, params: {cron: '*/8 * * * * *', trigger_payload: {name: 'test', place: 'hello'}} } - return triggers.create({triggerName: 'sample_feed_trigger'}).then(() => feeds.create(feed_params)).then(result => { + return triggers.create({triggerName: 'sample_feed_trigger'}).then(() => feeds.create(feedParams)).then(result => { t.is(result.response.success, true) - return feeds.get(feed_params).then(get_result => { - t.is(get_result.response.success, true) - return feeds.delete(feed_params).then(feed_result => { - t.is(feed_result.response.success, true) - return feeds.update(feed_params).then(update_result => { - t.is(feed_result.response.success, false) // alarms does not currently support update, hence should fail + return feeds.get(feedParams).then(getTesult => { + t.is(getTesult.response.success, true) + return feeds.delete(feedParams).then(feedResult => { + t.is(feedResult.response.success, true) + return feeds.update(feedParams).then(() => { + t.is(feedResult.response.success, false) // alarms does not currently support update, hence should fail return triggers.delete({triggerName: 'sample_feed_trigger'}).then(() => { t.pass() }) diff --git a/test/integration/namespaces.test.js b/test/integration/namespaces.test.js index 9506619..08a461a 100644 --- a/test/integration/namespaces.test.js +++ b/test/integration/namespaces.test.js @@ -7,7 +7,7 @@ const test = require('ava') const Namespaces = require('../../lib/namespaces.js') const Client = require('../../lib/client.js') const Utils = require('./utils.js') -const options = Utils.autoOptions(); +const options = Utils.autoOptions() const envParams = ['API_KEY', 'API_HOST', 'NAMESPACE'] @@ -19,8 +19,6 @@ envParams.forEach(key => { } }) -const NAMESPACE = process.env.__OW_NAMESPACE - test('list all namespaces', t => { const namespaces = new Namespaces(new Client(options)) return namespaces.list().then(result => { diff --git a/test/integration/packages.test.js b/test/integration/packages.test.js index d0a66e0..068a88b 100644 --- a/test/integration/packages.test.js +++ b/test/integration/packages.test.js @@ -7,7 +7,7 @@ const test = require('ava') const Packages = require('../../lib/packages.js') const Client = require('../../lib/client.js') const Utils = require('./utils.js') -const options = Utils.autoOptions(); +const options = Utils.autoOptions() const envParams = ['API_KEY', 'API_HOST', 'NAMESPACE'] @@ -52,7 +52,7 @@ test('list all packages using options namespace', t => { test('get a non-existing package, expecting 404', async t => { const packages = new Packages(new Client(options)) await packages.get({name: 'glorfindel'}).catch(err => { - t.is(err.statusCode, 404) + t.is(err.statusCode, 404) }) }) @@ -68,9 +68,9 @@ test('create, get and delete an package', t => { t.is(result.namespace, NAMESPACE) t.deepEqual(result.annotations, []) t.is(result.version, '0.0.1') - return packages.get({packageName: 'random_package_test'}).then(package_result => { - t.is(package_result.name, 'random_package_test') - t.is(package_result.namespace, NAMESPACE) + return packages.get({packageName: 'random_package_test'}).then(packageResult => { + t.is(packageResult.name, 'random_package_test') + t.is(packageResult.namespace, NAMESPACE) t.pass() return packages.delete({packageName: 'random_package_test'}).catch(errors) }).catch(errors) @@ -89,8 +89,8 @@ test('create and update an package', t => { t.is(result.namespace, NAMESPACE) t.deepEqual(result.annotations, []) t.is(result.version, '0.0.1') - return packages.update({packageName: 'random_package_update_test'}).then(update_result => { - t.is(update_result.version, '0.0.2') + return packages.update({packageName: 'random_package_update_test'}).then(updateResult => { + t.is(updateResult.version, '0.0.2') t.pass() return packages.delete({packageName: 'random_package_update_test'}).catch(errors) }).catch(errors) diff --git a/test/integration/routes.test.js b/test/integration/routes.test.js index 949e07c..2195865 100644 --- a/test/integration/routes.test.js +++ b/test/integration/routes.test.js @@ -8,7 +8,7 @@ const Actions = require('../../lib/actions.js') const Routes = require('../../lib/routes.js') const Client = require('../../lib/client.js') const Utils = require('./utils.js') -const options = Utils.autoOptions(); +const options = Utils.autoOptions() const envParams = ['API_KEY', 'API_HOST', 'APIGW_TOKEN'] @@ -25,7 +25,12 @@ test.serial('create, retrieve and delete action route using token', t => { const actions = new Actions(new Client(options)) return actions.create({actionName: 'routeAction', action: ''}).then(() => { - return routes.create({action: 'routeAction', basepath: '/testing', relpath: '/foo/bar', operation: 'POST'}).then(() => { + return routes.create({ + action: 'routeAction', + basepath: '/testing', + relpath: '/foo/bar', + operation: 'POST' + }).then(() => { return routes.list({basepath: '/testing'}).then(results => { t.is(results.apis.length, 1) const apidoc = results.apis[0].value.apidoc diff --git a/test/integration/rules.test.js b/test/integration/rules.test.js index 434bab9..7499b82 100644 --- a/test/integration/rules.test.js +++ b/test/integration/rules.test.js @@ -9,7 +9,7 @@ const Triggers = require('../../lib/triggers.js') const Actions = require('../../lib/actions.js') const Client = require('../../lib/client.js') const Utils = require('./utils.js') -const options = Utils.autoOptions(); +const options = Utils.autoOptions() const envParams = ['API_KEY', 'API_HOST', 'NAMESPACE'] @@ -54,7 +54,7 @@ test('list all rules using options namespace', t => { test('get a non-existing rule, expecting 404', async t => { const rules = new Rules(new Client(options)) await rules.get({name: 'glorfindel'}).catch(err => { - t.is(err.statusCode, 404) + t.is(err.statusCode, 404) }) }) @@ -70,14 +70,18 @@ test.serial('create, get and delete a rule', t => { const actions = new Actions(new Client(options)) return actions.create({actionName: 'hello', action: 'function main() {return {payload:"Hello world"}}'}).then(() => { return triggers.create({triggerName: 'sample_rule_trigger'}).then(() => { - return rules.create({ruleName: 'random_rule_test', action: `/${NAMESPACE}/hello`, trigger: `/${NAMESPACE}/sample_rule_trigger`}).then(result => { + return rules.create({ + ruleName: 'random_rule_test', + action: `/${NAMESPACE}/hello`, + trigger: `/${NAMESPACE}/sample_rule_trigger` + }).then(result => { t.is(result.name, 'random_rule_test') t.is(result.namespace, NAMESPACE) t.deepEqual(result.action, {path: NAMESPACE, name: 'hello'}) t.deepEqual(result.trigger, {path: NAMESPACE, name: 'sample_rule_trigger'}) - return rules.get({ruleName: result.name}).then(rule_result => { - t.is(rule_result.name, result.name) - t.is(rule_result.namespace, NAMESPACE) + return rules.get({ruleName: result.name}).then(ruleResult => { + t.is(ruleResult.name, result.name) + t.is(ruleResult.namespace, NAMESPACE) t.pass() return rules.disable({ruleName: 'random_rule_test'}) .then(() => rules.delete({ruleName: 'random_rule_test'})) @@ -99,17 +103,27 @@ test.serial('create and update a rule', t => { const triggers = new Triggers(new Client(options)) const actions = new Actions(new Client(options)) return actions.create({actionName: 'hello', action: 'function main() {return {payload:"Hello world"}}'}).then(() => { - - return actions.create({actionName: 'tests', action: 'function main() {return {payload:"Hello world"}}' }).then(() => { + return actions.create({ + actionName: 'tests', + action: 'function main() {return {payload:"Hello world"}}' + }).then(() => { return triggers.create({triggerName: 'sample_rule_trigger'}).then(() => { - return rules.create({ruleName: 'random_update_test', action: `/${NAMESPACE}/hello`, trigger: `/${NAMESPACE}/sample_rule_trigger`}).then(result => { + return rules.create({ + ruleName: 'random_update_test', + action: `/${NAMESPACE}/hello`, + trigger: `/${NAMESPACE}/sample_rule_trigger` + }).then(result => { t.is(result.name, 'random_update_test') t.is(result.namespace, NAMESPACE) t.deepEqual(result.action, {path: NAMESPACE, name: 'hello'}) t.deepEqual(result.trigger, {path: NAMESPACE, name: 'sample_rule_trigger'}) return rules.disable({ruleName: 'random_update_test'}).then(() => { - return rules.update({ruleName: 'random_update_test', action: 'tests', trigger: 'sample_rule_trigger'}).then(update_result => { - t.deepEqual(update_result.action, {path: NAMESPACE, name: 'tests'}) + return rules.update({ + ruleName: 'random_update_test', + action: 'tests', + trigger: 'sample_rule_trigger' + }).then(updateResult => { + t.deepEqual(updateResult.action, {path: NAMESPACE, name: 'tests'}) t.pass() return rules.delete({ruleName: 'random_update_test'}) .then(() => triggers.delete({triggerName: 'sample_rule_trigger'})) @@ -121,5 +135,5 @@ test.serial('create and update a rule', t => { }).catch(errors) }) }) - }).catch(errors); + }).catch(errors) }) diff --git a/test/integration/triggers.test.js b/test/integration/triggers.test.js index 04515db..086aa2e 100644 --- a/test/integration/triggers.test.js +++ b/test/integration/triggers.test.js @@ -71,9 +71,9 @@ test('create, get and delete an trigger', t => { t.is(result.version, '0.0.1') t.deepEqual(result.limits, {}) t.pass() - return triggers.get({triggerName: result.name}).then(trigger_result => { - t.is(trigger_result.name, result.name) - t.is(trigger_result.namespace, NAMESPACE) + return triggers.get({triggerName: result.name}).then(triggerResult => { + t.is(triggerResult.name, result.name) + t.is(triggerResult.namespace, NAMESPACE) t.pass() return triggers.delete({triggerName: result.name}).catch(errors) }).catch(errors) @@ -93,8 +93,8 @@ test('create and update an trigger', t => { t.deepEqual(result.annotations, []) t.is(result.version, '0.0.1') t.deepEqual(result.limits, {}) - return triggers.update({triggerName: 'random_create_update_test'}).then(update_result => { - t.is(update_result.version, '0.0.2') + return triggers.update({triggerName: 'random_create_update_test'}).then(updateResult => { + t.is(updateResult.version, '0.0.2') t.pass() return triggers.delete({triggerName: 'random_create_update_test'}).catch(errors) }).catch(errors) @@ -110,10 +110,10 @@ test('fire a trigger', t => { const triggers = new Triggers(new Client(options)) const rules = new Rules(new Client(options)) - return triggers.create({triggerName: 'random_fire_test'}).then(result => { - return rules.create({ruleName: 'echo_rule', action: `/whisk.system/utils/echo`, trigger: `/${NAMESPACE}/random_fire_test`}).then(rule_result => { - return triggers.invoke({triggerName: 'random_fire_test'}).then(update_result => { - t.true(update_result.hasOwnProperty('activationId')) + return triggers.create({triggerName: 'random_fire_test'}).then(() => { + return rules.create({ruleName: 'echo_rule', action: `/whisk.system/utils/echo`, trigger: `/${NAMESPACE}/random_fire_test`}).then(() => { + return triggers.invoke({triggerName: 'random_fire_test'}).then(updateResult => { + t.true(updateResult.hasOwnProperty('activationId')) t.pass() return triggers.delete({triggerName: 'random_fire_test'}) .then(() => rules.delete({ruleName: 'echo_rule'})) diff --git a/test/integration/utils.js b/test/integration/utils.js index 42ff656..87e0f42 100644 --- a/test/integration/utils.js +++ b/test/integration/utils.js @@ -1,21 +1,21 @@ -function getInsecureFlag(){ - let npmConfigArgObj = process.env.npm_config_argv ? JSON.parse(process.env.npm_config_argv) : null; - if(npmConfigArgObj){ - return npmConfigArgObj.original&&npmConfigArgObj.original[2]=="-i"; +function getInsecureFlag () { + let npmConfigArgObj = process.env.npm_config_argv ? JSON.parse(process.env.npm_config_argv) : null + if (npmConfigArgObj) { + return npmConfigArgObj.original && npmConfigArgObj.original[2] === '-i' } - return false; + return false } -function autoOptions(){ - var options = {}; - if(getInsecureFlag()){ - options.ignore_certs = true; - options.apigw_token = true; +function autoOptions () { + var options = {} + if (getInsecureFlag()) { + options.ignore_certs = true + options.apigw_token = true } - return options; + return options } module.exports = { - getInsecureFlag : getInsecureFlag, + getInsecureFlag: getInsecureFlag, autoOptions: autoOptions -}; +} diff --git a/test/unit/actions.test.js b/test/unit/actions.test.js index b1b552a..8d3140f 100644 --- a/test/unit/actions.test.js +++ b/test/unit/actions.test.js @@ -63,7 +63,6 @@ test('should retrieve action from identifier', t => { return actions.get({name: '12345'}) }) - test('should retrieve action from identifier with code query parameter', t => { t.plan(3) const ns = '_' @@ -219,13 +218,13 @@ test('should invoke action to retrieve result', t => { const ns = '_' const client = {} const actions = new Actions(client) - const result = { hello: 'world' } + const result = {hello: 'world'} client.request = (method, path, options) => { t.is(method, 'POST') t.is(path, `namespaces/${ns}/actions/12345`) t.deepEqual(options.qs, {blocking: true}) - return Promise.resolve({response: { result }}) + return Promise.resolve({response: {result}}) } return actions.invoke({name: '12345', result: true, blocking: true}).then(_result => { @@ -238,17 +237,17 @@ test('should invoke action to retrieve result without blocking', t => { const ns = '_' const client = {} const actions = new Actions(client) - const result = { hello: 'world' } + const result = {hello: 'world'} client.request = (method, path, options) => { t.is(method, 'POST') t.is(path, `namespaces/${ns}/actions/12345`) t.deepEqual(options.qs, {}) - return Promise.resolve({response: { result }}) + return Promise.resolve({response: {result}}) } return actions.invoke({name: '12345', result: true}).then(_result => { - t.deepEqual(_result, {response: { result } }) + t.deepEqual(_result, {response: {result}}) }) }) @@ -353,9 +352,12 @@ test('create a new action with default parameters', t => { t.is(method, 'PUT') t.is(path, `namespaces/${ns}/actions/12345`) t.deepEqual(options.qs, {}) - t.deepEqual(options.body, {exec: {kind: 'nodejs:default', code: action}, parameters: [ - { key: 'foo', value: 'bar' } - ]}) + t.deepEqual(options.body, { + exec: {kind: 'nodejs:default', code: action}, + parameters: [ + {key: 'foo', value: 'bar'} + ] + }) } return actions.create({name: '12345', action, params}) @@ -375,9 +377,10 @@ test('create a new action with annotations', t => { t.is(method, 'PUT') t.is(path, `namespaces/${ns}/actions/12345`) t.deepEqual(options.qs, {}) - t.deepEqual(options.body, {exec: {kind: 'nodejs:default', code: action}, annotations: [ - { key: 'foo', value: 'bar' } - ]}) + t.deepEqual(options.body, {exec: {kind: 'nodejs:default', code: action}, + annotations: [ + { key: 'foo', value: 'bar' } + ]}) } return actions.create({name: '12345', action, annotations}) @@ -425,5 +428,4 @@ test('create a new action with version parameter', t => { } return actions.create({name: '12345', action, version}) - }) diff --git a/test/unit/activations.test.js b/test/unit/activations.test.js index 0d6e31d..6058342 100644 --- a/test/unit/activations.test.js +++ b/test/unit/activations.test.js @@ -32,7 +32,15 @@ test('list all activations', t => { t.deepEqual(options.qs, {name: 'Hello', limit: 100, skip: 100, upto: 100, docs: true, since: 100}) } - return activations.list({namespace: 'options_namespace', name: 'Hello', limit: 100, skip: 100, since: 100, upto: 100, docs: true}) + return activations.list({ + namespace: 'options_namespace', + name: 'Hello', + limit: 100, + skip: 100, + since: 100, + upto: 100, + docs: true + }) }) test('list all activations with count parameter', t => { @@ -54,14 +62,14 @@ test('should retrieve an activation', t => { const ns = '_' const client = {} const activations = new Activations(client) - const activation_id = 'random_id' + const activationId = 'random_id' client.request = (method, path, options) => { t.is(method, 'GET') - t.is(path, `namespaces/${ns}/activations/${activation_id}`) + t.is(path, `namespaces/${ns}/activations/${activationId}`) } - return activations.get({name: activation_id}) + return activations.get({name: activationId}) }) test('should retrieve an activation using alt id parameter', t => { @@ -69,14 +77,14 @@ test('should retrieve an activation using alt id parameter', t => { const ns = '_' const client = {} const activations = new Activations(client) - const activation_id = 'random_id' + const activationId = 'random_id' client.request = (method, path, options) => { t.is(method, 'GET') - t.is(path, `namespaces/${ns}/activations/${activation_id}`) + t.is(path, `namespaces/${ns}/activations/${activationId}`) } - return activations.get({activation: activation_id}) + return activations.get({activation: activationId}) }) test('should retrieve an activation using string id parameter', t => { @@ -84,14 +92,14 @@ test('should retrieve an activation using string id parameter', t => { const ns = '_' const client = {} const activations = new Activations(client) - const activation_id = 'random_id' + const activationId = 'random_id' client.request = (method, path, options) => { t.is(method, 'GET') - t.is(path, `namespaces/${ns}/activations/${activation_id}`) + t.is(path, `namespaces/${ns}/activations/${activationId}`) } - return activations.get(activation_id) + return activations.get(activationId) }) test('should retrieve an activation logs using string id', t => { @@ -99,14 +107,14 @@ test('should retrieve an activation logs using string id', t => { const ns = '_' const client = {} const activations = new Activations(client) - const activation_id = 'random_id' + const activationId = 'random_id' client.request = (method, path, options) => { t.is(method, 'GET') - t.is(path, `namespaces/${ns}/activations/${activation_id}/logs`) + t.is(path, `namespaces/${ns}/activations/${activationId}/logs`) } - return activations.logs(activation_id) + return activations.logs(activationId) }) test('should retrieve an activation logs', t => { @@ -114,14 +122,14 @@ test('should retrieve an activation logs', t => { const ns = '_' const client = {} const activations = new Activations(client) - const activation_id = 'random_id' + const activationId = 'random_id' client.request = (method, path, options) => { t.is(method, 'GET') - t.is(path, `namespaces/${ns}/activations/${activation_id}/logs`) + t.is(path, `namespaces/${ns}/activations/${activationId}/logs`) } - return activations.logs({name: activation_id}) + return activations.logs({name: activationId}) }) test('should retrieve an activation result using string id', t => { @@ -129,14 +137,14 @@ test('should retrieve an activation result using string id', t => { const ns = '_' const client = {} const activations = new Activations(client) - const activation_id = 'random_id' + const activationId = 'random_id' client.request = (method, path, options) => { t.is(method, 'GET') - t.is(path, `namespaces/${ns}/activations/${activation_id}/result`) + t.is(path, `namespaces/${ns}/activations/${activationId}/result`) } - return activations.result(activation_id) + return activations.result(activationId) }) test('should retrieve an activation result', t => { @@ -144,17 +152,19 @@ test('should retrieve an activation result', t => { const ns = '_' const client = {} const activations = new Activations(client) - const activation_id = 'random_id' + const activationId = 'random_id' client.request = (method, path, options) => { t.is(method, 'GET') - t.is(path, `namespaces/${ns}/activations/${activation_id}/result`) + t.is(path, `namespaces/${ns}/activations/${activationId}/result`) } - return activations.result({name: activation_id}) + return activations.result({name: activationId}) }) test('should throw when retrieving activation without id', t => { const activations = new Activations() - return t.throws(() => { activations.get() }, /Missing mandatory/) + return t.throws(() => { + activations.get() + }, /Missing mandatory/) }) diff --git a/test/unit/base_operation.test.js b/test/unit/base_operation.test.js index f25aa43..886d3c9 100644 --- a/test/unit/base_operation.test.js +++ b/test/unit/base_operation.test.js @@ -11,14 +11,16 @@ test('should invoke client request for resource', t => { const namespace = '_' const resource = 'resource_id' const method = 'GET' - const client = {request: (_method, _path) => { - t.is(_method, method) - t.is(_path, `namespaces/${namespace}/${resource}`) - }} + const client = { + request: (_method, _path) => { + t.is(_method, method) + t.is(_path, `namespaces/${namespace}/${resource}`) + } + } - const base_operation = new BaseOperation(client) - base_operation.resource = resource - base_operation.request({method}) + const baseOperation = new BaseOperation(client) + baseOperation.resource = resource + baseOperation.request({method}) }) test('should invoke client request for resource with identifier', t => { @@ -27,14 +29,16 @@ test('should invoke client request for resource with identifier', t => { const resource = 'resource_id' const method = 'GET' const id = '12345' - const client = {request: (_method, _path) => { - t.is(_method, method) - t.is(_path, `namespaces/${namespace}/${resource}/${id}`) - }} + const client = { + request: (_method, _path) => { + t.is(_method, method) + t.is(_path, `namespaces/${namespace}/${resource}/${id}`) + } + } - const base_operation = new BaseOperation(client) - base_operation.resource = resource - base_operation.request({method, id}) + const baseOperation = new BaseOperation(client) + baseOperation.resource = resource + baseOperation.request({method, id}) }) test('should invoke client request with user parameters', t => { @@ -44,43 +48,45 @@ test('should invoke client request with user parameters', t => { const method = 'GET' const id = '12345' const options = {qs: {hello: 'world'}, body: {hello: 'world'}} - const client = {request: (_method, _path, _options) => { - t.is(_method, method) - t.is(_path, `namespaces/${namespace}/${resource}/${id}`) - t.deepEqual(_options, {qs: {hello: 'world'}, body: {hello: 'world'}}) - }} + const client = { + request: (_method, _path, _options) => { + t.is(_method, method) + t.is(_path, `namespaces/${namespace}/${resource}/${id}`) + t.deepEqual(_options, {qs: {hello: 'world'}, body: {hello: 'world'}}) + } + } - const base_operation = new BaseOperation(client) - base_operation.resource = resource - base_operation.request({method, id, options}) + const baseOperation = new BaseOperation(client) + baseOperation.resource = resource + baseOperation.request({method, id, options}) }) test('should extract available query string parameters', t => { - const base_operation = new BaseOperation() - t.deepEqual(base_operation.qs({}, ['a', 'b', 'c']), {}) - t.deepEqual(base_operation.qs({a: 1}, ['a', 'b', 'c']), {a: 1}) - t.deepEqual(base_operation.qs({a: 1, c: 2}, ['a', 'b', 'c']), {a: 1, c: 2}) - t.deepEqual(base_operation.qs({a: 1, c: 2, d: 3}, ['a', 'b', 'c']), {a: 1, c: 2}) + const baseOperation = new BaseOperation() + t.deepEqual(baseOperation.qs({}, ['a', 'b', 'c']), {}) + t.deepEqual(baseOperation.qs({a: 1}, ['a', 'b', 'c']), {a: 1}) + t.deepEqual(baseOperation.qs({a: 1, c: 2}, ['a', 'b', 'c']), {a: 1, c: 2}) + t.deepEqual(baseOperation.qs({a: 1, c: 2, d: 3}, ['a', 'b', 'c']), {a: 1, c: 2}) }) test('should return appropriate namespace', t => { - let base_operation = new BaseOperation() - t.is(base_operation.namespace({namespace: 'provided'}), 'provided') + let baseOperation = new BaseOperation() + t.is(baseOperation.namespace({namespace: 'provided'}), 'provided') // using global ns - base_operation = new BaseOperation({ options: { namespace: 'global_ns' }}) - t.is(base_operation.namespace({namespace: 'provided'}), 'provided') - base_operation = new BaseOperation({ options: { namespace: 'global_ns' }}) - t.is(base_operation.namespace({}), 'global_ns') - t.is(base_operation.namespace(), 'global_ns') + baseOperation = new BaseOperation({options: {namespace: 'global_ns'}}) + t.is(baseOperation.namespace({namespace: 'provided'}), 'provided') + baseOperation = new BaseOperation({options: {namespace: 'global_ns'}}) + t.is(baseOperation.namespace({}), 'global_ns') + t.is(baseOperation.namespace(), 'global_ns') - base_operation = new BaseOperation('default') - t.is(base_operation.namespace({namespace: 'provided'}), 'provided') - t.is(base_operation.namespace(), '_') + baseOperation = new BaseOperation('default') + t.is(baseOperation.namespace({namespace: 'provided'}), 'provided') + t.is(baseOperation.namespace(), '_') }) test('should url encode namespace parameter', t => { - let base_operation = new BaseOperation('sample@path') - t.is(base_operation.namespace({namespace: 'sample path'}), `sample%20path`) - t.is(base_operation.namespace({namespace: 'sample@path'}), `sample%40path`) + let baseOperation = new BaseOperation('sample@path') + t.is(baseOperation.namespace({namespace: 'sample path'}), `sample%20path`) + t.is(baseOperation.namespace({namespace: 'sample@path'}), `sample%40path`) }) diff --git a/test/unit/client.test.js b/test/unit/client.test.js index b013260..9db695f 100644 --- a/test/unit/client.test.js +++ b/test/unit/client.test.js @@ -9,24 +9,53 @@ const http = require('http') test('should use default constructor options', t => { const client = new Client({api_key: 'aaa', apihost: 'my_host'}) - t.false(client.options.ignore_certs) - t.is(client.options.api_key, 'aaa') + t.false(client.options.ignoreCerts) + t.is(client.options.apiKey, 'aaa') t.is(client.options.api, 'https://my_host/api/v1/') t.falsy(client.options.namespace) }) test('should support explicit constructor options', t => { - const client = new Client({namespace: 'ns', ignore_certs: true, api_key: 'aaa', api: 'my_host', apigw_token: 'oauth_token', apigw_space_guid: 'space_guid'}) + const client = new Client({ + namespace: 'ns', + ignore_certs: true, + api_key: 'aaa', + api: 'my_host', + apigw_token: 'oauth_token', + apigw_space_guid: 'space_guid' + }) t.is(client.options.api, 'my_host') - t.true(client.options.ignore_certs) + t.true(client.options.ignoreCerts) t.is(client.options.namespace, 'ns') - t.is(client.options.apigw_token, 'oauth_token') - t.is(client.options.apigw_space_guid, 'space_guid') + t.is(client.options.apigwToken, 'oauth_token') + t.is(client.options.apigwSpaceGuid, 'space_guid') +}) + +test('should support deprecated explicit constructor options', t => { + const client = new Client({ + namespace: 'ns', + ignore_certs: true, + api_key: 'aaa', + api: 'my_host', + apigw_token: 'oauth_token', + apigw_space_guid: 'space_guid' + }) + t.is(client.options.api, 'my_host') + t.true(client.options.ignoreCerts) + t.is(client.options.namespace, 'ns') + t.is(client.options.apigwToken, 'oauth_token') + t.is(client.options.apigwSpaceGuid, 'space_guid') }) test('should use uuid from auth key as space guid if apigw_token present', t => { - const client = new Client({namespace: 'ns', ignore_certs: true, api_key: 'uuid:pass', api: 'my_host', apigw_token: 'oauth_token'}) - t.is(client.options.apigw_space_guid, 'uuid') + const client = new Client({ + namespace: 'ns', + ignore_certs: true, + api_key: 'uuid:pass', + api: 'my_host', + apigw_token: 'oauth_token' + }) + t.is(client.options.apigwSpaceGuid, 'uuid') }) test('should use environment parameters for options if not set explicitly.', t => { @@ -35,10 +64,10 @@ test('should use environment parameters for options if not set explicitly.', t = process.env['__OW_APIGW_TOKEN'] = 'my-token' process.env['__OW_APIGW_SPACE_GUID'] = 'my-space' const client = new Client() - t.is(client.options.api_key, process.env['__OW_API_KEY']) + t.is(client.options.apiKey, process.env['__OW_API_KEY']) t.is(client.options.api, 'https://mywhiskhost/api/v1/') - t.is(client.options.apigw_token, 'my-token') - t.is(client.options.apigw_space_guid, 'my-space') + t.is(client.options.apigwToken, 'my-token') + t.is(client.options.apigwSpaceGuid, 'my-space') delete process.env['__OW_API_KEY'] delete process.env['__OW_API_HOST'] delete process.env['__OW_APIGW_TOKEN'] @@ -51,10 +80,10 @@ test('should use options for parameters even if environment parameters are avail process.env['__OW_APIGW_TOKEN'] = 'my-token' process.env['__OW_APIGW_SPACE_GUID'] = 'my-space' const client = new Client({apihost: 'openwhisk', api_key: 'mykey', apigw_token: 'token', apigw_space_guid: 'guid'}) - t.is(client.options.api_key, 'mykey') + t.is(client.options.apiKey, 'mykey') t.is(client.options.api, 'https://openwhisk/api/v1/') - t.is(client.options.apigw_token, 'token') - t.is(client.options.apigw_space_guid, 'guid') + t.is(client.options.apigwToken, 'token') + t.is(client.options.apigwSpaceGuid, 'guid') delete process.env['__OW_API_KEY'] delete process.env['__OW_API_HOST'] delete process.env['__OW_APIGW_TOKEN'] @@ -71,9 +100,9 @@ test('should throw error when missing both API and API Host options.', t => { test('should handle multiple api parameter formats', t => { const client = new Client({api_key: true, apihost: 'blah'}) - t.is(client.url_from_apihost('my_host'), 'https://my_host/api/v1/') - t.is(client.url_from_apihost('https://my_host:80'), 'https://my_host:80/api/v1/') - t.is(client.url_from_apihost('http://my_host:80'), 'http://my_host:80/api/v1/') + t.is(client.urlFromApihost('my_host'), 'https://my_host/api/v1/') + t.is(client.urlFromApihost('https://my_host:80'), 'https://my_host:80/api/v1/') + t.is(client.urlFromApihost('http://my_host:80'), 'http://my_host:80/api/v1/') }) test('should return default request parameters without options', t => { @@ -116,27 +145,50 @@ test('should return request parameters with explicit api option', t => { }) test('should generate auth header from API key', t => { - const api_key = 'some sample api key' - const client = new Client({api: true, api_key: api_key}) - t.is(client.auth_header(), `Basic ${Buffer.from(api_key).toString('base64')}`) + const apiKey = 'some sample api key' + const client = new Client({api: true, api_key: apiKey}) + t.is(client.authHeader(), `Basic ${Buffer.from(apiKey).toString('base64')}`) }) test('should return path and status code in error message', t => { const client = new Client({api_key: true, api: true}) - const method = 'METHOD', url = 'https://blah.com/api/v1/actions/list', statusCode = 400 - t.throws(() => client.handle_errors({options: { method, url }, statusCode }), `${method} ${url} Returned HTTP ${statusCode} (${http.STATUS_CODES[statusCode]}) --> "Response Missing Error Message."`) + const method = 'METHOD' + const url = 'https://blah.com/api/v1/actions/list' + const statusCode = 400 + t.throws(() => client.handleErrors({ + options: {method, url}, + statusCode + }), `${method} ${url} Returned HTTP ${statusCode} (${http.STATUS_CODES[statusCode]}) --> "Response Missing Error Message."`) }) test('should return response error string in error message', t => { const client = new Client({api_key: true, api: true}) - const method = 'METHOD', url = 'https://blah.com/api/v1/actions/list', statusCode = 400 - t.throws(() => client.handle_errors({error: { error: 'hello' }, options: { method, url }, statusCode }), `${method} ${url} Returned HTTP ${statusCode} (${http.STATUS_CODES[statusCode]}) --> "hello"`) - t.throws(() => client.handle_errors({error: { response: { result: { error: 'hello' } } }, options: { method, url }, statusCode }), `${method} ${url} Returned HTTP ${statusCode} (${http.STATUS_CODES[statusCode]}) --> "hello"`) - t.throws(() => client.handle_errors({error: { response: { result: { error: { error: 'hello' } } } }, options: { method, url }, statusCode }), `${method} ${url} Returned HTTP ${statusCode} (${http.STATUS_CODES[statusCode]}) --> "hello"`) - t.throws(() => client.handle_errors({error: { response: { result: { error: { statusCode: 404 } } } }, options: { method, url }, statusCode }), `${method} ${url} Returned HTTP ${statusCode} (${http.STATUS_CODES[statusCode]}) --> "application error, status code: ${404}"`) + const method = 'METHOD' + const url = 'https://blah.com/api/v1/actions/list' + const statusCode = 400 + t.throws(() => client.handleErrors({ + error: {error: 'hello'}, + options: {method, url}, + statusCode + }), `${method} ${url} Returned HTTP ${statusCode} (${http.STATUS_CODES[statusCode]}) --> "hello"`) + t.throws(() => client.handleErrors({ + error: {response: {result: {error: 'hello'}}}, + options: {method, url}, + statusCode + }), `${method} ${url} Returned HTTP ${statusCode} (${http.STATUS_CODES[statusCode]}) --> "hello"`) + t.throws(() => client.handleErrors({ + error: {response: {result: {error: {error: 'hello'}}}}, + options: {method, url}, + statusCode + }), `${method} ${url} Returned HTTP ${statusCode} (${http.STATUS_CODES[statusCode]}) --> "hello"`) + t.throws(() => client.handleErrors({ + error: {response: {result: {error: {statusCode: 404}}}}, + options: {method, url}, + statusCode + }), `${method} ${url} Returned HTTP ${statusCode} (${http.STATUS_CODES[statusCode]}) --> "application error, status code: ${404}"`) }) test('should throw errors for non-HTTP response failures', t => { const client = new Client({api_key: true, api: true}) - t.throws(() => client.handle_errors({message: 'error message'}), /error message/) + t.throws(() => client.handleErrors({message: 'error message'}), /error message/) }) diff --git a/test/unit/feeds.test.js b/test/unit/feeds.test.js index 005f3ec..6d92d66 100644 --- a/test/unit/feeds.test.js +++ b/test/unit/feeds.test.js @@ -7,259 +7,293 @@ const test = require('ava') const Feeds = require('../../lib/feeds') test('should be able to delete feed', t => { - const feed_name = 'feed_name' - const api_key = 'username:password' - const trigger_name = '/trigger_ns/trigger_name' + const feedName = 'feedName' + const apiKey = 'username:password' + const triggerName = '/trigger_ns/triggerName' const client = {} - client.options = { api_key } + client.options = {apiKey: apiKey} const ns = '_' const feeds = new Feeds(client) client.request = (method, path, options) => { t.is(method, 'POST') - t.is(path, `namespaces/${ns}/actions/${feed_name}`) + t.is(path, `namespaces/${ns}/actions/${feedName}`) t.deepEqual(options.qs, {blocking: true}) - t.deepEqual(options.body, {authKey: client.options.api_key, lifecycleEvent: 'DELETE', triggerName: `${trigger_name}`}) + t.deepEqual(options.body, { + authKey: client.options.apiKey, + lifecycleEvent: 'DELETE', + triggerName: `${triggerName}` + }) } t.plan(4) - return feeds.delete({name: feed_name, trigger: trigger_name}) + return feeds.delete({name: feedName, trigger: triggerName}) }) test('should be able to delete feed using feedName with params', t => { - const feed_name = 'feed_name' - const api_key = 'username:password' - const trigger_name = 'trigger_name' + const feedName = 'feedName' + const apiKey = 'username:password' + const triggerName = 'triggerName' const client = {} - client.options = { api_key } + client.options = {apiKey: apiKey} const ns = '_' const feeds = new Feeds(client) client.request = (method, path, options) => { t.is(method, 'POST') - t.is(path, `namespaces/${ns}/actions/${feed_name}`) + t.is(path, `namespaces/${ns}/actions/${feedName}`) t.deepEqual(options.qs, {blocking: true}) - t.deepEqual(options.body, {foo: 'bar', authKey: client.options.api_key, lifecycleEvent: 'DELETE', triggerName: `/_/${trigger_name}`}) + t.deepEqual(options.body, { + foo: 'bar', + authKey: client.options.apiKey, + lifecycleEvent: 'DELETE', + triggerName: `/_/${triggerName}` + }) } t.plan(4) const params = {foo: 'bar'} - return feeds.delete({feedName: feed_name, trigger: trigger_name, params}) + return feeds.delete({feedName: feedName, trigger: triggerName, params}) }) test('should be able to create feed', t => { - const feed_name = 'feed_name' - const api_key = 'username:password' - const trigger_name = '/trigger_ns/trigger_name' + const feedName = 'feedName' + const apiKey = 'username:password' + const triggerName = '/trigger_ns/triggerName' const client = {} - client.options = { api_key } + client.options = {apiKey: apiKey} const ns = '_' const feeds = new Feeds(client) client.request = (method, path, options) => { t.is(method, 'POST') - t.is(path, `namespaces/${ns}/actions/${feed_name}`) + t.is(path, `namespaces/${ns}/actions/${feedName}`) t.deepEqual(options.qs, {blocking: true}) - t.deepEqual(options.body, {authKey: client.options.api_key, lifecycleEvent: 'CREATE', triggerName: `${trigger_name}`}) + t.deepEqual(options.body, { + authKey: client.options.apiKey, + lifecycleEvent: 'CREATE', + triggerName: `${triggerName}` + }) } t.plan(4) - return feeds.create({name: feed_name, trigger: trigger_name}) + return feeds.create({name: feedName, trigger: triggerName}) }) test('should be able to create trigger ignoring global namespace', t => { - const feed_name = 'feed_name' - const api_key = 'username:password' - const trigger_name = '/a/trigger_name' + const feedName = 'feedName' + const apiKey = 'username:password' + const triggerName = '/a/triggerName' const client = {} - client.options = { api_key, namespace: 'global_ns' } + client.options = {apiKey: apiKey, namespace: 'global_ns'} const ns = 'global_ns' const feeds = new Feeds(client) client.request = (method, path, options) => { t.is(method, 'POST') - t.is(path, `namespaces/${ns}/actions/${feed_name}`) + t.is(path, `namespaces/${ns}/actions/${feedName}`) t.deepEqual(options.qs, {blocking: true}) - t.deepEqual(options.body, {authKey: client.options.api_key, lifecycleEvent: 'CREATE', triggerName: `${trigger_name}`}) + t.deepEqual(options.body, { + authKey: client.options.apiKey, + lifecycleEvent: 'CREATE', + triggerName: `${triggerName}` + }) } t.plan(4) - return feeds.create({name: feed_name, trigger: trigger_name}) + return feeds.create({name: feedName, trigger: triggerName}) }) test('should be able to create trigger using global namespace', t => { - const feed_name = 'feed_name' - const api_key = 'username:password' - const trigger_name = 'trigger_name' + const feedName = 'feedName' + const apiKey = 'username:password' + const triggerName = 'triggerName' const client = {} - client.options = { api_key, namespace: 'global_ns' } + client.options = {apiKey: apiKey, namespace: 'global_ns'} const ns = 'global_ns' const feeds = new Feeds(client) client.request = (method, path, options) => { t.is(method, 'POST') - t.is(path, `namespaces/${ns}/actions/${feed_name}`) + t.is(path, `namespaces/${ns}/actions/${feedName}`) t.deepEqual(options.qs, {blocking: true}) - t.deepEqual(options.body, {authKey: client.options.api_key, lifecycleEvent: 'CREATE', triggerName: `/global_ns/${trigger_name}`}) + t.deepEqual(options.body, { + authKey: client.options.apiKey, + lifecycleEvent: 'CREATE', + triggerName: `/global_ns/${triggerName}` + }) } t.plan(4) - return feeds.create({name: feed_name, trigger: trigger_name}) + return feeds.create({name: feedName, trigger: triggerName}) }) test('should be able to create trigger using options namespace', t => { - const feed_name = 'feed_name' - const api_key = 'username:password' - const trigger_name = 'trigger_name' + const feedName = 'feedName' + const apiKey = 'username:password' + const triggerName = 'triggerName' const client = {} - client.options = { api_key } + client.options = {apiKey: apiKey} const ns = 'custom' const feeds = new Feeds(client) client.request = (method, path, options) => { t.is(method, 'POST') - t.is(path, `namespaces/${ns}/actions/${feed_name}`) + t.is(path, `namespaces/${ns}/actions/${feedName}`) t.deepEqual(options.qs, {blocking: true}) - t.deepEqual(options.body, {authKey: client.options.api_key, lifecycleEvent: 'CREATE', triggerName: `/_/${trigger_name}`}) + t.deepEqual(options.body, { + authKey: client.options.apiKey, + lifecycleEvent: 'CREATE', + triggerName: `/_/${triggerName}` + }) } t.plan(4) - return feeds.create({name: feed_name, namespace: ns, trigger: trigger_name}) + return feeds.create({name: feedName, namespace: ns, trigger: triggerName}) }) test('should be able to create trigger ignoring options namespace', t => { - const feed_name = 'feed_name' - const api_key = 'username:password' - const trigger_name = '/a/trigger_name' + const feedName = 'feedName' + const apiKey = 'username:password' + const triggerName = '/a/triggerName' const client = {} - client.options = { api_key } + client.options = {apiKey: apiKey} - const ns = '_' const feeds = new Feeds(client) client.request = (method, path, options) => { t.is(method, 'POST') - t.is(path, `namespaces/ns/actions/${feed_name}`) + t.is(path, `namespaces/ns/actions/${feedName}`) t.deepEqual(options.qs, {blocking: true}) - t.deepEqual(options.body, {authKey: client.options.api_key, lifecycleEvent: 'CREATE', triggerName: `${trigger_name}`}) + t.deepEqual(options.body, { + authKey: client.options.apiKey, + lifecycleEvent: 'CREATE', + triggerName: `${triggerName}` + }) } t.plan(4) - return feeds.create({name: feed_name, namespace: 'ns', trigger: trigger_name}) + return feeds.create({name: feedName, namespace: 'ns', trigger: triggerName}) }) test('should be able to create trigger from full qualified feed', t => { - const feed_name = '/b/c/feed_name' - const api_key = 'username:password' - const trigger_name = '/a/trigger_name' + const feedName = '/b/c/feedName' + const apiKey = 'username:password' + const triggerName = '/a/triggerName' const client = {} - client.options = { api_key, namespace: 'global' } + client.options = {apiKey: apiKey, namespace: 'global'} - const ns = '_' const feeds = new Feeds(client) client.request = (method, path, options) => { t.is(method, 'POST') - t.is(path, `namespaces/b/actions/c/feed_name`) + t.is(path, `namespaces/b/actions/c/feedName`) t.deepEqual(options.qs, {blocking: true}) - t.deepEqual(options.body, {authKey: client.options.api_key, lifecycleEvent: 'CREATE', triggerName: `${trigger_name}`}) + t.deepEqual(options.body, { + authKey: client.options.apiKey, + lifecycleEvent: 'CREATE', + triggerName: `${triggerName}` + }) } t.plan(4) - return feeds.create({name: feed_name, namespace: 'ns', trigger: trigger_name}) + return feeds.create({name: feedName, namespace: 'ns', trigger: triggerName}) }) test('should be able to create feed using feedName with params', t => { - const feed_name = 'feed_name' - const api_key = 'username:password' - const trigger_name = 'trigger_name' + const feedName = 'feedName' + const apiKey = 'username:password' + const triggerName = 'triggerName' const client = {} - client.options = { api_key } + client.options = {apiKey: apiKey} const ns = '_' const feeds = new Feeds(client) client.request = (method, path, options) => { t.is(method, 'POST') - t.is(path, `namespaces/${ns}/actions/${feed_name}`) + t.is(path, `namespaces/${ns}/actions/${feedName}`) t.deepEqual(options.qs, {blocking: true}) - t.deepEqual(options.body, {foo: 'bar', authKey: client.options.api_key, lifecycleEvent: 'CREATE', triggerName: `/_/${trigger_name}`}) + t.deepEqual(options.body, { + foo: 'bar', + authKey: client.options.apiKey, + lifecycleEvent: 'CREATE', + triggerName: `/_/${triggerName}` + }) } t.plan(4) const params = {foo: 'bar'} - return feeds.create({feedName: feed_name, trigger: trigger_name, params}) + return feeds.create({feedName: feedName, trigger: triggerName, params}) }) test('should be able to get feed', t => { - const feed_name = 'feed_name' - const api_key = 'username:password' - const trigger_name = '/trigger_ns/trigger_name' + const feedName = 'feedName' + const apiKey = 'username:password' + const triggerName = '/trigger_ns/triggerName' const client = {} - client.options = { api_key } + client.options = { apiKey: apiKey } const ns = '_' const feeds = new Feeds(client) client.request = (method, path, options) => { t.is(method, 'POST') - t.is(path, `namespaces/${ns}/actions/${feed_name}`) + t.is(path, `namespaces/${ns}/actions/${feedName}`) t.deepEqual(options.qs, {blocking: true}) - t.deepEqual(options.body, {authKey: client.options.api_key, lifecycleEvent: 'READ', triggerName: `${trigger_name}`}) + t.deepEqual(options.body, {authKey: client.options.apiKey, lifecycleEvent: 'READ', triggerName: `${triggerName}`}) } t.plan(4) - return feeds.get({name: feed_name, trigger: trigger_name}) + return feeds.get({name: feedName, trigger: triggerName}) }) test('should be able to update feed', t => { - const feed_name = 'feed_name' - const api_key = 'username:password' - const trigger_name = '/trigger_ns/trigger_name' + const feedName = 'feed_name' + const apiKey = 'username:password' + const triggerName = '/trigger_ns/trigger_name' const client = {} - client.options = { api_key } + client.options = { api_key: apiKey } const ns = '_' const feeds = new Feeds(client) client.request = (method, path, options) => { t.is(method, 'POST') - t.is(path, `namespaces/${ns}/actions/${feed_name}`) + t.is(path, `namespaces/${ns}/actions/${feedName}`) t.deepEqual(options.qs, {blocking: true}) - t.deepEqual(options.body, {authKey: client.options.api_key, lifecycleEvent: 'UPDATE', triggerName: `${trigger_name}`}) + t.deepEqual(options.body, {authKey: client.options.apiKey, lifecycleEvent: 'UPDATE', triggerName: `${triggerName}`}) } t.plan(4) - return feeds.update({name: feed_name, trigger: trigger_name}) + return feeds.update({name: feedName, trigger: triggerName}) }) test('should throw errors without trigger parameter ', t => { - const ns = '_' - const client = { options: {} } + const client = {options: {}} const feeds = new Feeds(client) t.throws(() => feeds.feed('', {feedName: 'myFeed'}), /trigger/) }) test('should throw errors without id parameter', t => { - const ns = '_' - const client = { options: {} } + const client = {options: {}} const feeds = new Feeds(client) t.throws(() => feeds.feed('', {trigger: 'myFeed'}), /feedName/) }) diff --git a/test/unit/names.test.js b/test/unit/names.test.js index b1a788b..8218a1d 100644 --- a/test/unit/names.test.js +++ b/test/unit/names.test.js @@ -7,77 +7,77 @@ const test = require('ava') const names = require('../../lib/names') test('should return default namespace', t => { - t.is(names.default_namespace(), '_') + t.is(names.defaultNamespace(), '_') }) test('should return default namespace from env param', t => { process.env['__OW_NAMESPACE'] = 'testing' - t.is(names.default_namespace(), 'testing') + t.is(names.defaultNamespace(), 'testing') delete process.env['__OW_NAMESPACE'] }) test('should parse namespace from resource without explicit ns', t => { - t.is(names.parse_namespace('hello'), '_') + t.is(names.parseNamespace('hello'), '_') }) test('should parse namespace from package resource without explicit ns', t => { - t.is(names.parse_namespace('pkg/hello'), '_') + t.is(names.parseNamespace('pkg/hello'), '_') }) test('should parse namespace from resource with explicit ns', t => { - t.is(names.parse_namespace('/ns/hello'), 'ns') + t.is(names.parseNamespace('/ns/hello'), 'ns') }) test('should parse namespace from package resource with explicit ns', t => { - t.is(names.parse_namespace('/ns/pkg/hello'), 'ns') + t.is(names.parseNamespace('/ns/pkg/hello'), 'ns') }) test('should parse namespace from resource with explicit ns and package but missing leading slash', t => { - t.is(names.parse_namespace('ns/pkg/hello'), 'ns') + t.is(names.parseNamespace('ns/pkg/hello'), 'ns') }) test('should throw error for resource with only namespace', t => { - t.throws(() => names.parse_namespace('/ns'), /Invalid resource identifier/) + t.throws(() => names.parseNamespace('/ns'), /Invalid resource identifier/) }) test('should throw error for resource with only extra paths', t => { - t.throws(() => names.parse_namespace('/ns/pkg/action/extra'), /Invalid resource identifier/) - t.throws(() => names.parse_namespace('ns/pkg/action/extra'), /Invalid resource identifier/) + t.throws(() => names.parseNamespace('/ns/pkg/action/extra'), /Invalid resource identifier/) + t.throws(() => names.parseNamespace('ns/pkg/action/extra'), /Invalid resource identifier/) }) test('should throw error for resource with missing parts', t => { - t.throws(() => names.parse_namespace('/'), /Invalid resource identifier/) + t.throws(() => names.parseNamespace('/'), /Invalid resource identifier/) }) test('should parse id from resource without explicit ns', t => { - t.is(names.parse_id('hello'), 'hello') + t.is(names.parseId('hello'), 'hello') }) test('should parse id from package resource without explicit ns', t => { - t.is(names.parse_id('pkg/hello'), 'pkg/hello') + t.is(names.parseId('pkg/hello'), 'pkg/hello') }) test('should parse id from resource with explicit ns', t => { - t.is(names.parse_id('/ns/hello'), 'hello') + t.is(names.parseId('/ns/hello'), 'hello') }) test('should parse id from package resource with explicit ns', t => { - t.is(names.parse_id('/ns/pkg/hello'), 'pkg/hello') + t.is(names.parseId('/ns/pkg/hello'), 'pkg/hello') }) test('should parse id from resource with explicit ns and package but missing leading slash', t => { - t.is(names.parse_id('ns/pkg/hello'), 'pkg/hello') + t.is(names.parseId('ns/pkg/hello'), 'pkg/hello') }) test('should throw error for resource with only namespace', t => { - t.throws(() => names.parse_id('/ns'), /Invalid resource identifier/) + t.throws(() => names.parseId('/ns'), /Invalid resource identifier/) }) test('should throw error for resource with only extra paths', t => { - t.throws(() => names.parse_id('/ns/pkg/action/extra'), /Invalid resource identifier/) - t.throws(() => names.parse_id('ns/pkg/action/extra'), /Invalid resource identifier/) + t.throws(() => names.parseId('/ns/pkg/action/extra'), /Invalid resource identifier/) + t.throws(() => names.parseId('ns/pkg/action/extra'), /Invalid resource identifier/) }) test('should throw error for resource with missing parts', t => { - t.throws(() => names.parse_id('/'), /Invalid resource identifier/) + t.throws(() => names.parseId('/'), /Invalid resource identifier/) }) diff --git a/test/unit/namespaces.test.js b/test/unit/namespaces.test.js index 5a31673..2b543ed 100644 --- a/test/unit/namespaces.test.js +++ b/test/unit/namespaces.test.js @@ -26,7 +26,7 @@ test('should retrieve namespace entities', t => { let parts = path.split('/') t.is(parts[0], 'namespaces') t.is(parts[1], '_') - t.is(["actions", "triggers", "rules", "packages"].indexOf(parts[2]) > -1, true) + t.is(['actions', 'triggers', 'rules', 'packages'].indexOf(parts[2]) > -1, true) } const namespaces = new Namespaces(client) diff --git a/test/unit/packages.test.js b/test/unit/packages.test.js index e02558e..6051f05 100644 --- a/test/unit/packages.test.js +++ b/test/unit/packages.test.js @@ -187,7 +187,7 @@ test('should update an existing package', t => { client.request = (method, path, options) => { t.is(method, 'PUT') t.is(path, `namespaces/${ns}/packages/${id}`) - t.deepEqual(options.qs, { overwrite: true }) + t.deepEqual(options.qs, {overwrite: true}) t.deepEqual(options.body, {}) } diff --git a/test/unit/resources.test.js b/test/unit/resources.test.js index 88452bb..bceef3d 100644 --- a/test/unit/resources.test.js +++ b/test/unit/resources.test.js @@ -149,7 +149,7 @@ test('should send multiple requests for array parameters', t => { return Promise.resolve() } - return resources.operation_with_id('GET', [name, {name}, name, {name}, name]) + return resources.operationWithId('GET', [name, {name}, name, {name}, name]) .then(result => { t.is(result.length, 5) }) @@ -168,25 +168,25 @@ test('should throw errors when missing resource identifier', t => { test('should parse action name from identifier', t => { const resources = new Resources() const id = '12345' - const ns_id = '/ns/12345' - const ns_package_id = '/ns/package/12345' + const nsId = '/ns/12345' + const nsPackageId = '/ns/package/12345' - t.is(resources.parse_id({name: id}), id) - t.is(resources.parse_id({name: ns_id}), id) - t.is(resources.parse_id({name: ns_package_id}), `package/12345`) - t.throws(() => resources.parse_id({name: '/ns'}), /Invalid resource/) + t.is(resources.parseId({name: id}), id) + t.is(resources.parseId({name: nsId}), id) + t.is(resources.parseId({name: nsPackageId}), `package/12345`) + t.throws(() => resources.parseId({name: '/ns'}), /Invalid resource/) }) test('should parse namespace from identifier and options', t => { const resources = new Resources() const name = '12345' - const ns_name = '/ns/12345' - const ns_package_name = '/ns/package/12345' - - t.falsy(resources.parse_namespace({name})) - t.is(resources.parse_namespace({name: ns_name}), 'ns') - t.is(resources.parse_namespace({name: ns_package_name}), 'ns') - t.is(resources.parse_namespace({name, namespace: 'custom'}), 'custom') - t.is(resources.parse_namespace({name: ns_name, namespace: 'custom'}), 'ns') - t.is(resources.parse_namespace({name: ns_package_name, namespace: 'custom'}), 'ns') + const nsName = '/ns/12345' + const nsPackageName = '/ns/package/12345' + + t.falsy(resources.parseNamespace({name})) + t.is(resources.parseNamespace({name: nsName}), 'ns') + t.is(resources.parseNamespace({name: nsPackageName}), 'ns') + t.is(resources.parseNamespace({name, namespace: 'custom'}), 'custom') + t.is(resources.parseNamespace({name: nsName, namespace: 'custom'}), 'ns') + t.is(resources.parseNamespace({name: nsPackageName, namespace: 'custom'}), 'ns') }) diff --git a/test/unit/routes.test.js b/test/unit/routes.test.js index 9d039e4..30eab78 100644 --- a/test/unit/routes.test.js +++ b/test/unit/routes.test.js @@ -8,11 +8,11 @@ const Routes = require('../../lib/routes') test('should retrieve routes from name', t => { t.plan(3) - const client = { options: {} } + const client = {options: {}} client.request = (method, path, options) => { t.is(method, 'GET') t.is(path, routes.routeMgmtApiPath('getApi')) - t.deepEqual(options.qs, { basepath: 'testing' }) + t.deepEqual(options.qs, {basepath: 'testing'}) } const routes = new Routes(client) @@ -21,43 +21,47 @@ test('should retrieve routes from name', t => { test('should retrieve routes from basepath', t => { t.plan(3) - const client = { options: {} } + const client = {options: {}} client.request = (method, path, options) => { t.is(method, 'GET') t.is(path, routes.routeMgmtApiPath('getApi')) - t.deepEqual(options.qs, { basepath: 'testing' }) + t.deepEqual(options.qs, {basepath: 'testing'}) } const routes = new Routes(client) return routes.get({basepath: 'testing'}) }) -test('should retrieve routes with apigw_token and name', t => { +test('should retrieve routes with apigwToken and name', t => { t.plan(3) - const client = { options: { - apigw_token: 'token', - apigw_space_guid: 'space' - } } + const client = { + options: { + apigwToken: 'token', + apigwSpaceGuid: 'space' + } + } client.request = (method, path, options) => { t.is(method, 'GET') t.is(path, routes.routeMgmtApiPath('getApi')) - t.deepEqual(options.qs, { basepath: 'testing', spaceguid: 'space', accesstoken: 'token'}) + t.deepEqual(options.qs, {basepath: 'testing', spaceguid: 'space', accesstoken: 'token'}) } const routes = new Routes(client) return routes.get({name: 'testing'}) }) -test('should retrieve routes with apigw_token and basepath', t => { +test('should retrieve routes with apigwToken and basepath', t => { t.plan(3) - const client = { options: { - apigw_token: 'token', - apigw_space_guid: 'space' - } } + const client = { + options: { + apigwToken: 'token', + apigwSpaceGuid: 'space' + } + } client.request = (method, path, options) => { t.is(method, 'GET') t.is(path, routes.routeMgmtApiPath('getApi')) - t.deepEqual(options.qs, { basepath: 'testing', spaceguid: 'space', accesstoken: 'token'}) + t.deepEqual(options.qs, {basepath: 'testing', spaceguid: 'space', accesstoken: 'token'}) } const routes = new Routes(client) @@ -75,7 +79,7 @@ test('get routes with incorrect parameters', t => { test('should list all routes', t => { t.plan(2) - const client = { options: {} } + const client = {options: {}} client.request = (method, path, options) => { t.is(method, 'GET') t.is(path, routes.routeMgmtApiPath('getApi')) @@ -85,26 +89,27 @@ test('should list all routes', t => { return routes.list() }) -test('should list all routes with apigw_token', t => { +test('should list all routes with apigwToken', t => { t.plan(3) - const client = { options: { - apigw_token: 'token', - apigw_space_guid: 'space' - } } + const client = { + options: { + apigwToken: 'token', + apigwSpaceGuid: 'space' + } + } client.request = (method, path, options) => { t.is(method, 'GET') t.is(path, routes.routeMgmtApiPath('getApi')) - t.deepEqual(options.qs, { spaceguid: 'space', accesstoken: 'token'}) + t.deepEqual(options.qs, {spaceguid: 'space', accesstoken: 'token'}) } const routes = new Routes(client) return routes.list() }) - test('should list all routes with parameters including basepath', t => { t.plan(3) - const client = { options: {} } + const client = {options: {}} const options = {basepath: '/hello', relpath: '/foo/bar', operation: 'GET', limit: 10, skip: 10} client.request = (method, path, _options) => { t.is(method, 'GET') @@ -118,13 +123,13 @@ test('should list all routes with parameters including basepath', t => { test('should list all routes with parameters including name', t => { t.plan(3) - const client = { options: {} } + const client = {options: {}} const options = {name: '/hello', relpath: '/foo/bar', operation: 'GET', limit: 10, skip: 10} - const qs_options = {basepath: '/hello', relpath: '/foo/bar', operation: 'GET', limit: 10, skip: 10} + const qsOptions = {basepath: '/hello', relpath: '/foo/bar', operation: 'GET', limit: 10, skip: 10} client.request = (method, path, _options) => { t.is(method, 'GET') t.is(path, routes.routeMgmtApiPath('getApi')) - t.deepEqual(_options.qs, qs_options) + t.deepEqual(_options.qs, qsOptions) } const routes = new Routes(client) @@ -132,14 +137,19 @@ test('should list all routes with parameters including name', t => { }) test('list routes providing basepath and name', t => { - const client = { options: {} } + const client = {options: {}} const routes = new Routes(client) - return t.throws(() => { routes.list({basepath: 'bp', name: 'nm'}) }, /Invalid parameters: use basepath or name, not both/) + return t.throws(() => { + routes.list({ + basepath: 'bp', + name: 'nm' + }) + }, /Invalid parameters: use basepath or name, not both/) }) test('should delete a route with basepath', t => { t.plan(3) - const client = { options: {} } + const client = {options: {}} const options = {force: true, basepath: '/hello'} client.request = (method, path, _options) => { @@ -154,7 +164,7 @@ test('should delete a route with basepath', t => { test('should delete a route with name', t => { t.plan(3) - const client = { options: {} } + const client = {options: {}} const options = {force: true, basepath: '/hello'} client.request = (method, path, _options) => { @@ -169,15 +179,17 @@ test('should delete a route with name', t => { test('should delete a route with apigw token', t => { t.plan(3) - const client = { options: { - apigw_token: 'token', - apigw_space_guid: 'space' - } } + const client = { + options: { + apigwToken: 'token', + apigwSpaceGuid: 'space' + } + } client.request = (method, path, options) => { t.is(method, 'DELETE') t.is(path, routes.routeMgmtApiPath('deleteApi')) - t.deepEqual(options.qs, { basepath: '/hello', force: true, spaceguid: 'space', accesstoken: 'token'}) + t.deepEqual(options.qs, {basepath: '/hello', force: true, spaceguid: 'space', accesstoken: 'token'}) } const routes = new Routes(client) @@ -186,7 +198,7 @@ test('should delete a route with apigw token', t => { test('should delete a route with parameters', t => { t.plan(3) - const client = { options: {} } + const client = {options: {}} const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} client.request = (method, path, _options) => { @@ -200,24 +212,28 @@ test('should delete a route with parameters', t => { }) test('delete routes without providing basepath or name', t => { - const client = { options: {} } + const client = {options: {}} const routes = new Routes(client) return t.throws(() => { routes.delete() }, /Missing mandatory parameters: basepath or name/) }) test('delete routes providing basepath and name', t => { - const client = { options: {} } + const client = {options: {}} const routes = new Routes(client) - return t.throws(() => { routes.delete({basepath: 'bp', name: 'nm'}) }, /Invalid parameters: use basepath or name, not both/) + return t.throws(() => { + routes.delete({ + basepath: 'bp', + name: 'nm' + }) + }, /Invalid parameters: use basepath or name, not both/) }) test('should create a route', t => { t.plan(3) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key } - const client = { path_url, options: client_options } - const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -231,7 +247,8 @@ test('should create a route', t => { namespace: '_', backendMethod: 'GET', backendUrl: 'https://openwhisk.ng.bluemix.net/api/v1/web/_/default/helloAction.http', - authkey: api_key } + authkey: apiKey + } } } @@ -247,10 +264,10 @@ test('should create a route', t => { test('should create a route from swagger file', t => { t.plan(3) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key } - const client = { path_url, options: client_options } + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -271,11 +288,10 @@ test('should create a route from swagger file', t => { test('should create a route with api name', t => { t.plan(3) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key } - const client = { path_url, options: client_options } - const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -290,7 +306,8 @@ test('should create a route with api name', t => { namespace: '_', backendMethod: 'GET', backendUrl: 'https://openwhisk.ng.bluemix.net/api/v1/web/_/default/helloAction.http', - authkey: api_key } + authkey: apiKey + } } } @@ -304,13 +321,12 @@ test('should create a route with api name', t => { return routes.create({relpath: '/hello', operation: 'GET', action: 'helloAction', name: 'SOME_NAME'}) }) -test('should create a route with apigw_token', t => { +test('should create a route with apigwToken', t => { t.plan(4) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key, apigw_token: 'token', apigw_space_guid: 'space' } - const client = { path_url, options: client_options } - const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey, apigwToken: 'token', apigwSpaceGuid: 'space'} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -324,7 +340,8 @@ test('should create a route with apigw_token', t => { namespace: '_', backendMethod: 'GET', backendUrl: 'https://openwhisk.ng.bluemix.net/api/v1/web/_/default/helloAction.http', - authkey: api_key } + authkey: apiKey + } } } @@ -332,7 +349,7 @@ test('should create a route with apigw_token', t => { t.is(method, 'POST') t.is(path, routes.routeMgmtApiPath('createApi')) t.deepEqual(_options.body, body) - t.deepEqual(_options.qs, { spaceguid: 'space', accesstoken: 'token'}) + t.deepEqual(_options.qs, {spaceguid: 'space', accesstoken: 'token'}) } const routes = new Routes(client) @@ -341,11 +358,10 @@ test('should create a route with apigw_token', t => { test('should create a route with response type', t => { t.plan(4) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key } - const client = { path_url, options: client_options } - const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -359,7 +375,8 @@ test('should create a route with response type', t => { namespace: '_', backendMethod: 'GET', backendUrl: 'https://openwhisk.ng.bluemix.net/api/v1/web/_/default/helloAction.http', - authkey: api_key } + authkey: apiKey + } } } @@ -367,20 +384,19 @@ test('should create a route with response type', t => { t.is(method, 'POST') t.is(path, routes.routeMgmtApiPath('createApi')) t.deepEqual(_options.body, body) - t.deepEqual(_options.qs, { responsetype: 'http' }) + t.deepEqual(_options.qs, {responsetype: 'http'}) } const routes = new Routes(client) return routes.create({relpath: '/hello', operation: 'GET', action: 'helloAction', responsetype: 'http'}) }) -test('should create a route with apigw_token and action with package', t => { +test('should create a route with apigwToken and action with package', t => { t.plan(4) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key, apigw_token: 'token', apigw_space_guid: 'space' } - const client = { path_url, options: client_options } - const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey, apigwToken: 'token', apigwSpaceGuid: 'space'} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -394,7 +410,8 @@ test('should create a route with apigw_token and action with package', t => { namespace: '_', backendMethod: 'GET', backendUrl: 'https://openwhisk.ng.bluemix.net/api/v1/web/_/package/helloAction.http', - authkey: api_key } + authkey: apiKey + } } } @@ -402,20 +419,19 @@ test('should create a route with apigw_token and action with package', t => { t.is(method, 'POST') t.is(path, routes.routeMgmtApiPath('createApi')) t.deepEqual(_options.body, body) - t.deepEqual(_options.qs, { spaceguid: 'space', accesstoken: 'token'}) + t.deepEqual(_options.qs, {spaceguid: 'space', accesstoken: 'token'}) } const routes = new Routes(client) return routes.create({relpath: '/hello', operation: 'GET', action: 'package/helloAction'}) }) -test('should create a route with apigw_token and action with package & ns', t => { +test('should create a route with apigwToken and action with package & ns', t => { t.plan(4) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key, apigw_token: 'token', apigw_space_guid: 'space' } - const client = { path_url, options: client_options } - const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey, apigwToken: 'token', apigwSpaceGuid: 'space'} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -429,7 +445,8 @@ test('should create a route with apigw_token and action with package & ns', t => namespace: 'ns', backendMethod: 'GET', backendUrl: 'https://openwhisk.ng.bluemix.net/api/v1/web/ns/package/helloAction.http', - authkey: api_key } + authkey: apiKey + } } } @@ -437,7 +454,7 @@ test('should create a route with apigw_token and action with package & ns', t => t.is(method, 'POST') t.is(path, routes.routeMgmtApiPath('createApi')) t.deepEqual(_options.body, body) - t.deepEqual(_options.qs, { spaceguid: 'space', accesstoken: 'token'}) + t.deepEqual(_options.qs, {spaceguid: 'space', accesstoken: 'token'}) } const routes = new Routes(client) @@ -446,11 +463,10 @@ test('should create a route with apigw_token and action with package & ns', t => test('should create a route using global ns', t => { t.plan(3) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key, namespace: 'global_ns'} - const client = { path_url, options: client_options} - const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey, namespace: 'global_ns'} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -464,7 +480,8 @@ test('should create a route using global ns', t => { namespace: 'global_ns', backendMethod: 'GET', backendUrl: 'https://openwhisk.ng.bluemix.net/api/v1/web/global_ns/default/helloAction.http', - authkey: api_key } + authkey: apiKey + } } } @@ -480,11 +497,10 @@ test('should create a route using global ns', t => { test('should create a route using basepath', t => { t.plan(3) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key } - const client = { path_url, options: client_options } - const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -498,7 +514,8 @@ test('should create a route using basepath', t => { namespace: '_', backendMethod: 'GET', backendUrl: 'https://openwhisk.ng.bluemix.net/api/v1/web/_/default/helloAction.http', - authkey: api_key } + authkey: apiKey + } } } @@ -514,11 +531,10 @@ test('should create a route using basepath', t => { test('should create a route using fully-qualified action name', t => { t.plan(3) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key } - const client = { path_url, options: client_options } - const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -532,7 +548,8 @@ test('should create a route using fully-qualified action name', t => { namespace: 'test', backendMethod: 'GET', backendUrl: 'https://openwhisk.ng.bluemix.net/api/v1/web/test/foo/helloAction.http', - authkey: api_key } + authkey: apiKey + } } } @@ -548,11 +565,10 @@ test('should create a route using fully-qualified action name', t => { test('should create a route using action name with ns', t => { t.plan(3) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key } - const client = { path_url, options: client_options } - const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey: apiKey} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -566,7 +582,8 @@ test('should create a route using action name with ns', t => { namespace: 'test', backendMethod: 'GET', backendUrl: 'https://openwhisk.ng.bluemix.net/api/v1/web/test/default/helloAction.http', - authkey: api_key } + authkey: apiKey + } } } @@ -582,11 +599,10 @@ test('should create a route using action name with ns', t => { test('should create a route using action name with ns overriding defaults', t => { t.plan(3) - const path_url = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` - const api_key = 'username:password' - const client_options = { api_key, namespace: 'global' } - const client = { path_url, options: client_options } - const options = {force: true, basepath: '/hello', relpath: '/bar/1', operation: 'GET'} + const pathUrl = path => `https://openwhisk.ng.bluemix.net/api/v1/${path}` + const apiKey = 'username:password' + const clientOptions = {apiKey, namespace: 'global'} + const client = {pathUrl, options: clientOptions} const body = { apidoc: { @@ -600,7 +616,8 @@ test('should create a route using action name with ns overriding defaults', t => namespace: 'test', backendMethod: 'GET', backendUrl: 'https://openwhisk.ng.bluemix.net/api/v1/web/test/default/helloAction.http', - authkey: api_key } + authkey: apiKey + } } } diff --git a/test/unit/rules.test.js b/test/unit/rules.test.js index b2b8265..c6bac97 100644 --- a/test/unit/rules.test.js +++ b/test/unit/rules.test.js @@ -9,7 +9,7 @@ const Rules = require('../../lib/rules') test('should list all rules without parameters', t => { t.plan(3) const ns = '_' - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) client.request = (method, path, options) => { @@ -23,7 +23,7 @@ test('should list all rules without parameters', t => { test('should list all rules with parameters', t => { t.plan(3) - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) client.request = (method, path, options) => { @@ -52,7 +52,7 @@ test('should list all rules with parameter count', t => { test('should retrieve rule from identifier', t => { t.plan(2) const ns = '_' - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) client.request = (method, path, options) => { @@ -66,7 +66,7 @@ test('should retrieve rule from identifier', t => { test('should retrieve rule from ruleName identifier', t => { t.plan(2) const ns = '_' - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) client.request = (method, path, options) => { @@ -80,7 +80,7 @@ test('should retrieve rule from ruleName identifier', t => { test('should retrieve rule from string identifier', t => { t.plan(2) const ns = '_' - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) client.request = (method, path, options) => { @@ -94,7 +94,7 @@ test('should retrieve rule from string identifier', t => { test('should delete rule from identifier', t => { t.plan(2) const ns = '_' - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) client.request = (method, path, options) => { @@ -108,7 +108,7 @@ test('should delete rule from identifier', t => { test('should delete rule from string identifier', t => { t.plan(2) const ns = '_' - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) client.request = (method, path, options) => { @@ -127,7 +127,7 @@ test('should throw error trying to invoke rule', t => { test('create a new rule', t => { t.plan(4) const ns = '_' - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) const name = '12345' @@ -138,7 +138,7 @@ test('create a new rule', t => { t.is(method, 'PUT') t.is(path, `namespaces/${ns}/rules/${name}`) t.deepEqual(options.qs, {}) - t.deepEqual(options.body, { action: `/_/${action}`, trigger: `/_/${trigger}` }) + t.deepEqual(options.body, {action: `/_/${action}`, trigger: `/_/${trigger}`}) } return rules.create({name, action, trigger}) @@ -147,7 +147,7 @@ test('create a new rule', t => { test('create a new rule using fully qualified names', t => { t.plan(4) const ns = '_' - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) const name = '12345' @@ -158,32 +158,38 @@ test('create a new rule using fully qualified names', t => { t.is(method, 'PUT') t.is(path, `namespaces/${ns}/rules/${name}`) t.deepEqual(options.qs, {}) - t.deepEqual(options.body, { action, trigger }) + t.deepEqual(options.body, {action, trigger}) } return rules.create({name, action, trigger}) }) test('create a rule without providing a rule name', t => { - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) - return t.throws(() => { rules.create({action: '', trigger: ''}) }, /name, ruleName/) + return t.throws(() => { + rules.create({action: '', trigger: ''}) + }, /name, ruleName/) }) test('create a rule without providing an action name', t => { const rules = new Rules() - return t.throws(() => { rules.create({name: '', trigger: ''}) }, /Missing mandatory action parameter/) + return t.throws(() => { + rules.create({name: '', trigger: ''}) + }, /Missing mandatory action parameter/) }) test('create a rule without providing a trigger name', t => { const rules = new Rules() - return t.throws(() => { rules.create({name: '', action: ''}) }, /Missing mandatory trigger parameter/) + return t.throws(() => { + rules.create({name: '', action: ''}) + }, /Missing mandatory trigger parameter/) }) test('update existing rule', t => { t.plan(4) const ns = '_' - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) const name = '12345' @@ -194,7 +200,7 @@ test('update existing rule', t => { t.is(method, 'PUT') t.is(path, `namespaces/${ns}/rules/${name}`) t.deepEqual(options.qs, {overwrite: true}) - t.deepEqual(options.body, { action: `/_/${action}`, trigger: `/_/${trigger}` }) + t.deepEqual(options.body, {action: `/_/${action}`, trigger: `/_/${trigger}`}) } return rules.update({name, action, trigger}) @@ -203,7 +209,7 @@ test('update existing rule', t => { test('should enable rule', t => { t.plan(3) const ns = '_' - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) const name = '12345' @@ -220,7 +226,7 @@ test('should enable rule', t => { test('should disable rule', t => { t.plan(3) const ns = '_' - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) const name = '12345' @@ -235,21 +241,21 @@ test('should disable rule', t => { }) test('should parse correct namespace for actions names with no other namespaces', t => { - const client = { options: {} } + const client = {options: {}} const rules = new Rules(client) - t.is(rules.convert_to_fqn('simple'), '/_/simple') - t.is(rules.convert_to_fqn('simple', 'a'), '/a/simple') - t.is(rules.convert_to_fqn('/a/simple'), '/a/simple') - t.is(rules.convert_to_fqn('/a/simple', 'b'), '/a/simple') + t.is(rules.convertToFqn('simple'), '/_/simple') + t.is(rules.convertToFqn('simple', 'a'), '/a/simple') + t.is(rules.convertToFqn('/a/simple'), '/a/simple') + t.is(rules.convertToFqn('/a/simple', 'b'), '/a/simple') }) test('should parse correct namespace for actions names with global namespace', t => { - const client = { options: { namespace: 'global' } } + const client = {options: {namespace: 'global'}} const rules = new Rules(client) - t.is(rules.convert_to_fqn('simple'), '/global/simple') - t.is(rules.convert_to_fqn('simple', 'a'), '/a/simple') - t.is(rules.convert_to_fqn('/a/simple'), '/a/simple') - t.is(rules.convert_to_fqn('/a/simple', 'b'), '/a/simple') + t.is(rules.convertToFqn('simple'), '/global/simple') + t.is(rules.convertToFqn('simple', 'a'), '/a/simple') + t.is(rules.convertToFqn('/a/simple'), '/a/simple') + t.is(rules.convertToFqn('/a/simple', 'b'), '/a/simple') }) diff --git a/test/unit/triggers.test.js b/test/unit/triggers.test.js index d855727..d463b35 100644 --- a/test/unit/triggers.test.js +++ b/test/unit/triggers.test.js @@ -23,7 +23,6 @@ test('should list all triggers without parameters', t => { test('should list all triggers with parameters', t => { t.plan(3) - const ns = '_' const client = {} const triggers = new Triggers(client) @@ -38,7 +37,6 @@ test('should list all triggers with parameters', t => { test('should list all triggers with parameter count', t => { t.plan(3) - const ns = '_' const client = {} const triggers = new Triggers(client) @@ -152,7 +150,6 @@ test('should invoke trigger from string identifier', t => { test('should invoke fully qualified trigger', t => { t.plan(3) - const ns = '_' const client = {} const triggers = new Triggers(client) @@ -167,7 +164,6 @@ test('should invoke fully qualified trigger', t => { test('should invoke fully qualified trigger with package', t => { t.plan(3) - const ns = '_' const client = {} const triggers = new Triggers(client) @@ -230,7 +226,7 @@ test('create a new trigger with custom body', t => { t.plan(4) const ns = '_' const client = {} - const trigger = { foo: 'bar' } + const trigger = {foo: 'bar'} const triggers = new Triggers(client) client.request = (method, path, options) => { diff --git a/tools/travis/build.sh b/tools/travis/build.sh index 3aaf856..5abd836 100755 --- a/tools/travis/build.sh +++ b/tools/travis/build.sh @@ -35,5 +35,6 @@ key=$(cat $WHISKDIR/ansible/files/auth.guest) # Test cd $ROOTDIR npm install --dev +npm run lint npm run code-coverage-build npm run code-coverage-run $key $edgehost guest true "travis,insecure" -- To stop receiving notification emails like this one, please contact jamestho...@apache.org.