Skip to content

Commit

Permalink
Add test for sso2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tracy.Wu committed Jul 5, 2021
1 parent c86b593 commit 77d6bba
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 3 additions & 3 deletions fastapi_opa/auth/auth_saml.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def authenticate(
elif 'sso2' in request.query_params:
logger.debug(datetime.utcnow(), '--sso2--')
return_to = '%sattrs/' % request.base_url
return RedirectResponse(auth.login(return_to))
return await self.single_sign_on(auth, return_to)
elif "acs" in request.query_params:
logger.debug(datetime.utcnow(), '--acs--')
return await self.assertion_consumer_service(auth, request_args, request)
Expand Down Expand Up @@ -93,8 +93,8 @@ async def single_log_out(auth: OneLogin_Saml2_Auth) -> RedirectResponse:
spnq=name_id_spnq))

@staticmethod
async def single_sign_on(auth: OneLogin_Saml2_Auth) -> RedirectResponse:
redirect_url = auth.login()
async def single_sign_on(auth: OneLogin_Saml2_Auth, url: str = None) -> RedirectResponse:
redirect_url = auth.login(url)
return RedirectResponse(redirect_url)

@staticmethod
Expand Down
20 changes: 19 additions & 1 deletion tests/test_saml_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,30 @@ async def test_single_sign_on():

saml_auth_mock = Mock()
saml_auth_mock.login.return_value = "http://idp.com/cryptic-stuff"
response = await saml_auth.single_sign_on(saml_auth_mock)
url = r"http://idp.com/cryptic-stuff/attrs"
response = await saml_auth.single_sign_on(saml_auth_mock, url)

assert isinstance(response, RedirectResponse)
assert response.headers.get("location") == "http://idp.com/cryptic-stuff"


@pytest.mark.asyncio
async def test_single_sign_on_with_parameter():
saml_conf = SAMLConfig(settings_directory="./tests/test_data/saml")
saml_auth = SAMLAuthentication(saml_conf)

def side_effect(url):
return url

saml_auth_mock = Mock()
saml_auth_mock.login = Mock(side_effect=side_effect)
attr_url = "http://idp.com/cryptic-stuff/attrs"
response = await saml_auth.single_sign_on(saml_auth_mock, attr_url)

assert isinstance(response, RedirectResponse)
assert response.headers.get("location") == attr_url


@pytest.mark.asyncio
@patch("fastapi_opa.auth.auth_saml.OneLogin_Saml2_Utils")
async def test_assertion_consumer_service(saml_util_mock):
Expand Down

0 comments on commit 77d6bba

Please sign in to comment.