-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathplug_test.exs
More file actions
315 lines (262 loc) · 10.2 KB
/
plug_test.exs
File metadata and controls
315 lines (262 loc) · 10.2 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
defmodule Hexdocs.PlugTest do
use ExUnit.Case, async: true
import Plug.Conn
import Plug.Test
import Mox
alias Hexdocs.{HexpmMock, Store}
setup :verify_on_exit!
@bucket :docs_private_bucket
test "requests without subdomain not supported" do
conn = conn(:get, "http://localhost:5002/foo") |> call()
assert conn.status == 400
end
describe "OAuth flow" do
test "redirect to OAuth authorize with no session" do
conn = conn(:get, "http://plugtest.localhost:5002/foo") |> call()
assert conn.status == 302
[location] = get_resp_header(conn, "location")
assert String.starts_with?(location, "http://localhost:5000/oauth/authorize?")
uri = URI.parse(location)
query = URI.decode_query(uri.query)
assert query["response_type"] == "code"
assert query["client_id"] == "hexdocs"
assert query["scope"] == "docs:plugtest"
assert query["code_challenge_method"] == "S256"
assert query["state"] != nil
assert query["code_challenge"] != nil
# Should store PKCE verifier and state in session
assert get_session(conn, "oauth_code_verifier") != nil
assert get_session(conn, "oauth_state") != nil
assert get_session(conn, "oauth_return_path") == "/foo"
end
test "sanitize protocol-relative return path to prevent open redirect" do
conn = conn(:get, "http://plugtest.localhost:5002//evil.com") |> call()
assert conn.status == 302
assert get_session(conn, "oauth_return_path") == "/"
end
test "sanitize protocol-relative return path with extra slashes" do
conn = conn(:get, "http://plugtest.localhost:5002///evil.com/foo") |> call()
assert conn.status == 302
assert get_session(conn, "oauth_return_path") == "/"
end
test "preserve valid return path" do
conn = conn(:get, "http://plugtest.localhost:5002/some/path") |> call()
assert conn.status == 302
assert get_session(conn, "oauth_return_path") == "/some/path"
end
test "OAuth callback with invalid state returns error" do
conn =
conn(:get, "http://plugtest.localhost:5002/oauth/callback?code=abc&state=wrong")
|> init_test_session(%{
"oauth_state" => "correct_state",
"oauth_code_verifier" => "verifier"
})
|> call()
assert conn.status == 403
assert conn.resp_body =~ "Invalid OAuth state"
end
test "OAuth callback with missing code returns error" do
conn =
conn(:get, "http://plugtest.localhost:5002/oauth/callback?state=correct_state")
|> init_test_session(%{
"oauth_state" => "correct_state",
"oauth_code_verifier" => "verifier"
})
|> call()
assert conn.status == 400
assert conn.resp_body =~ "Missing authorization code"
end
test "OAuth callback with error parameter returns error" do
conn =
conn(
:get,
"http://plugtest.localhost:5002/oauth/callback?error=access_denied&error_description=User%20denied"
)
|> init_test_session(%{"oauth_state" => "state", "oauth_code_verifier" => "verifier"})
|> call()
assert conn.status == 403
assert conn.resp_body =~ "User denied"
end
test "serve page with valid OAuth token", %{test: test} do
Mox.expect(HexpmMock, :verify_key, fn token, organization ->
assert String.starts_with?(token, "eyJ")
assert organization == "plugtest"
:ok
end)
now = NaiveDateTime.utc_now()
expires_at = NaiveDateTime.add(now, 1800, :second)
Store.put!(@bucket, "plugtest/#{test}/index.html", "body")
conn =
conn(:get, "http://plugtest.localhost:5002/#{test}/index.html")
|> init_test_session(%{
"access_token" => "eyJhbGciOiJFUzI1NiJ9.test",
"refresh_token" => "eyJhbGciOiJFUzI1NiJ9.refresh",
"token_expires_at" => expires_at,
"token_created_at" => now
})
|> call()
assert conn.status == 200
assert conn.resp_body == "body"
end
test "redirect to OAuth when token expired and no refresh token" do
now = NaiveDateTime.utc_now()
expired = NaiveDateTime.add(now, -1800, :second)
conn =
conn(:get, "http://plugtest.localhost:5002/foo")
|> init_test_session(%{
"access_token" => "eyJhbGciOiJFUzI1NiJ9.test",
"token_expires_at" => expired,
"token_created_at" => NaiveDateTime.add(expired, -1800, :second)
})
|> call()
assert conn.status == 302
[location] = get_resp_header(conn, "location")
assert String.starts_with?(location, "http://localhost:5000/oauth/authorize?")
end
end
describe "page serving with OAuth" do
test "serve 200 page", %{test: test} do
Mox.expect(HexpmMock, :verify_key, fn token, organization ->
assert String.starts_with?(token, "eyJ")
assert organization == "plugtest"
:ok
end)
now = NaiveDateTime.utc_now()
expires_at = NaiveDateTime.add(now, 1800, :second)
Store.put!(@bucket, "plugtest/#{test}/index.html", "body")
conn =
conn(:get, "http://plugtest.localhost:5002/#{test}/index.html")
|> init_test_session(%{
"access_token" => "eyJhbGciOiJFUzI1NiJ9.test",
"refresh_token" => "eyJhbGciOiJFUzI1NiJ9.refresh",
"token_expires_at" => expires_at,
"token_created_at" => now
})
|> call()
assert conn.status == 200
assert conn.resp_body == "body"
end
test "serve 404 page", %{test: test} do
Mox.expect(HexpmMock, :verify_key, fn token, organization ->
assert String.starts_with?(token, "eyJ")
assert organization == "plugtest"
:ok
end)
now = NaiveDateTime.utc_now()
expires_at = NaiveDateTime.add(now, 1800, :second)
Store.put!(@bucket, "plugtest/#{test}/index.html", "body")
conn =
conn(:get, "http://plugtest.localhost:5002/#{test}/404.html")
|> init_test_session(%{
"access_token" => "eyJhbGciOiJFUzI1NiJ9.test",
"refresh_token" => "eyJhbGciOiJFUzI1NiJ9.refresh",
"token_expires_at" => expires_at,
"token_created_at" => now
})
|> call()
assert conn.status == 404
assert conn.resp_body =~ "Page not found"
end
test "redirect to root", %{test: test} do
Mox.expect(HexpmMock, :verify_key, fn token, organization ->
assert String.starts_with?(token, "eyJ")
assert organization == "plugtest"
:ok
end)
now = NaiveDateTime.utc_now()
expires_at = NaiveDateTime.add(now, 1800, :second)
Store.put!(@bucket, "plugtest/#{test}/index.html", "body")
conn =
conn(:get, "http://plugtest.localhost:5002/#{test}")
|> init_test_session(%{
"access_token" => "eyJhbGciOiJFUzI1NiJ9.test",
"refresh_token" => "eyJhbGciOiJFUzI1NiJ9.refresh",
"token_expires_at" => expires_at,
"token_created_at" => now
})
|> call()
assert conn.status == 302
assert get_resp_header(conn, "location") == ["/#{test}/"]
end
test "serve index.html for root requests", %{test: test} do
Mox.expect(HexpmMock, :verify_key, fn token, organization ->
assert String.starts_with?(token, "eyJ")
assert organization == "plugtest"
:ok
end)
now = NaiveDateTime.utc_now()
expires_at = NaiveDateTime.add(now, 1800, :second)
Store.put!(@bucket, "plugtest/#{test}/index.html", "body")
conn =
conn(:get, "http://plugtest.localhost:5002/#{test}/")
|> init_test_session(%{
"access_token" => "eyJhbGciOiJFUzI1NiJ9.test",
"refresh_token" => "eyJhbGciOiJFUzI1NiJ9.refresh",
"token_expires_at" => expires_at,
"token_created_at" => now
})
|> call()
assert conn.status == 200
assert conn.resp_body == "body"
end
test "serve docs_config.js for unversioned and versioned requests", %{test: test} do
Mox.expect(HexpmMock, :verify_key, 2, fn token, organization ->
assert String.starts_with?(token, "eyJ")
assert organization == "plugtest"
:ok
end)
now = NaiveDateTime.utc_now()
expires_at = NaiveDateTime.add(now, 1800, :second)
Store.put!(@bucket, "plugtest/#{test}/docs_config.js", "var versionNodes;")
conn =
conn(:get, "http://plugtest.localhost:5002/#{test}/docs_config.js")
|> init_test_session(%{
"access_token" => "eyJhbGciOiJFUzI1NiJ9.test",
"refresh_token" => "eyJhbGciOiJFUzI1NiJ9.refresh",
"token_expires_at" => expires_at,
"token_created_at" => now
})
|> call()
assert conn.status == 200
assert conn.resp_body == "var versionNodes;"
conn =
conn(:get, "http://plugtest.localhost:5002/#{test}/1.0.0/docs_config.js")
|> init_test_session(%{
"access_token" => "eyJhbGciOiJFUzI1NiJ9.test",
"refresh_token" => "eyJhbGciOiJFUzI1NiJ9.refresh",
"token_expires_at" => expires_at,
"token_created_at" => now
})
|> call()
assert conn.status == 200
assert conn.resp_body == "var versionNodes;"
end
test "token verification fails redirects to OAuth" do
Mox.expect(HexpmMock, :verify_key, fn _token, _organization ->
{:error, "account not authorized"}
end)
now = NaiveDateTime.utc_now()
expires_at = NaiveDateTime.add(now, 1800, :second)
conn =
conn(:get, "http://plugtest.localhost:5002/foo")
|> init_test_session(%{
"access_token" => "eyJhbGciOiJFUzI1NiJ9.test",
"refresh_token" => "eyJhbGciOiJFUzI1NiJ9.refresh",
"token_expires_at" => expires_at,
"token_created_at" => now
})
|> call()
assert conn.status == 403
assert conn.resp_body =~ "account not authorized"
end
test "handle no path redirects to OAuth" do
conn = conn(:get, "http://plugtest.localhost:5002/") |> call()
assert conn.status == 302
[location] = get_resp_header(conn, "location")
assert String.starts_with?(location, "http://localhost:5000/oauth/authorize?")
end
end
defp call(conn) do
Hexdocs.Plug.call(conn, [])
end
end