@@ -515,6 +515,66 @@ def test_attachments_graceful_failure(
515515 assert envelope .items [1 ].payload .get_bytes () == b""
516516
517517
518+ def test_attachments_exceptions (sentry_init ):
519+ sentry_init ()
520+
521+ scope = sentry_sdk .get_isolation_scope ()
522+
523+ # bytes and path are None
524+ with pytest .raises (TypeError ) as e :
525+ scope .add_attachment ()
526+
527+ assert str (e .value ) == "path or raw bytes required for attachment"
528+
529+ # filename is None
530+ with pytest .raises (TypeError ) as e :
531+ scope .add_attachment (bytes = b"Hello World!" )
532+
533+ assert str (e .value ) == "filename is required for attachment"
534+
535+
536+ def test_attachments_content_type_is_none (sentry_init , capture_envelopes ):
537+ sentry_init ()
538+ envelopes = capture_envelopes ()
539+
540+ scope = sentry_sdk .get_isolation_scope ()
541+
542+ scope .add_attachment (
543+ bytes = b"Hello World!" , filename = "message.txt" , content_type = "foo/bar"
544+ )
545+ capture_exception (ValueError ())
546+
547+ (envelope ,) = envelopes
548+ attachments = [x for x in envelope .items if x .type == "attachment" ]
549+ (message ,) = attachments
550+
551+ assert message .headers ["filename" ] == "message.txt"
552+ assert message .headers ["content_type" ] == "foo/bar"
553+
554+
555+ def test_attachments_repr (sentry_init ):
556+ sentry_init ()
557+
558+ scope = sentry_sdk .get_isolation_scope ()
559+
560+ scope .add_attachment (bytes = b"Hello World!" , filename = "message.txt" )
561+
562+ assert repr (scope ._attachments [0 ]) == "<Attachment 'message.txt'>"
563+
564+
565+ def test_attachments_bytes_callable_payload (sentry_init ):
566+ sentry_init ()
567+
568+ scope = sentry_sdk .get_isolation_scope ()
569+
570+ scope .add_attachment (bytes = bytes , filename = "message.txt" )
571+
572+ attachment = scope ._attachments [0 ]
573+ item = attachment .to_envelope_item ()
574+
575+ assert item .payload .bytes == b""
576+
577+
518578def test_integration_scoping (sentry_init , capture_events ):
519579 logger = logging .getLogger ("test_basics" )
520580
0 commit comments