PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/test/middleware/authenticate.error.callback.test.js

https://github.com/tharrington/passport
JavaScript | 91 lines | 69 code | 20 blank | 2 comment | 0 complexity | dcaebf4b3c66dcc17cbc97dcaea551c8 MD5 | raw file
  1. /* global describe, it, expect, before */
  2. /* jshint expr: true */
  3. var chai = require('chai')
  4. , authenticate = require('../../lib/middleware/authenticate')
  5. , Passport = require('../..').Passport;
  6. describe('middleware/authenticate', function() {
  7. describe('error with callback', function() {
  8. function Strategy() {
  9. }
  10. Strategy.prototype.authenticate = function(req) {
  11. this.error(new Error('something is wrong'));
  12. };
  13. var passport = new Passport();
  14. passport.use('error', new Strategy());
  15. var request, error, user;
  16. before(function(done) {
  17. function callback(e, u) {
  18. error = e;
  19. user = u;
  20. done();
  21. }
  22. chai.connect.use(authenticate(passport, 'error', callback))
  23. .req(function(req) {
  24. request = req;
  25. })
  26. .dispatch();
  27. });
  28. it('should pass error to callback', function() {
  29. expect(error).to.be.an.instanceOf(Error);
  30. expect(error.message).to.equal('something is wrong');
  31. });
  32. it('should pass user as undefined to callback', function() {
  33. expect(request.user).to.be.undefined;
  34. });
  35. it('should not set user on request', function() {
  36. expect(request.user).to.be.undefined;
  37. });
  38. });
  39. describe('error with callback and options passed to middleware', function() {
  40. function Strategy() {
  41. }
  42. Strategy.prototype.authenticate = function(req) {
  43. this.error(new Error('something is wrong'));
  44. };
  45. var passport = new Passport();
  46. passport.use('error', new Strategy());
  47. var request, error, user;
  48. before(function(done) {
  49. function callback(e, u) {
  50. error = e;
  51. user = u;
  52. done();
  53. }
  54. chai.connect.use(authenticate(passport, 'error', { foo: 'bar' }, callback))
  55. .req(function(req) {
  56. request = req;
  57. })
  58. .dispatch();
  59. });
  60. it('should pass error to callback', function() {
  61. expect(error).to.be.an.instanceOf(Error);
  62. expect(error.message).to.equal('something is wrong');
  63. });
  64. it('should pass user as undefined to callback', function() {
  65. expect(request.user).to.be.undefined;
  66. });
  67. it('should not set user on request', function() {
  68. expect(request.user).to.be.undefined;
  69. });
  70. });
  71. });