Coverage for tests/test_npmproxy.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 18:52 -0500

1"""Unit tests for Npm Proxy""" 

2 

3import os 

4import sys 

5import unittest 

6import tempfile 

7from unittest.mock import patch 

8 

9from urllib3._collections import HTTPHeaderDict 

10 

11from mocks import MockedLogger, MockedPoolManager 

12 

13sys.path.insert(0, os.path.abspath( 

14 os.path.join(os.path.dirname(__file__), "../creepo"))) 

15 

16from npmproxy import NpmProxy # pylint: disable=wrong-import-position 

17 

18 

19class TestNpmProxy(unittest.TestCase): 

20 """Unit tests for npmproxy""" 

21 

22 def test_npm(self): 

23 """Test npm proxy""" 

24 

25 def start_ok_response(status, headers): # pylint: disable=unused-argument 

26 self.assertEqual(status, '200 OK') 

27 content = b'{"versions": []}' 

28 request = { 

29 'REQUEST_URI': '/m2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom', 

30 'log': [] 

31 } 

32 

33 config = { 

34 'log': [], 

35 } 

36 

37 config['logger'] = MockedLogger(request['log']) 

38 

39 with patch('urllib3.PoolManager') as mock_poolmanager: 

40 

41 with tempfile.TemporaryDirectory() as tmpdirname: 

42 with unittest.mock.patch.dict('os.environ', {'HOME': tmpdirname}): 

43 

44 mock_poolmanager.return_value = MockedPoolManager( 

45 status_code=200, response_headers=HTTPHeaderDict(), content=content) 

46 

47 proxy = NpmProxy(config) 

48 

49 result = list(proxy.proxy(request, start_ok_response)) 

50 print( 

51 f" Actual type: {type(result)}: value: {result}") 

52 print( 

53 f"Leave this in place to capture MockedLogger output\n{request}") 

54 

55 self.assertEqual( 

56 content, result[0], "Expected the mocked response")