var assert = require('assert')
var auth = require('..')

function request (authorization) {
  return {
    headers: {
      authorization: authorization
    }
  }
}

describe('auth(req)', function () {
  describe('arguments', function () {
    describe('req', function () {
      it('should be required', function () {
        assert.throws(auth, /argument req is required/)
      })

      it('should accept a request', function () {
        var req = request('basic Zm9vOmJhcg==')
        var creds = auth(req)
        assert.equal(creds.name, 'foo')
        assert.equal(creds.pass, 'bar')
      })

      it('should accept a koa context', function () {
        var ctx = { req: request('basic Zm9vOmJhcg==') }
        var creds = auth(ctx)
        assert.equal(creds.name, 'foo')
        assert.equal(creds.pass, 'bar')
      })

      it('should reject null', function () {
        assert.throws(auth.bind(null, null), /argument req is required/)
      })

      it('should reject a number', function () {
        assert.throws(auth.bind(null, 42), /argument req is required/)
      })

      it('should reject an object without headers', function () {
        assert.throws(auth.bind(null, {}), /argument req is required/)
      })
    })
  })

  describe('with no Authorization field', function () {
    it('should return null', function () {
      var req = request()
      assert(auth(req) == null)
    })
  })

  describe('with malformed Authorization field', function () {
    it('should return null', function () {
      var req = request('Something')
      assert(auth(req) == null)
    })
  })

  describe('with malformed Authorization scheme', function () {
    it('should return null', function () {
      var req = request('basic_Zm9vOmJhcg==')
      assert(auth(req) == null)
    })
  })

  describe('with malformed credentials', function () {
    it('should return null', function () {
      var req = request('basic Zm9vcgo=')
      assert(auth(req) == null)
    })
  })

  describe('with valid credentials', function () {
    it('should return .name and .pass', function () {
      var req = request('basic Zm9vOmJhcg==')
      var creds = auth(req)
      assert.equal(creds.name, 'foo')
      assert.equal(creds.pass, 'bar')
    })
  })

  describe('with empty password', function () {
    it('should return .name and .pass', function () {
      var req = request('basic Zm9vOg==')
      var creds = auth(req)
      assert.equal(creds.name, 'foo')
      assert.equal(creds.pass, '')
    })
  })

  describe('with empty userid', function () {
    it('should return .name and .pass', function () {
      var req = request('basic OnBhc3M=')
      var creds = auth(req)
      assert.equal(creds.name, '')
      assert.equal(creds.pass, 'pass')
    })
  })

  describe('with empty userid and pass', function () {
    it('should return .name and .pass', function () {
      var req = request('basic Og==')
      var creds = auth(req)
      assert.equal(creds.name, '')
      assert.equal(creds.pass, '')
    })
  })

  describe('with colon in pass', function () {
    it('should return .name and .pass', function () {
      var req = request('basic Zm9vOnBhc3M6d29yZA==')
      var creds = auth(req)
      assert.equal(creds.name, 'foo')
      assert.equal(creds.pass, 'pass:word')
    })
  })
})

describe('auth.parse(string)', function () {
  describe('with undefined string', function () {
    it('should return undefined', function () {
      assert.strictEqual(auth.parse(), undefined)
    })
  })

  describe('with malformed string', function () {
    it('should return undefined', function () {
      assert.strictEqual(auth.parse('Something'), undefined)
    })
  })

  describe('with malformed scheme', function () {
    it('should return undefined', function () {
      assert.strictEqual(auth.parse('basic_Zm9vOmJhcg=='), undefined)
    })
  })

  describe('with malformed credentials', function () {
    it('should return undefined', function () {
      assert.strictEqual(auth.parse('basic Zm9vcgo='), undefined)
    })
  })

  describe('with valid credentials', function () {
    it('should return .name and .pass', function () {
      var creds = auth.parse('basic Zm9vOmJhcg==')
      assert.equal(creds.name, 'foo')
      assert.equal(creds.pass, 'bar')
    })
  })

  describe('with empty password', function () {
    it('should return .name and .pass', function () {
      var creds = auth.parse('basic Zm9vOg==')
      assert.equal(creds.name, 'foo')
      assert.equal(creds.pass, '')
    })
  })

  describe('with empty userid', function () {
    it('should return .name and .pass', function () {
      var creds = auth.parse('basic OnBhc3M=')
      assert.equal(creds.name, '')
      assert.equal(creds.pass, 'pass')
    })
  })

  describe('with empty userid and pass', function () {
    it('should return .name and .pass', function () {
      var creds = auth.parse('basic Og==')
      assert.equal(creds.name, '')
      assert.equal(creds.pass, '')
    })
  })

  describe('with colon in pass', function () {
    it('should return .name and .pass', function () {
      var creds = auth.parse('basic Zm9vOnBhc3M6d29yZA==')
      assert.equal(creds.name, 'foo')
      assert.equal(creds.pass, 'pass:word')
    })
  })
})

@@ -1314,36 +1314,41 @@
 ('should return 
-null
+undefined
 ', function () %7B
@@ -1386,34 +1386,49 @@
   assert
-(auth(req) == null
+.strictEqual(auth(req), undefined
 )%0A    %7D)
@@ -1511,36 +1511,41 @@
 ('should return 
-null
+undefined
 ', function () %7B
@@ -1594,34 +1594,49 @@
   assert
-(auth(req) == null
+.strictEqual(auth(req), undefined
 )%0A    %7D)
@@ -1720,36 +1720,41 @@
 ('should return 
-null
+undefined
 ', function () %7B
@@ -1812,34 +1812,49 @@
   assert
-(auth(req) == null
+.strictEqual(auth(req), undefined
 )%0A    %7D)
@@ -1937,20 +1937,25 @@
  return 
-null
+undefined
 ', funct
@@ -2021,26 +2021,41 @@
 sert
-(auth(req) == null
+.strictEqual(auth(req), undefined
 )%0A  

fix misleading test descriptions