<commit_before><commit_msg>Test for serve-file<commit_after>"use strict";

require('redis-mock');
require.cache[require.resolve('redis')] = require.cache[require.resolve('redis-mock')];

const fetch = require('node-fetch');
const fs = require('fs-extra-promise');
const path = require('path');
const pool = require('@npmcorp/redis-pool');
const restifyFixture = require('restify-test-fixture');
const tap = require('tap');
const serveFile = require('../serve-file')
const withFixtures = require('with-fixtures');
const { tempdirFixture } = require('mixed-fixtures');

tap.test('serve-file handler', async t => {

  const rootFixture = tempdirFixture();
  const filesFixture = testImageFixture(rootFixture);

  /*
  const handler = serveFile({
    root
  })
  */

  const handler = serveFile;

  const s = restifyFixture(server => {
    server.get('/-/files/:team/:user/:file', handler);
  });

  await withFixtures([
    s,
    filesFixture,
    rootFixture
  ], async () => {
    const url = (await s).url + '/-/files/TTEST/UTEST/test.png';

    await pool.withConnection(c => c.setAsync('deadbeef', JSON.stringify({
      team_id: 'TTEST',
      user_id: 'UTEST'
    })));

    const res = await fetch(url, {
      method: 'GET',
      headers: Object.assign({
	'Authorization': 'Token: <PASSWORD>'
      })
    });

    t.equal(res.headers.get('content-type'), 'image/png');
    t.equal(res.status, 200);

    if (res.status != 200) {
      t.equal(await res.json(), null);
    }
  });
})

tap.test('teardown', async () => {
  await pool.drain()
  pool.clear();
});

async function testImageFixture(fixture) {
  const root = await (await fixture).dir;
  const dir = path.join(root, 'TTEST', 'UTEST');
  const src = path.join(__dirname, 'test.png');
  const dest = path.join(dir, 'test.png');
  const destjson = path.join(dir, 'test.json');

  await fs.mkdirpAsync(dir);

  await Promise.all([
    fs.writeFileAsync(dest, await fs.readFileAsync(src)),
    fs.writeJsonAsync(destjson, {
      user: "UTEST",
      team: "TTEST",
      file: "test",
      name: "test",
      type: "image/png",
      unfurl: true,
      path: "/-/files/TTEST/UTEST/test.png"
    }),
  ])

  return {
    async done() {
      return Promise.all([
	fs.unlinkAsync(dest),
	fs.unlinkAsync(destjson)
      ]);
    }
  };
}
{'fraction_non_alphanumeric': 0.1807909604519774, 'fraction_numerical': 0.002607561929595828, 'mean_word_length': 3.3433962264150945, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, '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': '5857210', 'n_tokens_mistral': 806, 'n_tokens_neox': 790, 'n_words': 155}