forked from simple-login/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_oauth_models.py
79 lines (62 loc) · 2.67 KB
/
test_oauth_models.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
import flask
import pytest
from app.oauth_models import (
get_scopes,
Scope,
get_response_types,
ResponseType,
response_types_to_str,
get_response_types_from_str,
)
def test_get_scopes(flask_app):
with flask_app.test_request_context("/"):
scopes = get_scopes(flask.request)
assert scopes == set()
with flask_app.test_request_context("/?scope=email&scope=name"):
scopes = get_scopes(flask.request)
assert scopes == {Scope.NAME, Scope.EMAIL}
# a space between email and name
with flask_app.test_request_context("/?scope=email%20name"):
scopes = get_scopes(flask.request)
assert scopes == {Scope.NAME, Scope.EMAIL}
# a comma between email and name
with flask_app.test_request_context("/?scope=email,name"):
scopes = get_scopes(flask.request)
assert scopes == {Scope.NAME, Scope.EMAIL}
# non-existent scope: raise ValueError
with flask_app.test_request_context("/?scope=abcd"):
with pytest.raises(ValueError):
get_scopes(flask.request)
def test_get_response_types(flask_app):
with flask_app.test_request_context("/"):
response_types = get_response_types(flask.request)
assert response_types == set()
with flask_app.test_request_context("/?response_type=token&response_type=id_token"):
response_types = get_response_types(flask.request)
assert response_types == {ResponseType.TOKEN, ResponseType.ID_TOKEN}
# a space as separator
with flask_app.test_request_context("/?response_type=token%20id_token"):
response_types = get_response_types(flask.request)
assert response_types == {ResponseType.TOKEN, ResponseType.ID_TOKEN}
# a comma as separator
with flask_app.test_request_context("/?response_type=id_token,token"):
response_types = get_response_types(flask.request)
assert response_types == {ResponseType.TOKEN, ResponseType.ID_TOKEN}
# non-existent response_type: raise ValueError
with flask_app.test_request_context("/?response_type=abcd"):
with pytest.raises(ValueError):
get_response_types(flask.request)
def test_response_types_to_str():
assert response_types_to_str([]) == ""
assert response_types_to_str([ResponseType.CODE]) == "code"
assert (
response_types_to_str([ResponseType.CODE, ResponseType.ID_TOKEN])
== "code,id_token"
)
def test_get_response_types_from_str():
assert get_response_types_from_str("") == set()
assert get_response_types_from_str("token") == {ResponseType.TOKEN}
assert get_response_types_from_str("token id_token") == {
ResponseType.TOKEN,
ResponseType.ID_TOKEN,
}