Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update api.test.js #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 28 additions & 74 deletions test/api.test.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,37 @@
var sinon = require('sinon')
var request = require('supertest')
var createApi = require('../src/api')
var error = require('../src/error')
const sinon = require('sinon');
const request = require('supertest');
const createApi = require('../src/api');

describe('api: POST /', function () {
var api
beforeEach(function(){
api = createApi(function(){}, {
token: '1234'
})
})
describe('api: POST /', () => {
let api;

it('should return 401 if no token is given', function(done) {
request(api)
.post('/')
.expect(401, done)
})
beforeEach(() => {
api = createApi(() => {}, { token: '1234' });
});

it('should return 401 if invalid token is give', function (done) {
request(api)
.post('/')
.set('Authorization', 'Bearer test')
.expect(401, done)
})
it('should return 401 if no token is given', async () => {
await request(api).post('/').expect(401);
});

it('should return 422 on errorneous responses', function(done) {
queue = function () {
return {
addToQueue: function() {
return new Promise(function (resolve) {
resolve({
code: '001',
error: true
})
})
},
close: function(){}
}
}
var api = createApi(queue, {
token: '1234'
})
it('should return 401 if invalid token is given', async () => {
await request(api).post('/').set('Authorization', 'Bearer test').expect(401);
});

request(api)
.post('/')
.set('Authorization', 'Bearer 1234')
.send({})
.expect(422, done)
})
it('should return 422 on erroneous responses', async () => {
const queue = { addToQueue: () => Promise.resolve({ code: '001', error: true }), close: () => {} };
const api = createApi(queue, { token: '1234' });

it('should run the queue with the correct params', function (done) {
var meta = {id: 1}
await request(api).post('/').set('Authorization', 'Bearer 1234').send({}).expect(422);
});

var addToQueue = sinon.stub()
addToQueue.onCall(0).returns(new Promise(function (resolve) { resolve({ id: '1234' }) }))
it('should run the queue with the correct params', async () => {
const meta = { id: 1 };
const addToQueue = sinon.stub().resolves({ id: '1234' });
const queue = { addToQueue, close: () => {} };
const api = createApi(queue, { token: '1234' });

var queue = function() {
return {
addToQueue: addToQueue,
close: function(){}
}
}
var api = createApi(queue, {
token: '1234'
})
await request(api).post('/').set('Authorization', 'Bearer 1234').send({ url: 'https://google.com', meta: meta }).expect(201);

request(api)
.post('/')
.set('Authorization', 'Bearer 1234')
.send({ url: 'https://google.com', meta: meta })
.expect(201)
.end(function (err, res) {
if (err) return done(err)

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

done()
})
})
})
sinon.assert.calledWith(addToQueue, { url: 'https://google.com', meta: meta });
});
});