###
Module dependencies
###
nock = require 'nock'
Concierge = require '../../src/connect-concierge'

concierge = new Concierge
getEnvironment = concierge.getEnvironment()

describe 'getEnvironment', ->

  it 'should exist as a public function', (done) ->
    getEnvironment.bind(null).should.be.a.Function
    done()

  it 'should skip to next middleware if path is /meta/whoami', (done) ->
    req =
      url: '/meta/whoami'

    getEnvironment req, {}, ->
      done()

  it 'should skip to next middleware if user is not logged in', (done) ->
    req =
      url: '/admin/jonny-quest'
      cookies: {}
      headers: {}
      vtex: {}

    getEnvironment req, {}, ->
      done()

  it 'should get environment from header as first priority', (done) ->
    req =
      url: '/admin/jonny-quest'
      headers:
        'x-vtex-environment': 'stable'
      cookies:
        'VtexIdclientAutCookie': '1234'
        'concierge-env': 'beta'
      query:
        'environment': 'alfa'
      vtex:
        hostParts: ['', '', 'next']

    getEnvironment req, {}, ->
      req.vtex.environment.should.be.equal 'stable'
      done()

  it 'should get environment name from cookie as second priority', (done) ->
    req =
      url: '/admin/jonny-quest'
      headers: {}
      cookies:
        'VtexIdclientAutCookie': '1234'
        'concierge-env': 'beta'
      query:
        'environment': 'alfa'
      vtex:
        hostParts: ['', '', 'next']

    getEnvironment req, {}, ->
      req.vtex.environment.should.be.equal 'beta'
      done()

  it 'should get environment name from query string as third priority', (done) ->
    req =
      url: '/admin/jonny-quest'
      headers: {}
      cookies:
        'VtexIdclientAutCookie': '1234'
      query:
        'environment': 'alfa'
      vtex:
        hostParts: ['', '', 'next']

    getEnvironment req, {}, ->
      req.vtex.environment.should.be.equal 'alfa'
      done()

  it 'should get environment name from host as fourth priority', (done) ->
    req =
      url: '/admin/jonny-quest'
      headers: {}
      cookies:
        'VtexIdclientAutCookie': '1234'
      vtex:
        hostParts: ['', '', 'next']

    getEnvironment req, {}, ->
      req.vtex.environment.should.be.equal 'next'
      done()
