Coverage for tests/test_authentication.py: 44%
34 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-01 10:59 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-01 10:59 +0000
1import os
2import unittest
3from contextlib import contextmanager
4from unittest.mock import patch
6try:
7 from lsst.daf.butler.remote_butler import RemoteButler
8 from lsst.daf.butler.remote_butler._authentication import (
9 _EXPLICIT_BUTLER_ACCESS_TOKEN_ENVIRONMENT_KEY,
10 _RSP_JUPYTER_ACCESS_TOKEN_ENVIRONMENT_KEY,
11 get_authentication_headers,
12 get_authentication_token_from_environment,
13 )
14except ImportError:
15 RemoteButler = None
18@contextmanager
19def _mock_env(new_environment):
20 with patch.dict(os.environ, new_environment, clear=True):
21 yield
24@unittest.skipIf(
25 RemoteButler is None, "RemoteButler could not be imported, optional dependencies may not be installed"
26)
27class TestButlerClientAuthentication(unittest.TestCase):
28 """Test access-token logic"""
30 def test_explicit_butler_token(self):
31 with _mock_env(
32 {
33 _EXPLICIT_BUTLER_ACCESS_TOKEN_ENVIRONMENT_KEY: "token1",
34 _RSP_JUPYTER_ACCESS_TOKEN_ENVIRONMENT_KEY: "not-this-token",
35 }
36 ):
37 token = get_authentication_token_from_environment("https://untrustedserver.com")
38 self.assertEqual(token, "token1")
40 def test_jupyter_token_with_safe_server(self):
41 with _mock_env({_RSP_JUPYTER_ACCESS_TOKEN_ENVIRONMENT_KEY: "token2"}):
42 token = get_authentication_token_from_environment("https://data.LSST.cloud/butler")
43 self.assertEqual(token, "token2")
45 def test_jupyter_token_with_unsafe_server(self):
46 with _mock_env({_RSP_JUPYTER_ACCESS_TOKEN_ENVIRONMENT_KEY: "token2"}):
47 token = get_authentication_token_from_environment("https://untrustedserver.com/butler")
48 self.assertIsNone(token)
50 def test_missing_token(self):
51 with _mock_env({}):
52 token = get_authentication_token_from_environment("https://data.lsst.cloud/butler")
53 self.assertIsNone(token)
55 def test_header_generation(self):
56 headers = get_authentication_headers("tokendata")
57 self.assertEqual(headers, {"Authorization": "Bearer tokendata"})