def test_should_throw_network_error_if_host_not_reachable( self, mocked_call, mock_requests_post ): with pytest.raises(exceptions.WABizNetworkError): client = WABizAPI(**self.MOCK_COMPLETE_CONFIG) client.get_support_info() assert mocked_call.call_count == 1
def test_should_throw_access_error_if_insufficient_role_permissions( self, mocked_call, mock_requests_post ): with pytest.raises(exceptions.WABizAccessError): client = WABizAPI(**self.MOCK_COMPLETE_CONFIG) client.get_webhook_url() assert mocked_call.call_count == 1
def test_should_return_none_if_no_cert(self, mocked_call, mock_requests_post): try: client = WABizAPI(**self.MOCK_COMPLETE_CONFIG) webhook_cert = client.get_webhook_cert() assert mocked_call.call_count == 1 assert webhook_cert is None except Exception as e: pytest.fail(e)
def _run(cls, config, *args, **kwargs): api = WABizAPI(**config.get("webapp")) try: webhook_url = api.get_webhook_url() if not webhook_url: return _result_webhook_not_set(cls) if not webhook_url.startswith("https"): return _result_webhook_not_https(cls) except (WABizAccessError, WABizAuthError, WABizNetworkError, ValueError) as e: return _result_get_webhook_error(cls, e) # explicitly catching a possible exception except WABizGeneralError as e: # WABizGeneralError is likely not a user error, # should be handled by app-wide catch raise e wacore_containers = docker_utils.get_running_wacore_containers() if not wacore_containers: return results.Problem( cls, "Webhook check failed", "There is no wacore container running", "Please check results from other actions to diagnose", ) container = wacore_containers[0] cert_str = api.get_webhook_cert() dest_cert = None if cert_str: with tempfile.NamedTemporaryFile() as cert_file: cert_file.write(cert_str) cert_file.seek(0) docker_utils.put_archive_to_container(container, cert_file.name, DEST_PATH) dest_cert = os.path.join(DEST_PATH, os.path.basename(cert_file.name)) result, response_time = curl_utils.https_post_request_from_container( container, webhook_url, TEST_POST_DATA_STRING, REQ_TIMEOUT, dest_cert) if result == CURLTestResult.CONNECTION_ERROR: return _result_webhook_could_not_connect(cls) elif result == CURLTestResult.SSL_CERT_UNKNOWN: return _result_webhook_no_cert_uploaded(cls) elif result == CURLTestResult.CONNECTION_TIMEOUT: return _result_webhook_did_not_respond(cls) elif result == CURLTestResult.HTTP_STATUS_NOT_OK: return _result_webhook_did_not_return_ok(cls) elif response_time > ACCEPTABLE_RESPONSE_TIME: return _result_webhook_slow_response(cls, response_time) return results.OK(cls)
def test_should_not_throw_network_error_if_host_reachable( self, mocked_call, mock_requests_post ): try: client = WABizAPI(**self.MOCK_COMPLETE_CONFIG) support_info = client.get_support_info() assert mocked_call.call_count == 1 assert "support" in support_info except Exception as e: pytest.fail(e)
def test_should_return_cert_if_no_error(self, mocked_call, mock_requests_post): expected_webhook_cert = b"valid certificate" try: client = WABizAPI(**self.MOCK_COMPLETE_CONFIG) webhook_cert = client.get_webhook_cert() assert mocked_call.call_count == 1 assert webhook_cert == expected_webhook_cert except Exception as e: pytest.fail(e)
def test_should_return_webhook_url_if_no_error( self, mocked_call, mock_requests_post ): expected_webhook_url = "fake_url" try: client = WABizAPI(**self.MOCK_COMPLETE_CONFIG) webhook_url = client.get_webhook_url() assert mocked_call.call_count == 1 assert webhook_url == expected_webhook_url except Exception as e: pytest.fail(e)
def get_support_info(): support_info_filename = os.path.join(OUTPUT_FOLDER, SUPPORT_INFO_LOG_FILE) try: config = Config().values if config: api = WABizAPI(**config.get("webapp")) support_info_content = api.get_support_info() else: return except Exception: return docker_utils.write_to_file(support_info_filename, json.dumps(support_info_content, indent=2)) return support_info_filename
def test_should_not_throw_auth_error_if_user_pass_is_valid(self, mocked_call): expected_header = { "AUTHORIZATION": "Bearer fake_token", "CONTENT_TYPE": "application/json", } try: client = WABizAPI(**self.MOCK_COMPLETE_CONFIG) assert mocked_call.call_count == 1 assert client.api_header == expected_header except Exception as e: pytest.fail(e)
def send_logs_to_fb( zipped_logs_file_handle, success_callback=None, failure_callback=None ): phone_number = None try: config = Config().values if config: api = WABizAPI(**config.get("webapp")) phone_number = api.get_phone_number() except Exception: pass try: run_id = Analytics.send_logs_to_fb(zipped_logs_file_handle, phone_number) if success_callback: return success_callback(run_id) except Exception as e: if failure_callback: return failure_callback(e)
def send_results_to_fb(result, success_callback=None, failure_callback=None): phone_number = None try: config = Config().values if config: api = WABizAPI(**config.get("webapp")) phone_number = api.get_phone_number() except Exception: pass try: event = Events.RUN_ACTIONS_AND_SEND_RESULTS data = json.dumps(result) run_id = Analytics.send_event(event, data, phone_number) result["run_id"] = run_id if success_callback: success_callback(result) except Exception as e: if failure_callback: failure_callback(e)
def test_should_throw_auth_error_if_user_pass_is_invalid(self, mocked_call): with pytest.raises(exceptions.WABizAuthError): WABizAPI(**self.MOCK_COMPLETE_CONFIG) assert mocked_call.call_count == 1
def test_should_throw_valueerror_for_wrong_config_input(self): with pytest.raises(ValueError): WABizAPI(**self.MOCK_INCOMPLETE_CONFIG)