#!/bin/bash 
export ADMIN_TOKEN=foobar

request(){
  echo "curl $*"
  curl  -s -H 'Content-Type: application/json' "$@"
  echo ""
}

clear
redis-cli flushdb
export PORT=4455 
coffee server.coffee &
pid=$!
read -p "press a key to run tests.." p
if [[ -d node_modules/coffeerest-api-db ]]; then 
  # basic CRUD
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X POST http://localhost:$PORT/v1/article --data '{"title":"foo","content":"bar"}' 
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X GET  http://localhost:$PORT/v1/article/all 
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X PUT http://localhost:$PORT/v1/article/1 --data '{"title":"flopje", "content":"bar"}' 
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN http://localhost:$PORT/v1/article/1 
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN http://localhost:$PORT/v1/article/1 
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X DELETE http://localhost:$PORT/v1/article/1 
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X POST http://localhost:$PORT/v1/user --data '{"email":"foo@hotmail.com","apikey":"flop"}' 
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN http://localhost:$PORT/v1/user/1 
  # add tags
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X POST http://localhost:$PORT/v1/user/tag --data '{"name":"is user"}' 
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X POST http://localhost:$PORT/v1/user/tag --data '{"name":"is admin","subtags":["can create,read,update,delete user","can create,read,update article","cannot update article date"]}' 
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X GET  http://localhost:$PORT/v1/user/tag/all 
  ## tag user (handy generic purposes: activity/role based permissions, categories etc)
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X GET http://localhost:$PORT/v1/user/tag/1/1/enable
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X GET http://localhost:$PORT/v1/user/tag/1/all 
  ## get user using user-specific apikey
  request -H 'X-FOO-TOKEN: fflop' -X GET  http://localhost:$PORT/v1/user/1 
  request -H 'X-FOO-TOKEN: flop'  -X GET http://localhost:$PORT/v1/user/1?populate=tags 
  ## try updating email without having permission for it
  request -H 'X-FOO-TOKEN: flop' -X PUT  http://localhost:$PORT/v1/user/1 --data '{"email":"foo@barrrr.com","apikey":"flop"}' 
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X GET http://localhost:$PORT/v1/user/tag/1/2/enable
  request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X GET http://localhost:$PORT/v1/user/tag/1/all 
  request -H 'X-FOO-TOKEN: flop' -X PUT  http://localhost:$PORT/v1/user/1 --data '{"email":"foo@barrrr.com","apikey":"flop"}' 
  #request -H 'X-FOO-TOKEN: '$ADMIN_TOKEN -X GET http://localhost:$PORT/v1/user/tag/1/2/enable
  ##request -H 'X-FOO-TOKEN: flop' -X PUT  http://localhost:$PORT/v1/user/1 --data '{"email":"foo@barrrr.com","apikey":"flop"}' 
  #request -H 'X-FOO-TOKEN: flop'  -X GET http://localhost:$PORT/v1/user/tag/1/1/disable
  ##request -H 'X-FOO-TOKEN: fflop' -X GET  http://localhost:$PORT/v1/article/1 
  ##request -H 'X-FOO-TOKEN: flop'  -X GET  http://localhost:$PORT/v1/user/all 
  #request -H 'X-FOO-TOKEN: flop'  -X GET  http://localhost:$PORT/v1/user/tag/all 
  #request -H 'X-FOO-TOKEN: flop'  -X GET  http://localhost:$PORT/v1/user/1 
  #request -H 'X-FOO-TOKEN: flop'  -X PUT  http://localhost:$PORT/v1/user/1 --data '{"email": "flop@bar.com","apikey":"flop"}'
  #request -H 'X-FOO-TOKEN: flop'  -X POST http://localhost:$PORT/v1/user/tag --data '{"name":"can create,read,update article"}' 
  #request -H 'X-FOO-TOKEN: flop'  -X GET  http://localhost:$PORT/v1/user/tag/1 
  #request -H 'X-FOO-TOKEN: flop'  -X GET  http://localhost:$PORT/v1/user/all 
  #request -H 'X-FOO-TOKEN: flop'  -X GET  http://localhost:$PORT/v1/user/tag/all 
  #request -H 'X-FOO-TOKEN: flop'  -X POST http://localhost:$PORT/v1/article --data '{"title":"foo","content":"bar"}' 
  #request -H 'X-FOO-TOKEN: flop'  -X PUT  http://localhost:$PORT/v1/user/1 --data '{"email": "flop@bar.com","apikey":"flop","tags_ids":[1,3]}'
  #request -H 'X-FOO-TOKEN: flop'  -X GET  http://localhost:$PORT/v1/user/1
  #request -H 'X-FOO-TOKEN: flop'  -X PUT  http://localhost:$PORT/v1/article/1 --data '{"title":"foo", "content": "bar2"}'
  sleep 1s
  exit
fi
[[ -d node_modules/coffeerest-api-doc ]] &&
  request http://localhost:$PORT/v1/doc/html
request http://localhost:$PORT/v1/book -X POST --data '{"foo":"foobar"}'
request http://localhost:$PORT/v1/book -X POST --data '{"foo":"foo"}'
request http://localhost:$PORT/v1/book -X POST --data '{}'
exit 0
