-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
134 lines (112 loc) · 4.69 KB
/
tests.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
import os
import unittest
import posixpath
from glob import glob
from urlparse import urlsplit
from StringIO import StringIO
import requests
from bs4 import BeautifulSoup
from PIL import Image
class MosaicoServerTestCase(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(MosaicoServerTestCase, self).__init__(*args, **kwargs)
self.base_url = os.environ.get("MOSAICO_URL", "http://127.0.0.1:9006")
self.photo_path = os.path.join(os.path.dirname(__file__), 'test.png')
class TestImage(MosaicoServerTestCase):
def setUp(self):
self.url = posixpath.join(self.base_url, 'img/')
def do_upload(self):
files = {'file': open(self.photo_path, 'rb')}
upload_url = posixpath.join(self.base_url, 'upload/')
response = requests.post(upload_url, files=files)
self.assertEquals(response.status_code, 200)
upload, = response.json()['files']
return upload
def test_placeholder(self):
params = {
'method': 'placeholder',
'params': '166, 130',
}
response = requests.get(self.url, params)
self.assertEquals(response.status_code, 200)
self.assertTrue(response.headers['Content-Type'].startswith('image/'))
image = Image.open(StringIO(response.content))
self.assertEqual(image.size, (166, 130))
def test_cover(self):
upload = self.do_upload()
photo = Image.open(open(self.photo_path, 'rb'))
new_size = tuple([d/2 for d in photo.size])
params = {
'method': 'cover',
'src': upload['url'],
'params': ','.join([str(d) for d in new_size]),
}
response = requests.get(self.url, params)
self.assertEquals(response.status_code, 200)
self.assertTrue(response.headers['Content-Type'].startswith('image/'))
image = Image.open(StringIO(response.content))
self.assertEqual(image.size, new_size)
def test_resize(self):
upload = self.do_upload()
params = {
'method': 'resize',
'src': upload['url'],
'params': '166,null',
}
response = requests.get(self.url, params)
self.assertEquals(response.status_code, 200)
self.assertTrue(response.headers['Content-Type'].startswith('image/'))
image = Image.open(StringIO(response.content))
self.assertEqual(image.size[0], 166)
class TestUpload(MosaicoServerTestCase):
def setUp(self):
self.url = posixpath.join(self.base_url, 'upload/')
def assertValidURL(self, url):
parts = urlsplit(url)
if not any([parts.netloc, parts.path]):
raise AssertionError("%s is not a URL" % url)
def test_upload(self):
photo_file = open(self.photo_path, 'rb')
files = {'file': photo_file}
response = requests.post(self.url, files=files)
photo_filename = os.path.basename(photo_file.name)
self.assertEquals(response.status_code, 200)
data = response.json()
file_data = data['files'][0]
self.assertEquals(file_data['deleteType'], 'DELETE')
self.assertValidURL(file_data['deleteUrl'])
self.assertNotEquals(file_data['name'], '')
self.assertEquals(file_data['originalName'], photo_filename)
self.assertEquals(file_data['size'], os.path.getsize(self.photo_path))
self.assertValidURL(file_data['thumbnailUrl'])
self.assertEquals(file_data['type'], None)
self.assertValidURL(file_data['url'])
class TestDownload(MosaicoServerTestCase):
def setUp(self):
self.url = posixpath.join(self.base_url, 'dl/')
def assertValidHTML(self, html):
try:
soup = BeautifulSoup(html, "html.parser")
except:
raise AssertionError("Invalid HTML: %s" % html)
def test_email(self):
data = {
'action': 'email',
'rcpt': 'example@example.org',
'subject': 'test subject',
'html': "<html><head></head><body><p></p></body></html>",
}
response = requests.post(self.url, data=data)
self.assertEquals(response.status_code, 200)
self.assertRegexpMatches(response.text, r"^OK: 250 OK id=.*$")
def test_download(self):
data = {
'action': 'download',
'filename': 'email.html',
'html': "<html><head></head><body><p></p></body></html>",
}
response = requests.post(self.url, data=data)
self.assertEquals(response.status_code, 200)
self.assertEquals(response.headers['Content-disposition'], "attachment; filename=email.html")
self.assertEquals(response.headers['Content-type'], 'text/html')
self.assertValidHTML(response.text)