var tape = require('tape')
var async = require('async')
var request = require('request')

var routerPort = null
var emailaddress = 'bob' + (new Date().getTime()) + '@bob.com'

function get(host, url, done){
  request(host + url, function (error, response, body) {
    if (!error) {
      done(null, body, response)
    }
    else{
      done(error)
    }
  })
}

function k8surl(path){
  return 'http://127.0.0.1:8080' + path
}

function routerurl(path){
  return 'http://127.0.0.1:' + routerPort + path
}

tape('can connect to the k8s api', function (t) {
  
  request({
    url:k8surl('/version'),
    method: 'GET',
    json: true
  }, function(err, res, body){
    if(err){
      t.error(err)
      return t.end()
    }
    t.equal(res.body.major, "1", "the major version is equal")
    t.end()
  })
 

})

tape('can read the exposed port for the route', function (t) {

  request({
    url:k8surl('/api/v1/namespaces/default/services/jenca-router-public'),
    method:'GET',
    json:true
  }, function(err, res){
    if(err){
      t.error(err)
      return t.end()
    }

    t.equal(typeof(res.body.spec.ports[0].nodePort), 'number', 'the node port is a number')
    
    routerPort = res.body.spec.ports[0].nodePort

    console.log('router port: ' + routerPort)

    t.end()
  })

})

tape('can read /v1/projects/version', function (t) {
  request({
    url:routerurl('/v1/projects/version'),
    method:'GET'
  }, function(err, res){
    if(err){
      t.error(err)
      return t.end()
    }

    console.log('-------------------------------------------');
    console.log(res.body)

    t.equal(res.body.match(/^\d+\.\d+\.\d+$/) ? true : false, true, 'the version is a semver')

    t.end()
  })
})

tape('can read /v1/projects/version via the k8s proxy', function (t) {
  request({
    url:k8surl('/api/v1/proxy/namespaces/default/services/projects/v1/projects/version'),
    method:'GET'
  }, function(err, res){
    if(err){
      t.error(err)
      return t.end()
    }

    t.equal(res.body.match(/^\d+\.\d+\.\d+$/) ? true : false, true, 'the version is a semver')

    t.end()
  })
})


tape('can signup to /v1/auth/signup', function (t) {

  request({
    url:routerurl('/v1/auth/signup'),
    method:'POST',
    json:true,
    body:{
      email:emailaddress,
      password:'apples'
    }
  }, function(err, res){
    if(err){
      t.error(err)
      return t.end()
    }

    console.log('-------------------------------------------');
    console.log(res.statusCode)
    console.dir(res.body)

    t.equal(res.statusCode, 201, 'the status code is 201')
    t.equal(res.body.email, emailaddress, 'the email is the same')
    t.equal(res.body.password, 'apples', 'the password is the same')

    //t.equal(res.body.match(/^\d+\.\d+\.\d+$/) ? true : false, true, 'the version is a semver')

    t.end()
  })
})


tape('can login to /v1/auth/login', function (t) {

  request({
    url:routerurl('/v1/auth/login'),
    method:'POST',
    json:true,
    body:{
      email:emailaddress,
      password:'apples'
    }
  }, function(err, res){
    if(err){
      t.error(err)
      return t.end()
    }

    console.log('-------------------------------------------');
    console.log(res.statusCode)
    console.dir(res.body)


    t.equal(res.statusCode, 200, 'the status code is 200')
    t.equal(res.body.email, emailaddress, 'the email is the same')
    t.equal(res.body.password, 'apples', 'the password is the same')


    //t.equal(res.body.match(/^\d+\.\d+\.\d+$/) ? true : false, true, 'the version is a semver')

    t.end()
  })
})


tape('can view the libary at /v1/library', function (t) {

  request({
    url:routerurl('/v1/library'),
    method:'GET'
  }, function(err, res){
    if(err){
      t.error(err)
      return t.end()
    }

    var data = JSON.parse(res.body)
    t.equal(res.statusCode, 200, 'the status code is 200')
    t.equal(data.bimserver.controller.kind, 'ReplicationController', 'the library has loaded the aps')
    
    t.end()
  })
})

@@ -83,16 +83,57 @@
 uest')%0A%0A
+request = request.defaults(%7Bjar: true%7D)%0A%0A
 var rout
@@ -3476,17 +3476,16 @@
 same')%0A%0A
-%0A
     //t.
@@ -3587,32 +3587,633 @@
 .end()%0A  %7D)%0A%7D)%0A%0A
+tape('the cookie works', function (t) %7B%0A%0A  request(%7B%0A    url:routerurl('/v1/auth/status'),%0A    method:'GET',%0A    headers:%7B%0A      'Content-type':'application/json'%0A    %7D%0A  %7D, function(err, res)%7B%0A    if(err)%7B%0A      t.error(err)%0A      return t.end()%0A    %7D%0A%0A%0A    t.equal(res.statusCode, 200, 'the status code is 200')%0A    t.equal(res.body.email, emailaddress, 'the email is the same')%0A    t.equal(res.body.is_authenticated, true, 'the cookie is authenticated')%0A%0A    console.log('-------------------------------------------');%0A    console.log(res.statusCode)%0A    console.dir(res.body)%0A%0A    t.end()%0A  %7D)%0A%7D)%0A
 %0Atape('can view 

test for the cookie working