Skip to content

test_sso_authentication#

make_sso_auth_request#

Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
@pytest.fixture
def make_sso_auth_request(enable_sso):
    def f(sso_session, use_wrong_key=False) -> Tuple[Optional[MetaxUser], Optional[str]]:
        factory = APIRequestFactory()
        request = factory.get("/")
        if sso_session:
            key = "this_key_is_wrong" if use_wrong_key else django_settings.SSO_SECRET_KEY
            sso_token = jwt.encode(sso_session, key=key)
            request.COOKIES[django_settings.SSO_SESSION_COOKIE] = sso_token
        try:
            authentication = SSOAuthentication()
            auths = authentication.authenticate(request=request)
            if auths is None:
                return None, None
            return auths[0], None
        except exceptions.AuthenticationFailed as e:
            return None, e.get_codes()

    return f

test_sso_authentication_ok#

Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
def test_sso_authentication_ok(make_sso_auth_request, sso_session_teppo):
    user, error = make_sso_auth_request(sso_session_teppo)
    assert user.username == "fd_teppo3"
    assert user.csc_projects == ["fd_teppo3_project"]
    assert error == None

test_sso_authentication_sync#

Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
def test_sso_authentication_sync(make_sso_auth_request, sso_session_teppo, sso_format_datetime):
    user, error = make_sso_auth_request(sso_session_teppo)
    sso_session_teppo["services"]["IDA"]["projects"] = ["fd_teppo3_project", "new_project"]
    sso_session_teppo["initiated"] = sso_format_datetime(
        parse(sso_session_teppo["initiated"]) + timedelta(hours=1)
    )

    # session is newer than previous sync, should sync user details
    user, error = make_sso_auth_request(sso_session_teppo)
    assert user.csc_projects == ["fd_teppo3_project", "new_project"]
    assert error == None

test_sso_authentication_dont_sync_old#

Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
def test_sso_authentication_dont_sync_old(make_sso_auth_request, sso_session_teppo):
    make_sso_auth_request(sso_session_teppo)
    sso_session_teppo["services"]["IDA"]["projects"] = ["fd_teppo3_project", "new_project"]

    # session is not newer than previous sync, should not sync user details
    user, error = make_sso_auth_request(sso_session_teppo)
    assert user.csc_projects == ["fd_teppo3_project"]
    assert error == None

test_sso_authentication_unauthenticated#

Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
def test_sso_authentication_unauthenticated(make_sso_auth_request):
    user, error = make_sso_auth_request(None)
    assert user == None
    assert error == None

test_sso_authentication_disabled#

Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
def test_sso_authentication_disabled(make_sso_auth_request, sso_session_teppo, settings):
    settings.ENABLE_SSO_AUTH = False
    user, error = make_sso_auth_request(sso_session_teppo)
    assert user == None
    assert error == None

test_sso_authentication_missing_secret#

Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
def test_sso_authentication_missing_secret(make_sso_auth_request, sso_session_teppo, settings):
    settings.SSO_SECRET_KEY = ""
    user, error = make_sso_auth_request(sso_session_teppo)
    assert error == "invalid_sso_configuration"
Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
def test_sso_authentication_missing_cookie_setting(
    make_sso_auth_request, sso_session_teppo, settings
):
    settings.SSO_SESSION_COOKIE = ""
    user, error = make_sso_auth_request(sso_session_teppo)
    assert error == "invalid_sso_configuration"

test_sso_authentication_wrong_secret#

Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
def test_sso_authentication_wrong_secret(make_sso_auth_request, sso_session_teppo):
    user, error = make_sso_auth_request(sso_session_teppo, use_wrong_key=True)
    assert error == "authentication_failed"

test_sso_authentication_missing_fairdata_user_id#

Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
def test_sso_authentication_missing_fairdata_user_id(make_sso_auth_request, sso_session_teppo):
    sso_session_teppo["fairdata_user"]["id"] = None
    user, error = make_sso_auth_request(sso_session_teppo)
    assert user == None
    assert error == "missing_fairdata_user_id"

test_sso_authentication_missing_organization_user_id#

Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
def test_sso_authentication_missing_organization_user_id(make_sso_auth_request, sso_session_teppo):
    sso_session_teppo["authenticated_user"]["organization"]["id"] = None
    user, error = make_sso_auth_request(sso_session_teppo)
    assert user == None
    assert error == "missing_organization_id"

test_sso_authentication_locked_user#

Source code in tests/unit/apps/users/authentication/test_sso_authentication.py
def test_sso_authentication_locked_user(make_sso_auth_request, sso_session_teppo):
    sso_session_teppo["fairdata_user"]["locked"] = True
    user, error = make_sso_auth_request(sso_session_teppo)
    assert user == None
    assert error == "fairdata_user_locked"