Coverage for tests/test_httpproxy.py: 100%

47 statements  

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

1"""Unit tests for Proxy while mocking cherrypy (and others)""" 

2import unittest 

3import tempfile 

4from unittest.mock import patch 

5 

6from diskcache import Cache 

7 

8from mocks import MockedLogger, MockedPoolManager 

9from creepo.httpproxy import HttpProxy 

10 

11 

12class TestHttpProxyCache(unittest.TestCase): 

13 """Unit tests for httpproxy module using cherrypy and mocks""" 

14 

15 def test_no_cache_false(self): 

16 """Test for persist - just testing the cache here""" 

17 

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

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

20 

21 request = { 

22 'path': '/proxy/file.name', 

23 'storage': 'test', 

24 'content_type': 'text/html', 

25 'headers': {}, 

26 'method': 'GET', 

27 'log': [] 

28 } 

29 

30 content = b"It werx!" 

31 request['logger'] = MockedLogger(request['log']) 

32 

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

34 

35 with tempfile.TemporaryDirectory() as tmpdirname: 

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

37 

38 mock_poolmanager.return_value = MockedPoolManager( 

39 status_code=200, response_headers={}, content=content) 

40 

41 proxy = HttpProxy( 

42 { 

43 'no_cache': 'False', 'logger': request['logger'], 

44 'test': 

45 {'registry': 'https://some.random.url/path:10111'} 

46 }, 'test') 

47 

48 result = proxy.rest_proxy( 

49 request, start_ok_response) 

50 # 

51 # Note: Leave both of the next two 'print' statements in place to facilitate debugging 

52 # ATM I speculate that request['response'] does not get populated until the "result" 

53 # generator is listed, e.g. list(result), below 

54 print( 

55 f" Actual type: {type(result)}: value: {list(result)}") 

56 print( 

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

58 

59 self.assertNotEqual(request.get( 

60 'response'), None, "Expected a response in the original request") 

61 

62 self.assertEqual( 

63 request['response'], content, "Expected the mocked response") 

64 

65 with Cache(proxy.base) as cache: 

66 testdata = cache.get(request['output_filename']) 

67 self.assertNotEqual( 

68 testdata, None, 'Expect to cache the response but it was not found') 

69 self.assertEqual( 

70 testdata, content, "Expected the actual response to have been cached but found something else") 

71 

72 # Subsequent requests work slightly differently 

73 result = proxy.rest_proxy(request, start_ok_response) 

74 

75 def test_no_cache_true(self): 

76 """Test persistence""" 

77 

78 def start_response(status, headers): # pylint: disable=unused-argument 

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

80 

81 request = { 

82 'path': '/proxy/file.name', 

83 'storage': 'test', 

84 'content_type': 'text/html', 

85 'headers': {}, 

86 'method': 'GET', 

87 'log': [] 

88 } 

89 

90 content = b"It werx!" 

91 

92 request['logger'] = MockedLogger(request['log']) 

93 

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

95 

96 with tempfile.TemporaryDirectory() as tmpdirname: 

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

98 

99 mock_poolmanager.return_value = MockedPoolManager( 

100 status_code=200, response_headers={}, content=content) 

101 

102 proxy = HttpProxy( 

103 { 

104 'logger': request['logger'], 

105 'test': 

106 {'registry': 'https://some.random.url/path:10111'} 

107 }, 'test') 

108 

109 result = proxy.rest_proxy(request, start_response) 

110 # 

111 # Note: Leave both of the next two 'print' statements in place to facilitate debugging 

112 # ATM I speculate that request['response'] does not get populated until the "result" 

113 # generator is listed, e.g. list(result), below 

114 print( 

115 f" Actual type: {type(result)}: value: {list(result)}") 

116 print( 

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

118 

119 self.assertNotEqual(request.get( 

120 'response'), None, "Expected the response in the original request") 

121 

122 self.assertEqual( 

123 request.get('response'), content, "Expected the mocked response") 

124 

125 with Cache(proxy.base) as cache: 

126 testdata = cache.get(request['output_filename']) 

127 self.assertEqual( 

128 testdata, None, 'Expect NOT to cache the response but it was found instead')