<commit_before>var chai = require('chai')
  , OAuthStrategy = require('../../lib/strategies/oauth');


describe('OAuthStrategy with default options', function() {
    
  var strategy = new OAuthStrategy({
      requestTokenURL: 'https://www.example.com/oauth/request_token',
      accessTokenURL: 'https://www.example.com/oauth/access_token',
      userAuthorizationURL: 'https://www.example.com/oauth/authorize',
      consumerKey: 'ABC123',
      consumerSecret: 'secret'
    }, function(token, tokenSecret, profile, done) {
      if (token == '<PASSWORD>' && tokenSecret == '<KEY>' && Object.keys(profile).length == 0) {
        return done(null, { id: '1234' }, { message: 'Hello' });
      }
      return done(null, false);
    });
    
  // inject a "mock" oauth instance
  strategy._oauth.getOAuthAccessToken = function(token, tokenSecret, verifier, callback) {
    if (token == '<PASSWORD>' && tokenSecret == '<KEY>' && verifier == '<PASSWORD>') {
      return callback(null, '<PASSWORD>', '<KEY>', {});
    } else {
      return callback(null, 'wrong-token', 'wrong-token-secret');
    }
  }
  
  strategy._oauth.getOAuthRequestToken = function(extraParams, callback) {
    if (extraParams.oauth_callback == undefined) {
      callback(null, 'hh5s93j4hdidpola', '<KEY>', {});
    } else {
      callback(new Error('wrong request token params'));
    }
  }
    
  describe('handling an authorized callback request', function() {
    var request
      , user
      , info;

    before(function(done) {
      chai.passport(strategy)
        .success(function(u, i) {
          user = u;
          info = i;
          done();
        })
        .req(function(req) {
          request = req;
          req.query = {};
          req.query['oauth_token'] = '<PASSWORD>';
          req.query['oauth_verifier'] = 'hfdp7dh39dks9884';
          req.session = {};
          req.session['oauth'] = {};
          req.session['oauth']['oauth_token'] = '<PASSWORD>';
          req.session['oauth']['oauth_token_secret'] = '<KEY>';
        })
        .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.message).to.equal('Hello');
    });
    
    it('should remove token and token secret from session', function() {
      expect(request.session['oauth']).to.be.undefined;
    });
  });
  
  describe('handling an authorized callback request that lacks request token in session', function() {
    var request
      , err;

    before(function(done) {
      chai.passport(strategy)
        .error(function(e) {
          err = e;
          done();
        })
        .req(function(req) {
          request = req;
          req.query = {};
          req.query['oauth_token'] = '<PASSWORD>';
          req.query['oauth_verifier'] = 'hfdp7dh39dks9884';
          req.session = {};
        })
        .authenticate();
    });

    it('should error', function() {
      expect(err).to.be.an.instanceof(Error);
      expect(err.message).to.equal('Failed to find request token in session');
    });
    
    it('should still lack token and token secret from session', function() {
      expect(request.session['oauth']).to.be.undefined;
    });
  });
  
  describe('handling an request to be redirected after obtaining a request token', function() {
    var request
      , url;

    before(function(done) {
      chai.passport(strategy)
        .redirect(function(u) {
          url = u;
          done();
        })
        .req(function(req) {
          request = req;
          req.session = {};
        })
        .authenticate();
    });

    it('should be redirected', function() {
      expect(url).to.equal('https://www.example.com/oauth/authorize?oauth_token=<PASSWORD>');
    });
    
    it('should store token and token secret in session', function() {
      expect(request.session['oauth']).to.not.be.undefined;
      expect(request.session['oauth']['oauth_token']).to.equal('<PASSWORD>');
      expect(request.session['oauth']['oauth_token_secret']).to.equal('<KEY>');
    });
  });
  
});


describe('OAuthStrategy with default options and URL with query parameters', function() {
    
  var strategy = new OAuthStrategy({
      requestTokenURL: 'https://www.example.com/oauth/request_token',
      accessTokenURL: 'https://www.example.com/oauth/access_token',
      userAuthorizationURL: 'https://www.example.com/oauth/authorize?foo=bar',
      consumerKey: 'ABC123',
      consumerSecret: 'secret'
    }, function(token, tokenSecret, profile, done) {
      if (token == 'nn<PASSWORD>' && tokenSecret == '<KEY>' && Object.keys(profile).length == 0) {
        return done(null, { id: '1234' }, { message: 'Hello' });
      }
      return done(null, false);
    });
    
  // inject a "mock" oauth instance
  strategy._oauth.getOAuthAccessToken = function(token, tokenSecret, verifier, callback) {
    if (token == '<PASSWORD>' && tokenSecret == '<KEY>' && verifier == 'hfdp7dh39dks9884') {
      return callback(null, 'nnch734d00sl2jdk', 'pfkk<PASSWORD>', {});
    } else {
      return callback(null, 'wrong-token', 'wrong-token-secret');
    }
  }
  
  strategy._oauth.getOAuthRequestToken = function(extraParams, callback) {
    callback(null, '<PASSWORD>', '<KEY>', {});
  }
  
  describe('handling an request to be redirected after obtaining a request token', function() {
    var request
      , url;

    before(function(done) {
      chai.passport(strategy)
        .redirect(function(u) {
          url = u;
          done();
        })
        .req(function(req) {
          request = req;
          req.session = {};
        })
        .authenticate();
    });

    it('should be redirected', function() {
      expect(url).to.equal('https://www.example.com/oauth/authorize?foo=bar&oauth_token=<PASSWORD>');
    });
    
    it('should store token and token secret in session', function() {
      expect(request.session['oauth']).to.not.be.undefined;
      expect(request.session['oauth']['oauth_token']).to.equal('<PASSWORD>');
      expect(request.session['oauth']['oauth_token_secret']).to.equal('<KEY>');
    });
  });
  
});<commit_msg>Clean up OAuth default test cases.<commit_after>var chai = require('chai')
  , OAuthStrategy = require('../../lib/strategies/oauth');


describe('OAuthStrategy', function() {
    
  var strategy = new OAuthStrategy({
      requestTokenURL: 'https://www.example.com/oauth/request_token',
      accessTokenURL: 'https://www.example.com/oauth/access_token',
      userAuthorizationURL: 'https://www.example.com/oauth/authorize',
      consumerKey: 'ABC123',
      consumerSecret: 'secret'
    }, function(token, tokenSecret, profile, done) {
      if (Object.keys(profile).length !== 0) { return done(null, false); }
      
      if (token == '<PASSWORD>' && tokenSecret == '<KEY>') {
        return done(null, { id: '1234' }, { message: 'Hello' });
      }
      return done(null, false);
    });
    
  // inject a "mock" oauth instance
  strategy._oauth.getOAuthAccessToken = function(token, tokenSecret, verifier, callback) {
    if (token == '<PASSWORD>' && tokenSecret == '<KEY>' && verifier == 'h<PASSWORD>') {
      return callback(null, 'nn<PASSWORD>', 'pf<PASSWORD>', {});
    } else {
      return callback(null, 'wrong-token', 'wrong-token-secret');
    }
  }
  
  strategy._oauth.getOAuthRequestToken = function(extraParams, callback) {
    if (extraParams.oauth_callback == undefined) {
      callback(null, 'hh5s93j4hdidpola', 'hd<PASSWORD>2<PASSWORD>3', {});
    } else {
      callback(null, 'wrong-token');
    }
  }
    
  describe('handling an authorized callback request', function() {
    var request
      , user
      , info;

    before(function(done) {
      chai.passport(strategy)
        .success(function(u, i) {
          user = u;
          info = i;
          done();
        })
        .req(function(req) {
          request = req;
          req.query = {};
          req.query['oauth_token'] = '<PASSWORD>';
          req.query['oauth_verifier'] = 'hfdp7dh39dks9884';
          req.session = {};
          req.session['oauth'] = {};
          req.session['oauth']['oauth_token'] = '<PASSWORD>';
          req.session['oauth']['oauth_token_secret'] = '<KEY>';
        })
        .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.message).to.equal('Hello');
    });
    
    it('should remove token and token secret from session', function() {
      expect(request.session['oauth']).to.be.undefined;
    });
  });
  
  describe('handling an authorized callback request that lacks request token in session', function() {
    var request
      , err;

    before(function(done) {
      chai.passport(strategy)
        .error(function(e) {
          err = e;
          done();
        })
        .req(function(req) {
          request = req;
          req.query = {};
          req.query['oauth_token'] = '<PASSWORD>';
          req.query['oauth_verifier'] = '<PASSWORD>';
          req.session = {};
        })
        .authenticate();
    });

    it('should error', function() {
      expect(err).to.be.an.instanceof(Error);
      expect(err.message).to.equal('Failed to find request token in session');
    });
    
    it('should still lack token and token secret in session', function() {
      expect(request.session['oauth']).to.be.undefined;
    });
  });
  
  describe('handling a request to be redirected after obtaining a request token', function() {
    var request
      , url;

    before(function(done) {
      chai.passport(strategy)
        .redirect(function(u) {
          url = u;
          done();
        })
        .req(function(req) {
          request = req;
          req.session = {};
        })
        .authenticate();
    });

    it('should be redirected', function() {
      expect(url).to.equal('https://www.example.com/oauth/authorize?oauth_token=<PASSWORD>');
    });
    
    it('should store token and token secret in session', function() {
      expect(request.session['oauth']).to.not.be.undefined;
      expect(request.session['oauth']['oauth_token']).to.equal('<PASSWORD>');
      expect(request.session['oauth']['oauth_token_secret']).to.equal('<KEY>');
    });
  });


  describe('with user authorization URL that contains query parameters', function() {
    var strategy = new OAuthStrategy({
        requestTokenURL: 'https://www.example.com/oauth/request_token',
        accessTokenURL: 'https://www.example.com/oauth/access_token',
        userAuthorizationURL: 'https://www.example.com/oauth/authorize?foo=bar',
        consumerKey: 'ABC123',
        consumerSecret: 'secret'
      }, function(token, tokenSecret, profile, done) {
        if (Object.keys(profile).length !== 0) { return done(null, false); }
        
        if (token == 'nn<PASSWORD>' && tokenSecret == '<KEY>') {
          return done(null, { id: '1234' }, { message: 'Hello' });
        }
        return done(null, false);
      });
    
    // inject a "mock" oauth instance
    strategy._oauth.getOAuthAccessToken = function(token, tokenSecret, verifier, callback) {
      if (token == '<PASSWORD>' && tokenSecret == '<KEY>' && verifier == '<PASSWORD>') {
        return callback(null, '<PASSWORD>', '<KEY>', {});
      } else {
        return callback(null, 'wrong-token', 'wrong-token-secret');
      }
    }
  
    strategy._oauth.getOAuthRequestToken = function(extraParams, callback) {
      callback(null, '<PASSWORD>', '<KEY>', {});
    }
  
    describe('handling a request to be redirected after obtaining a request token', function() {
      var request
        , url;

      before(function(done) {
        chai.passport(strategy)
          .redirect(function(u) {
            url = u;
            done();
          })
          .req(function(req) {
            request = req;
            req.session = {};
          })
          .authenticate();
      });

      it('should be redirected', function() {
        expect(url).to.equal('https://www.example.com/oauth/authorize?foo=bar&oauth_token=<PASSWORD>');
      });
    
      it('should store token and token secret in session', function() {
        expect(request.session['oauth']).to.not.be.undefined;
        expect(request.session['oauth']['oauth_token']).to.equal('<PASSWORD>');
        expect(request.session['oauth']['oauth_token_secret']).to.equal('<KEY>');
      });
    });
  });
  
});

{'fraction_non_alphanumeric': 0.16820731610966264, 'fraction_numerical': 0.006636643754444181, 'mean_word_length': 2.5939806927881883, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 16, 'lorem ipsum': 0, 'www.': 16, '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': '3383570', 'n_tokens_mistral': 3915, 'n_tokens_neox': 3833, 'n_words': 718}