This repository has been archived by the owner on Dec 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtest_Direct_AttachmentDetection.py
135 lines (121 loc) · 6.97 KB
/
test_Direct_AttachmentDetection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from email.mime.multipart import MIMEMultipart
import os
from tests.BaseTestClasses import Email2PDFTestCase
class AttachmentDetection(Email2PDFTestCase):
def setUp(self):
super(AttachmentDetection, self).setUp()
self.msg = MIMEMultipart()
def test_pdf_as_octet_stream(self):
self.addHeaders()
self.attachText("Some basic textual content")
filename = self.attachPDF("Some PDF content", mainContentType="application", subContentType="octet-stream")
error = self.invokeDirectly()
self.assertEqual('', error)
self.assertTrue(self.existsByTime())
self.assertTrue(os.path.exists(os.path.join(self.workingDir, filename)))
self.assertRegex(self.getPDFText(self.getTimedFilename()), "Some basic textual content")
self.assertRegex(self.getPDFText(os.path.join(self.workingDir, filename)), "Some PDF content")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_pdf_with_invalid_extension(self):
self.addHeaders()
self.attachText("Some basic textual content")
filename = self.attachPDF("Some PDF content", extension="pdf")
error = self.invokeDirectly()
self.assertEqual('', error)
self.assertTrue(self.existsByTime())
self.assertTrue(os.path.exists(os.path.join(self.workingDir, filename)))
self.assertRegex(self.getPDFText(self.getTimedFilename()), "Some basic textual content")
self.assertRegex(self.getPDFText(os.path.join(self.workingDir, filename)), "Some PDF content")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_pdf_as_octet_stream_with_invalid_extension(self):
self.addHeaders()
self.attachText("Some basic textual content")
filename = self.attachPDF("Some PDF content", extension="xyz", mainContentType="application", subContentType="octet-stream")
error = self.invokeDirectly()
self.assertEqual('', error)
self.assertTrue(self.existsByTime())
self.assertTrue(os.path.exists(os.path.join(self.workingDir, filename)))
self.assertRegex(self.getPDFText(self.getTimedFilename()), "Some basic textual content")
self.assertRegex(self.getPDFText(os.path.join(self.workingDir, filename)), "Some PDF content")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_pdf_as_octet_stream_no_body(self):
self.addHeaders()
self.attachText("Some basic textual content")
filename = self.attachPDF("Some PDF content", mainContentType="application", subContentType="octet-stream")
error = self.invokeDirectly(extraParams=['--no-body'])
self.assertEqual('', error)
self.assertFalse(self.existsByTime())
self.assertTrue(os.path.exists(os.path.join(self.workingDir, filename)))
self.assertRegex(self.getPDFText(os.path.join(self.workingDir, filename)), "Some PDF content")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_jpeg_as_octet_stream(self):
self.addHeaders()
self.attachText("Some basic textual content")
image_filename = self.attachImage(jpeg=True, content_type="application/octet-stream")
error = self.invokeDirectly()
self.assertEqual('', error)
self.assertTrue(self.existsByTime())
self.assertTrue(os.path.exists(os.path.join(self.workingDir, image_filename)))
self.assertIsJPG(os.path.join(self.workingDir, image_filename))
self.assertRegex(self.getPDFText(self.getTimedFilename()), "Some basic textual content")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_jpeg_with_invalid_extension(self):
self.addHeaders()
self.attachText("Some basic textual content")
image_filename = self.attachImage(jpeg=True, extension="blah")
error = self.invokeDirectly()
self.assertEqual('', error)
self.assertTrue(self.existsByTime())
self.assertTrue(os.path.exists(os.path.join(self.workingDir, image_filename)))
self.assertIsJPG(os.path.join(self.workingDir, image_filename))
self.assertRegex(self.getPDFText(self.getTimedFilename()), "Some basic textual content")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_jpeg_as_octet_stream_with_invalid_extension(self):
self.addHeaders()
self.attachText("Some basic textual content")
image_filename = self.attachImage(jpeg=True, content_type="application/octet-stream", extension="xyz")
error = self.invokeDirectly()
self.assertEqual('', error)
self.assertTrue(self.existsByTime())
self.assertTrue(os.path.exists(os.path.join(self.workingDir, image_filename)))
self.assertIsJPG(os.path.join(self.workingDir, image_filename))
self.assertRegex(self.getPDFText(self.getTimedFilename()), "Some basic textual content")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_word_document(self):
self.addHeaders()
self.attachText("Some basic textual content")
self.attachAttachment("application", "vnd.openxmlformats-officedocument.wordprocessingml.document",
"Word document content", "somefile.docx")
error = self.invokeDirectly()
self.assertEqual('', error)
self.assertTrue(self.existsByTime())
self.assertTrue(os.path.exists(os.path.join(self.workingDir, "somefile.docx")))
self.assertRegex(self.getPDFText(self.getTimedFilename()), "Some basic textual content")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_unidentified_file(self):
self.addHeaders()
self.attachText("Some basic textual content")
self.attachAttachment("application", "data", "some data in some format", "somefile.xyz")
error = self.invokeDirectly()
self.assertEqual('', error)
self.assertTrue(self.existsByTime())
self.assertTrue(os.path.exists(os.path.join(self.workingDir, "somefile.xyz")))
self.assertRegex(self.getPDFText(self.getTimedFilename()), "Some basic textual content")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_attachment_filename_has_encoding(self):
path = os.path.join(self.workingDir, "somefile.xyz")
self.attachAttachment("application", "data", "some data in some format", "somefile.xyz", file_name_encoding="utf-8")
(rc, output, error) = self.invokeAsSubprocess(extraParams=['--no-body'])
self.assertTrue(os.path.exists(path))
self.assertEqual('', error)
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())