Skip to content

Commit

Permalink
Adding first test for creating of new swagger file via the API
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Mar 7, 2018
1 parent cfb683b commit 866d0d1
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 16 deletions.
2 changes: 1 addition & 1 deletion config/test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"cli": "rdme",
"host": {
"url": "http://readme.local:3000"
"url": "http://dash.readme.local:3000"
}
}
24 changes: 11 additions & 13 deletions lib/push.js
Original file line number Diff line number Diff line change
@@ -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();
});

Expand Down
99 changes: 99 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 25 additions & 0 deletions test/push.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit 866d0d1

Please sign in to comment.