2016-11-10 19:30:09 +01:00
from zerver . lib . test_classes import WebhookTestCase
2016-09-20 22:51:11 +02:00
2020-01-14 22:06:24 +01:00
2016-09-20 22:51:11 +02:00
class SentryHookTests ( WebhookTestCase ) :
2024-05-04 22:02:50 +02:00
CHANNEL_NAME = " sentry "
2018-03-16 22:53:50 +01:00
URL_TEMPLATE = " /api/v1/external/sentry?&api_key= {api_key} &stream= {stream} "
2021-06-26 09:18:33 +02:00
WEBHOOK_DIR_NAME = " sentry "
2016-09-20 22:51:11 +02:00
2020-04-29 04:03:36 +02:00
def test_event_for_exception_golang ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = ' *url.Error: Get " bad_url " : unsupported protocol scheme " " '
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-04-29 04:03:36 +02:00
* * New exception : * * [ * url . Error : Get " bad_url " : unsupported protocol scheme " " ] ( https : / / sentry . io / organizations / hypro999 - personal - organization / issues / 1637164584 / events / 80777 a9cc30e4d0eb8904333d5c298b0 / )
` ` ` quote
* * level : * * error
* * timestamp : * * 2020 - 04 - 29 11 : 23 : 45
* * filename : * * trigger - exception . go
` ` `
Traceback :
` ` ` go
/ / Set the timeout to the maximum duration the program can afford to wait .
defer sentry . Flush ( 2 * time . Second )
resp , err := http . Get ( os . Args [ 1 ] )
if err != nil {
- - - > sentry . CaptureException ( err )
log . Printf ( " reported to Sentry: %s " , err )
return
}
defer resp . Body . Close ( )
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " event_for_exception_golang " , expected_topic_name , expected_message )
2019-04-17 23:09:05 +02:00
2020-04-29 04:03:36 +02:00
def test_event_for_exception_node ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Error: Sample error from node. "
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-04-29 04:03:36 +02:00
* * New exception : * * [ Error : Sample error from node . ] ( https : / / sentry . io / organizations / hypro999 - personal - organization / issues / 1638852747 / events / f9cb0f2afff74a5aa92e766fb7ac3fe3 / )
` ` ` quote
* * level : * * error
* * timestamp : * * 2020 - 04 - 30 06 : 19 : 33
* * filename : * * / home / hemanth / Desktop / sentry / trigger - exception . js
2019-04-17 23:09:05 +02:00
` ` `
2020-04-29 04:03:36 +02:00
Traceback :
` ` ` javascript
dsn : ' https://redacted.ingest.sentry.io/5216640 ' ,
} ) ;
2020-08-17 22:40:14 +02:00
Sentry . withScope ( function ( scope ) {
2020-04-29 04:03:36 +02:00
scope . addEventProcessor ( function ( event , hint ) {
return event ;
} ) ;
2020-08-17 22:40:14 +02:00
- - - > Sentry . captureException ( new Error ( ' Sample error from node. ' ) ) ;
2020-04-29 04:03:36 +02:00
} ) ;
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " event_for_exception_node " , expected_topic_name , expected_message )
2020-04-29 04:03:36 +02:00
def test_event_for_exception_python ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Exception: Custom exception! "
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-04-29 04:03:36 +02:00
* * New exception : * * [ Exception : Custom exception ! ] ( https : / / sentry . io / organizations / hypro999 - personal - organization / issues / 1635244907 / events / 599349254 a1447a99774b5310711c1a8 / )
` ` ` quote
* * level : * * error
* * timestamp : * * 2020 - 04 - 28 13 : 56 : 05
* * filename : * * trigger - exception . py
` ` `
Traceback :
` ` ` python3
if __name__ == " __main__ " :
sentry_sdk . init ( dsn = SECRET_DSN )
try :
- - - > raise Exception ( " Custom exception! " )
except Exception as e :
sentry_sdk . capture_exception ( e )
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " event_for_exception_python " , expected_topic_name , expected_message )
2020-04-29 04:03:36 +02:00
2023-05-29 12:47:05 +02:00
def test_event_for_exception_rails ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " ZeroDivisionError: divided by 0 "
2023-05-29 12:47:05 +02:00
expected_message = """ \
* * New exception : * * [ ZeroDivisionError : divided by 0 ] ( https : / / sentry . io / organizations / nitk - 46 / issues / 4213933362 / events / 49 b528e13e45497ab9adc3173fd2ed34 / )
` ` ` quote
* * level : * * error
* * timestamp : * * 2023 - 05 - 29 10 : 12 : 33
* * filename : * * app / controllers / articles_controller . rb
` ` `
Traceback :
` ` ` ruby
class ArticlesController < ApplicationController
def index
begin
- - - > 132312 / 0
rescue ZeroDivisionError = > exception
Sentry . capture_exception ( exception )
end
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " event_for_exception_rails " , expected_topic_name , expected_message )
2023-05-29 12:47:05 +02:00
2023-05-30 06:41:20 +02:00
def test_event_for_exception_vue ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " TypeError: Cannot read properties of null (reading ' inser... "
2023-05-30 06:41:20 +02:00
expected_message = """ \
* * New exception : * * [ TypeError : Cannot read properties of null ( reading ' insertBefore ' ) ] ( https : / / sentry . io / organizations / nitk - 46 / issues / 4214010673 / events / 292 f78454e774e62999506f759ad791d / )
` ` ` quote
* * level : * * error
* * timestamp : * * 2023 - 05 - 29 11 : 08 : 30
* * filename : * * / node_modules / . vite / deps / chunk - G4DFXOZZ . js
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " event_for_exception_vue " , expected_topic_name , expected_message )
2023-05-30 06:41:20 +02:00
2020-10-23 08:20:24 +02:00
def test_webhook_event_for_exception_python ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " ValueError: new sentry error. "
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-12-02 06:22:44 +01:00
* * New exception : * * [ ValueError : new sentry error . ] ( https : / / sentry . io / organizations / bar - foundation / issues / 1972208801 / events / c916dccfd58e41dcabaebef0091f0736 / )
2020-10-23 08:20:24 +02:00
` ` ` quote
* * level : * * error
* * timestamp : * * 2020 - 10 - 21 23 : 25 : 11
* * filename : * * trigger - exception . py
` ` `
Traceback :
` ` ` python3
if __name__ == " __main__ " :
sentry_sdk . init ( dsn = DSN_SECRET )
try :
- - - > raise ValueError ( " new sentry error. " )
except Exception as e :
sentry_sdk . capture_exception ( e )
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook (
" webhook_event_for_exception_python " , expected_topic_name , expected_message
)
2020-10-23 08:20:24 +02:00
2020-12-02 06:16:00 +01:00
def test_webhook_event_for_exception_javascript ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = ' TypeError: can \' t access property " bar " , x.foo is undefined '
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-12-02 06:22:44 +01:00
* * New exception : * * [ TypeError : can ' t access property " bar " , x.foo is undefined](https://sentry.io/organizations/foo-bar-org/issues/1982047746/events/f3bf5fc4e354451db9e885a69b2a2b51/)
2020-12-02 06:16:00 +01:00
` ` ` quote
* * level : * * error
* * timestamp : * * 2020 - 10 - 26 16 : 39 : 54
* * filename : * * None
` ` ` """
2021-02-12 08:19:30 +01:00
self . check_webhook (
2024-01-17 15:53:30 +01:00
" webhook_event_for_exception_javascript " , expected_topic_name , expected_message
2021-02-12 08:19:30 +01:00
)
2020-12-02 06:16:00 +01:00
2020-09-22 23:01:40 +02:00
def test_event_for_exception_js ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Error: Something external broke. "
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-09-22 23:01:40 +02:00
* * New exception : * * [ Error : Something external broke . ] ( https : / / sentry . io / organizations / hypro999 - personal - organization / issues / 1731239773 / events / 355 c3b2a142046629dd410db2fdda003 / )
` ` ` quote
* * level : * * error
* * timestamp : * * 2020 - 06 - 17 14 : 42 : 54
* * filename : * * / mnt / data / Documents / Stuff % 20 for % 20 Zulip / Repos / sentry / js / external . js
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " event_for_exception_js " , expected_topic_name , expected_message )
2020-09-22 23:01:40 +02:00
2020-04-29 04:03:36 +02:00
def test_event_for_message_golang ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " A test message event from golang. "
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-04-29 04:03:36 +02:00
* * New message event : * * [ A test message event from golang . ] ( https : / / sentry . io / organizations / hypro999 - personal - organization / issues / 1638844654 / events / 01 ecb45633bc4f5ca940ada671124c8f / )
` ` ` quote
* * level : * * info
* * timestamp : * * 2020 - 04 - 30 06 : 14 : 13
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " event_for_message_golang " , expected_topic_name , expected_message )
2020-04-29 04:03:36 +02:00
def test_event_for_message_node ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Test event from node. "
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-04-29 04:03:36 +02:00
* * New message event : * * [ Test event from node . ] ( https : / / sentry . io / organizations / hypro999 - personal - organization / issues / 1638840427 / events / 6886 bb1fe7ce4497b7836f6083d5fd34 / )
` ` ` quote
* * level : * * info
* * timestamp : * * 2020 - 04 - 30 06 : 09 : 56
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " event_for_message_node " , expected_topic_name , expected_message )
2020-04-29 04:03:36 +02:00
def test_event_for_message_python ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " A simple message-based issue. "
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-04-29 04:03:36 +02:00
* * New message event : * * [ A simple message - based issue . ] ( https : / / sentry . io / organizations / hypro999 - personal - organization / issues / 1635261062 / events / 8 da63b42375e4d3b803c377fefb062f8 / )
` ` ` quote
* * level : * * info
* * timestamp : * * 2020 - 04 - 28 14 : 05 : 04
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " event_for_message_python " , expected_topic_name , expected_message )
2020-04-29 04:03:36 +02:00
def test_issue_assigned_to_individual ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " A test message event from golang. "
2021-07-05 17:55:02 +02:00
expected_message = """ Issue **A test message event from golang.** has now been assigned to **Hemanth V. Alluri** by **Hemanth V. Alluri**. """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " issue_assigned_to_individual " , expected_topic_name , expected_message )
2020-04-29 04:03:36 +02:00
def test_issue_assigned_to_team ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Exception: program has entered an invalid state. "
2021-07-05 17:55:02 +02:00
expected_message = """ Issue **Exception: program has entered an invalid state.** has now been assigned to **team lone-wolf** by **Hemanth V. Alluri**. """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " issue_assigned_to_team " , expected_topic_name , expected_message )
2020-04-29 04:03:36 +02:00
def test_issue_created_for_exception ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Exception: Custom exception! "
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-04-29 04:03:36 +02:00
* * New issue created : * * Exception : Custom exception !
` ` ` quote
* * level : * * error
* * timestamp : * * 2020 - 04 - 28 13 : 56 : 05
* * assignee : * * No one
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " issue_created_for_exception " , expected_topic_name , expected_message )
2020-04-29 04:03:36 +02:00
def test_issue_created_for_message ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " A simple message-based issue. "
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-04-29 04:03:36 +02:00
* * New issue created : * * A simple message - based issue .
` ` ` quote
* * level : * * info
* * timestamp : * * 2020 - 04 - 28 14 : 05 : 04
* * assignee : * * No one
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " issue_created_for_message " , expected_topic_name , expected_message )
2020-04-29 04:03:36 +02:00
def test_issue_ignored ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Exception: program has entered an invalid state. "
2021-07-05 17:55:02 +02:00
expected_message = """ Issue **Exception: program has entered an invalid state.** was ignored by **Hemanth V. Alluri**. """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " issue_ignored " , expected_topic_name , expected_message )
2020-04-29 04:03:36 +02:00
def test_issue_resolved ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " Exception: program has entered an invalid state. "
2021-07-05 17:55:02 +02:00
expected_message = """ Issue **Exception: program has entered an invalid state.** was marked as resolved by **Hemanth V. Alluri**. """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " issue_resolved " , expected_topic_name , expected_message )
2020-05-07 19:52:30 +02:00
def test_deprecated_exception_message ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " zulip "
2021-07-05 17:55:02 +02:00
expected_message = """ \
2020-05-07 19:52:30 +02:00
New [ issue ] ( https : / / sentry . io / zulip / zulip / issues / 156699934 / ) ( level : ERROR ) :
` ` ` quote
This is an example python exception
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " deprecated_exception_message " , expected_topic_name , expected_message )
2023-05-31 14:53:29 +02:00
2023-07-10 16:05:23 +02:00
def test_sample_event_through_alert ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " This is an example Python exception "
2023-05-31 14:53:29 +02:00
expected_message = """ \
* * New message event : * * [ This is an example Python exception ] ( https : / / sentry . io / organizations / nitk - 46 / issues / 4218258981 / events / b6eff1a49b1f4132850b1238d968da70 / )
` ` ` quote
* * level : * * error
* * timestamp : * * 2023 - 05 - 31 11 : 06 : 16
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " sample_event_through_alert " , expected_topic_name , expected_message )
2023-07-13 10:09:57 +02:00
2023-07-10 16:06:33 +02:00
def test_sample_event_through_plugin ( self ) - > None :
2024-01-17 15:53:30 +01:00
expected_topic_name = " This is an example Python exception "
2023-07-10 16:06:33 +02:00
expected_message = """ \
* * New message event : * * [ This is an example Python exception ] ( https : / / nitk - 46. sentry . io / issues / 4218258981 / events / 4 dc4fc2858aa450eb658be9e5b8ad149 / )
` ` ` quote
* * level : * * error
* * timestamp : * * 2023 - 07 - 09 20 : 41 : 24
` ` ` """
2024-01-17 15:53:30 +01:00
self . check_webhook ( " sample_event_through_plugin " , expected_topic_name , expected_message )
2023-07-10 16:06:33 +02:00
2023-07-13 10:09:57 +02:00
def test_raven_sdk_python_event ( self ) - > None :
payload = self . get_body ( " raven_sdk_python_event " )
result = self . client_post (
self . url ,
payload ,
content_type = " application/json " ,
)
self . assert_json_success ( result )
self . assert_in_response (
" The ' Raven SDK ' event isn ' t currently supported by the Sentry webhook; ignoring " ,
result ,
)