diff --git a/config/test.json b/config/test.json index c4d638d97..c6e3b8d1f 100644 --- a/config/test.json +++ b/config/test.json @@ -1,6 +1,6 @@ { "cli": "rdme", "host": { - "url": "http://readme.local:3000" + "url": "http://dash.readme.local:3000" } } diff --git a/lib/push.js b/lib/push.js index 4e8ecab45..0ac523f65 100644 --- a/lib/push.js +++ b/lib/push.js @@ -1,32 +1,30 @@ var utils = require('../utils'); var request = require('request'); +var fs = require('fs'); +var path = require('path'); exports.swagger = true; exports.login = true; exports.desc = "Get a public URL for your API"; exports.category = "services"; -exports.run = function(config, info) { - var form = { - token: info.opts.token, - swagger: JSON.stringify(info.swagger), +exports.run = function(config, info, cb) { + if (!cb) cb = () => {}; + + var formData = { + swagger: fs.createReadStream(path.join(process.cwd(), info.args[0])), }; - request.post(`${config.host.url}/cli/swagger`, { json: form }, function(a, b, data) { - if (data.success) { + request.post(`${config.host.url}/v1/api/swagger`, { formData, auth: { user: info.opts.token } }, function(err, response, data) { + if (err) return cb(err); + if (response.statusCode === 201) { console.log(""); console.log("Success! ".green); - // TODO: Link to the docs here - /* - console.log(""); - console.log(" " + info.swaggerUrl); - console.log(""); - console.log("You can also use .yaml to get the YAML representation.".grey); - */ } else { console.log(data) console.error("There was an error uploading!".red); } + if (cb) return cb(); process.exit(); }); diff --git a/package-lock.json b/package-lock.json index 4dfdbe3e3..4bc9f7b76 100644 --- a/package-lock.json +++ b/package-lock.json @@ -630,6 +630,12 @@ "inherits": "2.0.3" } }, + "check-error": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz", + "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=", + "dev": true + }, "cli-cursor": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-1.0.2.tgz", @@ -830,6 +836,12 @@ } } }, + "deep-equal": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", + "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=", + "dev": true + }, "define-property": { "version": "0.2.5", "resolved": "https://registry.npmjs.org/define-property/-/define-property-0.2.5.tgz", @@ -1024,6 +1036,12 @@ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, + "get-func-name": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz", + "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=", + "dev": true + }, "get-value": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/get-value/-/get-value-2.0.6.tgz", @@ -1773,6 +1791,69 @@ "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.6.tgz", "integrity": "sha1-SJYrGeFp/R38JAs/HnMXYnu8R9s=" }, + "nock": { + "version": "9.2.3", + "resolved": "https://registry.npmjs.org/nock/-/nock-9.2.3.tgz", + "integrity": "sha512-4XYNSJDJ/PvNoH+cCRWcGOOFsq3jtZdNTRIlPIBA7CopGWJO56m5OaPEjjJ3WddxNYfe5HL9sQQAtMt8oyR9AA==", + "dev": true, + "requires": { + "chai": "4.1.2", + "debug": "3.1.0", + "deep-equal": "1.0.1", + "json-stringify-safe": "5.0.1", + "lodash": "4.17.5", + "mkdirp": "0.5.1", + "propagate": "1.0.0", + "qs": "6.5.1", + "semver": "5.5.0" + }, + "dependencies": { + "chai": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chai/-/chai-4.1.2.tgz", + "integrity": "sha1-D2RYS6ZC8PKs4oBiefTwbKI61zw=", + "dev": true, + "requires": { + "assertion-error": "1.0.2", + "check-error": "1.0.2", + "deep-eql": "3.0.1", + "get-func-name": "2.0.0", + "pathval": "1.1.0", + "type-detect": "4.0.8" + } + }, + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "deep-eql": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz", + "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==", + "dev": true, + "requires": { + "type-detect": "4.0.8" + } + }, + "lodash": { + "version": "4.17.5", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.5.tgz", + "integrity": "sha512-svL3uiZf1RwhH+cWrfZn3A4+U58wbP0tGVTLQPbjplZxZ8ROD9VLuNgsRniTlLe7OlSqR79RUehXgpBW/s0IQw==", + "dev": true + }, + "type-detect": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", + "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", + "dev": true + } + } + }, "node-status": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/node-status/-/node-status-1.0.0.tgz", @@ -1889,6 +1970,12 @@ "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" }, + "pathval": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz", + "integrity": "sha1-uULm1L3mUwBe9rcTYd74cn0GReA=", + "dev": true + }, "performance-now": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", @@ -1932,6 +2019,12 @@ "resolved": "https://registry.npmjs.org/prompt-sync/-/prompt-sync-4.1.5.tgz", "integrity": "sha1-cJrBgjiLDppKRbVoPtBEntGfPrg=" }, + "propagate": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/propagate/-/propagate-1.0.0.tgz", + "integrity": "sha1-AMLa7t2iDofjeCs0Stuhzd1q1wk=", + "dev": true + }, "punycode": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz", @@ -2109,6 +2202,12 @@ "integrity": "sha1-vsEf3IOp/aBjQBIQ5AF2wwJNFWc=", "dev": true }, + "semver": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz", + "integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA==", + "dev": true + }, "set-getter": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/set-getter/-/set-getter-0.1.0.tgz", diff --git a/package.json b/package.json index df81b2b44..0ffbd7715 100644 --- a/package.json +++ b/package.json @@ -53,8 +53,9 @@ "chai-string": "^1.2.0", "mocha": "^2.5.3", "mocha-sinon": "^1.1.5", - "sinon-chai": "^2.8.0", - "sinon": "^1.17.4" + "nock": "^9.2.3", + "sinon": "^1.17.4", + "sinon-chai": "^2.8.0" }, "scripts": { "test": "mocha" diff --git a/test/push.test.js b/test/push.test.js new file mode 100644 index 000000000..7e631c057 --- /dev/null +++ b/test/push.test.js @@ -0,0 +1,25 @@ +const nock = require('nock'); + +const utils = require('../utils'); +const push = require('../lib/push'); + +const config = utils.config('test'); +const apiKey = 'Xmw4bGctRVIQz7R7dQXqH9nQe5d0SPQs' + +describe('push action', () => { + after(() => nock.cleanAll()); + + it('should POST to the swagger api if no id provided', (done) => { + const mock = nock(config.host.url).post('/v1/api/swagger', (body) => { + return true; + return body.match('form-data; name=\"swagger\"'); + }).basicAuth({ user: apiKey, pass: '' }).reply(201); + + push.run(config, { args: ['./test/fixtures/json/swagger.json'], opts: { token: apiKey } }, (err) => { + if (err) return done(err); + mock.done(); + + return done(); + }); + }); +}); \ No newline at end of file