Coverage for tests/test_authentication.py: 39%
28 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-13 09:56 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-13 09:56 +0000
1import unittest
3from lsst.daf.butler.tests.utils import mock_env
5try:
6 from lsst.daf.butler.remote_butler import RemoteButler
7 from lsst.daf.butler.remote_butler._authentication import (
8 _EXPLICIT_BUTLER_ACCESS_TOKEN_ENVIRONMENT_KEY,
9 _RSP_JUPYTER_ACCESS_TOKEN_ENVIRONMENT_KEY,
10 get_authentication_headers,
11 get_authentication_token_from_environment,
12 )
13except ImportError:
14 RemoteButler = None
17@unittest.skipIf(
18 RemoteButler is None, "RemoteButler could not be imported, optional dependencies may not be installed"
19)
20class TestButlerClientAuthentication(unittest.TestCase):
21 """Test access-token logic"""
23 def test_explicit_butler_token(self):
24 with mock_env(
25 {
26 _EXPLICIT_BUTLER_ACCESS_TOKEN_ENVIRONMENT_KEY: "token1",
27 _RSP_JUPYTER_ACCESS_TOKEN_ENVIRONMENT_KEY: "not-this-token",
28 }
29 ):
30 token = get_authentication_token_from_environment("https://untrustedserver.com")
31 self.assertEqual(token, "token1")
33 def test_jupyter_token_with_safe_server(self):
34 with mock_env({_RSP_JUPYTER_ACCESS_TOKEN_ENVIRONMENT_KEY: "token2"}):
35 token = get_authentication_token_from_environment("https://data.LSST.cloud/butler")
36 self.assertEqual(token, "token2")
38 def test_jupyter_token_with_unsafe_server(self):
39 with mock_env({_RSP_JUPYTER_ACCESS_TOKEN_ENVIRONMENT_KEY: "token2"}):
40 token = get_authentication_token_from_environment("https://untrustedserver.com/butler")
41 self.assertIsNone(token)
43 def test_missing_token(self):
44 with mock_env({}):
45 token = get_authentication_token_from_environment("https://data.lsst.cloud/butler")
46 self.assertIsNone(token)
48 def test_header_generation(self):
49 headers = get_authentication_headers("tokendata")
50 self.assertEqual(headers, {"Authorization": "Bearer tokendata"})