forked from biluohc/actixweb-sqlx-jwt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gotest.jl
executable file
·72 lines (55 loc) · 2.33 KB
/
gotest.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# https://docs.julialang.org/en/v1.2/manual/faq/#How-do-I-catch-CTRL-C-in-a-script?-1
#=
exec julia --color=yes -e 'include(popfirst!(ARGS))' \
"${BASH_SOURCE[0]}" "$@" =#
using Test
checkout() = run(`git checkout -f .env Cargo.toml template.json`)
bd = read(pipeline(`cargo metadata --format-version 1`, `jq -r .target_directory`), String) |> strip
clean() = run(`rm -frv $bd/release/actixweb-sqlx-jwt`)
function test(kind::String)
clean()
run(`cargo build --release`)
proc = run(`$bd/release/actixweb-sqlx-jwt -v`, wait=false)
# wait server startup
sleep(1)
try
run(`curl 0.0.0.0:8080/assets`)
run(`curl -s --data '{"name": "Bob", "email": "Bob@google.com", "password": "Bobpass"}' -H "Content-Type: application/json" -X POST localhost:8080/user/register`)
jwt = read(pipeline(`curl -s --data '{"name": "Bob", "email": "Bob@google.com", "password": "Bobpass"}' -H "Content-Type: application/json" -X POST localhost:8080/user/login`, `jq -r .data`), String) |> strip
@test length(jwt) > 100
authead = "Authorization: Bearer $jwt"
code = read(pipeline(`curl -sH $authead localhost:8080/user/info/_`, `jq -r .code`), String) |> strip
@test code == "200"
authuri = "localhost:8080/user/info/_?access_token=$jwt"
code = read(pipeline(`curl -s $authuri`, `jq -r .code`), String) |> strip
@test code == "200"
catch e
@error "test api for $kind failed: $e"
finally
println()
kill(proc)
clean()
end
end
function test_and_checkout(kind::String)
checkout()
@info("Test for $kind prepare")
run(`sed -i "s/default\ =\ \[\ \"mysql\"\ \]/default = [ \"$kind\" ]/g" Cargo.toml `)
kind == "mysql" || run(pipeline(`cat .env `, `grep $kind`, `sed 's/\#//gw .env'`))
json = read(pipeline(`cat template.json`, `grep -v sql`), String)
db = read(pipeline(`cat template.json`, `grep $kind`, `sed 's/\/\/ //g'`), String)
json2 = """{$db\n$(json[2:end])"""
run(pipeline(`echo $json2`, `jq .`, `sed 'w template.json'`))
@info("Test for $kind ..")
try
test(kind)
@warn("Test for $kind Ok\n")
catch e
@error("Test for $kind failed: $e\n")
end
checkout()
end
test_and_checkout("mysql")
test_and_checkout("sqlite")
test_and_checkout("postgres")