Skip to content

Commit

Permalink
Issues fixed with plugin dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
toonvanstrijp committed Jul 23, 2018
1 parent dc9dd4d commit 8ac6550
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
15 changes: 2 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ function plugin (fastify, options, next) {

fastify.decorateRequest('oauth', oauth);

if(!fastify.hasContentTypeParser('application/x-www-form-urlencoded')){
fastify.addContentTypeParser(
'application/x-www-form-urlencoded',
{parseAs: 'buffer', bodyLimit: opts.bodyLimit},
contentParser
);
}

function oauth(request, reply, next){
if(next === undefined){
oauthServer.authenticate()(request.raw, reply.res, () => {
Expand All @@ -27,10 +19,6 @@ function plugin (fastify, options, next) {
}
}

function contentParser (req, body, done) {
done(null, qs.parse(body.toString()));
}

fastify.post(prefix+'/authorize', (req, reply) => {
req.raw.query = req.query;
req.raw.body = req.body;
Expand All @@ -53,5 +41,6 @@ function plugin (fastify, options, next) {

module.exports = fp(plugin, {
fastify: '>= 0.39.0',
name: 'fastify-oauth-server'
name: 'fastify-oauth-server',
dependencies: ['fastify-formbody']
});
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"co-mocha": "^1.1.0",
"co-supertest": "0.0.8",
"fastify": "^1.8.0",
"fastify-formbody": "^2.0.1",
"jshint": "^2.8.0",
"mocha": "^2.0.1",
"request": "^2.87.0",
Expand Down
26 changes: 26 additions & 0 deletions test/integration/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ describe('FastifyOAuthServer', function() {

beforeEach(function() {
app = fastify();
app.register(require('fastify-formbody'))
app.register(require('../../'), {prefix: '/oauth', model: {}});
});

describe('authenticate()', function() {
it('should return an error if `model` is empty', function(done) {
app = fastify();

app.register(require('fastify-formbody'))

app.register(require('../../'), {prefix: '/oauth', model: {}});

app.get('/test', (req, res) => {
Expand All @@ -48,6 +51,9 @@ describe('FastifyOAuthServer', function() {

it('should authenticate the request', function(done) {
app = fastify();

app.register(require('fastify-formbody'))

var tokenExpires = new Date();
tokenExpires.setDate(tokenExpires.getDate() + 1);

Expand All @@ -73,6 +79,9 @@ describe('FastifyOAuthServer', function() {

it('should authenticate the request with middleware', function(done) {
app = fastify();

app.register(require('fastify-formbody'))

var tokenExpires = new Date();
tokenExpires.setDate(tokenExpires.getDate() + 1);

Expand Down Expand Up @@ -100,6 +109,9 @@ describe('FastifyOAuthServer', function() {

it('should unauthorized the request with middleware', function(done) {
app = fastify();

app.register(require('fastify-formbody'))

var tokenExpires = new Date();
tokenExpires.setDate(tokenExpires.getDate() - 60000);

Expand All @@ -125,6 +137,9 @@ describe('FastifyOAuthServer', function() {

it('should unauthorized the request with middleware custom response', function(done) {
app = fastify();

app.register(require('fastify-formbody'))

var tokenExpires = new Date();
tokenExpires.setDate(tokenExpires.getDate() + 60000);

Expand Down Expand Up @@ -155,6 +170,9 @@ describe('FastifyOAuthServer', function() {
describe('authorize()', function() {
it('should return an error', function(done) {
app = fastify();

app.register(require('fastify-formbody'));

var model = {
getAccessToken: function() {
return { user: {}, accessTokenExpiresAt: new Date() };
Expand Down Expand Up @@ -183,6 +201,9 @@ describe('FastifyOAuthServer', function() {

it('should return a `location` header with the code', function(done) {
app = fastify();

app.register(require('fastify-formbody'))

var model = {
getAccessToken: function() {
return { user: {}, accessTokenExpiresAt: new Date().setDate(new Date().getTime() + 60 * 60 * 1000) };
Expand All @@ -208,6 +229,8 @@ describe('FastifyOAuthServer', function() {
it('should return an error if `model` is empty', function(done) {
app = fastify();

app.register(require('fastify-formbody'))

app.register(require('../../'), {prefix: '/oauth', model: {}});

request(listen())
Expand All @@ -220,6 +243,9 @@ describe('FastifyOAuthServer', function() {
describe('token()', function() {
it('should return an `access_token`', function(done) {
app = fastify();

app.register(require('fastify-formbody'))

var model = {
getClient: function() {
return { grants: ['password'] };
Expand Down

0 comments on commit 8ac6550

Please sign in to comment.