Coverage for tests\request\test_request_script.py: 100%

65 statements  

« prev     ^ index     » next       coverage.py v7.3.4, created at 2023-12-20 14:17 -0500

1import unittest 

2from unittest import mock 

3import httpx 

4 

5 

6from zapy.requests import send_request 

7from zapy.requests.models import ZapyRequest, KeyValueItem, RequestMetadata 

8from zapy.store.manager import Store 

9 

10from zapy.test import AssertTestResultMixin, assert_test_result_dict 

11 

12 

13class TestScript(unittest.IsolatedAsyncioTestCase, AssertTestResultMixin): 

14 

15 @mock.patch.object( 

16 httpx.AsyncClient, 'send', 

17 return_value = httpx.Response(200) 

18 ) 

19 async def test_print(self, mock_request): 

20 zapy_request = ZapyRequest( 

21 endpoint="http://test/", 

22 method="GET", 

23 script=[ 

24 'print("test_print", "hello")', 

25 ], 

26 ) 

27 

28 mock_print = mock.MagicMock() 

29 

30 await zapy_request.send(logger=mock_print) 

31 

32 mock_print.assert_called_with("test_print", "hello") 

33 

34 @mock.patch.object( 

35 httpx.AsyncClient, 'send', 

36 return_value = httpx.Response(200) 

37 ) 

38 async def test_pre_request_hook(self, mock_request): 

39 from decimal import Decimal 

40 zapy_request = ZapyRequest( 

41 endpoint="http://test/", 

42 method="GET", 

43 params=[ 

44 KeyValueItem(key='param1', value='{{ 0.3 }}', active=True), 

45 ], 

46 script=[ 

47 'from decimal import Decimal', 

48 

49 'ctx.store.var2 = Decimal("0.2")', 

50 

51 '@ctx.hooks.pre_request', 

52 'async def on_pre_request(request_args):', 

53 ' ctx.store.pre_req = "Test pre script"', 

54 ' ctx.store.request_args = request_args', 

55 ' request_args["params"]["X-custom"] = ["hello world"]', 

56 ' print("pre_request")', 

57 ], 

58 ) 

59 

60 store = Store() 

61 mock_print = mock.MagicMock() 

62 

63 await zapy_request.send(store=store, logger=mock_print) 

64 

65 self.assertEqual(Decimal("0.2"), store.var2) 

66 self.assertEqual('Test pre script', store.pre_req) 

67 self.assertEqual(dict, type(store.request_args)) 

68 mock_print.assert_called_with("pre_request") 

69 (req,) = mock_request.call_args.args 

70 self.assertEqual('http://test/?param1=0.3&X-custom=hello%20world', req.url) 

71 

72 

73 @mock.patch.object( 

74 httpx.AsyncClient, 'send', 

75 return_value = httpx.Response(200, json={'id': 'test-id'}) 

76 ) 

77 async def test_post_request_hook(self, mock_request): 

78 from decimal import Decimal 

79 zapy_request = ZapyRequest( 

80 endpoint="http://test/", 

81 method="GET", 

82 params=[ 

83 KeyValueItem(key='param1', value='{{ 0.3 }}', active=True), 

84 ], 

85 script=[ 

86 'from decimal import Decimal', 

87 

88 'ctx.store.var2 = Decimal("0.2")', 

89 

90 '@ctx.hooks.post_request', 

91 'async def on_post_request(response):', 

92 ' ctx.store.post_req = "Test post script"', 

93 ' print(response)', 

94 ], 

95 ) 

96 

97 store = Store() 

98 mock_print = mock.MagicMock() 

99 

100 await zapy_request.send(store=store, logger=mock_print) 

101 

102 self.assertEqual(Decimal("0.2"), store.var2) 

103 self.assertEqual('Test post script', store.post_req) 

104 (response,) = mock_print.call_args.args 

105 self.assertEqual(httpx.Response, type(response)) 

106 self.assertEqual({'id': 'test-id'}, response.json()) 

107 

108 

109 @mock.patch.object( 

110 httpx.AsyncClient, 'send', 

111 return_value = httpx.Response(200) 

112 ) 

113 async def test_request_hook_order(self, mock_request): 

114 zapy_request = ZapyRequest( 

115 endpoint="http://test/", 

116 method="GET", 

117 script=[ 

118 'import unittest', 

119 'print("script 1")', 

120 

121 '@ctx.hooks.pre_request', 

122 'async def on_pre_request(request_args):', 

123 ' print("pre_request")', 

124 

125 '@ctx.hooks.post_request', 

126 'async def on_post_request(response):', 

127 ' print("post_request")', 

128 

129 '@ctx.hooks.test', 

130 'class TestStringMethods(unittest.TestCase):', 

131 ' def test_upper(self):', 

132 ' print("test")', 

133 ' self.assertEqual("foo".upper(), "FOO")', 

134 

135 'print("script 2")', 

136 ], 

137 ) 

138 

139 mock_print = mock.MagicMock() 

140 

141 await zapy_request.send(logger=mock_print) 

142 

143 mock_print.assert_has_calls([ 

144 mock.call("script 1"), 

145 mock.call("script 2"), 

146 mock.call("pre_request"), 

147 mock.call("post_request"), 

148 mock.call("test"), 

149 ]) 

150 

151 

152 @mock.patch.object( 

153 httpx.AsyncClient, 'send', 

154 return_value = httpx.Response(200, json={'id': 'test-id'}) 

155 ) 

156 async def test_test_hook_fails(self, mock_request): 

157 zapy_request = ZapyRequest( 

158 endpoint="http://test/", 

159 method="GET", 

160 script=[ 

161 'import unittest', 

162 

163 '@ctx.hooks.test', 

164 'class TestStringMethods(unittest.TestCase):', 

165 ' def test_response(self):', 

166 ' self.assertEqual({"id": "test-id2"}, self.response.json())', 

167 ], 

168 ) 

169 

170 mock_print = mock.MagicMock() 

171 with self.assertRaises(AssertionError): 

172 await zapy_request.send(logger=mock_print) 

173 

174 

175 @mock.patch.object( 

176 httpx.AsyncClient, 'send', 

177 return_value = httpx.Response(200, json={'id': 'test-id'}) 

178 ) 

179 async def test_test_hook_passes(self, mock_request): 

180 zapy_request = ZapyRequest( 

181 endpoint="http://test/", 

182 method="GET", 

183 script=[ 

184 'import unittest', 

185 

186 '@ctx.hooks.test', 

187 'class TestStringMethods(unittest.TestCase):', 

188 ' def test_response(self):', 

189 ' self.assertEqual({"id": "test-id"}, self.response.json())', 

190 ], 

191 ) 

192 

193 mock_print = mock.MagicMock() 

194 response_wrapper = await send_request(zapy_request, logger=mock_print) 

195 

196 self.assertZapyTestResults(response_wrapper.test_result) 

197 assert_test_result_dict(response_wrapper.test_result) 

198 

199 @mock.patch.object( 

200 httpx.AsyncClient, 'send', 

201 return_value = httpx.Response(200) 

202 ) 

203 async def test_hook_with_metadata(self, mock_request): 

204 zapy_request = ZapyRequest( 

205 metadata=RequestMetadata(tags=['meta_tag_1']), 

206 endpoint="http://test/", 

207 method="GET", 

208 script=[ 

209 'from zapy.base import Metadata', 

210 

211 '@ctx.hooks.pre_request', 

212 'async def on_pre_request(request_args, metadata: Metadata):', 

213 ' ctx.store.metadata = metadata', 

214 ], 

215 ) 

216 

217 store = Store() 

218 await zapy_request.send(store=store) 

219 

220 self.assertEqual(['meta_tag_1'], store.metadata.tags) 

221 self.assertEqual(RequestMetadata, type(store.metadata))