<commit_before>var chai = require('chai')
  , Strategy = require('../lib/strategy');


describe('Strategy', function() {
    
  var strategy = new Strategy(function(token, done) {
    if (token == '<PASSWORD>') { 
      return done(null, { id: '1234' }, { scope: 'read' });
    }
    return done(null, false);
  });
  
  it('should authenticate request with token in header', function(done) {
    var strategy = new Strategy(function(token, cb) {
      expect(token).to.equal('<PASSWORD>');
      return cb(null, { id: '248289761001' }, { scope: [ 'profile', 'email' ] });
    });
    
    chai.passport.use(strategy)
      .request(function(req) {
        req.headers['authorization'] = 'Bearer <PASSWORD>';
      })
      .success(function(user, info) {
        expect(user).to.deep.equal({ id: '248289761001' });
        expect(info).to.deep.equal({ scope: [ 'profile', 'email' ] });
        done();
      })
      .authenticate();
  });
  
  describe('handling a request with valid token in form-encoded body parameter', function() {
    var user
      , info;
    
    before(function(done) {
      chai.passport.use(strategy)
        .success(function(u, i) {
          user = u;
          info = i;
          done();
        })
        .request(function(req) {
          req.body = {};
          req.body.access_token = '<PASSWORD>';
        })
        .authenticate();
    });
    
    it('should supply user', function() {
      expect(user).to.be.an.object;
      expect(user.id).to.equal('1234');
    });
    
    it('should supply info', function() {
      expect(info).to.be.an.object;
      expect(info.scope).to.equal('read');
    });
  });
  
  describe('handling a request with valid credential in URI query parameter', function() {
    var user
      , info;
    
    before(function(done) {
      chai.passport.use(strategy)
        .success(function(u, i) {
          user = u;
          info = i;
          done();
        })
        .request(function(req) {
          req.query = {};
          req.query.access_token = '<PASSWORD>';
        })
        .authenticate();
    });
    
    it('should supply user', function() {
      expect(user).to.be.an.object;
      expect(user.id).to.equal('1234');
    });
    
    it('should supply info', function() {
      expect(info).to.be.an.object;
      expect(info.scope).to.equal('read');
    });
  });
  
  describe('handling a request with wrong token in header', function() {
    var challenge;
    
    before(function(done) {
      chai.passport.use(strategy)
        .fail(function(c) {
          challenge = c;
          done();
        })
        .request(function(req) {
          req.headers.authorization = 'Bearer WRONG';
        })
        .authenticate();
    });
    
    it('should fail with challenge', function() {
      expect(challenge).to.be.a.string;
      expect(challenge).to.equal('Bearer realm="Users", error="invalid_token"');
    });
  });
  
  describe('handling a request without credentials', function() {
    var challenge;
    
    before(function(done) {
      chai.passport.use(strategy)
        .fail(function(c) {
          challenge = c;
          done();<commit_msg>Refactoring tests.<commit_after>var chai = require('chai')
  , Strategy = require('../lib/strategy');


describe('Strategy', function() {
    
  var strategy = new Strategy(function(token, done) {
    if (token == '<PASSWORD>') { 
      return done(null, { id: '1234' }, { scope: 'read' });
    }
    return done(null, false);
  });
  
  describe('valid token', function() {
    
    var strategy = new Strategy(function(token, cb) {
      expect(token).to.equal('<PASSWORD>');
      return cb(null, { id: '248289761001' }, { scope: [ 'profile', 'email' ] });
    });
  
  
    it('should authenticate request with token in header field', function(done) {
      chai.passport.use(strategy)
        .request(function(req) {
          req.headers['authorization'] = 'Bearer <PASSWORD>';
        })
        .success(function(user, info) {
          expect(user).to.deep.equal({ id: '248289761001' });
          expect(info).to.deep.equal({ scope: [ 'profile', 'email' ] });
          done();
        })
        .authenticate();
    }); // should authenticate request with token in header field
  
    it('should authenticate request with token in form-encoded body parameter', function(done) {
      chai.passport.use(strategy)
        .success(function(user, info) {
          expect(user).to.deep.equal({ id: '248289761001' });
          expect(info).to.deep.equal({ scope: [ 'profile', 'email' ] });
          done();
        })
        .request(function(req) {
          req.body = {};
          req.body.access_token = '<PASSWORD>';
        })
        .authenticate();
    });
    
    it('should authenticate request with token in URI query parameter', function(done) {
      chai.passport.use(strategy)
        .success(function(user, info) {
          expect(user).to.deep.equal({ id: '248289761001' });
          expect(info).to.deep.equal({ scope: [ 'profile', 'email' ] });
          done();
        })
        .request(function(req) {
          req.query = {};
          req.query.access_token = '<PASSWORD>';
        })
        .authenticate();
    }); // should authenticate request with token in URI query parameter
  
  }); // valid token
  
  describe('invalid token', function() {
    
    var strategy = new Strategy(function(token, cb) {
      return cb(null, false);
    });
    
    
    it('should refuse request', function(done) {
      chai.passport.use(strategy)
        .fail(function(challenge, status) {
          expect(challenge).to.equal('Bearer realm="Users", error="invalid_token"');
          expect(status).to.be.undefined;
          done();
        })
        .request(function(req) {
          req.headers['authorization'] = 'Bearer m<PASSWORD>';
        })
        .authenticate();
    }); // should refuse request
    
  }); // invalid token
  
  
  
  
  
  
  describe('handling a request without credentials', function() {
    var challenge;
    
    before(function(done) {
      chai.passport.use(strategy)
        .fail(function(c) {
          challenge = c;
          done();
{'fraction_non_alphanumeric': 0.1626554676142788, 'fraction_numerical': 0.014214181876918107, 'mean_word_length': 2.2033109156751163, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1979154', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1853, 'n_words': 369}