<commit_before>/* global describe it beforeEach */

/*
 * Unit tests for the /asset endpoint.
 */

require('./helpers/before');

var chai = require('chai');
var dirtyChai = require('dirty-chai');

chai.use(dirtyChai);

var request = require('supertest');
var resetHelper = require('./helpers/reset');
var authHelper = require('./helpers/auth');
var storage = require('../src/storage');
var server = require('../src/server');

describe('/assets', function () {
  beforeEach(resetHelper);

  it('accepts an asset file and produces a fingerprinted filename', function (done) {
    // shasum -a 256 test/fixtures/asset-file.txt
    var finalName = storage.assetURLPrefix() +
      'asset-file-0a1b4ceeaee9f0b7325a5dbdb93497e1f8c98d03b6f2518084294faa3452efc1.txt';

    request(server.create())
      .post('/assets')
      .set('Authorization', authHelper.AUTH_USER)
      .attach('first', 'test/fixtures/asset-file.txt')
      .expect(200)
      .expect('Content-Type', /json/)
      .expect({
        'asset-file.txt': finalName
      }, done);
  });

  it('requires authentication', function (done) {
    authHelper.ensureAuthIsRequired(
      request(server.create())
        .post('/assets')
        .attach('first', 'test/fixtures/asset-file.txt'),
      done);
  });

  it('lists fingerprinted assets', function (done) {
    var finalName = storage.assetURLPrefix() +
      'asset-file-0a1b4ceeaee9f0b7325a5dbdb93497e1f8c98d03b6f2518084294faa3452efc1.txt';

    var app = server.create();

    request(app)
      .post('/assets')
      .query({
        named: 'true'
      })
      .set('Authorization', authHelper.AUTH_USER)
      .attach('first', 'test/fixtures/asset-file.txt')
      .end(function (err, res) {
        if (err) throw err;

        request(app)
          .get('/assets')
          .expect(200)
          .expect('Content-Type', /json/)
          .expect('{"first":"' + finalName + '"}', done);
      });
  });

});<commit_msg>Unit test for the new endpoint.<commit_after>/* global describe it beforeEach */

/*
 * Unit tests for the /asset endpoint.
 */

require('./helpers/before');

var fs = require('fs');
var chai = require('chai');
var dirtyChai = require('dirty-chai');

chai.use(dirtyChai);

var request = require('supertest');
var resetHelper = require('./helpers/reset');
var authHelper = require('./helpers/auth');
var storage = require('../src/storage');
var server = require('../src/server');

describe('/assets', function () {
  // shasum -a 256 test/fixtures/asset-file.txt
  var fingerprintedFilename =
  'asset-file-0a1b4ceeaee9f0b7325a5dbdb93497e1f8c98d03b6f2518084294faa3452efc1.txt';
  var finalName;

  beforeEach(resetHelper);
  beforeEach(function () {
    finalName = storage.assetURLPrefix() + fingerprintedFilename;
  });

  it('accepts an asset file and produces a fingerprinted filename', function (done) {
    request(server.create())
      .post('/assets')
      .set('Authorization', authHelper.AUTH_USER)
      .attach('first', 'test/fixtures/asset-file.txt')
      .expect(200)
      .expect('Content-Type', /json/)
      .expect({
        'asset-file.txt': finalName
      }, done);
  });

  it('requires authentication', function (done) {
    authHelper.ensureAuthIsRequired(
      request(server.create())
        .post('/assets')
        .attach('first', 'test/fixtures/asset-file.txt'),
      done);
  });

  it('lists fingerprinted assets', function (done) {
    var app = server.create();

    request(app)
      .post('/assets')
      .query({
        named: 'true'
      })
      .set('Authorization', authHelper.AUTH_USER)
      .attach('first', 'test/fixtures/asset-file.txt')
      .end(function (err, res) {
        if (err) throw err;

        request(app)
          .get('/assets')
          .expect(200)
          .expect('Content-Type', /json/)
          .expect('{"first":"' + finalName + '"}', done);
      });
  });

  it('retrieves assets by fingerprinted filename', function (done) {
    var app = server.create();
    var rawAssetContents = fs.readFileSync('test/fixtures/asset-file.txt').toString();

    request(app)
      .post('/assets')
      .set('Authorization', authHelper.AUTH_USER)
      .attach('first', 'test/fixtures/asset-file.txt')
      .end(function (err) {
        if (err) throw err;

        request(app)
          .get('/assets/' + fingerprintedFilename)
          .expect(200)
          .expect(rawAssetContents, done);
      });
  });

});
{'fraction_non_alphanumeric': 0.16828114440189232, 'fraction_numerical': 0.029736427123225953, 'mean_word_length': 2.9153439153439153, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '1494142', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1413, 'n_words': 270}