Skip to content

Commit d54c3ec

Browse files
committed
update tests
1 parent f9cf5ce commit d54c3ec

File tree

2 files changed

+76
-24
lines changed

2 files changed

+76
-24
lines changed

test/authserver.jl

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import Pkg: TOML
44
const EXPIRY = 30
55
const CHALLENGE_EXPIRY = 10
66
const PORT = 8888
7-
const LEGACY_MODE = 1
8-
const DEVICE_FLOW_MODE = 2
7+
@enum AuthFlowMode CLASSIC_MODE DEVICE_FLOW_MODE DEVICE_FLOW_NO_SCOPE_MODE
98

109
const ID_TOKEN = Random.randstring(100)
1110
const TOKEN = Ref(Dict())
12-
const MODE = Ref(LEGACY_MODE)
11+
const MODE = Ref(CLASSIC_MODE)
1312

1413
challenge_response_map = Dict()
1514
challenge_timeout = Dict()
@@ -102,29 +101,40 @@ function check_validity(req)
102101
return HTTP.Response(200, payload == TOKEN[])
103102
end
104103

105-
function set_mode_legacy(req)
106-
MODE[] = LEGACY_MODE
107-
return HTTP.Response(200)
108-
end
109-
110-
function set_mode_device(req)
111-
MODE[] = DEVICE_FLOW_MODE
104+
function set_mode(req)
105+
global MODE
106+
mode = get(HTTP.getparams(req), "mode", nothing)
107+
if mode == "classic"
108+
MODE[] = CLASSIC_MODE
109+
elseif mode == "device"
110+
MODE[] = DEVICE_FLOW_MODE
111+
elseif mode == "device-no-scope"
112+
MODE[] = DEVICE_FLOW_NO_SCOPE_MODE
113+
else
114+
return HTTP.Response(400, "Invalid Mode $(mode)")
115+
end
112116
return HTTP.Response(200)
113117
end
114118

115119
function auth_configuration(req)
116-
if MODE[] == LEGACY_MODE
117-
return HTTP.Response(200)
120+
global MODE
121+
if MODE[] == CLASSIC_MODE
122+
# classic mode could also return `auth_flows = ["classic"]`, but we choose to test
123+
# the legacy case where the configuration is not implemented at all (which also
124+
# implies the classic mode).
125+
return HTTP.Response(501, "Not Implemented")
118126
else
119-
return HTTP.Response(
120-
200,
121-
""" {
122-
"auth_flows": ["classic", "device"],
123-
"device_token_refresh_url": "http://localhost:$PORT/auth/renew/token.toml/device/",
124-
"device_authorization_endpoint": "http://localhost:$PORT/auth/device/code",
125-
"device_token_endpoint": "http://localhost:$PORT/auth/token"
126-
} """,
127+
body = Dict(
128+
"auth_flows" => ["classic", "device"],
129+
"device_token_refresh_url" => "http://localhost:$PORT/auth/renew/token.toml/device/",
130+
"device_authorization_endpoint" => "http://localhost:$PORT/auth/device/code",
131+
"device_token_endpoint" => "http://localhost:$PORT/auth/token",
127132
)
133+
# device_token_scope omitted in DEVICE_FLOW_NO_SCOPE_MODE
134+
if MODE[] == DEVICE_FLOW_MODE
135+
body["device_token_scope"] = "openid"
136+
end
137+
return HTTP.Response(200, JSON.json(body))
128138
end
129139
end
130140

@@ -189,8 +199,7 @@ HTTP.register!(router, "POST", "/auth/device/code", auth_device_code)
189199
HTTP.register!(router, "GET", "/auth/device", auth_device)
190200
HTTP.register!(router, "POST", "/auth/token", auth_token)
191201
HTTP.register!(router, "GET", "/auth/renew/token.toml/device", renew_handler)
192-
HTTP.register!(router, "POST", "/set_mode/legacy", set_mode_legacy)
193-
HTTP.register!(router, "POST", "/set_mode/device", set_mode_device)
202+
HTTP.register!(router, "POST", "/set_mode/{mode}", set_mode)
194203

195204
function run()
196205
println("starting server")

test/tests.jl

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ function delete_token()
1919
rm(servers_dir; force = true, recursive = true)
2020
end
2121

22+
# Helper function to do the GET against /auth/configuration
23+
# Note: having a / at the end of the first argument of NoAuthentication
24+
# will break the HTTP call.
25+
get_auth_configuration() = PkgAuthentication.get_auth_configuration(
26+
PkgAuthentication.NoAuthentication(rstrip(test_pkg_server, '/'), "auth")
27+
)
28+
2229
@testset "auth without server" begin
2330
delete_token()
2431
success = PkgAuthentication.authenticate(test_pkg_server)
@@ -30,7 +37,7 @@ authserver_file = joinpath(@__DIR__, "authserver.jl")
3037
cmd = `$(Base.julia_cmd()) $(authserver_file)`
3138
env2 = copy(ENV)
3239
env2["JULIA_PROJECT"] = Base.active_project()
33-
p = run(pipeline(setenv(cmd, env2), stdout="server_out.log", stderr="server_err.log"), wait=false)
40+
p = run(pipeline(setenv(cmd, env2), stdout=stdout, stderr=stdout), wait=false)
3441
atexit(() -> kill(p))
3542
sleep(10)
3643

@@ -65,6 +72,42 @@ end
6572
delete_token()
6673
HTTP.post(joinpath(test_pkg_server, "set_mode/device"))
6774

75+
# Double check that the test server is responding with the correct
76+
# configuration information.
77+
config = get_auth_configuration()
78+
@test haskey(config, "device_token_scope")
79+
@test config["device_token_scope"] == "openid"
80+
81+
@info "testing inital auth"
82+
success = PkgAuthentication.authenticate(test_pkg_server)
83+
84+
@test success isa PkgAuthentication.Success
85+
@test success.token["expires_at"] > time()
86+
@test startswith(success.token["id_token"], "device-")
87+
@test !occursin("id_token", sprint(show, success))
88+
89+
sleeptimer = ceil(Int, success.token["expires_at"] - time() + 1)
90+
@info "sleep for $(sleeptimer)s (until refresh necessary)"
91+
sleep(sleeptimer)
92+
93+
@info "testing auth refresh"
94+
success2 = PkgAuthentication.authenticate(test_pkg_server)
95+
@test success2 isa PkgAuthentication.Success
96+
@test !occursin("id_token", sprint(show, success2))
97+
@test success2.token["expires_at"] > time()
98+
@test success2.token["refresh_token"] !== success.token["refresh_token"]
99+
@test startswith(success2.token["id_token"], "refresh-")
100+
101+
HTTP.post(joinpath(test_pkg_server, "set_mode/classic"))
102+
end
103+
104+
@testset "auth with running server (device flow; no scope)" begin
105+
delete_token()
106+
HTTP.post(joinpath(test_pkg_server, "set_mode/device-no-scope"))
107+
108+
config = get_auth_configuration()
109+
@test !haskey(config, "device_token_scope")
110+
68111
@info "testing inital auth"
69112
success = PkgAuthentication.authenticate(test_pkg_server)
70113

@@ -85,7 +128,7 @@ end
85128
@test success2.token["refresh_token"] !== success.token["refresh_token"]
86129
@test startswith(success2.token["id_token"], "refresh-")
87130

88-
HTTP.post(joinpath(test_pkg_server, "set_mode/legacy"))
131+
HTTP.post(joinpath(test_pkg_server, "set_mode/classic"))
89132
end
90133

91134

0 commit comments

Comments
 (0)