<commit_before>const test = require('tape')
const awsSdk = require('aws-sdk')
const config = require('config')
const crypto = require('../../lib/crypto')
const request = require('request')
const serializer = require('../../lib/serializer.js')
const usersRouter = require('../../server/users.js')
const util = require('../../server/lib/util.js')
const Express = require('express')

test('users router', (t) => {
  t.plan(1)

  const app = Express()
  app.use('/', usersRouter)
  const server = app.listen(0, 'localhost', () => {
    serializer.init().then(() => {
      const serverUrl = `http://localhost:${server.address().port}`
      console.log(`server up on ${serverUrl}`)

      const keys = crypto.deriveKeys(crypto.getSeed())
      const userId = Buffer.from(keys.publicKey).toString('base64')
      const baseRequest = request.defaults({
        baseUrl: `${serverUrl}/${encodeURIComponent(userId)}`
      })

      const apiVersion = config.apiVersion
      const categoryIdHistorySites = config.categoryIdHistorySites
      const s3Bucket = config.awsS3Bucket

      function signedTimestamp (secretKey, timestamp) {
        if (!timestamp) { timestamp = Math.floor(Date.now() / 1000) }
        const message = timestamp.toString()
        return crypto.sign(serializer.serializer.stringToByteArray(message), secretKey)
      }

      t.test('POST /:userId/credentials', (t) => {
        t.plan(6)

        const sharedParams = {
          encoding: null,
          method: 'POST',
          url: '/credentials'
        }
        baseRequest(sharedParams, (_error, response, _body) => {
          if (response.statusCode >= 400 && response.statusCode <= 499) {
            t.pass('required signed timestamp')
          } else {
            t.fail('should not work')
          }
        })

        const params = Object.assign(
          sharedParams,
          { body: Buffer.from(signedTimestamp(keys.secretKey).buffer) }
        )
        baseRequest(params, (error, response, body) => {
          if (error) { return t.fail(`${t.name} ${error} ${response}`) }
          t.equals(response.statusCode, 200, `${t.name} -> 200`)

          const parsedBody = serializer.serializer.byteArrayToCredentials(response.body)
          const credentials = parsedBody.aws<commit_msg>minor: server: better post fail message<commit_after>const test = require('tape')
const awsSdk = require('aws-sdk')
const config = require('config')
const crypto = require('../../lib/crypto')
const request = require('request')
const serializer = require('../../lib/serializer.js')
const usersRouter = require('../../server/users.js')
const util = require('../../server/lib/util.js')
const Express = require('express')

test('users router', (t) => {
  t.plan(1)

  const app = Express()
  app.use('/', usersRouter)
  const server = app.listen(0, 'localhost', () => {
    serializer.init().then(() => {
      const serverUrl = `http://localhost:${server.address().port}`
      console.log(`server up on ${serverUrl}`)

      const keys = crypto.deriveKeys(crypto.getSeed())
      const userId = Buffer.from(keys.publicKey).toString('base64')
      const baseRequest = request.defaults({
        baseUrl: `${serverUrl}/${encodeURIComponent(userId)}`
      })

      const apiVersion = config.apiVersion
      const categoryIdHistorySites = config.categoryIdHistorySites
      const s3Bucket = config.awsS3Bucket

      function signedTimestamp (secretKey, timestamp) {
        if (!timestamp) { timestamp = Math.floor(Date.now() / 1000) }
        const message = timestamp.toString()
        return crypto.sign(serializer.serializer.stringToByteArray(message), secretKey)
      }

      t.test('POST /:userId/credentials', (t) => {
        t.plan(6)

        const sharedParams = {
          encoding: null,
          method: 'POST',
          url: '/credentials'
        }
        baseRequest(sharedParams, (_error, response, _body) => {
          if (response.statusCode >= 400 && response.statusCode <= 499) {
            t.pass('required signed timestamp')
          } else {
            t.fail('should not work')
          }
        })

        const params = Object.assign(
          sharedParams,
          { body: Buffer.from(signedTimestamp(keys.secretKey).buffer) }
        )
        baseRequest(params, (error, response, body) => {
          if (error) { return t.fail(`${t.name} ${error} ${response}`) }
          t.equals(response.statusCode, 200, `${t.name} -> 200`)

          let parsedBody = null
          try {
            parsedBody = serializer.serializer.byteArrayToCredentials(response.body)
          } catch (e) {
            t.fail(`Couldn't deserialize body / ${e}: ${parsedBody}`)
          }
          const credentials = parsedBody.aws
{'fraction_non_alphanumeric': 0.15101095197978096, 'fraction_numerical': 0.009688289806234204, 'mean_word_length': 2.6474654377880182, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '2445419', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1335, 'n_words': 288}