Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lipp committed Jan 16, 2016
1 parent 95a2c4d commit 75de8ce
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 7 deletions.
62 changes: 58 additions & 4 deletions test/doc-worker_spec.js
@@ -1,4 +1,4 @@
/* global describe it before beforeEach after */
/* global describe it before beforeEach after afterEach */
var assert = require('assert')
var Doclet = require('../lib/models/doclet')
var services = require('../lib/services')
Expand All @@ -8,6 +8,7 @@ var fs = require('fs')
var fse = require('fs-extra')
var path = require('path')
var docWorker = require('../lib/doc-worker')
var sinon = require('sinon')

var loadGitHubEvent = function (eventDir) {
var payload = fs.readFileSync(path.join(__dirname, 'fixtures/events', eventDir, 'payload.json'))
Expand All @@ -18,7 +19,9 @@ var tmpPath = path.join(__dirname, 'git-temp')

describe('The doc-worker module', function () {
this.timeout(10000)
this.slow(8000)
var inbox
var sandbox

before(function (done) {
docWorker.init(tmpPath)
Expand All @@ -32,10 +35,15 @@ describe('The doc-worker module', function () {
})

beforeEach(function (done) {
sandbox = sinon.sandbox.create()
Doclet.remove({}, done)
})

it('pushing a push event to the inbox will create a doclet', function (done) {
afterEach(function () {
sandbox.restore()
})

it('a push event will create a doclet', function (done) {
var event = loadGitHubEvent('acme-push')
inbox.add(event)
setTimeout(function () {
Expand All @@ -53,11 +61,29 @@ describe('The doc-worker module', function () {
}, 3000)
})

it('pushing a push event to the inbox will create a doclet', function (done) {
it('a failing Doclet.createFromGitHubEvent is handled', function (done) {
var event = loadGitHubEvent('acme-push')
var failed = new Bull('failed', services.redis.port, services.redis.host)
failed.on('ready', function () {
inbox.add(event)
failed.process(function (job, jobDone) {
assert.equal(job.data.ref, event.ref)
jobDone()
inbox.count().then(function (count) {
assert.equal(count, 0)
done()
})
failed.close()
})
})
sandbox.stub(Doclet, 'createFromGitHubEvent').yields('some error')
})

it('a tag event will create a doclet', function (done) {
var event = loadGitHubEvent('acme-tag')
inbox.add(event)
setTimeout(function () {
Doclet.findById('lipp/acme-jsdoc-example/master', function (err, doclet) {
Doclet.findById('lipp/acme-jsdoc-example/v1.0.11', function (err, doclet) {
if (err) {
done(err)
} else {
Expand All @@ -70,4 +96,32 @@ describe('The doc-worker module', function () {
})
}, 3000)
})

it('pushing a trash event to the inbox will not create a doclet', function (done) {
var event = {a: 123}
sandbox.stub(Doclet, 'createFromGitHubEvent')
var failed = new Bull('failed', services.redis.port, services.redis.host)
failed.on('ready', function () {
inbox.add(event)
failed.process(function (job, jobDone) {
assert.equal(job.data.a, 123)
jobDone()
failed.close().then(done)
})
})
})

it('pushing a push event with non branch head ref will not create a doclet', function (done) {
var event = loadGitHubEvent('acme-push')
event.ref = 'refs/foo/master'
sandbox.stub(Doclet, 'createFromGitHubEvent')
inbox.add(event)
setTimeout(function () {
assert(!Doclet.createFromGitHubEvent.called)
inbox.count().then(function (count) {
assert.equal(count, 0)
done()
})
}, 100)
})
})
92 changes: 89 additions & 3 deletions test/repo_spec.js
Expand Up @@ -63,7 +63,9 @@ describe('The repo module', function () {
})

it('.getHook() wrong url', function (done) {
sandbox.stub(repo.github(), 'authenticate').returns()
sandbox.stub(repo.github(), 'authenticate')
.withArgs({foo: 1})
.returns()
sandbox.stub(repo.github().repos, 'getHooks')
.withArgs({user: 'lipp', repo: 'bar', per_page: 100})
.yields(null, [{config: {url: 'hrr'}}])
Expand All @@ -75,8 +77,10 @@ describe('The repo module', function () {
})
})

it('.getHook() wrong url', function (done) {
sandbox.stub(repo.github(), 'authenticate').returns()
it('.addHook() creating new hook', function (done) {
sandbox.stub(repo.github(), 'authenticate')
.withArgs({foo: 1})
.returns()
sandbox.stub(repo.github().repos, 'getHooks')
.withArgs({user: 'lipp', repo: 'bar', per_page: 100})
.yields(null, [{config: {url: 'hrr'}}])
Expand All @@ -102,6 +106,88 @@ describe('The repo module', function () {
})
})

it('.addHook() updating existing hook', function (done) {
var hook = {
config: {
url: 'http://api.doclets.io/github/callback'
},
id: 123
}
sandbox.stub(repo.github(), 'authenticate')
.withArgs({foo: 1})
.returns()
sandbox.stub(repo.github().repos, 'getHooks')
.withArgs({user: 'lipp', repo: 'bar', per_page: 100})
.yields(null, [hook])

sandbox.stub(repo.github().repos, 'updateHook')
.withArgs({
user: 'lipp',
repo: 'bar',
name: 'web',
active: true,
id: hook.id,
config: {
secret: '12345678',
url: 'http://api.doclets.io/github/callback',
'content_type': 'json'
}
}).yields(null, 123)

repo.addHook('lipp', 'bar', {foo: 1}, function (err, hook) {
assert(!err)
assert.equal(hook, 123)
done()
})
})

it('.addHook() forwards getHook error', function (done) {
sandbox.stub(repo.github(), 'authenticate')
.withArgs({foo: 1})
.returns()
sandbox.stub(repo.github().repos, 'getHooks')
.withArgs({user: 'lipp', repo: 'bar', per_page: 100})
.yields('some error')

repo.addHook('lipp', 'bar', {foo: 1}, function (err, hook) {
assert.equal(err, 'some error')
assert(!hook)
done()
})
})

it('.removeHook() an existing hook', function (done) {
var hook = {
config: {
url: 'http://api.doclets.io/github/callback'
},
id: 123
}
sandbox.stub(repo.github(), 'authenticate')
.withArgs({foo: 1})
.returns()

sandbox.stub(repo.github().repos, 'updateHook')
.withArgs({
user: 'lipp',
repo: 'bar',
name: 'web',
active: false,
id: hook.id,
config: {
secret: '12345678',
url: 'http://api.doclets.io/github/callback',
'content_type': 'json'
}
}).yields(null, 123)

repo.removeHook('lipp', 'bar', {foo: 1}, hook, function (err, hook) {
assert(!err)
assert.equal(hook, 123)
done()
})
})

it('.getUserRepos() ', function (done) {
sandbox.stub(repo.github(), 'authenticate').returns()
sandbox.stub(repo.github().repos, 'getFromUser')
Expand Down

0 comments on commit 75de8ce

Please sign in to comment.