###
Module dependencies.
###
WhiteList = require '../libs/white-list'

describe 'Lib', ->

  describe 'WhiteList', ->

    it 'should receive an optional list in the constructor', (done) ->

      customList = [
        '/route/one'
        '/route/two'
        new RegExp '^admin.*Content$'
      ]

      whiteList = new WhiteList(customList)
      whiteList.publicUris.length.should.be.equal 3
      done()

    describe '#addToPublicUris()', ->

      whiteList = new WhiteList

      it 'should exist as a public function', (done) ->

        whiteList.addToPublicUris.should.be.a.Function
        done()

      it 'should throw an error if no publicUris was passed as parameter', (done) ->

        whiteList.addToPublicUris.bind(null).should.throw '"publicUris" is required'
        done()

      it 'should be able to receive a single string and add it to publicUris property', (done) ->

        whiteList = new WhiteList

        newPublicUri = '/admin/route'

        whiteList.addToPublicUris newPublicUri
        whiteList.publicUris.should.containEql '/admin/route'
        done()

      it 'should be able to receive a single regexp and add it to publicUris property', (done) ->

        whiteList = new WhiteList

        newPublicUri = new RegExp '^admin.*Content$'

        whiteList.addToPublicUris newPublicUri
        whiteList.publicUris.should.containEql /^admin.*Content$/
        done()

      it 'should be able to receive an array containing multiple publicUris and concatenate to publicUris property', (done) ->

        whiteList = new WhiteList

        newPublicUrisList = [
          '/route/1'
          '/route/2'
          new RegExp '^admin.*Content$'
        ]

        whiteList.addToPublicUris newPublicUrisList
        whiteList.publicUris.should.containEql '/route/1'
        whiteList.publicUris.should.containEql '/route/2'
        whiteList.publicUris.should.containEql /^admin.*Content$/
        done()

    describe '#isInWhiteList()', ->

      whiteList = new WhiteList

      it 'should exist as a public function', (done) ->
        whiteList.isInWhiteList.should.be.a.Function
        done()

      it 'should throw an error if url string was not passed as parameter', (done) ->

        whiteList.isInWhiteList.bind(null).should.throw '"url" parameter is required'
        done()

      it 'should return true if url is /admin/Content', (done) ->

        url = '/admin/Content'

        whiteList.isInWhiteList(url).should.be.true
        done()

      it 'should return true if url is /admin/Scripts', (done) ->

        url = '/admin/Scripts'

        whiteList.isInWhiteList(url).should.be.true
        done()

      it 'should return true if url is /meta/whoami', (done) ->

        url = '/meta/whoami'

        whiteList.isInWhiteList(url).should.be.true
        done()

      it 'should return true if url start with /admin/login', (done) ->

        url = '/admin/login?RedirectUrl=/admin/checkout'

        whiteList.isInWhiteList(url).should.be.true
        done()

      it 'should return false if url contains but does not start with /admin/login', (done) ->

        url = '/admin/checkout?RedirectUrl=/admin/login'

        whiteList.isInWhiteList(url).should.be.false
        done()

      it 'should return false if url is not in white list', (done) ->

        url = '/admin/checkout'

        whiteList.isInWhiteList(url).should.be.false
        done()
