Skip to content

Commit

Permalink
fix removal bug
Browse files Browse the repository at this point in the history
  • Loading branch information
esbenp committed Aug 13, 2017
1 parent 4771ebf commit 03f221f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
21 changes: 16 additions & 5 deletions bin/pdf-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ program
.command('api')
.description('Start the API')
.action(function (options) {
openConfig()
// We delay initiation of queue. This is because the API will load the DB in memory as
// copy A. When we make changes through the CLI this creates copy B. But next time the
// user pushes to the queue using the API copy A will be persisted again.
var initiateQueue = openConfig(true)

var apiOptions = configuration.api
var port = apiOptions.port

createApi(queue, {
createApi(initiateQueue, {
port: port,
token: apiOptions.token
}).listen(port, function() {
Expand Down Expand Up @@ -322,7 +325,7 @@ function processJob(job, configuration) {
})
}

function openConfig() {
function openConfig(delayQueueCreation = false) {
configuration = defaultConfig

if (!program.config) {
Expand Down Expand Up @@ -354,8 +357,16 @@ function openConfig() {
throw new Error('There is no pdf folder in the storage folder. Create it: storage/pdf')
}

var queueOptions = configuration.queue
queue = createQueue(path.join(configuration.storagePath, 'db/db.json'), queueOptions.lowDbOptions)
function initiateQueue() {
var queueOptions = configuration.queue
return createQueue(path.join(configuration.storagePath, 'db/db.json'), queueOptions.lowDbOptions)
}

if (delayQueueCreation) {
return initiateQueue
} else {
queue = initiateQueue()
}
}

function listJobs(queue, failed = false, limit) {
Expand Down
3 changes: 2 additions & 1 deletion src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var bodyParser = require('body-parser')
var debug = require('debug')('pdf:api')
var error = require('./error')

function createApi(queue, options = {}) {
function createApi(createQueue, options = {}) {
var api = express()
api.use(bodyParser.json())

Expand All @@ -15,6 +15,7 @@ function createApi(queue, options = {}) {
}

api.post('/', function(req, res) {
var queue = createQueue()
var authHeader = req.get('Authorization')

if (token && (!authHeader || authHeader.replace(/Bearer (.*)$/i, '$1') !== token)) {
Expand Down
38 changes: 23 additions & 15 deletions test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ var createApi = require('../src/api')
var error = require('../src/error')

describe('api: POST /', function () {
var api, queue
beforeEach(function() {
queue = {
addToQueue: sinon.spy()
}
api = createApi(queue, {
var api
beforeEach(function(){
api = createApi(function(){}, {
token: '1234'
})
})
Expand All @@ -28,15 +25,17 @@ describe('api: POST /', function () {
})

it('should return 422 on errorneous responses', function(done) {
queue = {
addToQueue: function() {
return {
code: '001',
error: true
queue = function () {
return {
addToQueue: function() {
return {
code: '001',
error: true
}
}
}
}
api = createApi(queue, {
var api = createApi(queue, {
token: '1234'
})

Expand All @@ -50,8 +49,17 @@ describe('api: POST /', function () {
it('should run the queue with the correct params', function (done) {
var meta = {id: 1}

queue.addToQueue = sinon.stub()
queue.addToQueue.onCall(0).returns({ id: '1234' })
var addToQueue = sinon.stub()
addToQueue.onCall(0).returns({ id: '1234' })

var queue = function() {
return {
addToQueue: addToQueue
}
}
var api = createApi(queue, {
token: '1234'
})

request(api)
.post('/')
Expand All @@ -61,7 +69,7 @@ describe('api: POST /', function () {
.end(function (err, res) {
if (err) return done(err)

if (!queue.addToQueue.calledWith({ url: 'https://google.com', meta: meta })) {
if (!addToQueue.calledWith({ url: 'https://google.com', meta: meta })) {
throw new Error('Queue was not called with correct url')
}

Expand Down

0 comments on commit 03f221f

Please sign in to comment.