Skip to content

Commit

Permalink
finally reintroduce some unittest thanks to nock
Browse files Browse the repository at this point in the history
  • Loading branch information
malko committed Jun 14, 2015
1 parent b1f1248 commit 6455d84
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 71 deletions.
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -37,7 +37,7 @@
"url": "https://github.com/malko/node-promised-redmine.git"
},
"scripts": {
"test": "TEST_REDMINE_APIKEY= TEST_REDMINE_HOST= node_modules/.bin/expresso test/*"
"test": "mocha test/*"
},
"bugs": {
"url": "https://github.com/malko/node-promised-redmine/issues"
Expand All @@ -54,6 +54,8 @@
"split-ca": "^1.0.0"
},
"devDependencies": {
"expresso": ">=0.9.2"
"chai": "^3.0.0",
"mocha": "^2.2.5",
"nock": "^2.5.0"
}
}
322 changes: 253 additions & 69 deletions test/redmine.test.js
@@ -1,80 +1,264 @@
var path = require('path');
var assert = require('assert');
var util = require('util');
/*global describe, it, beforeEach*/
'use strict';
var mocha = require('mocha'); //jshint ignore:line
var expect = require('chai').expect;
var nock = require('nock');
var Redmine = require('../lib/redmine.js');
var dummyObj = {id: 5, status: 'ok'};

var basedir = path.join(__dirname, '..');
var libdir = path.join(basedir, 'lib');
var assert = require('assert');
function expectDummy(promise, done){
promise.then(function(res) {
expect(res).to.eql(dummyObj);
done();
});
}

var Redmine = require(path.join(libdir, 'redmine.js'));

assert.ok('TEST_REDMINE_APIKEY' in process.env);
assert.ok('TEST_REDMINE_HOST' in process.env);
describe('node-promised-redmine', function() {
var redmineApi, mockServer;
beforeEach(function() {
// nock.recorder.rec();
nock.disableNetConnect();
mockServer = nock('http://127.0.0.1:9090');
// mockServer.log(console.log);
var config = {
host: "127.0.0.1", // required
port: 9090,
apiKey: "XXXXXX", // required
pathPrefix: "/myRedminePath",
protocol: "http"
};
redmineApi = new Redmine(config);
// redmineApi.setVerbose(true);
});

var config = {
host: process.env.TEST_REDMINE_HOST,
apiKey: process.env.TEST_REDMINE_APIKEY
};

var redmine = new Redmine(config);
module.exports = {
'config': function(beforeExit, assert)
{
assert.ok(process.env.TEST_REDMINE_HOST == redmine.getHost())
assert.ok(process.env.TEST_REDMINE_APIKEY == redmine.getApiKey())
}
,'config error': function(beforeExit, assert)
{
try {
new Redmine({});
} catch (e) {
assert.type(e, 'object');
assert.includes(e.message, 'Error: apiKey and host must be configured.');
}
}
,'get issue error': function(beforeExit, assert)
{
redmine.getIssue({foo: 1}).error(function(err){
assert.type(err, 'object');
assert.includes(err.message, 'Error: Argument #1 id must be integer');
});
}
,'get issue': function(beforeExit, assert)
{
redmine.getIssue(1)
.error(function(err){ assert.isNull(err, 'Err is null'); })
.success(function(data){
assert.type(data.issue, 'object', 'Data issue is an object');
describe('have a request method which', function() {
it('should add a "x-redmine-api-key" header', function(done) {
mockServer.get('/myRedminePath/testApiKey')
.matchHeader('x-redmine-api-key', 'XXXXXX')
.reply(200, dummyObj)
;
expectDummy(redmineApi.request('get', '/testApiKey'), done);
});
it('should handle a "impersonate" parameter', function(done) {
mockServer.get('/myRedminePath/testImpersonate')
.matchHeader('X-Redmine-Switch-User', 'fakeuser')
.reply(200, dummyObj)
;
expectDummy(redmineApi.request('get', '/testImpersonate', {impersonate: 'fakeuser'}), done);
});
it('should handle a "retry" object parameter', function(done) {
mockServer.get('/myRedminePath/testRetry')
.replyWithError('Broken')
.get('/myRedminePath/testRetry')
.reply(200, dummyObj)
;
expectDummy(redmineApi.request('get', '/testRetry', {retry: {maxTry: 3, maxDelay: 50}}), done);
});
});

var issue = data.issue;
assert.equal(1, issue.id);
assert.equal('Ticket1', issue.subject);
describe('have a get method which', function(){
it('should prepend "/" and append ".json" to the given path', function(done) {
mockServer
.get('/myRedminePath/testpath.json')
.reply(200, dummyObj)
;
redmineApi.get('testpath')
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
it('should handle retry, and impersonate parameters', function(done) {
mockServer
.get('/myRedminePath/testpath.json')
.matchHeader('X-Redmine-Switch-User', 'fakeuser')
.replyWithError('Broken')
.get('/myRedminePath/testpath.json')
.matchHeader('X-Redmine-Switch-User', 'fakeuser')
.reply(200, dummyObj)
;
redmineApi.get('testpath', {retry:{maxTry: 2}, impersonate: 'fakeuser'})
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
it('should handle additional params', function(done) {
mockServer
.get('/myRedminePath/testpath.json?param=1&param2=ok')
.reply(200, dummyObj)
;
redmineApi.get('testpath', {param:1, param2: 'ok'})
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
});

});
}
,'get issues': function(beforeExit, assert)
{
redmine.getIssues({project_id: 1, limit: 2})
.error(function(err){ assert.isNull(err, 'Err is null'); })
.success(function(data) { assert.equal(data.limit, 2); })
;
}
,'JSONStringify': function(beforeExit, assert)
{
var hoge = {hoge: 1}, converted;
assert.equal(redmine.JSONStringify(hoge), '{"hoge":1}'); // plain JSON
describe('have a post method which', function(){
it('should prepend "/" and append ".json" to the given path', function(done) {
mockServer
.post('/myRedminePath/testpath.json')
.reply(200, dummyObj)
;
redmineApi.post('testpath')
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
it('should handle retry, and impersonate parameters', function(done) {
mockServer
.post('/myRedminePath/testpath.json')
.matchHeader('X-Redmine-Switch-User', 'fakeuser')
.replyWithError('Broken')
.post('/myRedminePath/testpath.json')
.matchHeader('X-Redmine-Switch-User', 'fakeuser')
.reply(200, dummyObj)
;
redmineApi.post('testpath', {retry:{maxTry: 2}, impersonate: 'fakeuser'})
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
it('should handle additional params', function(done) {
mockServer
.post('/myRedminePath/testpath.json', {param:1, param2: 'ok'})
.reply(200, dummyObj)
;
redmineApi.post('testpath', {param:1, param2: 'ok'})
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
});

hoge = {hoge: 'JSON with "escape string"'};
assert.equal(redmine.JSONStringify(hoge), '{"hoge":"JSON with \\\"escape string\\\""}');
describe('have a put method which', function(){
it('should prepend "/" and append ".json" to the given path', function(done) {
mockServer
.put('/myRedminePath/testpath.json')
.reply(200, dummyObj)
;
redmineApi.put('testpath')
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
it('should handle retry, and impersonate parameters', function(done) {
mockServer
.put('/myRedminePath/testpath.json')
.matchHeader('X-Redmine-Switch-User', 'fakeuser')
.replyWithError('Broken')
.put('/myRedminePath/testpath.json')
.matchHeader('X-Redmine-Switch-User', 'fakeuser')
.reply(200, dummyObj)
;
redmineApi.put('testpath', {retry:{maxTry: 2}, impersonate: 'fakeuser'})
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
it('should handle additional params', function(done) {
mockServer
.put('/myRedminePath/testpath.json', {param:1, param2: 'ok'})
.reply(200, dummyObj)
;
redmineApi.put('testpath', {param:1, param2: 'ok'})
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
});

describe('have a del method which', function(){
it('should prepend "/" and append ".json" to the given path', function(done) {
mockServer
.delete('/myRedminePath/testpath.json') //jshint ignore:line
.reply(200, dummyObj)
;
redmineApi.del('testpath')
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
it('should handle retry, and impersonate parameters', function(done) {
mockServer
.delete('/myRedminePath/testpath.json') //jshint ignore:line
.matchHeader('X-Redmine-Switch-User', 'fakeuser')
.replyWithError('Broken')
.delete('/myRedminePath/testpath.json') //jshint ignore:line
.matchHeader('X-Redmine-Switch-User', 'fakeuser')
.reply(200, dummyObj)
;
redmineApi.del('testpath', {retry:{maxTry: 2}, impersonate: 'fakeuser'})
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
it('should handle additional params', function(done) {
mockServer
.delete('/myRedminePath/testpath.json', {param:1, param2: 'ok'}) //jshint ignore:line
.reply(200, dummyObj)
;
redmineApi.del('testpath', {param:1, param2: 'ok'})
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
});

hoge = {hoge: 'JSON with 日本語'};
converted = redmine.JSONStringify(hoge);
assert.equal(converted, '{"hoge":"JSON with \\u65e5\\u672c\\u8a9e"}');
assert.eql(JSON.parse(converted), hoge); // invertible
describe('have a getIssue method which', function(){
it('should return a single issue', function(done) {
mockServer
.get('/myRedminePath/issues/5.json')
.reply(200, dummyObj)
;
redmineApi.getIssue(5)
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
});

hoge = {hoge: 'JSON with \n \r \b \\ \f \t '};
converted = redmine.JSONStringify(hoge);
assert.equal(converted, '{"hoge":"JSON with \\n \\r \\b \\\\ \\f \\t "}');
}
};
describe('have a postIssue method which', function(){
it('should post given issue to the server as an issue property', function(done) {
mockServer
.post('/myRedminePath/issues.json', {issue: dummyObj})
.reply(200, dummyObj)
;
redmineApi.postIssue(dummyObj)
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
it('should handle retry, and impersonate parameters', function(done) {
mockServer
.post('/myRedminePath/issues.json', {issue: dummyObj})
.matchHeader('X-Redmine-Switch-User', 'fakeuser')
.replyWithError('Broken')
.post('/myRedminePath/issues.json', {issue: dummyObj})
.matchHeader('X-Redmine-Switch-User', 'fakeuser')
.reply(200, dummyObj)
;
redmineApi.postIssue(dummyObj, {retry:{maxTry: 2}, impersonate: 'fakeuser'})
.then(function(issue){
expect(issue).to.eql(dummyObj);
done();
});
});
});

});

0 comments on commit 6455d84

Please sign in to comment.