Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
"version": "1.0.0",
"description": "",
"main": "server.js",
"type": "module",
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"google-play-scraper": "^7.1.3",
"jsonwebtoken": "^8.5.1"
"jsonwebtoken": "^8.5.1",
"sinon": "^9.0.2"
},
"devDependencies": {
"jest": "^26.2.2",
"request": "^2.88.2"
},
"scripts": {
"test": "jest"
}
}
25 changes: 25 additions & 0 deletions tests/fixtures/auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"success": {
"res": {
"statusCode": 200,
"headers": {
"content-type": "application/json"
}
},
"body": {
"status": "success"
}
},
"failure": {
"res": {
"statusCode": 401,
"headers": {
"content-type": "application/json"
}
},
"body": {
"status": "error",
"message": "blabla no auth"
}
}
}
41 changes: 41 additions & 0 deletions tests/isAuthorized.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { isAuthorized } = require('../src/middlewares/isAuthorized');
const sinon = require('sinon');
const request = require('request');
const authFixtures = require('./fixtures/auth.json');

const base = 'http://localhost:3000';

describe('authTest', () => {
beforeEach(() => {
this.get = sinon.stub(request, 'get');
});
afterEach(() => {
request.restore();
});

describe('GET /auth', () => {
it('should respond with success', (done) => {
const obj = authFixtures.success;
this.get.yields(null, obj.res, JSON.stringify(obj.body));
request.get(`${base}/auth`, (err, res, body) => {
res.statusCode.should.equal(200);
res.headers['content-type'].should.contain('application/json');
body = JSON.parse(body);
body.status.should.eql('success');
done();
});
});
it('should respond with an error of missing auth in header', (done) => {
const obj = authFixtures.failure;
this.get.yields(null, obj.res, JSON.stringify(obj.body));
request.get(`${base}/api/v1/movies/999`, (err, res, body) => {
res.statusCode.should.equal(404);
res.headers['content-type'].should.contain('application/json');
body = JSON.parse(body);
body.status.should.eql('error');
body.message.should.eql('That movie does not exist.');
done();
});
});
});
})