This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
pyoidc-ozwillo/tests/test_oauth2_message.py

599 lines
19 KiB
Python
Raw Normal View History

2011-11-02 12:05:04 +01:00
__author__ = 'rohe0002'
import json
2012-03-19 15:49:31 +01:00
from oic.oauth2.message import *
2013-12-02 14:42:36 +01:00
from jwkest.jwk import SYMKey
2011-11-02 12:05:04 +01:00
from pytest import raises
from utils_for_tests import _eq
class Dummy_Message(Message):
c_param = {
2012-03-19 15:19:50 +01:00
"req_str": SINGLE_REQUIRED_STRING,
"opt_str": SINGLE_OPTIONAL_STRING,
"opt_int": SINGLE_OPTIONAL_INT,
"opt_str_list": OPTIONAL_LIST_OF_STRINGS,
"req_str_list": REQUIRED_LIST_OF_STRINGS,
"opt_json": SINGLE_OPTIONAL_JSON}
2012-03-19 15:19:50 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_1():
ar = AuthorizationRequest(response_type=["code"], client_id="foobar")
2012-03-19 15:19:50 +01:00
ue = ar.to_urlencoded()
2011-11-02 12:05:04 +01:00
print ue
assert ue == "response_type=code&client_id=foobar"
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_2():
ar = AuthorizationRequest(response_type=["code"], client_id="foobar",
redirect_uri="http://foobar.example.com/oaclient",
state="cold")
2012-03-19 15:19:50 +01:00
ue = ar.to_urlencoded()
ue_splits = ue.split('&')
expected_ue_splits = "state=cold&redirect_uri=http%3A%2F%2Ffoobar.example.com%2Foaclient&response_type=code&client_id=foobar".split('&')
assert _eq(ue_splits, expected_ue_splits)
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_3():
ar = AuthorizationRequest(response_type=["token"],
client_id="s6BhdRkqt3",
redirect_uri="https://client.example.com/cb",
state="xyz")
2012-03-19 15:19:50 +01:00
ue = ar.to_urlencoded()
2011-11-02 12:05:04 +01:00
print ue
assert ue == "state=xyz&redirect_uri=https%3A%2F%2Fclient.example.com%2Fcb&response_type=token&client_id=s6BhdRkqt3"
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_4():
ar = AuthorizationRequest(response_type=["code"],
client_id="foobar")
2012-03-19 15:19:50 +01:00
urlencoded = ar.to_urlencoded()
ar2 = AuthorizationRequest().deserialize(urlencoded, "urlencoded")
2011-11-02 12:05:04 +01:00
print ar
print ar2
2012-03-19 15:19:50 +01:00
for attr in ar.c_param.keys():
2011-11-02 12:05:04 +01:00
assert getattr(ar, attr) == getattr(ar2, attr)
2012-03-19 15:19:50 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_5():
ar = AuthorizationRequest(response_type=["code"], client_id="foobar",
redirect_uri="http://foobar.example.com/oaclient",
scope=["foo", "bar"], state="cold")
2011-11-02 12:05:04 +01:00
2012-03-19 15:19:50 +01:00
ue = ar.to_urlencoded()
ue_splits = ue.split('&')
expected_ue_splits = "scope=foo+bar&state=cold&redirect_uri=http%3A%2F%2Ffoobar.example.com%2Foaclient&response_type=code&client_id=foobar".split('&')
assert _eq(ue_splits, expected_ue_splits)
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_6():
ar = AuthorizationRequest(response_type=["code"],
client_id="foobar",
redirect_uri="http://foobar.example.com/oaclient",
scope=["foo", "bar"], state="cold")
2011-11-02 12:05:04 +01:00
2012-03-19 15:19:50 +01:00
urlencoded = ar.to_urlencoded()
ar2 = AuthorizationRequest().deserialize(urlencoded, "urlencoded")
2011-11-02 12:05:04 +01:00
2012-03-19 15:19:50 +01:00
assert ar == ar2
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_7():
ar = AuthorizationRequest(response_type=["code"])
2011-11-02 12:05:04 +01:00
raises(MissingRequiredAttribute, ar.verify)
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_8():
args = {"response_type": [10], "client_id": "foobar",
"redirect_uri": "http://foobar.example.com/oaclient",
"scope": ["foo", "bar"], "state": "cold"}
2011-11-02 12:05:04 +01:00
raises(Exception, 'AuthorizationRequest(**args)')
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_9():
txt = "scope=foo+bar&state=-11&redirect_uri=http%3A%2F%2Ffoobar.example.com%2Foaclient&response_type=code&client_id=foobar"
ar = AuthorizationRequest().deserialize(txt, "urlencoded")
2011-11-02 12:05:04 +01:00
print ar
2012-03-19 15:19:50 +01:00
assert ar["state"] == "-11"
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_10():
txt = "scope=openid&state=id-6a3fc96caa7fd5cb1c7d00ed66937134&redirect_uri=http%3A%2F%2Flocalhost%3A8087authz&response_type=code&client_id=a1b2c3"
ar = AuthorizationRequest().deserialize(txt, "urlencoded")
2011-11-02 12:05:04 +01:00
print ar
2012-03-19 15:19:50 +01:00
assert ar["scope"] == ["openid"]
assert ar["response_type"] == ["code"]
2011-11-02 12:05:04 +01:00
def test_authz_req_json_1():
ar = AuthorizationRequest(response_type=["code"],
client_id="foobar")
js_obj = json.loads(ar.serialize(method="json"))
expected_js_obj = {"response_type": "code", "client_id": "foobar"}
assert js_obj == expected_js_obj
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_json_2():
ar = AuthorizationRequest(response_type=["code"],
client_id="foobar",
redirect_uri="http://foobar.example.com/oaclient",
state="cold")
ue_obj = json.loads(ar.serialize(method="json"))
expected_ue_obj = {
"response_type": "code",
"state": "cold",
"redirect_uri": "http://foobar.example.com/oaclient",
"client_id": "foobar"
}
assert ue_obj == expected_ue_obj
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_3():
ar = AuthorizationRequest(response_type=["token"],
client_id="s6BhdRkqt3",
redirect_uri="https://client.example.com/cb",
state="xyz")
ue_obj = json.loads(ar.serialize(method="json"))
expected_ue_obj = {"state": "xyz", "redirect_uri": "https://client.example.com/cb", "response_type": "token", "client_id": "s6BhdRkqt3"}
assert ue_obj == expected_ue_obj
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_urlencoded_4():
ar = AuthorizationRequest(response_type=["code"],
client_id="foobar")
2012-03-19 15:19:50 +01:00
jtxt = ar.serialize(method="json")
ar2 = AuthorizationRequest().deserialize(jtxt, "json")
2011-11-02 12:05:04 +01:00
print ar
print ar2
2012-03-19 15:19:50 +01:00
assert ar == ar2
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_x1():
query = 'redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fauthz&response_type=code&client_id=0123456789'
ar = AuthorizationRequest().deserialize(query, "urlencoded")
2011-11-02 12:05:04 +01:00
print ar
assert ar.verify()
# AuthorizationErrorResponse
def test_authz_err_resp_1():
aer = AuthorizationErrorResponse(error="access_denied",
state="xyz")
2011-11-02 12:05:04 +01:00
assert aer
print aer.__dict__.items()
2012-03-19 15:19:50 +01:00
assert aer["error"] == "access_denied"
assert aer["state"] == "xyz"
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_err_resp_2():
aer = AuthorizationErrorResponse(error="access_denied",
error_description="brewers has a four game series",
foo="bar")
2011-11-02 12:05:04 +01:00
assert aer
print aer.__dict__.items()
2012-03-19 15:19:50 +01:00
assert aer["error"] == "access_denied"
assert aer["error_description"] == "brewers has a four game series"
2011-11-02 12:05:04 +01:00
# TokenErrorResponse
def test_authz_err_resp_1():
ter = TokenErrorResponse(error="access_denied", state="xyz")
2011-11-02 12:05:04 +01:00
assert ter
print ter.__dict__.items()
2012-03-19 15:19:50 +01:00
assert ter["error"] == "access_denied"
2012-03-19 15:47:55 +01:00
assert _eq(ter.keys(), ['state', 'error'])
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_err_resp_2():
ter = TokenErrorResponse(error="access_denied",
2012-03-19 15:19:50 +01:00
error_description="brewers has a four game series",
foo="bar")
2011-11-02 12:05:04 +01:00
assert ter
print ter.__dict__.items()
2012-03-19 15:19:50 +01:00
assert ter["error"] == "access_denied"
assert ter["error_description"] == "brewers has a four game series"
assert ter["foo"] == "bar"
2011-11-02 12:05:04 +01:00
2011-12-10 16:36:08 +01:00
assert "error" in ter
assert "error_description" in ter
assert "foo" in ter
2011-11-02 12:05:04 +01:00
# AccessTokenResponse
def test_accesstokenreponse_1():
at = AccessTokenResponse(access_token="SlAV32hkKG",
token_type="Bearer", expires_in=3600)
2011-11-02 12:05:04 +01:00
assert at
2012-03-19 15:19:50 +01:00
atj = at.serialize(method="json")
atj_obj = json.loads(atj)
expected_atj_obj = {"token_type": "Bearer", "access_token": "SlAV32hkKG", "expires_in": 3600}
assert atj_obj == expected_atj_obj
2011-11-02 12:05:04 +01:00
# AccessTokenRequest
def test_extra():
atr = AccessTokenRequest(grant_type="authorization_code",
code="SplxlOBeZQQYbYS6WxSbIA",
redirect_uri="https://client.example.com/cb",
extra="foo")
2011-11-02 12:05:04 +01:00
assert atr
2012-03-19 15:19:50 +01:00
query = atr.to_urlencoded()
query_splits = query.split('&')
expected_query_splits = "code=SplxlOBeZQQYbYS6WxSbIA&redirect_uri=https%3A%2F" \
"%2Fclient.example.com%2Fcb&grant_type=authorization_code&extra=foo".split('&')
assert _eq(query_splits, expected_query_splits)
2011-11-02 12:05:04 +01:00
atr2 = AccessTokenRequest().deserialize(query, "urlencoded")
2012-03-19 15:19:50 +01:00
print atr.to_dict()
print atr2.to_dict()
assert atr == atr2
2011-11-02 12:05:04 +01:00
# AuthorizationResponse
def test_authz_resp_1():
atr = AuthorizationResponse(code="SplxlOBeZQQYbYS6WxSbIA",
state="Fun_state", extra="foo")
2011-11-02 12:05:04 +01:00
2012-03-19 15:19:50 +01:00
assert atr["code"] == "SplxlOBeZQQYbYS6WxSbIA"
assert atr["state"] == "Fun_state"
assert atr["extra"] == "foo"
2011-11-02 12:05:04 +01:00
# ROPCAccessTokenRequest
#noinspection PyArgumentEqualDefault
def test_ropc_acc_token_req():
ropc = ROPCAccessTokenRequest(grant_type="password",
username="johndoe", password="A3ddj3w")
2011-11-02 12:05:04 +01:00
2012-03-19 15:19:50 +01:00
assert ropc["grant_type"] == "password"
assert ropc["username"] == "johndoe"
assert ropc["password"] == "A3ddj3w"
2011-11-02 12:05:04 +01:00
# CCAccessTokenRequest
def test_cc_acc_token_req():
cc = CCAccessTokenRequest(scope="/foo")
2011-11-02 12:05:04 +01:00
2012-03-19 15:19:50 +01:00
assert cc["grant_type"] == "client_credentials"
2013-09-23 12:08:42 +02:00
assert cc["scope"] == ["/foo"]
2011-11-02 12:05:04 +01:00
# RefreshAccessTokenRequest
def test_ratr():
ratr = RefreshAccessTokenRequest(refresh_token="ababababab",
2012-03-19 15:19:50 +01:00
client_id="Client_id")
2011-11-02 12:05:04 +01:00
2012-03-19 15:19:50 +01:00
assert ratr["grant_type"] == "refresh_token"
assert ratr["refresh_token"] == "ababababab"
assert ratr["client_id"] == "Client_id"
2011-11-02 12:05:04 +01:00
assert ratr.verify()
2011-11-02 12:05:04 +01:00
def test_authz_load_dict():
bib = {"scope": ["openid"],
"state": "id-6da9ca0cc23959f5f33e8becd9b08cae",
"redirect_uri": "http://localhost:8087authz",
"response_type": ["code"],
"client_id": "a1b2c3"}
arq = AuthorizationRequest(**bib)
2011-11-02 12:05:04 +01:00
2012-03-19 15:19:50 +01:00
assert arq["scope"] == bib["scope"]
assert arq["response_type"] == bib["response_type"]
assert arq["redirect_uri"] == bib["redirect_uri"]
assert arq["state"] == bib["state"]
assert arq["client_id"] == bib["client_id"]
2011-11-02 12:05:04 +01:00
2011-11-02 12:05:04 +01:00
def test_authz_req_set_json():
argv = {"scope": ["openid"],
"state": "id-b0be8bb64118c3ec5f70093a1174b039",
"redirect_uri": "http://localhost:8087authz",
"response_type": ["code"],
"client_id": "a1b2c3"}
arq = AuthorizationRequest(**argv)
2011-11-02 12:05:04 +01:00
2012-03-19 15:19:50 +01:00
jstr = arq.serialize(method="json")
2011-11-02 12:05:04 +01:00
jarq = AuthorizationRequest().deserialize(jstr, "json")
2011-11-02 12:05:04 +01:00
2012-03-19 15:19:50 +01:00
assert jarq["scope"] == ["openid"]
assert jarq["response_type"] == ["code"]
assert jarq["redirect_uri"] == "http://localhost:8087authz"
assert jarq["state"] == "id-b0be8bb64118c3ec5f70093a1174b039"
assert jarq["client_id"] == "a1b2c3"
2011-11-30 15:06:19 +01:00
2011-11-30 15:06:19 +01:00
def test_sp_sep_list_deserializer():
2011-12-02 13:56:05 +01:00
vals = sp_sep_list_deserializer("foo bar zen")
2011-11-30 15:06:19 +01:00
assert len(vals) == 3
assert _eq(vals, ["foo", "bar", "zen"])
2011-12-02 13:56:05 +01:00
vals = sp_sep_list_deserializer(["foo bar zen"])
2011-11-30 15:06:19 +01:00
assert len(vals) == 3
assert _eq(vals, ["foo", "bar", "zen"])
2011-11-30 15:06:19 +01:00
def test_json_serializer():
2011-12-02 13:56:05 +01:00
val = json_serializer({"foo": ["bar", "stool"]})
2011-11-30 15:06:19 +01:00
print val
assert val == '{"foo": ["bar", "stool"]}'
2011-11-30 15:06:19 +01:00
def test_json_deserializer():
_dict = {"foo": ["bar", "stool"]}
2011-12-02 13:56:05 +01:00
val = json_serializer(_dict)
2011-11-30 15:06:19 +01:00
2011-12-02 13:56:05 +01:00
sdict = json_deserializer(val)
2011-11-30 15:06:19 +01:00
assert _dict == sdict
2011-11-30 15:06:19 +01:00
def test_omit():
err = ErrorResponse(error="invalid_request",
error_description="Something was missing",
error_uri="http://example.com/error_message.html")
2011-11-30 15:06:19 +01:00
ue_str = err.to_urlencoded()
2012-03-19 15:19:50 +01:00
del err["error_uri"]
ueo_str = err.to_urlencoded()
2011-11-30 15:06:19 +01:00
assert ue_str != ueo_str
assert "error_message" not in ueo_str
assert "error_message" in ue_str
2011-11-30 15:06:19 +01:00
def test_missing_required():
err = ErrorResponse()
2012-03-19 15:19:50 +01:00
assert "error" not in err
2011-11-30 15:06:19 +01:00
raises(MissingRequiredAttribute, "err.to_urlencoded()")
2012-03-19 15:19:50 +01:00
def test_to_urlencoded():
atr = AccessTokenResponse(
2012-03-19 15:19:50 +01:00
access_token="2YotnFZFEjr1zCsicMWpAA",
token_type="example",
expires_in=3600,
refresh_token="tGzv3JOkF0XG5Qx2TlKWIA",
example_parameter="example_value",
scope=["inner", "outer"])
2011-11-30 15:06:19 +01:00
2012-03-19 15:19:50 +01:00
assert _eq(atr["scope"], ["inner", "outer"])
2011-11-30 15:06:19 +01:00
2012-03-19 15:19:50 +01:00
uec = atr.to_urlencoded()
2011-11-30 15:06:19 +01:00
print uec
assert "inner+outer" in uec
2012-03-19 15:19:50 +01:00
def test_to_urlencoded_extended_omit():
atr = AccessTokenResponse(
2012-03-19 15:19:50 +01:00
access_token="2YotnFZFEjr1zCsicMWpAA",
token_type="example",
expires_in=3600,
refresh_token="tGzv3JOkF0XG5Qx2TlKWIA",
example_parameter="example_value",
scope=["inner", "outer"],
extra=["local", "external"],
level=3)
uec = atr.to_urlencoded()
2011-11-30 15:06:19 +01:00
print uec
assert "level=3" in uec
assert "example_parameter=example_value" in uec
2012-03-19 15:19:50 +01:00
assert "extra=local" in uec
assert "extra=external" in uec
del atr["extra"]
ouec = atr.to_urlencoded()
2011-11-30 15:06:19 +01:00
print ouec
assert "level=3" in ouec
assert "example_parameter=example_value" in ouec
2012-03-19 15:19:50 +01:00
assert "extra=local" not in ouec
2011-11-30 15:06:19 +01:00
assert uec != ouec
2012-03-19 15:19:50 +01:00
assert len(uec) == (len(ouec) + len("extra=local") +
len("extra=external") + 2)
2011-11-30 15:06:19 +01:00
atr2 = AccessTokenResponse().deserialize(uec, "urlencoded")
2011-11-30 15:06:19 +01:00
print atr2.keys()
assert _eq(atr2.keys(), ['access_token', 'expires_in', 'token_type',
'scope', 'refresh_token', 'level',
'example_parameter', 'extra'])
2011-11-30 15:06:19 +01:00
atr3 = AccessTokenResponse().deserialize(ouec, "urlencoded")
2011-11-30 15:06:19 +01:00
print atr3.keys()
assert _eq(atr3.keys(), ['access_token', 'expires_in', 'token_type',
'scope', 'refresh_token', 'level',
'example_parameter'])
2011-11-30 15:06:19 +01:00
##noinspection PyUnusedLocal
2012-03-19 15:19:50 +01:00
#def test_to_urlencoded_to_many_values():
2011-11-30 15:06:19 +01:00
# uec = "access_token=2YotnFZFEjr1zCsicMWpAA+AAA111BBB222CCC333"
2011-12-02 13:56:05 +01:00
# raises(ValueError, "AccessTokenResponse.set_urlencoded(uec)")
2011-11-30 15:06:19 +01:00
#noinspection PyUnusedLocal
2012-03-19 15:19:50 +01:00
def test_to_set_json():
2011-11-30 15:06:19 +01:00
"""
"""
item = Dummy_Message(req_str="Fair", opt_str="game", opt_int=9,
opt_str_list=["one", "two"],
req_str_list=["spike", "lee"],
opt_json='{"ford": "green"}')
2011-11-30 15:06:19 +01:00
2012-03-19 15:19:50 +01:00
jso = item.serialize(method="json")
2011-11-30 15:06:19 +01:00
print jso
item2 = Dummy_Message().deserialize(jso, "json")
2011-11-30 15:06:19 +01:00
print item2
assert _eq(item2.keys(), ['opt_str', 'req_str', 'opt_json', 'req_str_list',
'opt_str_list', 'opt_int'])
2011-11-30 15:06:19 +01:00
jso_1 = '{"req_str": "Fair", "req_str_list": ["spike", "lee"], "opt_int": [9]}'
item3 = Dummy_Message().deserialize(jso_1, "json")
2012-03-19 15:19:50 +01:00
assert _eq(item3.keys(), ['req_str', 'req_str_list', 'opt_int'])
2012-03-19 15:19:50 +01:00
assert item3["opt_int"] == 9
2011-11-30 15:06:19 +01:00
jso_2 = '{"req_str": "Fair", "req_str_list": ["spike", "lee"], "opt_int": [9, 10]}'
raises(TooManyValues, 'Dummy_Message().deserialize(jso_2, "json")')
2011-11-30 15:06:19 +01:00
jso_3 = '{"req_str": "Fair", "req_str_list": ["spike", "lee"], "extra": "out"}'
item4 = Dummy_Message().deserialize(jso_3, "json")
2011-11-30 15:06:19 +01:00
print item4
assert _eq(item4.keys(), ['req_str', 'req_str_list', 'extra'])
2012-03-19 15:19:50 +01:00
assert item4["extra"] == "out"
item4 = Dummy_Message().deserialize(jso_3, "json")
2011-11-30 15:06:19 +01:00
print item4
assert _eq(item4.keys(), ['req_str', 'req_str_list', 'extra'])
2012-03-19 15:19:50 +01:00
assert item4["extra"] == "out"
2011-11-30 15:06:19 +01:00
2011-11-30 15:06:19 +01:00
def test_to_from_jwt():
item = Dummy_Message(req_str="Fair", opt_str="game", opt_int=9,
opt_str_list=["one", "two"],
req_str_list=["spike", "lee"],
opt_json='{"ford": "green"}')
2011-11-30 15:06:19 +01:00
2013-12-02 14:42:36 +01:00
keys = [SYMKey(key="A1B2C3D4")]
jws = item.to_jwt(keys, "HS256")
2011-11-30 15:06:19 +01:00
print jws
jitem = Dummy_Message().from_jwt(jws, key=keys)
2011-11-30 15:06:19 +01:00
print jitem.keys()
assert _eq(jitem.keys(), ['opt_str', 'req_str', 'opt_json',
'req_str_list', 'opt_str_list', 'opt_int'])
def test_TokenRevocationRequest():
trr = TokenRevocationRequest(token="token")
2011-11-30 15:06:19 +01:00
assert trr.verify()
2012-03-19 15:19:50 +01:00
def test_message():
_dict = {"req_str": "Fair", "opt_str": "game", "opt_int": 9,
"opt_str_list": ["one", "two"], "req_str_list": ["spike", "lee"],
"opt_json": '{"ford": "green"}'}
2011-11-30 15:06:19 +01:00
cls = Dummy_Message(**_dict)
2011-11-30 15:06:19 +01:00
cls.verify()
assert _eq(cls.keys(), ['opt_str', 'req_str', 'opt_json',
2012-03-19 15:19:50 +01:00
'req_str_list', 'opt_str_list', 'opt_int'])
2011-11-30 15:06:19 +01:00
_dict = {"req_str": "Fair", "opt_str": "game", "opt_int": 9,
"opt_str_list": ["one", "two"], "req_str_list": ["spike", "lee"],
"opt_json": '{"ford": "green"}', "extra": "internal"}
2011-11-30 15:06:19 +01:00
cls = Dummy_Message(**_dict)
2011-11-30 15:06:19 +01:00
cls.verify()
2012-03-19 15:19:50 +01:00
print cls.keys()
assert _eq(cls.keys(), ['opt_str', 'req_str', 'extra', 'opt_json',
'req_str_list', 'opt_str_list', 'opt_int'])
2011-11-30 15:06:19 +01:00
_dict = {"req_str": "Fair", "opt_str": "game", "opt_int": 9,
"opt_str_list": ["one", "two"], "req_str_list": ["spike", "lee"]}
2011-11-30 15:06:19 +01:00
cls = Dummy_Message(**_dict)
2011-11-30 15:06:19 +01:00
cls.verify()
assert _eq(cls.keys(), ['opt_str', 'req_str', 'req_str_list',
'opt_str_list', 'opt_int'])
2011-12-10 16:36:08 +01:00
def test_request():
req = Dummy_Message(req_str="Fair",
req_str_list=["game"]).request("http://example.com")
2011-12-10 16:36:08 +01:00
assert req == "http://example.com?req_str=Fair&req_str_list=game"
2012-03-25 21:57:51 +02:00
def test_multiple_response_types_urlencoded():
ar = AuthorizationRequest(response_type=["code", "token"],
client_id="foobar")
ue = ar.to_urlencoded()
ue_splits = ue.split('&')
expected_ue_splits = "response_type=code+token&client_id=foobar".split('&')
assert _eq(ue_splits, expected_ue_splits)
are = AuthorizationRequest().deserialize(ue, "urlencoded")
assert _eq(are.keys(), ["response_type", "client_id"])
assert _eq(are["response_type"], ["code", "token"])
2012-03-25 21:57:51 +02:00
def test_multiple_scopes_urlencoded():
ar = AuthorizationRequest(response_type=["code", "token"],
client_id="foobar", scope=["openid", "foxtrot"])
ue = ar.to_urlencoded()
ue_splits = ue.split('&')
expected_ue_splits = "scope=openid+foxtrot&response_type=code+token&client_id=foobar".split('&')
assert _eq(ue_splits, expected_ue_splits)
are = AuthorizationRequest().deserialize(ue, "urlencoded")
assert _eq(are.keys(), ["response_type", "client_id", "scope"])
assert _eq(are["response_type"], ["code", "token"])
assert _eq(are["scope"], ["openid", "foxtrot"])
2012-03-25 21:57:51 +02:00
2012-03-25 21:57:51 +02:00
def test_multiple_response_types_json():
ar = AuthorizationRequest(response_type=["code", "token"],
client_id="foobar")
2012-03-25 21:57:51 +02:00
ue = ar.to_json()
ue_obj = json.loads(ue)
expected_ue_obj = {"response_type": "code token", "client_id": "foobar"}
assert ue_obj == expected_ue_obj
2012-03-25 21:57:51 +02:00
are = AuthorizationRequest().deserialize(ue, "json")
2012-03-25 21:57:51 +02:00
print are.keys()
assert _eq(are.keys(), ["response_type", "client_id"])
assert _eq(are["response_type"], ["code", "token"])
2012-03-25 21:57:51 +02:00
def test_multiple_scopes_json():
ar = AuthorizationRequest(response_type=["code", "token"],
client_id="foobar", scope=["openid", "foxtrot"])
2012-03-25 21:57:51 +02:00
ue = ar.to_json()
ue_obj = json.loads(ue)
expected_ue_obj = {"scope": "openid foxtrot", "response_type": "code token", "client_id": "foobar"}
assert ue_obj == expected_ue_obj
2012-03-25 21:57:51 +02:00
are = AuthorizationRequest().deserialize(ue, "json")
2012-03-25 21:57:51 +02:00
assert _eq(are.keys(), ["response_type", "client_id", "scope"])
assert _eq(are["response_type"], ["code", "token"])
assert _eq(are["scope"], ["openid", "foxtrot"])
if __name__ == "__main__":
test_to_from_jwt()