Skip to content

Commit

Permalink
More test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Jul 11, 2016
1 parent ae44add commit cd0c860
Showing 1 changed file with 69 additions and 1 deletion.
70 changes: 69 additions & 1 deletion test/middleware/resume.test.js
Expand Up @@ -440,7 +440,75 @@ describe('authorization', function() {
});
});

// WIP: Sending locals
describe('based on client, user, scope, and type, authorization request, and transaction locals, that supplies additional locals', function() {
var immediate, request, response, err;

before(function() {
immediate = function(client, user, scope, type, areq, locals, done) {
if (client.id !== '1234') { return done(new Error('incorrect client argument')); }
if (user.id !== 'u123') { return done(new Error('incorrect user argument')); }
if (scope !== 'email') { return done(new Error('incorrect scope argument')); }
if (type !== 'code') { return done(new Error('incorrect type argument')); }
if (areq.audience !== 'https://api.example.com/') { return done(new Error('incorrect areq argument')); }
if (locals.service.name !== 'Contacts') { return done(new Error('incorrect locals argument')) };

return done(null, true, { scope: 'profile email' }, { ip: '127.0.0.1' });
};
});

before(function(done) {
chai.connect.use('express', resume(server, immediate))
.req(function(req) {
request = req;
req.body = { code: '832076', _xsrf: '3ndukf8s'};
req.session = {};
req.session['authorize'] = {};
req.session['authorize']['abc123'] = { protocol: 'oauth2' };
req.user = { id: 'u123', username: 'bob' };
req.oauth2 = {};
req.oauth2.transactionID = 'abc123';
req.oauth2.client = { id: '1234', name: 'Example' };
req.oauth2.redirectURI = 'http://example.com/auth/callback';
req.oauth2.req = { type: 'code', scope: 'email', audience: 'https://api.example.com/' };
req.oauth2.locals = { service: { name: 'Contacts' } };
})
.end(function(res) {
response = res;
done();
})
.dispatch();
});

it('should not error', function() {
expect(err).to.be.undefined;
});

it('should set user on transaction', function() {
expect(request.oauth2.user).to.be.an('object');
expect(request.oauth2.user.id).to.equal('u123');
expect(request.oauth2.user.username).to.equal('bob');
});

it('should set response on transaction', function() {
expect(request.oauth2.res).to.be.an('object');
expect(request.oauth2.res.allow).to.be.true;
expect(request.oauth2.res.scope).to.equal('profile email');
expect(request.oauth2.info).to.be.undefined;
expect(request.oauth2.locals).to.be.an('object');
expect(Object.keys(request.oauth2.locals)).to.have.length(2);
expect(request.oauth2.locals.service.name).to.equal('Contacts');
expect(request.oauth2.locals.ip).to.equal('127.0.0.1');
});

it('should respond', function() {
expect(response.statusCode).to.equal(302);
expect(response.getHeader('Location')).to.equal('http://example.com/auth/callback');
});

it('should remove transaction from session', function() {
expect(request.session['authorize']['abc123']).to.be.undefined;
});
});

describe('encountering an error', function() {
var immediate, request, response, err;
Expand Down

0 comments on commit cd0c860

Please sign in to comment.