forked from jupyterhub/oauthenticator
/
test_oauth2.py
67 lines (56 loc) · 1.98 KB
/
test_oauth2.py
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
import re
import uuid
from unittest.mock import Mock
from ..oauth2 import (
STATE_COOKIE_NAME,
OAuthenticator,
OAuthLogoutHandler,
_deserialize_state,
_serialize_state,
)
from .mocks import mock_handler
def test_serialize_state():
state1 = {
'state_id': uuid.uuid4().hex,
'next': 'url',
}
b64_state = _serialize_state(state1)
assert isinstance(b64_state, str)
state2 = _deserialize_state(b64_state)
assert state2 == state1
async def test_custom_logout(monkeypatch):
login_url = "http://myhost/login"
authenticator = OAuthenticator()
logout_handler = mock_handler(
OAuthLogoutHandler, authenticator=authenticator, login_url=login_url
)
logout_handler.clear_login_cookie = Mock()
logout_handler.clear_cookie = Mock()
logout_handler._jupyterhub_user = Mock()
monkeypatch.setitem(logout_handler.settings, 'statsd', Mock())
# Sanity check: Ensure the logout handler and url are set on the hub
handlers = [handler for _, handler in authenticator.get_handlers(None)]
assert any([h == OAuthLogoutHandler for h in handlers])
assert authenticator.logout_url('http://myhost') == 'http://myhost/logout'
await logout_handler.get()
assert logout_handler.clear_login_cookie.called
logout_handler.clear_cookie.assert_called_once_with(STATE_COOKIE_NAME)
async def test_httpfetch(client):
authenticator = OAuthenticator()
authenticator.http_request_kwargs = {
"proxy_host": "proxy.example.org",
"proxy_port": 8080,
}
# Return request fields as the response so we can examine it
client.add_host(
"example.org",
[
(
re.compile(".*"),
lambda req: [req.url, req.method, req.proxy_host, req.proxy_port],
),
],
)
authenticator.http_client = client
r = await authenticator.httpfetch("http://example.org/a")
assert r == ['http://example.org/a', 'GET', "proxy.example.org", 8080]