<commit_before>var defaultsDeep = require('lodash/defaultsDeep')
var nock = require('nock')
var test = require('tap').test

var getServer = require('../../utils/get-server')

var couchdbErrorTests = require('../../utils/couchdb-error-tests')

var routeOptions = {
  method: 'GET',
  url: '/session',
  headers: {
    accept: 'application/vnd.api+json',
    authorization: '<KEY>',
    'content-type': 'application/vnd.api+json'
  }
}

var mockCouchDbGetUser = nock('http://localhost:5984')
  .get('/_users/org.couchdb.user%3Apat-doe')
  .query(true)

test('GET /session', function (group) {
  getServer(function (error, server) {
    if (error) {
      group.error(error)
      group.end()
      return
    }
    couchdbErrorTests(server, group, mockCouchDbGetUser, routeOptions)

    group.test('No Authorization header sent', function (t) {
      server.inject({
        method: 'GET',
        url: '/session',
        headers: {}
      }, function (response) {
        t.is(response.statusCode, 403, 'returns 403 status')
        t.end()
      })
    })

    group.test('User not found', function (t) {
      var couchdb = mockCouchDbGetUser.reply(404, {error: 'Not Found'})

      server.inject(routeOptions, function (response) {
        t.is(couchdb.pendingMocks()[0], undefined, 'CouchDB received request')
        t.is(response.statusCode, 404, 'returns 404 status')
        t.is(response.result.errors.length, 1, 'returns one error')
        t.is(response.result.errors[0].title, 'Not Found', 'returns "Not Found" error')
        t.end()
      })
    })

    group.test('User found', function (subGroup) {
      subGroup.test('Session valid', function (t) {
        mockCouchDbGetUser.reply(200, {
          name: 'pat-doe',
          roles: [
            'id:userid123', 'mycustomrole'
          ],<commit_msg>test: prepared test for https://github.com/hoodiehq/hoodie-server-account/issues/94<commit_after>var defaultsDeep = require('lodash/defaultsDeep')
var nock = require('nock')
var test = require('tap').test

var getServer = require('../../utils/get-server')

var couchdbErrorTests = require('../../utils/couchdb-error-tests')

var routeOptions = {
  method: 'GET',
  url: '/session',
  headers: {
    accept: 'application/vnd.api+json',
    authorization: '<KEY>',
    'content-type': 'application/vnd.api+json'
  }
}

var mockCouchDbGetUser = nock('http://localhost:5984')
  .get('/_users/org.couchdb.user%3Apat-doe')
  .query(true)

test('GET /session', function (group) {
  getServer(function (error, server) {
    if (error) {
      group.error(error)
      group.end()
      return
    }
    couchdbErrorTests(server, group, mockCouchDbGetUser, routeOptions)

    group.test('No Authorization header sent', function (t) {
      server.inject({
        method: 'GET',
        url: '/session',
        headers: {}
      }, function (response) {
        t.is(response.statusCode, 403, 'returns 403 status')

        // prepared test for https://github.com/hoodiehq/hoodie-server-account/issues/94
        // replace the line above with the 3 lines below
        // t.is(response.statusCode, 401, 'returns 401 status')
        // t.is(response.result.error, 'Unauthorized', 'returns "Unauthorized" error')
        // t.is(response.result.message, 'Authorization header missing', 'returns "Authorization header missing" error')

        // TODO:
        // - response.result.error should be response.result.errors[0].title
        // - response.result.message should be  response.result.errors[0].msesage
        // - and we should check for response.result.errors.length === 1
        // ^ these are currently blocked by https://github.com/wraithgar/hapi-json-api/issues/20
        t.end()
      })
    })

    group.test('User not found', function (t) {
      var couchdb = mockCouchDbGetUser.reply(404, {error: 'Not Found'})

      server.inject(routeOptions, function (response) {
        t.is(couchdb.pendingMocks()[0], undefined, 'CouchDB received request')
        t.is(response.statusCode, 404, 'returns 404 status')
        t.is(response.result.errors.length, 1, 'returns one error')
        t.is(response.result.errors[0].title, 'Not Found', 'returns "Not Found" error')
        t.end()
      })
    })

    group.test('User found', function (subGroup) {
      subGroup.test('Session valid', function (t) {
        mockCouchDbGetUser.reply(200, {
          name: 'pat-doe',
          roles: [
            'id:userid123', 'mycustomrole'
          ],
{'fraction_non_alphanumeric': 0.15415549597855227, 'fraction_numerical': 0.01653261840929401, 'mean_word_length': 2.8330479452054793, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 3, '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': '4436355', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1399, 'n_words': 298}