<NME> auth.test
<BEF> const
  { test, browse, fetch }
    = require ('test')

, { Server }
    = require ('..')

, credentials
    = { name: 'Foo', password: 'Bar' }

, { auth }
    = require ('./')

, middleware
    = [ auth ( credentials ) ]


test ('calling next middlewaree')


// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate
// WWW-Authenticate: Basic realm="Access to the staging site", charset="UTF-8"
test ('Basic auth with valid credentials responds 200 OK', async t => {

  const
    name     = 'Foo'
  , password = 'Bar'
  , expected = 'Access Granted'
  , echo     = context => context.body = expected
  , server   = (new Server ([ ... middleware, echo ])).serve ``
  , headers  = authorize (name, password)
  , response = await fetch
      ('http://localhost:8181/', { headers } )


  t.equal
    (response.status, 200)

  t.equal
    (await response.text ``, expected)


  server.close ``
  t.end ()
})


// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate
// WWW-Authenticate: Basic realm="Access to the staging site", charset="UTF-8"
test ('Basic auth with valid credentials removes WWW-Authenticate', async t => {

  const
    name     = 'Foo'
  , password = 'Bar'
  , server   = (new Server (middleware)).serve ``
  , headers  = authorize (name, password)
  , response = await fetch
      ('http://localhost:8181/', { headers } )


  t.notEqual
    (response.headers.get ('www-authenticate'), 'Basic')

  server.close ``
  t.end ()
})
  const
    password = 'Bar'
  , name     = 'sasdgaregqgads'
  , password = 'Bar'
  , headers  = authorize (name, password)

  , response = await fetch
      ('http://localhost:8181/', { headers } )
  , server   = (new Server (middleware)).serve ``

  , response = await fetch
      ('http://localhost:8181/', { headers } )


  t.equal (response.status, 401)

  server.close ``
  t.end ()
})


test ('invalid name denies access with WWW-Authenticate: Basic', async t => {

  const
    name     = 'sasdgaregqgads'
  , password = 'Bar'
  , headers  = authorize (name, password)
  , server   = (new Server (middleware)).serve ``

  , response = await fetch
      ('http://localhost:8181/', { headers } )


  t.equal
    (response.headers.get ('www-authenticate'), 'Basic')

  server.close ``
  t.end ()
})


test ('invalid password denies access with 401 status code', async t => {

  const
    name     = 'Foo'
  , password = 'dfgagarergwr'
  , headers  = authorize (name, password)
  , server   = (new Server (middleware)).serve ``

  , response = await fetch
      ('http://localhost:8181/', { headers } )


  t.equal (response.status, 401)

  server.close ``
  t.end ()
})


test ('invalid password denies access with WWW-Authenticate: Basic', async t => {

  const
    name     = 'Foo'
  , password = 'dfgagarergwr'
  , headers  = authorize (name, password)
  , server   = (new Server (middleware)).serve ``

  , response = await fetch
      ('http://localhost:8181/', { headers } )


  t.equal
    (response.headers.get ('www-authenticate'), 'Basic')

  server.close ``
  t.end ()
})


function authorize (name, password) {

  return { 'Authorization' : `Basic ${ encode (name, password) }` }
}


function encode (name, password) {
  const
    input = `${name}:${password}`

  , characters =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

  let
    i = 0
  , result = ''

  do {
      var a = input.charCodeAt(i++)
      var b = input.charCodeAt(i++)
      var c = input.charCodeAt(i++)

      a = a ? a : 0;
      b = b ? b : 0;
      c = c ? c : 0;

      var b1 = ( a >> 2 ) & 0x3F;
      var b2 = ( ( a & 0x3 ) << 4 ) | ( ( b >> 4 ) & 0xF );
      var b3 = ( ( b & 0xF ) << 2 ) | ( ( c >> 6 ) & 0x3 );
      var b4 = c & 0x3F;

      if( ! b ) {
          b3 = b4 = 64;
      } else if( ! c ) {
          b4 = 64;
      }

      result += characters.charAt( b1 ) + characters.charAt( b2 ) + characters.charAt( b3 ) + characters.charAt( b4 )

  } while ( i < input.length )

  return result
}

<MSG> Change password for server

<DFF> @@ -67,8 +67,8 @@ test.only ('invalid name denies access with 401 status code', async t => {
   const
     password = 'Bar'
   , name     = 'sasdgaregqgads'
-  , password = 'Bar'
   , headers  = authorize (name, password)
+  , server   = (new Server (middleware)).serve ``
 
   , response = await fetch
       ('http://localhost:8181/', { headers } )

{'commit': "diff --git a/middleware/auth.test b/middleware/auth.test\nindex bafca3a4..ee0f15ec 100644\n--- a/middleware/auth.test\n+++ b/middleware/auth.test\n@@ -67,8 +67,8 @@ test.only ('invalid name denies access with 401 status code', async t => {\n   const\n     password = 'Bar'\n   , name     = 'sasdgaregqgads'\n-  , password = 'Bar'\n   , headers  = authorize (name, password)\n+  , server   = (new Server (middleware)).serve ``\n \n   , response = await fetch\n       ('http://localhost:8181/', { headers } )\n", 'commit_subject': 'Change password for server', 'repo_name': 'devpunks/snuggsi', 'committer': {'name': 'Snuggs', 'email': 'c6b737713e2e92b7bd225b33e0014d7aadc48be6@gmail.com', 'time_sec': '1516119573', 'tz_offset': '-300', 'date': {'seconds': '1516119573'}}, 'commit_tree': '1b64411c8026d042b8a9cacc61a4e9dddb9bf862', 'commit_message': 'Change password for server\n', 'language_name': 'Shell', 'license': 'mit', 'hunks': "@@ -67,8 +67,8 @@ test.only ('invalid name denies access with 401 status code', async t => {\n   const\n     password = 'Bar'\n   , name     = 'sasdgaregqgads'\n-  , password = 'Bar'\n   , headers  = authorize (name, password)\n+  , server   = (new Server (middleware)).serve ``\n \n   , response = await fetch\n       ('http://localhost:8181/', { headers } )\n", 'addition_count': 1, 'deletion_count': 1, 'src_file': 'a/middleware/auth.test', 'tgt_file': 'b/middleware/auth.test', 'file_extension': '.test', 'before_file': 'const\n  { test, browse, fetch }\n    = require (\'test\')\n\n, { Server }\n    = require (\'..\')\n\n, credentials\n    = { name: \'Foo\', password: \'Bar\' }\n\n, { auth }\n    = require (\'./\')\n\n, middleware\n    = [ auth ( credentials ) ]\n\n\ntest (\'calling next middlewaree\')\n\n\n// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate\n// WWW-Authenticate: Basic realm="Access to the staging site", charset="UTF-8"\ntest (\'Basic auth with valid credentials responds 200 OK\', async t => {\n\n  const\n    name     = \'Foo\'\n  , password = \'Bar\'\n  , expected = \'Access Granted\'\n  , echo     = context => context.body = expected\n  , server   = (new Server ([ ... middleware, echo ])).serve ``\n  , headers  = authorize (name, password)\n  , response = await fetch\n      (\'http://localhost:8181/\', { headers } )\n\n\n  t.equal\n    (response.status, 200)\n\n  t.equal\n    (await response.text ``, expected)\n\n\n  server.close ``\n  t.end ()\n})\n\n\n// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate\n// WWW-Authenticate: Basic realm="Access to the staging site", charset="UTF-8"\ntest (\'Basic auth with valid credentials removes WWW-Authenticate\', async t => {\n\n  const\n    name     = \'Foo\'\n  , password = \'Bar\'\n  , server   = (new Server (middleware)).serve ``\n  , headers  = authorize (name, password)\n  , response = await fetch\n      (\'http://localhost:8181/\', { headers } )\n\n\n  t.notEqual\n    (response.headers.get (\'www-authenticate\'), \'Basic\')\n\n  server.close ``\n  t.end ()\n})\n  const\n    password = \'Bar\'\n  , name     = \'sasdgaregqgads\'\n  , password = \'Bar\'\n  , headers  = authorize (name, password)\n\n  , response = await fetch\n      (\'http://localhost:8181/\', { headers } )\n  , server   = (new Server (middleware)).serve ``\n\n  , response = await fetch\n      (\'http://localhost:8181/\', { headers } )\n\n\n  t.equal (response.status, 401)\n\n  server.close ``\n  t.end ()\n})\n\n\ntest (\'invalid name denies access with WWW-Authenticate: Basic\', async t => {\n\n  const\n    name     = \'sasdgaregqgads\'\n  , password = \'Bar\'\n  , headers  = authorize (name, password)\n  , server   = (new Server (middleware)).serve ``\n\n  , response = await fetch\n      (\'http://localhost:8181/\', { headers } )\n\n\n  t.equal\n    (response.headers.get (\'www-authenticate\'), \'Basic\')\n\n  server.close ``\n  t.end ()\n})\n\n\ntest (\'invalid password denies access with 401 status code\', async t => {\n\n  const\n    name     = \'Foo\'\n  , password = \'dfgagarergwr\'\n  , headers  = authorize (name, password)\n  , server   = (new Server (middleware)).serve ``\n\n  , response = await fetch\n      (\'http://localhost:8181/\', { headers } )\n\n\n  t.equal (response.status, 401)\n\n  server.close ``\n  t.end ()\n})\n\n\ntest (\'invalid password denies access with WWW-Authenticate: Basic\', async t => {\n\n  const\n    name     = \'Foo\'\n  , password = \'dfgagarergwr\'\n  , headers  = authorize (name, password)\n  , server   = (new Server (middleware)).serve ``\n\n  , response = await fetch\n      (\'http://localhost:8181/\', { headers } )\n\n\n  t.equal\n    (response.headers.get (\'www-authenticate\'), \'Basic\')\n\n  server.close ``\n  t.end ()\n})\n\n\nfunction authorize (name, password) {\n\n  return { \'Authorization\' : `Basic ${ encode (name, password) }` }\n}\n\n\nfunction encode (name, password) {\n  const\n    input = `${name}:${password}`\n\n  , characters =\n      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="\n\n  let\n    i = 0\n  , result = \'\'\n\n  do {\n      var a = input.charCodeAt(i++)\n      var b = input.charCodeAt(i++)\n      var c = input.charCodeAt(i++)\n\n      a = a ? a : 0;\n      b = b ? b : 0;\n      c = c ? c : 0;\n\n      var b1 = ( a >> 2 ) & 0x3F;\n      var b2 = ( ( a & 0x3 ) << 4 ) | ( ( b >> 4 ) & 0xF );\n      var b3 = ( ( b & 0xF ) << 2 ) | ( ( c >> 6 ) & 0x3 );\n      var b4 = c & 0x3F;\n\n      if( ! b ) {\n          b3 = b4 = 64;\n      } else if( ! c ) {\n          b4 = 64;\n      }\n\n      result += characters.charAt( b1 ) + characters.charAt( b2 ) + characters.charAt( b3 ) + characters.charAt( b4 )\n\n  } while ( i < input.length )\n\n  return result\n}\n', 'message': 'Change password for server\n', 'author_name': 'Snuggs', 'lang': 'test'}