# This is a bash script to test the API endpoints of IITK-Coin.
# This will work on any environment that runs bash (like Ubuntu, git bash, etc.). NO INSTALLATIONS REQUIRED
# It displays the total time taken to run the tests in seconds.milliseconds format.
# Simply uncomment the line for the endpoint which you want to test.
# Change the number of repetitions in the for loop

cmd=""
url="http://localhost:8080"
dflt_hdr="-H 'Content-Type: application/json' -H 'Accept: application/json'"

signup="curl -s -X POST {$url}/signup $dflt_hdr -d '{\"rollno\": 191197, \"name\": \"Natasha Romanoff\", \"password\": \"Natalia Alianovna 'Natasha' Romanova aka Black Widow\", \"batch\": \"Y19\"}'"
login="curl -s -X POST {$url}/login $dflt_hdr -d '{\"rollno\": 180197, \"password\": \"pa55w0Rd\"}'"

view_coins="curl -s -X GET {$url}/view_coins $dflt_hdr -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJiYXRjaCI6IlkxOSIsImVtYWlsIjoiZGV2dGVzdC5hc2lzaEBnbWFpbC5jb20iLCJleHAiOjE2MjcwMzY3NjMsInJvbGUiOiIiLCJyb2xsbm8iOjE5MTE5N30.kHvpRyCoMxziVrdZ_D5rnrNR2RpElmUqK_o4AKvwrwc'"
reward="curl -s -X POST {$url}/reward $dflt_hdr -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJiYXRjaCI6IlkxOCIsImVtYWlsIjoiZGV2dGVzdC5hc2lzaEBnbWFpbC5jb20iLCJleHAiOjE2MjcwMzY4NDksInJvbGUiOiJBZG1pbiIsInJvbGxubyI6MTgxMTk3fQ.u5XQO1OGFvDiqVUuXt-HMcn7xQwNHnl6vG7zycupFII' -d '{\"receiver\": 191197, \"amount\": 200, \"description\": \"testing\"}'"

transfer="curl -s -X POST {$url}/transfer $dflt_hdr -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJiYXRjaCI6IlkxOSIsImVtYWlsIjoiZGV2dGVzdC5hc2lzaEBnbWFpbC5jb20iLCJleHAiOjE2MjcwMzY4MDYsInJvbGUiOiIiLCJyb2xsbm8iOjE5MjE5N30.p7znpdk8NxYjGRcWN3o1Yjq04Ue3YA0esEEtfW8fd0M' -d '{\"receiver\": 190197, \"amount\": 10, \"description\": \"testing\"}'"

redeem="curl -s -X POST {$url}/redeems/new $dflt_hdr -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJiYXRjaCI6IlkxOSIsImVtYWlsIjoiZGV2dGVzdC5hc2lzaEBnbWFpbC5jb20iLCJleHAiOjE2MjcwMzY4MDYsInJvbGUiOiIiLCJyb2xsbm8iOjE5MjE5N30.p7znpdk8NxYjGRcWN3o1Yjq04Ue3YA0esEEtfW8fd0M' -d '{\"item_id\": 91051, \"amount\": 100, \"description\": \"Testing an eligible sender\"}'"

for i in {1..10}; do
  cmd+="$transfer &"
  cmd+="$redeem &"
  cmd+="$view_coins &"
  cmd+="$reward &"
done


# Start time
t_i=$(date +%s%N)

eval $cmd
wait
# End time
t_f=$(date +%s%N)

# Total time
t_tot=$((($t_f - $t_i)/1000000))
echo $(($t_tot / 1000)).$(($t_tot % 1000)) s