Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_start_auth(self, context):
context.path = 'facebook/sso/redirect'
internal_request = InternalRequest(UserIdHashType.transient, 'test_requester')
resp = self.fb_backend.start_auth(context, internal_request, mock_get_state)
login_url = resp.message
assert login_url.startswith(FB_AUTH_ENDPOINT)
expected_params = {
"client_id": CLIENT_ID,
"state": mock_get_state.return_value,
"response_type": "code",
"redirect_uri": "%s/%s" % (BASE_URL, AUTHZ_PAGE)
}
actual_params = dict(parse_qsl(urlparse(login_url).query))
assert actual_params == expected_params
def test_set_user_id_from_attributes():
# uid = "my_id"
attributes = {"attr_1": "v1", "attr_2": "v2", "attr_3": "v3"}
uid_attributes = ["attr_1", "attr_3"]
uid = "v1v3"
internal_response = InternalResponse(UserIdHashType.persistent)
internal_response.add_attributes(attributes)
internal_response.set_user_id_from_attr(uid_attributes)
assert uid == internal_response.get_user_id()
def test_id_hash_transient():
requesters = ["test_requester0", "test_requester0", "test_requester2"]
user_ids = ["userid0", "userid1", "userid2"]
hash_type = UserIdHashType.transient
ids = []
for requester in requesters:
for id in user_ids:
hashed_id = _get_id(requester, id, hash_type)
assert hashed_id not in ids
ids.append(hashed_id)
def test_entire_flow(self, context):
"""Tests start of authentication (incoming auth req) and receiving auth response."""
responses.add(responses.POST,
"https://graph.facebook.com/v2.5/oauth/access_token",
body=json.dumps({"access_token": "qwerty",
"token_type": "bearer",
"expires_in": 9999999999999}),
status=200,
content_type='application/json')
self.setup_facebook_response()
context.path = 'facebook/sso/redirect'
internal_request = InternalRequest(UserIdHashType.transient, 'test_requester')
self.fb_backend.start_auth(context, internal_request, mock_get_state)
context.request = {
"code": FB_RESPONSE_CODE,
"state": mock_get_state.return_value
}
self.fb_backend._authn_response(context)
assert self.fb_backend.name not in context.state
self.assert_expected_attributes()
def test_from_string(self, str_value, expected_value):
assert UserIdHashType.from_string(str_value) == expected_value
def test_id_hash_persistent():
requesters = ["test_requester0"]
user_ids = ["userid0", "userid1", "userid2"]
hash_type = UserIdHashType.persistent
ids = []
for requester in requesters:
for id in user_ids:
hashed_id = _get_id(requester, id, hash_type)
assert hashed_id == _get_id(requester, id, hash_type)
assert hashed_id not in ids
ids.append(hashed_id)
def test_set_user_id():
uid = "my_id"
attributes = {"attr_1": "v1", "attr_2": "v2", "attr_3": "v3"}
internal_response = InternalResponse(UserIdHashType.persistent)
internal_response.add_attributes(attributes)
internal_response.set_user_id(uid)
assert uid == internal_response.get_user_id()
("transient", UserIdHashType.transient),
("persistent", UserIdHashType.persistent),
("pairwise", UserIdHashType.pairwise),
("public", UserIdHashType.public)
])
def test_from_string(self, str_value, expected_value):
assert UserIdHashType.from_string(str_value) == expected_value
def saml_name_format_to_hash_type(name_format):
"""
Translate pySAML2 name format to statosa format
:type name_format: str
:rtype: satosa.internal_data.UserIdHashType
:param name_format: SAML2 name format
:return: satosa format
"""
if name_format == NAMEID_FORMAT_TRANSIENT:
return UserIdHashType.transient
elif name_format == NAMEID_FORMAT_PERSISTENT:
return UserIdHashType.persistent
return None
def saml_name_format_to_hash_type(name_format):
"""
Translate pySAML2 name format to statosa format
:type name_format: str
:rtype: satosa.internal_data.UserIdHashType
:param name_format: SAML2 name format
:return: satosa format
"""
if name_format == NAMEID_FORMAT_TRANSIENT:
return UserIdHashType.transient
elif name_format == NAMEID_FORMAT_PERSISTENT:
return UserIdHashType.persistent
return None