def test_analytic_events_disabled(): settings = application_settings() assert settings.collect_analytics_events assert not settings.transaction_events.enabled assert settings.browser_monitoring.enabled assert settings.browser_monitoring.attributes.enabled assert settings.js_agent_loader response = fully_featured_application.get('/') header = response.html.html.head.script.string content = response.html.html.body.p.string footer = response.html.html.body.script.string # Validate actual body content. assert content == 'RESPONSE' # We no longer are in control of the JS contents of the header so # just check to make sure it contains at least the magic string # 'NREUM'. assert header.find('NREUM') != -1 # Now validate that attributes are present, since should # be enabled. data = json.loads(footer.split('NREUM.info=')[1]) assert 'atts' in data
def test_no_attributes_recorded(): settings = application_settings() assert settings.browser_monitoring.enabled assert settings.browser_monitoring.attributes.enabled assert settings.js_agent_loader response = fully_featured_application.get( '/', extra_environ={'record_attributes': 'FALSE'}) header = response.html.html.head.script.string content = response.html.html.body.p.string footer = response.html.html.body.script.string # Validate actual body content. assert content == 'RESPONSE' # We no longer are in control of the JS contents of the header so # just check to make sure it contains at least the magic string # 'NREUM'. assert header.find('NREUM') != -1 # Now validate the various fields of the footer related to analytics. # The fields are held by a JSON dictionary. data = json.loads(footer.split('NREUM.info=')[1]) # As we are not recording any user or agent attributes, we should not # actually have an entry at all in the footer. assert 'atts' not in data
def test_notice_error_strip_message_enabled(): settings = application_settings() assert settings.strip_exception_messages.enabled try: raise RuntimeError("message not displayed") except RuntimeError: notice_error()
def test_notice_error_strip_message_disabled(): settings = application_settings() assert not settings.strip_exception_messages.enabled try: raise RuntimeError('one') except RuntimeError: notice_error()
def test_record_exception_strip_message_enabled(): settings = application_settings() assert settings.strip_exception_messages.enabled try: raise RuntimeError('message not displayed') except RuntimeError: record_exception()
def test_transaction_error_cross_agent(): test_environ = { "err_message": ERR_MESSAGE, } settings = application_settings() transaction_data = [7, 1, 77, "/path-hash"] headers = make_cross_agent_headers(transaction_data, settings.encoding_key, settings.cross_process_id) response = fully_featured_application.get("/", headers=headers, extra_environ=test_environ)
def test_notice_error_strip_message_in_whitelist(): settings = application_settings() assert settings.strip_exception_messages.enabled assert _runtime_error_name in settings.strip_exception_messages.whitelist try: raise RuntimeError('original error message') except RuntimeError: notice_error()
def test_record_exception_strip_message_not_in_whitelist(): settings = application_settings() assert settings.strip_exception_messages.enabled assert _runtime_error_name not in settings.strip_exception_messages.whitelist try: raise RuntimeError('message not displayed') except RuntimeError: record_exception()
def test_notice_error_strip_message_not_in_allowlist(): settings = application_settings() assert settings.strip_exception_messages.enabled assert _runtime_error_name not in settings.strip_exception_messages.allowlist try: raise RuntimeError("message not displayed") except RuntimeError: notice_error()
def test_ssl_for_http_is_false(): settings = application_settings() assert settings.browser_monitoring.ssl_for_http is False response = target_application_manual_rum.get('/') footer = response.html.html.body.script.string data = json.loads(footer.split('NREUM.info=')[1]) assert data['sslForHttp'] is False
def test_ssl_for_http_is_true(): settings = application_settings() assert settings.browser_monitoring.ssl_for_http is True response = target_application_manual_rum.get('/') html = BeautifulSoup(response.body, 'html.parser') footer = html.html.body.script.string data = json.loads(footer.split('NREUM.info=')[1]) assert data['sslForHttp'] is True
def test_capture_attributes_enabled(): settings = application_settings() assert settings.browser_monitoring.enabled assert settings.browser_monitoring.attributes.enabled assert settings.js_agent_loader response = fully_featured_application.get('/') header = response.html.html.head.script.string content = response.html.html.body.p.string footer = response.html.html.body.script.string # Validate actual body content. assert content == 'RESPONSE' # We no longer are in control of the JS contents of the header so # just check to make sure it contains at least the magic string # 'NREUM'. assert header.find('NREUM') != -1 # Now validate the various fields of the footer related to analytics. # The fields are held by a JSON dictionary. data = json.loads(footer.split('NREUM.info=')[1]) obfuscation_key = settings.license_key[:13] attributes = json.loads(deobfuscate(data['atts'], obfuscation_key)) user_attrs = attributes['u'] # When you round-trip through json encoding and json decoding, you # always end up with unicode (unicode in Python 2, str in Python 3.) # # Previously, we would drop attribute values of type 'bytes' in Python 3. # Now, we accept them and `json_encode` uses an encoding of 'latin-1', # just like it does for Python 2. This only applies to attributes in browser # monitoring browser_attributes = _user_attributes.copy() browser_attributes['bytes'] = u'bytes-value' browser_attributes['invalid-utf8'] = _user_attributes[ 'invalid-utf8'].decode('latin-1') browser_attributes['multibyte-utf8'] = _user_attributes[ 'multibyte-utf8'].decode('latin-1') for attr, value in browser_attributes.items(): assert user_attrs[attr] == value, ( "attribute %r expected %r, found %r" % (attr, value, user_attrs[attr]))
def test_footer_attributes(): settings = application_settings() assert settings.browser_monitoring.enabled assert settings.browser_key assert settings.browser_monitoring.loader_version assert settings.js_agent_loader assert isinstance(settings.js_agent_file, six.string_types) assert settings.beacon assert settings.error_beacon token = '0123456789ABCDEF' headers = {'Cookie': 'NRAGENT=tk=%s' % token} response = target_application_manual_rum.get('/', headers=headers) html = BeautifulSoup(response.body, 'html.parser') header = html.html.head.script.string content = html.html.body.p.string footer = html.html.body.script.string # Validate actual body content. assert content == 'RESPONSE' # Validate the insertion of RUM header. assert header.find('NREUM HEADER') != -1 # Now validate the various fields of the footer. The fields are # held by a JSON dictionary. data = json.loads(footer.split('NREUM.info=')[1]) assert data['licenseKey'] == settings.browser_key assert data['applicationID'] == settings.application_id assert data['agent'] == settings.js_agent_file assert data['beacon'] == settings.beacon assert data['errorBeacon'] == settings.error_beacon assert data['applicationTime'] >= 0 assert data['queueTime'] >= 0 obfuscation_key = settings.license_key[:13] assert type(data['transactionName']) == type(u'') txn_name = deobfuscate(data['transactionName'], obfuscation_key) assert txn_name == u'WebTransaction/Uri/' assert 'atts' not in data
def test_transaction_error_with_synthetics(): test_environ = { 'err_message': ERR_MESSAGE, } settings = application_settings() headers = make_synthetics_header(settings.trusted_account_ids[0], SYNTHETICS_RESOURCE_ID, SYNTHETICS_JOB_ID, SYNTHETICS_MONITOR_ID, settings.encoding_key) response = fully_featured_application.get('/', headers=headers, extra_environ=test_environ)
def test_notice_error_strip_message_enabled_outside_transaction(): settings = application_settings() assert settings.strip_exception_messages.enabled try: assert not error_is_saved(ErrorTwo) raise ErrorTwo(ErrorTwo.message) except ErrorTwo: application_instance = application() application_instance.notice_error() my_error = core_application_stats_engine_error(_error_two_name) assert my_error.message == STRIP_EXCEPTION_MESSAGE
def test_notice_error_strip_message_disabled_outside_transaction(): settings = application_settings() assert not settings.strip_exception_messages.enabled try: assert not error_is_saved(ErrorOne) raise ErrorOne(ErrorOne.message) except ErrorOne: application_instance = application() application_instance.notice_error() my_error = core_application_stats_engine_error(_error_one_name) assert my_error.message == ErrorOne.message
def test_notice_error_strip_message_in_allowlist_outside_transaction(): settings = application_settings() assert settings.strip_exception_messages.enabled assert _error_three_name in settings.strip_exception_messages.allowlist try: assert not error_is_saved(ErrorThree) raise ErrorThree(ErrorThree.message) except ErrorThree: application_instance = application() application_instance.notice_error() my_error = core_application_stats_engine_error(_error_three_name) assert my_error.message == ErrorThree.message
def test_notice_error_strip_message_not_in_allowlist_outside_transaction(): settings = application_settings() assert settings.strip_exception_messages.enabled assert _error_four_name not in settings.strip_exception_messages.allowlist try: assert not error_is_saved(ErrorFour) raise ErrorFour(ErrorFour.message) except ErrorFour: application_instance = application() application_instance.notice_error() my_error = core_application_stats_engine_error(_error_four_name) assert my_error.message == STRIP_EXCEPTION_MESSAGE
def get_metadata_string(log_message, is_txn): host = platform.uname()[1] assert host entity_guid = application_settings().entity_guid if is_txn: metadata_string = "".join(( 'NR-LINKING|', entity_guid, '|', host, '|abcdefgh12345678|abcdefgh|Python%20Agent%20Test%20%28logger_logging%29|' )) else: metadata_string = "".join( ('NR-LINKING|', entity_guid, '|', host, '|||Python%20Agent%20Test%20%28logger_logging%29|')) formatted_string = log_message + " " + metadata_string return formatted_string
def test_analytic_events_background_task(): settings = application_settings() assert settings.collect_analytics_events assert settings.transaction_events.enabled assert settings.browser_monitoring.enabled assert settings.browser_monitoring.attributes.enabled assert settings.js_agent_loader response = fully_featured_application.get( '/', extra_environ={'newrelic.set_background_task': True}) assert response.html.html.head.script is None
def test_no_database_or_external_attributes_in_analytics(): """Make no external calls or database calls in the transaction and check if the analytic event doesn't have the databaseCallCount, databaseDuration, externalCallCount and externalDuration attributes. """ settings = application_settings() assert settings.browser_monitoring.enabled response = fully_featured_application.get('/') # Validation of analytic data happens in the decorator. content = response.html.html.body.p.string # Validate actual body content. assert content == 'RESPONSE'
def test_external_attributes_in_analytics(): """Make external calls in the transaction and check if the analytic event has the externalCallCount and externalDuration attributes. """ settings = application_settings() assert settings.browser_monitoring.enabled test_environ = { 'external': '2', } response = fully_featured_application.get('/ext', extra_environ=test_environ) # Validation of analytic data happens in the decorator. content = response.html.html.body.p.string # Validate actual body content. assert content == 'RESPONSE'