def testSendingAnalytics(self, mockSessionObj): session = mockSessionObj() response = MockResponse() response.appendResponse("test response") response.appendResponseCode(000) session.get.return_value = response self.assertEquals(0, session.get.call_count) self.assertEquals(0, session.get.call_count) obj = Analytics() obj.setSession(session) obj.disableAnalytics() t = obj.async_send_analytics(debug=False) t.join() self.assertEquals(0, session.get.call_count) obj.enableAnalytics() t = obj.async_send_analytics(debug=False) t.join() self.assertEquals(1, session.get.call_count) obj.enableAnalytics() t = obj.async_send_analytics(debug=True) t.join() self.assertEquals(2, session.get.call_count)
def test_DataStreamAggregateMockAPI(self, mockSessionObj): session = mockSessionObj() response = MockResponse() for mockJson in self.aggregate_logs: response.appendResponse(response.getJSONFromFile(mockJson)) session.post.return_value = response session.get.return_value = response response.status_code = 200 response.headers = {} commandTester = CommandTester(self) args = [ "datastream_agg", "--debug", "--section", "default", "--edgerc", commandTester.edgeRc, "--streamId", "0007", "--timeRange", "20m" ] stdOutResultArray = commandTester.wrapSuccessCommandStdOutOnly(func=main, args=args) self.assertEqual(5, len(stdOutResultArray) ) header = stdOutResultArray[:1] header = json.loads(header[0]) expectedHeaders = ["startTime", "1xx", "2xx", "3xx", "4xx", "5xx", "endTime"] startTime = header[0] self.assertNotIn(startTime, expectedHeaders) values = stdOutResultArray[1:] expectedStartTimes = [ "2019-03-13T02:00:00Z", "2019-03-13T02:37:00Z", "2019-03-13T03:13:00Z", "2019-03-13T04:31:00Z", "2019-03-13T04:56:00Z"] for j in values: row = json.loads(j) self.assertIn(row[0], expectedStartTimes)
def test_DataStreamRAWMockAPI(self, mockSessionObj): session = mockSessionObj() response = MockResponse() for mockJson in self.raw_logs: response.appendResponse(response.getJSONFromFile(mockJson)) session.post.return_value = response session.get.return_value = response response.status_code = 200 response.headers = {} commandTester = CommandTester(self) args = [ "datastream_raw", "--debug", "--section", "default", "--edgerc", commandTester.edgeRc, "--streamId", "0007", "--timeRange", "20m" ] stdOutResultArray = commandTester.wrapSuccessCommandStdOutOnly(func=main, args=args) self.assertEqual(2, len(stdOutResultArray) ) header = stdOutResultArray[:1] header = json.loads(header[0]) expectedHeaders = ['CPCODE', 'ResponseCode', 'Method', 'Protocol', 'Host', 'Path'] CPCODE = header[0] self.assertNotIn(CPCODE, expectedHeaders) values = stdOutResultArray[1:] for j in values: json.loads(j)
def test_CachedContextHandler(self, mockSessionObj): factory = CredentialFactory() self.context = factory.load(self.edgerc, None, "account_key_789") cache = Cache() cachedHandler = CachedContextHandler(self.context, cache) session = mockSessionObj() response = MockResponse() session.get.return_value = response response.status_code = 200 datevalue = str( datetime.datetime.now() ) response.appendResponse(json.dumps( { "date" : datevalue } )) url = "https://test" _, value = cachedHandler.get(url, requestHeaders=None, bypassCache=False) newDict = json.loads(value) newDate = newDict["date"] self.assertEquals(datevalue, newDate) response.appendResponse(json.dumps( { "date" : "date-should-not-be-used" } )) url = "https://test" _, value = cachedHandler.get(url, requestHeaders=None, bypassCache=False) newDict = json.loads(value) newDate = newDict["date"] self.assertEquals(datevalue, newDate) response.reset() response.appendResponse(json.dumps( { "date" : "date-should-be-used" } )) response.status_code = 200 url = "https://test" _, value = cachedHandler.get(url, requestHeaders=None, bypassCache=True) newDict = json.loads(value) newDate = newDict["date"] self.assertEquals("date-should-be-used", newDate)
def test_BulkSearchCommand(self, getArgFromSTDIN, mockSessionObj): accountKey = "1-abcdef" contractId = "ctr_C-0000" postdata = { "bulkSearchQuery": { "syntax": "JSONPATH", "match": "$.behaviors[?(@.name == 'cpCode')].options.value.id" } } postdata = json.dumps(postdata) getArgFromSTDIN.return_value = postdata session = mockSessionObj() response = MockResponse() commandTester = CommandTester(self) ## First Check for mockJson in self.asyncAllsearchresponses: response.appendResponse(response.getJSONFromFile(mockJson)) session.post.return_value = response session.get.return_value = response response.status_code = 202 response.headers = {"Location": "https://dummy.html"} args = [ "bulksearch", "--section", "default", "--edgerc", commandTester.edgeRc, "--use-searchstdin", "--contractId", contractId, "--account-key", accountKey, "--network", "production" ] stdOutResultArray = commandTester.wrapSuccessCommandStdOutOnly( func=main, args=args) self.assertEqual(2, len(stdOutResultArray)) header = stdOutResultArray[:1] header = json.loads(header[0]) expectedHeaders = ['propertyName', 'results'] self.assertIn(header[0], expectedHeaders) self.assertIn(header[1], expectedHeaders) values = stdOutResultArray[1:] values = list(map(lambda x: json.loads(x), values)) self.assertEquals(1, len(values)) self.assertEquals(12345, values[0][1]) ## Next Check response.reset() response.status_code = 202 for mockJson in self.asyncAllsearchresponses: response.appendResponse(response.getJSONFromFile(mockJson)) args = [ "bulksearch", "--section", "default", "--edgerc", commandTester.edgeRc, "--contractId", contractId, "--account-key", accountKey, "--network", "production" ] stdOutResultArray = commandTester.wrapSuccessCommandStdOutOnly( func=main, args=args) header = stdOutResultArray[:1] header = json.loads(header[0]) expectedHeaders = ['propertyName', 'results'] self.assertIn(header[0], expectedHeaders) self.assertIn(header[1], expectedHeaders) values = stdOutResultArray[1:] values = list(map(lambda x: json.loads(x), values)) self.assertEquals(1, len(values)) self.assertEquals(12345, values[0][1]) ## Next Check response.reset() response.status_code = 202 for mockJson in self.asyncAllsearchresponses: response.appendResponse(response.getJSONFromFile(mockJson)) args = [ "bulksearch", "--section", "default", "--edgerc", commandTester.edgeRc, "--contractId", contractId, "--account-key", accountKey, "--network", "production", "--searchname", "default.json" ] stdOutResultArray = commandTester.wrapSuccessCommandStdOutOnly( func=main, args=args) header = stdOutResultArray[:1] header = json.loads(header[0]) expectedHeaders = ['propertyName', 'results'] self.assertIn(header[0], expectedHeaders) self.assertIn(header[1], expectedHeaders) values = stdOutResultArray[1:] values = list(map(lambda x: json.loads(x), values)) self.assertEquals(1, len(values)) self.assertEquals(12345, values[0][1])
def testIntegration(self, mockSessionObj): fetch = PropertyManagerFetch() accountKey = "1-abcdef" contractId = "ctr_C-0000" postdata = { "bulkSearchQuery": { "syntax": "JSONPATH", "match": "$.behaviors[?(@.name == 'cpCode')].options.value.id" } } session = mockSessionObj() response = MockResponse() for mockJson in self.allsearchresponses: response.appendResponse(response.getJSONFromFile(mockJson)) session.post.return_value = response session.get.return_value = response response.status_code = 200 response.headers = {} edgerc = self.edgerc (_, json) = fetch.bulksearch(edgerc=edgerc, postdata=postdata, account_key=accountKey, contractId=contractId, network="Production", debug=False) self.assertEquals(1, len(json)) results = json[0]["matchLocationResults"] self.assertEquals(1, len(results)) self.assertEquals(12345, results[0]) self.assertIn("versionInfo", json[0]) self.assertIn("hostnames", json[0]) response.reset() for mapObj in self.allstagingresponsesMaps: response.appendResponse(response.getJSONFromFile(mapObj["uri"])) response.appendResponseCode(mapObj["code"]) (_, json) = fetch.bulksearch(edgerc=edgerc, postdata=postdata, account_key=accountKey, contractId=contractId, network="Staging", debug=False) self.assertEquals(1, len(json)) results = json[0]["matchLocationResults"] self.assertEquals(2, len(results)) self.assertEquals(12345, results[0]) self.assertEquals(678910, results[1]) self.assertIn("versionInfo", json[0]) self.assertIn("hostnames", json[0]) return json