<commit_before>const credentials = require('../lib/credentials');
const http = require('http');
const jwt = require('jsonwebtoken');

describe('/oauth', function () {

  const token = (payload, opts) => {
    const defaultPayload = { scope: 'db', service: 'onedrive' };
    const defaultOpts = {
      algorithm: 'RS256',
      audience: 'https://api.digitallinguistics.org',
      expiresIn: 300,
      jwtid: '1<PASSWORD>',
      subject: '010101'
    };
    payload = payload || {};
    opts = opts || {};
    Object.assign(defaultPayload, payload);
    Object.assign(defaultOpts, opts);
    return jwt.sign(defaultPayload, credentials.key, defaultOpts);
  };

  const options = props => {
    props = props || {};
    const defaults = {
      hostname: 'localhost',
      method: 'POST',
      path: '/oauth',
      port: 3000,
      headers: { Authorization: `Bearer ${token()}` }
    };
    Object.assign(defaults, props);
    return defaults;
  };

  const makeRequest = (opts, handler) => {
    http.request(opts || options(), res => {
      var data = '';
      res.on('data', chunk => data += chunk);
      res.on('error', err => fail(err));
      res.on('end', () => handler(JSON.parse(data)));
    }).end();
  };

  beforeAll(function () {
    console.log('OAuth: starting');
  });

  afterAll(function () {
    console.log('OAuth: finished');
  });

  it('returns a 401 response if the token is missing', function (done) {
    const handler = result => {
      expect(result.status).toEqual(401);
      done();
    };
    const opts = options({ headers: {} });
    makeRequest(opts, handler);
  });

  it('returns a 401 response if the token is invalid', function (done) {
    const handler = result => {
      expect(result.status).toEqual(401);
      done();
    };
    const opts = options({ headers: { Authorization: `Bearer ${token() + 'hello'}` } });
    makeRequest(opts, handler);
  });

  it('returns a 401 response if the sub claim is missing', function (done) {
    const handler = result => {
      expect(result.status).toEqual(401);
      done();
    };
    const t = token(null, { subject: undefined });
    const opts = options({ headers: { Authorization: `Bearer ${t}` } });
    makeRequest(opts, handler);
  });

  it('returns a 401 response if the service claim is missing');

  it('returns a 401 response if the JWT ID (state) is missing');

  it('returns a 403 response if the scope is not `db`');

  it('returns a 405 response for non-POST requests');

  // should include the original state as well as a DLx token in the querystring
  it('redirects to the original redirect URI if the token is valid');

});<commit_msg>passing test<commit_after>const credentials = require('../lib/credentials');
const http = require('http');
const jwt = require('jsonwebtoken');

describe('/oauth', function () {

  const token = (payload, opts) => {
    const defaultPayload = { scope: 'db', service: 'onedrive' };
    const defaultOpts = {
      algorithm: 'RS256',
      audience: 'https://api.digitallinguistics.org',
      expiresIn: 300,
      jwtid: '<PASSWORD>',
      subject: '010101'
    };
    payload = payload || {};
    opts = opts || {};
    Object.assign(defaultPayload, payload);
    Object.assign(defaultOpts, opts);
    return jwt.sign(defaultPayload, credentials.key, defaultOpts);
  };

  const options = props => {
    props = props || {};
    const defaults = {
      hostname: 'localhost',
      method: 'POST',
      path: '/oauth',
      port: 3000,
      headers: { Authorization: `Bearer ${token()}` }
    };
    Object.assign(defaults, props);
    return defaults;
  };

  const makeRequest = (opts, handler) => {
    http.request(opts || options(), res => {
      var data = '';
      res.on('data', chunk => data += chunk);
      res.on('error', err => fail(err));
      res.on('end', () => handler(JSON.parse(data)));
    }).end();
  };

  beforeAll(function () {
    console.log('OAuth: starting');
  });

  afterAll(function () {
    console.log('OAuth: finished');
  });

  it('returns a 401 response if the token is missing', function (done) {
    const handler = result => {
      expect(result.status).toEqual(401);
      done();
    };
    const opts = options({ headers: {} });
    makeRequest(opts, handler);
  });

  it('returns a 401 response if the token is invalid', function (done) {
    const handler = result => {
      expect(result.status).toEqual(401);
      done();
    };
    const opts = options({ headers: { Authorization: `Bearer ${token() + 'hello'}` } });
    makeRequest(opts, handler);
  });

  it('returns a 401 response if the sub claim is missing', function (done) {
    const handler = result => {
      expect(result.status).toEqual(401);
      done();
    };
    const t = token(null, { subject: undefined });
    const opts = options({ headers: { Authorization: `Bearer ${t}` } });
    makeRequest(opts, handler);
  });

  it('returns a 401 response if the service claim is missing', function (done) {
    const handler = result => {
      expect(result.status).toEqual(401);
      done();
    };
    const t = token(null, { service: undefined });
    const opts = options({ headers: { Authorization: `Bearer ${t}` } });
    makeRequest(opts, handler);
  });

  it('returns a 401 response if the JWT ID (state) is missing');

  it('returns a 403 response if the scope is not `db`');

  it('returns a 405 response for non-POST requests');

  // should include the original state as well as a DLx token in the querystring
  it('redirects to the original redirect URI if the token is valid');

});
{'fraction_non_alphanumeric': 0.16266571121461842, 'fraction_numerical': 0.017198136868505912, 'mean_word_length': 3.051523947750363, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 2, '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': '7514206', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1635, 'n_words': 492}