-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtest_sso.py
171 lines (137 loc) · 3.83 KB
/
test_sso.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
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
import time
import pytest
from garth import sso
from garth.auth_tokens import OAuth1Token, OAuth2Token
from garth.exc import GarthException, GarthHTTPError
from garth.http import Client
@pytest.mark.vcr
def test_login_email_password_fail(client: Client):
with pytest.raises(GarthHTTPError):
@pytest.mark.vcr
def test_login_success(client: Client):
oauth1, oauth2 = sso.login(
)
assert oauth1
assert isinstance(oauth1, OAuth1Token)
assert oauth2
assert isinstance(oauth2, OAuth2Token)
@pytest.mark.vcr
def test_login_success_mfa(monkeypatch, client: Client):
def mock_input(_):
return "671091"
monkeypatch.setattr("builtins.input", mock_input)
oauth1, oauth2 = sso.login(
)
assert oauth1
assert isinstance(oauth1, OAuth1Token)
assert oauth2
assert isinstance(oauth2, OAuth2Token)
@pytest.mark.vcr
def test_login_success_mfa_async(monkeypatch, client: Client):
def mock_input(_):
return "031174"
async def prompt_mfa():
return input("MFA code: ")
monkeypatch.setattr("builtins.input", mock_input)
oauth1, oauth2 = sso.login(
"correct_password",
client=client,
prompt_mfa=prompt_mfa,
)
assert oauth1
assert isinstance(oauth1, OAuth1Token)
assert oauth2
assert isinstance(oauth2, OAuth2Token)
@pytest.mark.vcr
def test_login_return_on_mfa(client: Client):
result = sso.login(
"correct_password",
client=client,
return_on_mfa=True,
)
assert isinstance(result, dict)
assert result["needs_mfa"] is True
assert "client_state" in result
assert "csrf_token" in result["client_state"]
assert "signin_params" in result["client_state"]
assert "client" in result["client_state"]
code = "123456" # obtain from custom login
# test resuming the login
oauth1, oauth2 = sso.resume_login(result["client_state"], code)
assert oauth1
assert isinstance(oauth1, OAuth1Token)
assert oauth2
assert isinstance(oauth2, OAuth2Token)
def test_set_expirations(oauth2_token_dict: dict):
token = sso.set_expirations(oauth2_token_dict)
assert (
token["expires_at"] - time.time() - oauth2_token_dict["expires_in"] < 1
)
assert (
token["refresh_token_expires_at"]
- time.time()
- oauth2_token_dict["refresh_token_expires_in"]
< 1
)
@pytest.mark.vcr
def test_exchange(authed_client: Client):
assert authed_client.oauth1_token
oauth1_token = authed_client.oauth1_token
oauth2_token = sso.exchange(oauth1_token, client=authed_client)
assert not oauth2_token.expired
assert not oauth2_token.refresh_expired
assert oauth2_token.token_type.title() == "Bearer"
assert authed_client.oauth2_token != oauth2_token
def test_get_csrf_token():
html = """
<html>
<head>
</head>
<body>
<h1>Success</h1>
<input name="_csrf" value="foo">
</body>
</html>
"""
assert sso.get_csrf_token(html) == "foo"
def test_get_csrf_token_fail():
html = """
<html>
<head>
</head>
<body>
<h1>Success</h1>
</body>
</html>
"""
with pytest.raises(GarthException):
sso.get_csrf_token(html)
def test_get_title():
html = """
<html>
<head>
<title>Success</title>
</head>
<body>
<h1>Success</h1>
</body>
</html>
"""
assert sso.get_title(html) == "Success"
def test_get_title_fail():
html = """
<html>
<head>
</head>
<body>
<h1>Success</h1>
</body>
</html>
"""
with pytest.raises(GarthException):
sso.get_title(html)