-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauthentication.jl
183 lines (180 loc) · 7.78 KB
/
authentication.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
@testset "JuliaHub.authenticate()" begin
empty!(MOCK_JULIAHUB_STATE)
Mocking.apply(mocking_patch) do
withenv("JULIA_PKG_SERVER" => nothing) do
@test_throws JuliaHub.AuthenticationError JuliaHub.authenticate()
@test JuliaHub.authenticate("https://juliahub.example.org") isa JuliaHub.Authentication
@test JuliaHub.authenticate("juliahub.example.org") isa JuliaHub.Authentication
end
withenv("JULIA_PKG_SERVER" => "juliahub.example.org") do
@test JuliaHub.authenticate() isa JuliaHub.Authentication
end
withenv("JULIA_PKG_SERVER" => "https://juliahub.example.org") do
@test JuliaHub.authenticate() isa JuliaHub.Authentication
end
# Conflicting declarations, argument takes precendence
withenv("JULIA_PKG_SERVER" => "https://juliahub-one.example.org") do
auth = JuliaHub.authenticate("https://juliahub-two.example.org")
@test auth isa JuliaHub.Authentication
@test auth.server == URIs.URI("https://juliahub-two.example.org")
# check_authentication
MOCK_JULIAHUB_STATE[:invalid_authentication] = false
@test JuliaHub.check_authentication(; auth) === true
MOCK_JULIAHUB_STATE[:invalid_authentication] = true
@test JuliaHub.check_authentication(; auth) === false
delete!(MOCK_JULIAHUB_STATE, :invalid_authentication)
end
end
end
# In the general authenticate() tests, we mock the call to JuliaHub._authenticate()
# So here we call a lower level JuliaHub._authenticat**ion** implementation, with
# the REST calls mocked.
@testset "JuliaHub._authentication()" begin
empty!(MOCK_JULIAHUB_STATE)
server = URIs.URI("https://juliahub.example.org")
token = JuliaHub.Secret("")
Mocking.apply(mocking_patch) do
let a = JuliaHub._authentication(server; token)
@test a isa JuliaHub.Authentication
@test a.server == server
@test a.username == MOCK_USERNAME
@test a.token == token
@test a._api_version == v"0.0.1"
@test a._email === nothing
@test a._expires === nothing
end
let a = JuliaHub._authentication(
server;
token,
username="authfile_username",
expires=1234,
email="authfile@example.org",
)
@test a isa JuliaHub.Authentication
@test a.server == server
@test a.username == MOCK_USERNAME
@test a._api_version == v"0.0.1"
@test a._email == "authfile@example.org"
@test a._expires == 1234
end
# On old instances, we handle if /api/v1 404s
MOCK_JULIAHUB_STATE[:auth_v1_status] = 404
let a = JuliaHub._authentication(
server;
token,
username="authfile_username",
expires=1234,
email="authfile@example.org",
)
@test a isa JuliaHub.Authentication
@test a.server == server
@test a.username == MOCK_USERNAME
@test a._api_version == JuliaHub._MISSING_API_VERSION
@test a._email == "testuser@example.org"
@test a._expires == 1234
end
# .. but on a 500, it will actually throw
MOCK_JULIAHUB_STATE[:auth_v1_status] = 500
@test_throws JuliaHub.AuthenticationError JuliaHub._authentication(
server;
token,
username="authfile_username",
expires=1234,
email="authfile@example.org",
)
# Testing the fallback to legacy GQL endpoint
MOCK_JULIAHUB_STATE[:auth_v1_status] = 404
let a = JuliaHub._authentication(
server;
token,
username="authfile_username",
email="authfile@example.org",
)
@test a isa JuliaHub.Authentication
@test a.server == server
@test a.username == MOCK_USERNAME
@test a._api_version == JuliaHub._MISSING_API_VERSION
@test a._email == "testuser@example.org"
@test a._expires === nothing
end
# Error when the fallback also 500s
MOCK_JULIAHUB_STATE[:auth_gql_fail] = true
@test_throws JuliaHub.AuthenticationError JuliaHub._authentication(
server;
token,
username="authfile_username",
expires=1234,
email="authfile@example.org",
)
# Missing username in /api/v1 -- success, but with a warning
delete!(MOCK_JULIAHUB_STATE, :auth_v1_status)
MOCK_JULIAHUB_STATE[:auth_v1_username] = nothing
let a = @test_logs (:warn,) JuliaHub._authentication(
server;
token,
username="authfile_username",
email="authfile@example.org",
)
@test a isa JuliaHub.Authentication
@test a.server == server
@test a.username == "authfile_username"
@test a._api_version == v"0.0.1"
@test a._email == "authfile@example.org"
end
end
end
# The two-argument JuliaHub.authenticate does not trigger PkgAuthentication, but
# it does do the REST calls, like JuliaHub._authentication() above
@testset "JuliaHub.authenticate(server, token)" begin
empty!(MOCK_JULIAHUB_STATE)
server = "https://juliahub.example.org"
token = JuliaHub.Secret("")
Mocking.apply(mocking_patch) do
let a = JuliaHub.authenticate(server, token)
@test a isa JuliaHub.Authentication
@test a.server == URIs.URI(server)
@test a.username == MOCK_USERNAME
@test a.token == token
@test a._api_version == v"0.0.1"
@test a._email === nothing
@test a._expires === nothing
end
# On old instances, we handle if /api/v1 404s
MOCK_JULIAHUB_STATE[:auth_v1_status] = 404
let a = JuliaHub.authenticate(server, token)
@test a isa JuliaHub.Authentication
@test a.server == URIs.URI(server)
@test a.username == MOCK_USERNAME
@test a._api_version == JuliaHub._MISSING_API_VERSION
@test a._email === "testuser@example.org"
@test a._expires === nothing
end
# .. but on a 500, it will actually throw
MOCK_JULIAHUB_STATE[:auth_v1_status] = 500
@test_throws JuliaHub.AuthenticationError JuliaHub.authenticate(server, token)
# Testing the fallback to legacy GQL endpoint
MOCK_JULIAHUB_STATE[:auth_v1_status] = 404
let a = JuliaHub.authenticate(server, token)
@test a isa JuliaHub.Authentication
@test a.server == URIs.URI(server)
@test a.username == MOCK_USERNAME
@test a._api_version == JuliaHub._MISSING_API_VERSION
@test a._email === "testuser@example.org"
@test a._expires === nothing
end
# Error when the fallback also 500s
MOCK_JULIAHUB_STATE[:auth_gql_fail] = true
@test_throws JuliaHub.AuthenticationError JuliaHub.authenticate(server, token)
# Missing username in /api/v1 -- throws an AuthenticationError, since there is
# no auth.toml file to fall back to.
delete!(MOCK_JULIAHUB_STATE, :auth_v1_status)
MOCK_JULIAHUB_STATE[:auth_v1_username] = nothing
@test_throws JuliaHub.AuthenticationError JuliaHub.authenticate(server, token)
# Test that we handle InvalidAuthentication correctly in _authentication()
empty!(MOCK_JULIAHUB_STATE)
MOCK_JULIAHUB_STATE[:auth_v1_status] = 401
@test_throws JuliaHub.AuthenticationError(
"The authentication token is invalid"
) JuliaHub.authenticate(server, token)
end
end