Coverage for tests/test_gafaelfawr.py: 20%

45 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-10-09 03:22 -0700

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28from __future__ import annotations 

29 

30import unittest 

31 

32try: 

33 import fastapi 

34 import httpx 

35 

36 from lsst.daf.butler.remote_butler.server._dependencies import repository_authorization_dependency 

37 from lsst.daf.butler.remote_butler.server._gafaelfawr import GafaelfawrClient, GafaelfawrGroupAuthorizer 

38 

39 _IMPORT_FAILURE = None 

40except ImportError as e: 

41 _IMPORT_FAILURE = e 

42 

43 

44# FastAPI is not installed during LSST Pipelines stack builds, so skip these 

45# tests if it is not available. 

46@unittest.skipIf(_IMPORT_FAILURE, str(_IMPORT_FAILURE)) 

47class GafaelfawrAuthorizationTestCase(unittest.IsolatedAsyncioTestCase): 

48 """Test authorization checks using Gafaelfawr group membership.""" 

49 

50 async def test_gafaelfawr_group_auth(self) -> None: 

51 response_code = 200 

52 response_data = {"username": "some-user", "groups": [{"name": "some-group"}, {"name": "b"}]} 

53 request_headers: httpx.Headers = httpx.Headers(None) 

54 request_count = 0 

55 

56 def handler(request: httpx.Request): 

57 nonlocal request_headers 

58 request_headers = request.headers 

59 nonlocal request_count 

60 request_count += 1 

61 return httpx.Response(response_code, json=response_data) 

62 

63 transport = httpx.MockTransport(handler) 

64 

65 client = GafaelfawrClient("http://gafaelfawr.example", transport=transport) 

66 authorizer = GafaelfawrGroupAuthorizer( 

67 client, {"any_group": ["*"], "group_a": ["a"], "group_b": ["c", "b"]} 

68 ) 

69 

70 with self.assertRaises(fastapi.HTTPException) as e: 

71 await repository_authorization_dependency("group_a", "username", "mock-token", authorizer) 

72 self.assertEqual(e.exception.status_code, 403) 

73 self.assertEqual(request_headers.get("Authorization"), "Bearer mock-token") 

74 

75 # Should authorize the special '*' all users group without hitting 

76 # Gafaelfawr service. 

77 request_count = 0 

78 await repository_authorization_dependency("any_group", "username", "mock-token", authorizer) 

79 self.assertEqual(request_count, 0) 

80 

81 # Should hit the Gafaelfawr service to check that the user is in group 

82 # "b". 

83 request_count = 0 

84 await repository_authorization_dependency("group_b", "username", "mock-token", authorizer) 

85 self.assertEqual(request_count, 1) 

86 

87 # A second request with the same username should be cached... 

88 request_count = 0 

89 await repository_authorization_dependency("group_b", "username", "mock-token", authorizer) 

90 self.assertEqual(request_count, 0) 

91 

92 # But it should go back to the server for a different user 

93 request_count = 0 

94 response_data = {"username": "other-username", "groups": [{"name": "incorrect-group"}]} 

95 with self.assertRaises(fastapi.HTTPException) as e: 

96 await repository_authorization_dependency("group_b", "other-username", "mock-token", authorizer) 

97 self.assertEqual(e.exception.status_code, 403) 

98 self.assertEqual(request_count, 1) 

99 

100 # Bad repository name 

101 with self.assertRaises(ValueError): 

102 await repository_authorization_dependency( 

103 "unknown_repository", "username", "mock-token", authorizer 

104 )