Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2VerizonAPI test module 

3""" 

4 

5import json 

6import logging 

7from datetime import datetime 

8from unittest import TestCase, mock 

9import pytest 

10from sdc_etl_libs.api_helpers.apis.Verizon.VerizonAPI import Verizon 

11from sdc_etl_libs.sdc_data_schema.schema_toolbox import SchemaToolbox 

12from sdc_etl_libs.api_helpers.SDCAPIExceptions import (AccessException, InputArgumentException) 

13from sdc_etl_libs.sdc_file_helpers.SDCFileHelpers import SDCFileHelpers 

14 

15 

16# pylint: disable=R0201 

17class TestVerizonAPI(TestCase): 

18 """ 

19 Test suite for Module VerizonAPI 

20 """ 

21 

22 @classmethod 

23 def setup_class(cls): 

24 """ 

25 Code block executed once on instance creation time 

26 """ 

27 logging.info("setup class: %s execution", cls.__name__) 

28 

29 @classmethod 

30 def teardown_class(cls): 

31 """ 

32 Code block executed once on instance destruction time 

33 """ 

34 logging.info("teardown class: %s execution", cls.__name__) 

35 

36 # pylint: disable=R0201 

37 def setup_method(self, method): 

38 """ 

39 Code block executed before every test execution 

40 """ 

41 logging.info("setup execution of test case: %s", method.__name__) 

42 

43 # pylint: disable=R0201 

44 def teardown_method(self, method): 

45 """ 

46 Code block executed after every test execution 

47 """ 

48 logging.info("teardown execution of test case: %s", method.__name__) 

49 

50 m_dict = { 

51 'customerReportId': 

52 'd35c9401-2dd7-4eb9-8d30-f40203d87bb9', 

53 'status': 

54 'Success', 

55 'url': 

56 'https://hostname/to/data.csv', 

57 'reportFormat': 

58 'CSV', 

59 'requestPayload': 

60 '{"startDate":"2020-01-01T00:00:00-05:00","endDate":"2020-01-01T11:59:59-05:00","dateTypeId": 11,'\ 

61 '"reportOption": {"timezone": "America/New_York","metricTypeIds": [44],"currency": 4,'\ 

62 '"dimensionTypeIds": [5, 6, 7, 8]},"intervalTypeId": 5}', 

63 'jobStartDate': 

64 '2020-05-21T23:34:23.000-07:00', 

65 'jobEndDate': 

66 '2020-05-21T23:34:23.000-07:00', 

67 'numRows': 

68 246 

69 } 

70 

71 m_text_dict = json.dumps(m_dict) 

72 

73 get_credentials_mock_obj = mock.MagicMock( 

74 return_value={ 

75 "encoded_client_credentials": "MOCKED_ENCODED_CLIENT_CREDENTIALS", 

76 "refresh_token": "MOCKED_REFRESH_TOKEN", 

77 "token_url": "https://url/to/get_token", 

78 "base_url": "http://url/to/extreport/" 

79 }) 

80 

81 input_values = { 

82 "report_start_date": "2020-01-01T00:00:00-05:00", 

83 "report_end_date": "2020-01-01T11:59:59-05:00", 

84 "report_date_type_id": 11, 

85 "report_timezone": "America/New_York", 

86 "report_metrics": [44], 

87 "report_currency": 4, 

88 "report_dimensions": [5, 6, 7, 8], 

89 "report_interval_type_id": 5 

90 } 

91 

92 @mock.patch("sdc_etl_libs.api_helpers.API.API.get_credentials", get_credentials_mock_obj) 

93 def test_verizon_init(self): 

94 """ 

95 Validate object instantiation 

96 """ 

97 schema_name = "Verizon/spending" 

98 data_schema = json.loads(open(SDCFileHelpers.get_file_path('schema', f"{schema_name}.json")).read()) 

99 endpoint_schema = SchemaToolbox.get_endpoint_data_from_schema(data_schema, "main_source", True) 

100 verizon_api = Verizon( 

101 data_schema_= data_schema, 

102 endpoint_schema_= endpoint_schema 

103 ) 

104 assert verizon_api.credentials is not None 

105 

106 @mock.patch("sdc_etl_libs.api_helpers.API.API.get_credentials", get_credentials_mock_obj) 

107 @mock.patch("requests.post") 

108 def test_get_access_token_method(self, mock_post): 

109 """ 

110 Validate access token retrieving 

111 """ 

112 schema_name = "Verizon/spending" 

113 data_schema = json.loads(open(SDCFileHelpers.get_file_path('schema', f"{schema_name}.json")).read()) 

114 endpoint_schema = SchemaToolbox.get_endpoint_data_from_schema(data_schema, "main_source", True) 

115 

116 mock_post.return_value.status_code = 200 

117 mock_post.return_value.text = '{"access_token":"mocked_access_token"}' 

118 

119 verizon_api = Verizon( 

120 data_schema_=data_schema, 

121 endpoint_schema_=endpoint_schema 

122 ) 

123 access_token_ = verizon_api.get_new_access_token() 

124 assert access_token_ == "mocked_access_token" 

125 

126 @mock.patch("sdc_etl_libs.api_helpers.API.API.get_credentials", get_credentials_mock_obj) 

127 @mock.patch("requests.post") 

128 def test_get_access_token_method2(self, mock_post): 

129 """ 

130 Validate access token after retry 

131 """ 

132 schema_name = "Verizon/spending" 

133 data_schema = json.loads(open(SDCFileHelpers.get_file_path('schema', f"{schema_name}.json")).read()) 

134 endpoint_schema = SchemaToolbox.get_endpoint_data_from_schema(data_schema, "main_source", True) 

135 

136 mock_post.return_value.status_code = 200 

137 mock_post.return_value.text = '{"access_token":"mocked_access_token"}' 

138 

139 verizon_api = Verizon( 

140 data_schema_=data_schema, 

141 endpoint_schema_=endpoint_schema 

142 ) 

143 access_token_ = verizon_api.get_new_access_token() 

144 assert access_token_ == "mocked_access_token" 

145 

146 @mock.patch("sdc_etl_libs.api_helpers.API.API.get_credentials", get_credentials_mock_obj) 

147 @mock.patch("requests.post") 

148 def test_get_new_access_token_http2XX(self, mock_post): 

149 """ 

150 Validate access token refresh 

151 """ 

152 schema_name = "Verizon/spending" 

153 data_schema = json.loads(open(SDCFileHelpers.get_file_path('schema', f"{schema_name}.json")).read()) 

154 endpoint_schema = SchemaToolbox.get_endpoint_data_from_schema(data_schema, "main_source", True) 

155 

156 mock_post.return_value.status_code = 204 

157 mock_post.return_value.text = '{"access_token":"mocked_access_token"}' 

158 

159 verizon_api = Verizon( 

160 data_schema_=data_schema, 

161 endpoint_schema_=endpoint_schema 

162 ) 

163 access_token_ = verizon_api.get_new_access_token() 

164 assert access_token_ == "mocked_access_token" 

165 

166 @mock.patch("sdc_etl_libs.api_helpers.API.API.get_credentials", get_credentials_mock_obj) 

167 @mock.patch("requests.post") 

168 def test_get_new_access_token_http4XX(self, mock_post): 

169 """ 

170 Validate access token refresh failure 

171 """ 

172 schema_name = "Verizon/spending" 

173 data_schema = json.loads(open(SDCFileHelpers.get_file_path('schema', f"{schema_name}.json")).read()) 

174 endpoint_schema = SchemaToolbox.get_endpoint_data_from_schema(data_schema, "main_source", True) 

175 

176 mock_post.return_value.status_code = 404 

177 mock_post.return_value.text = '{"debug_message":"Response Not OK."}' 

178 mock_post.return_value.ok = False 

179 

180 verizon_api = Verizon( 

181 data_schema_=data_schema, 

182 endpoint_schema_=endpoint_schema 

183 ) 

184 with pytest.raises(AccessException) as exc_info: 

185 access_token_ = verizon_api.get_new_access_token() 

186 logging.debug("New access token: %s", access_token_) 

187 # Validate accessException message + debug_message 

188 assert "Access token reset failed:Response Not OK." in str(exc_info.value) 

189 

190 @mock.patch("sdc_etl_libs.api_helpers.API.API.get_credentials", get_credentials_mock_obj) 

191 def test_get_response_data_wrong_endpoint(self): 

192 """ 

193 Validate trying to access not supported endpoint 

194 """ 

195 schema_name = "Verizon/spending" 

196 data_schema = json.loads(open(SDCFileHelpers.get_file_path('schema', f"{schema_name}.json")).read()) 

197 endpoint_schema = SchemaToolbox.get_endpoint_data_from_schema(data_schema, "main_source", True) 

198 

199 verizon_api = Verizon( 

200 data_schema_=data_schema, 

201 endpoint_schema_=endpoint_schema 

202 ) 

203 

204 verizon_api.endpoint_name = "endpoint_not_present" 

205 with pytest.raises(InputArgumentException) as exc_info: 

206 access_token_ = verizon_api.get_response_data() 

207 logging.debug("New access token: %s", access_token_) 

208 # Validate accessException message + debug_message 

209 assert "Verizon endpoint not supported." in str(exc_info.value) 

210 

211 @mock.patch("sdc_etl_libs.api_helpers.API.API.get_credentials", get_credentials_mock_obj) 

212 @mock.patch("requests.request") 

213 @mock.patch("requests.get") 

214 @mock.patch("requests.post") 

215 def test_get_response_data(self, mock_request, mock_get, mock_post): 

216 """ 

217 Validate get response data 

218 """ 

219 schema_name = "Verizon/advertiser-spending" 

220 data_schema = json.loads(open(SDCFileHelpers.get_file_path('schema', f"{schema_name}.json")).read()) 

221 endpoint_schema = SchemaToolbox.get_endpoint_data_from_schema(data_schema, "main_source", True) 

222 

223 mock_post.return_value.status_code = 200 

224 mock_post.return_value.text = self.__class__.m_text_dict 

225 mock_request.return_value.status_code = 200 

226 mock_get.return_value.text = """ 

227Hour,Order Id,Order,Line Id,Line,Ad Id,Ad,Creative Id,Creative,Advertiser Spending 

22801/01/2020 00:00,200144,Smile Direct - Native - US,1088071,Native | Video | Neustar - Young Pioneers | Prospecting | National_eCPC,4650092,Instream_Video_Neustar_Young_Pioneers_Prospecting_National_TIAA,2123904,Instream_Video_Neustar_Young_Pioneers_Prospecting_National_TIAA,0.0 

22901/01/2020 00:00,200144,Smile Direct - Native - US,1088071,Native | Video | Neustar - Young Pioneers | Prospecting | National_eCPC,4650093,Instream_Video_Neustar_Young_Pioneers_Prospecting_National_TIANCA_Kiara,2123906,Instream_Video_Neustar_Young_Pioneers_Prospecting_National_TIANCA_Kiara,0.0 

23001/01/2020 00:00,200144,Smile Direct - Native - US,1088097,Native | Video | Neustar - HSA FSA_SDC_Facebook | Prospecting | National_eCPC,4650141,Instream_Video_HSA_FSA_Interests_Prospecting_National_TIANCA_Jarred,2123917,Instream_Video_HSA_FSA_Interests_Prospecting_National_TIANCA_Jarred,0.0 

231""" 

232 verizon_api = Verizon( 

233 data_schema_=data_schema, 

234 endpoint_schema_=endpoint_schema 

235 ) 

236 verizon_api.input_dict = self.__class__.input_values 

237 data = verizon_api.get_response_data() 

238 assert isinstance(data, list), 'Argument of wrong type!' 

239 assert isinstance(data[0], dict) 

240 assert len(data) == 3 

241 

242 @mock.patch("sdc_etl_libs.api_helpers.API.API.get_credentials", get_credentials_mock_obj) 

243 @mock.patch("requests.post") 

244 @mock.patch("requests.request") 

245 # False positive, the mock is required on a downstream call 

246 # pylint: disable=W0613 

247 def test_check_data_request_status(self, mock_post, mock_request): 

248 """ 

249 Validate report request status 

250 """ 

251 schema_name = "Verizon/spending" 

252 data_schema = json.loads(open(SDCFileHelpers.get_file_path('schema', f"{schema_name}.json")).read()) 

253 endpoint_schema = SchemaToolbox.get_endpoint_data_from_schema(data_schema, "main_source", True) 

254 

255 mock_post.return_value.status_code = 200 

256 mock_post.return_value.text = self.__class__.m_text_dict 

257 

258 verizon_api = Verizon( 

259 data_schema_=data_schema, 

260 endpoint_schema_=endpoint_schema 

261 ) 

262 verizon_api.base_url = "some-url" 

263 

264 status_object = verizon_api.check_data_request_status(request_id="some-id") 

265 str(status_object) 

266 

267 assert status_object.status == "Success"