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_resolve_profile_env(mocker):
mocker.patch("faculty.config.resolve_credentials_path")
mocker.patch("faculty.config.load_profile", return_value=DEFAULT_PROFILE)
mocker.patch.dict(
os.environ,
{
"FACULTY_DOMAIN": "other.domain.com",
"FACULTY_PROTOCOL": "other-protocol",
"FACULTY_CLIENT_ID": "other-client-id",
"FACULTY_CLIENT_SECRET": "other-client-secret",
},
)
assert config.resolve_profile() == OTHER_PROFILE
def test_resolve_profile_missing_client_secret(mocker):
mocker.patch("faculty.config.resolve_credentials_path")
mocker.patch(
"faculty.config.load_profile", return_value=PROFILE_WITHOUT_SECRET
)
with pytest.raises(config.CredentialsError, match="client_secret"):
config.resolve_profile()
mocker.patch("faculty.config.resolve_credentials_path")
mocker.patch("faculty.config.load_profile", return_value=DEFAULT_PROFILE)
mocker.patch.dict(
os.environ,
{
"FACULTY_DOMAIN": "other.domain.com",
"FACULTY_PROTOCOL": "other-protocol",
"FACULTY_CLIENT_ID": "other-client-id",
"FACULTY_CLIENT_SECRET": "other-client-secret",
"SHERLOCKML_DOMAIN": "ignored",
"SHERLOCKML_PROTOCOL": "ignored",
"SHERLOCKML_CLIENT_ID": "ignored",
"SHERLOCKML_CLIENT_SECRET": "ignored",
},
)
assert config.resolve_profile() == OTHER_PROFILE
def test_resolve_profile_env_sherlockml(mocker):
mocker.patch("faculty.config.resolve_credentials_path")
mocker.patch("faculty.config.load_profile", return_value=DEFAULT_PROFILE)
mocker.patch.dict(
os.environ,
{
"SHERLOCKML_DOMAIN": "other.domain.com",
"SHERLOCKML_PROTOCOL": "other-protocol",
"SHERLOCKML_CLIENT_ID": "other-client-id",
"SHERLOCKML_CLIENT_SECRET": "other-client-secret",
},
)
with pytest.warns(UserWarning) as records:
assert config.resolve_profile() == OTHER_PROFILE
assert len(records) == 4
assert "SHERLOCKML_DOMAIN is deprecated" in str(records[0].message)
assert "SHERLOCKML_PROTOCOL is deprecated" in str(records[1].message)
assert "SHERLOCKML_CLIENT_ID is deprecated" in str(records[2].message)
assert "SHERLOCKML_CLIENT_SECRET is deprecated" in str(records[3].message)
def test_get_session_defaults(mocker, isolated_session_cache):
mocker.patch("faculty.config.resolve_profile", return_value=PROFILE)
access_token_cache = mocker.Mock()
mocker.patch(
"faculty.session.AccessTokenMemoryCache",
return_value=access_token_cache,
)
mocker.spy(Session, "__init__")
session = get_session()
faculty.config.resolve_profile.assert_called_once_with(
credentials_path=None,
profile_name=None,
domain=None,
protocol=None,
client_id=None,
client_secret=None,
)
Session.__init__.assert_called_once_with(
session, PROFILE, access_token_cache
)
def test_resolve_profile_defaults(mocker):
mocker.patch("faculty.config.resolve_credentials_path")
mocker.patch(
"faculty.config.load_profile", return_value=CREDENTIALS_ONLY_PROFILE
)
profile = config.resolve_profile()
assert profile == config.Profile(
domain=config.DEFAULT_DOMAIN,
protocol=config.DEFAULT_PROTOCOL,
client_id="test-client-id",
client_secret="test-client-secret",
)
def test_resolve_profile_profile_name_env_faculty_precendence(mocker):
mocker.patch("faculty.config.resolve_credentials_path")
mocker.patch("faculty.config.load_profile", return_value=OTHER_PROFILE)
mocker.patch.dict(
os.environ,
{"FACULTY_PROFILE": "other", "SHERLOCKML_PROFILE": "ignored"},
)
config.resolve_profile()
config.load_profile.assert_called_once_with(
config.resolve_credentials_path.return_value, "other"
)
def test_resolve_profile_profile_name_env(mocker):
mocker.patch("faculty.config.resolve_credentials_path")
mocker.patch("faculty.config.load_profile", return_value=OTHER_PROFILE)
mocker.patch.dict(os.environ, {"FACULTY_PROFILE": "other"})
assert config.resolve_profile() == OTHER_PROFILE
config.load_profile.assert_called_once_with(
config.resolve_credentials_path.return_value, "other"
)
def test_resolve_profile_overrides(mocker):
mocker.patch("faculty.config.resolve_credentials_path")
mocker.patch("faculty.config.load_profile", return_value=DEFAULT_PROFILE)
profile = config.resolve_profile(
domain="other.domain.com",
protocol="other-protocol",
client_id="other-client-id",
client_secret="other-client-secret",
)
assert profile == OTHER_PROFILE
client_id=None,
client_secret=None,
):
key = (
access_token_cache,
credentials_path,
profile_name,
domain,
protocol,
client_id,
client_secret,
)
try:
session = _SESSION_CACHE[key]
except KeyError:
profile = faculty.config.resolve_profile(
credentials_path=credentials_path,
profile_name=profile_name,
domain=domain,
protocol=protocol,
client_id=client_id,
client_secret=client_secret,
)
access_token_cache = access_token_cache or AccessTokenMemoryCache()
session = Session(profile, access_token_cache)
_SESSION_CACHE[key] = session
return session