Coverage for tests/test_authentication.py: 39%

28 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-13 10:56 +0000

1import unittest 

2 

3from lsst.daf.butler.tests.utils import mock_env 

4 

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 

15 

16 

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""" 

22 

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") 

32 

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") 

37 

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) 

42 

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) 

47 

48 def test_header_generation(self): 

49 headers = get_authentication_headers("tokendata") 

50 self.assertEqual(headers, {"Authorization": "Bearer tokendata"})