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_Errors.py
170 lines (150 loc) · 7.45 KB
/
test_Direct_Errors.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from email.mime.multipart import MIMEMultipart
import os
import tempfile
import unittest.mock
from tests import BaseTestClasses
class Direct_Errors(BaseTestClasses.Email2PDFTestCase):
def setUp(self):
super(Direct_Errors, self).setUp()
self.msg = MIMEMultipart()
def test_plaincontent_fileexist(self):
self.attachText("Hello!")
with tempfile.NamedTemporaryFile() as tmpfile:
with self.assertRaisesRegex(Exception, "file.*exist"):
self.invokeDirectly(outputFile=tmpfile.name, okToExist=True)
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_plaincontent_dirnotexist(self):
self.attachText("Hello!")
with self.assertRaisesRegex(Exception, "(?i)directory.*not.*exist"):
self.invokeDirectly(outputDirectory="/notexist/")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_image_doesnt_exist(self):
if self.isOnline:
path = os.path.join(self.examineDir, "remoteImageDoesntExist.pdf")
self.addHeaders()
self.attachHTML('<img src="' + self.NONEXIST_IMG + '">')
error = self.invokeDirectly(outputFile=path)
self.assertTrue(os.path.exists(path))
self.assertRegex(error, "(?i)could not retrieve")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
else:
self.skipTest("Not online.")
def test_image_doesnt_exist_blacklist(self):
path = os.path.join(self.examineDir, "remoteImageDoesntExistBlacklist.pdf")
self.addHeaders()
self.attachHTML('<img src="' + self.NONEXIST_IMG_BLACKLIST + '">')
error = self.invokeDirectly(outputFile=path)
self.assertTrue(os.path.exists(path))
self.assertEqual('', error)
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_image_doesnt_exist_blacklist_upper(self):
path = os.path.join(self.examineDir, "remoteImageDoesntExistBlacklistUpper.pdf")
self.addHeaders()
self.attachHTML('<img src="' + self.NONEXIST_IMG_BLACKLIST.upper() + '">')
error = self.invokeDirectly(outputFile=path)
self.assertTrue(os.path.exists(path))
self.assertEqual('', error)
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_image_doesnt_exist_with_pdf(self):
if self.isOnline:
self.addHeaders()
self.attachHTML('<img src="' + self.NONEXIST_IMG + '">')
filename = self.attachPDF("Some PDF content")
error = self.invokeDirectly()
self.assertTrue(self.existsByTime())
self.assertTrue(os.path.exists(os.path.join(self.workingDir, filename)))
self.assertRegex(error, "(?i)could not retrieve")
self.assertTrue(self.existsByTimeWarning())
self.assertRegex(self.getWarningFileContents(), "(?i)could not retrieve")
self.assertTrue(self.existsByTimeOriginal())
self.assertValidOriginalFileContents()
else:
self.skipTest("Not online.")
def test_local_image_doesnt_exist(self):
path = os.path.join(self.examineDir, "localImageDoesntExist.pdf")
self.addHeaders()
self.attachHTML('<img src="/test.png">')
error = self.invokeDirectly(outputFile=path)
self.assertTrue(os.path.exists(path))
self.assertRegex(error, "(?i)could not retrieve")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_local_image_with_query_doesnt_exist(self):
path = os.path.join(self.examineDir, "localImageWithQueryDoesntExist.pdf")
self.addHeaders()
self.attachHTML('<img src="/test.png?foo=bar">')
error = self.invokeDirectly(outputFile=path)
self.assertTrue(os.path.exists(path))
self.assertRegex(error, "(?i)could not retrieve")
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_local_script_doesnt_exist(self):
path = os.path.join(self.examineDir, "localScriptDoesntExist.pdf")
self.addHeaders()
self.attachHTML("<script src=\"/test.js\"></script>")
error = self.invokeDirectly(outputFile=path)
self.assertTrue(os.path.exists(path))
self.assertEqual('', error)
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_local_script_with_query_doesnt_exist(self):
path = os.path.join(self.examineDir, "localScriptWithQueryDoesntExist.pdf")
self.addHeaders()
self.attachHTML("<script src=\"/test.js?muh\"></script>")
error = self.invokeDirectly(outputFile=path)
self.assertTrue(os.path.exists(path))
self.assertEqual('', error)
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_local_stylesheet_doesnt_exist(self):
path = os.path.join(self.examineDir, "localStylesheetDoesntExist.pdf")
self.addHeaders()
self.attachHTML("<html><head><link href=\"/test.css\" rel=\"stylesheet\"></head></html>")
error = self.invokeDirectly(outputFile=path)
self.assertTrue(os.path.exists(path))
self.assertEqual('', error)
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_local_stylesheet_with_query_doesnt_exist(self):
path = os.path.join(self.examineDir, "localStylesheetWithQueryDoesntExist.pdf")
self.addHeaders()
self.attachHTML("<html><head><link href=\"/test.css?muh\" rel=\"stylesheet\"></head></html>")
error = self.invokeDirectly(outputFile=path)
self.assertTrue(os.path.exists(path))
self.assertEqual('', error)
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_no_explicit_parts(self):
# If we don't add any parts explicitly, email2pdf should find a
# plain-text part
error = self.invokeDirectly()
self.assertEqual('', error)
self.assertTrue(self.existsByTime())
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_fuzz(self):
with self.assertRaisesRegex(Exception, "(?i)defects parsing email"):
self.invokeDirectly(completeMessage="This is total junk")
self.assertFalse(self.existsByTime())
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_broken_html(self):
self.addHeaders()
self.attachHTML('<img<a<h href')
error = self.invokeDirectly()
self.assertEqual('', error)
self.assertTrue(self.existsByTime())
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())
def test_missing_wkhtmltopdf(self):
with unittest.mock.patch.dict(os.environ, {'PATH': ''}):
with self.assertRaisesRegex(Exception, "(?i)email2pdf requires wkhtmltopdf"):
self.invokeDirectly()
self.assertFalse(self.existsByTime())
self.assertFalse(self.existsByTimeWarning())
self.assertFalse(self.existsByTimeOriginal())