-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtumblrtest.py
446 lines (360 loc) · 16.4 KB
/
tumblrtest.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/env python
"""Tumblr API client unit test cases"""
__author__ = "SNF Labs"
__TODO__ = """TODO List
- link-via detection
- photo-caption not present
- all strings are Unicode strings
"""
import os
import unittest
import tumblr
import urlparse
class WTFError(Exception): pass
def isUrl(str):
"""Attempts to determine if the given string is really an HTTP URL.
This is a quick-and-dirty test that just looks for an http protocol handler."""
u = urlparse.urlparse(str)
if u[0] == 'http' or u[0] == 'https':
return True
else:
return False
def clear_http_cache():
cache_dir = tumblr.DEFAULT_HTTP_CACHE_DIR
if cache_dir == '/' or cache_dir == os.getenv('HOME'):
raise WTFError
else:
if os.path.exists(cache_dir):
for f in os.listdir(cache_dir):
f_path = os.path.join(cache_dir, f)
if os.path.isfile(f_path):
os.unlink(f_path)
class SanityTestCase(unittest.TestCase):
"""Sanity checks against specific production tumblelogs."""
def setUp(self):
self.urls = (
"http://golden.cpl593h.net/api/read",
"http://thelongesttrainieversaw.tumblr.com/api/read",
"http://industry.tumblr.com/api/read",
"http://marco.tumblr.com/api/read",
"http://demo.tumblr.com/api/read"
)
# self.urls = ( "http://marco.tumblr.com/api/read", "http://www.marco.org/api/read" )
self.logs = []
for url in self.urls:
self.logs.append(tumblr.parse(url))
def testNumPosts(self):
"""Posts are present in the API XML."""
for log in self.logs:
assert len(log.posts) > 0, "No posts found!"
def testNoUnknownPosts(self):
"""No posts are of an unrecognized type."""
types = [ 'regular',
'link',
'quote',
'photo',
'conversation',
'video',
'audio'
]
for log in self.logs:
for post in log.posts:
assert post.type in types
def testHasUrl(self):
"""Tumblelog's URL can be determined."""
for log in self.logs:
assert log.url is not None and log.url != ''
def testHasTitle(self):
"""Tumblelog's title can be determined."""
for log in self.logs:
assert log.title is not None and log.title != ''
class FileOrStringTestCase(unittest.TestCase):
"""Tests that an open file object or a string can be parsed."""
def setUp(self):
self.filename = os.getcwd() + os.sep + 'tests' + os.sep + 'file' + os.sep + 'golden.xml'
def testOpenFile(self):
"""An open file object can be passed to the parser."""
f = open(self.filename, 'r')
log = tumblr.parse(f)
f.close()
assert log.title == 'golden hours'
def testString(self):
"""An XML string can be passed to the parser."""
f = open(self.filename, 'r')
xmlString = f.read()
f.close()
log = tumblr.parse(xmlString)
assert log.title == 'golden hours'
class ContentTypeParserTestCase(unittest.TestCase):
"""Tests the simple HTTP Content-Type parser."""
def testContentTypeAndCharset(self):
"""Typical Content-Type with charset can be parsed."""
ct = 'application/xml; charset=utf-8'
contentType, charset = tumblr._parse_content_type(ct)
assert (contentType == 'application/xml') and (charset == 'utf-8')
def testContentTypeWithoutCharset(self):
"""Typical Content-Type with no charset can be parsed."""
ct = 'text/xml'
contentType, charset = tumblr._parse_content_type(ct)
assert (contentType == 'text/xml') and (charset is None)
class NetworkingTestCase(unittest.TestCase):
"""Checks various network conditions, including HTTP errors."""
def setUp(self):
self.url = 'http://demo.tumblr.com/api/read'
self.urlUnreachable = 'http://gukbluh.duh123ugh3huh241gah.ugh/eek'
self.urlRedirectDestination = 'http://chompy.net/lab/tumblrapi/tests/http/redirects/demo.xml'
self.urlMovedPermanently = 'http://chompy.net/lab/tumblrapi/tests/http/redirects/301'
self.urlFound = 'http://chompy.net/lab/tumblrapi/tests/http/redirects/302'
self.urlTemporaryRedirect = 'http://chompy.net/lab/tumblrapi/tests/http/redirects/307'
self.urlUnauthorized = 'http://chompy.net/lab/tumblrapi/tests/errors/http/401'
self.urlForbidden = 'http://chompy.net/lab/tumblrapi/tests/http/errors/403'
self.urlNotFound = 'http://chompy.net/lab/tumblrapi/tests/http/errors/404'
self.urlGone = 'http://chompy.net/lab/tumblrapi/tests/http/errors/410'
self.urlServerError = 'http://chompy.net/lab/tumblrapi/tests/http/errors/500'
self.urlServiceUnavailable = 'http://chompy.net/lab/tumblrapi/tests/http/errors/503'
self.urlContentTypeTextPlain = 'http://chompy.net/lab/tumblrapi/tests/http/contenttype/?type=textplain'
self.urlContentTypeTextXml = 'http://chompy.net/lab/tumblrapi/tests/http/contenttype/?type=textxml'
self.urlContentTypeTextHtml = 'http://chompy.net/lab/tumblrapi/tests/http/contenttype/?type=texthtml'
self.urlContentTypeApplicationXhtmlXml = 'http://chompy.net/lab/tumblrapi/tests/http/contenttype/?type=applicationxhtmlxml'
self.urlContentTypeApplicationXml = 'http://chompy.net/lab/tumblrapi/tests/http/contenttype/?type=applicationxml'
def testUnreachableHost(self):
"""Error thrown if host cannot be reached."""
from httplib2 import ServerNotFoundError
try:
tumblr.parse(self.urlUnreachable)
except ServerNotFoundError:
pass
else:
self.fail("Expected a ServerNotFoundError!")
def testHTTP301MovedPermanently(self):
"""Redirect via HTTP 301 is recorded."""
log = tumblr.parse(self.urlMovedPermanently)
assert (log.http_response.previous.status == 301) and (log.http_response['content-location'] == self.urlRedirectDestination)
def testHTTP302Found(self):
"""Redirect via HTTP 302 is recorded."""
log = tumblr.parse(self.urlFound)
assert (log.http_response.previous.status == 302) and (log.http_response['content-location'] == self.urlRedirectDestination)
def testHTTP307(self):
"""Redirect via HTTP 307 is recorded."""
log = tumblr.parse(self.urlTemporaryRedirect)
assert (log.http_response.previous.status == 307) and (log.http_response['content-location'] == self.urlRedirectDestination)
def testHTTP403Forbidden(self):
"""Error thrown if URL is forbidden (HTTP 403)."""
try:
tumblr.parse(self.urlForbidden)
except tumblr.URLForbiddenError:
pass
else:
self.fail("Expected a URLForbiddenError!")
def testHTTP404NotFound(self):
"""Error thrown if URL cannot be found (HTTP 404)."""
try:
tumblr.parse(self.urlNotFound)
except tumblr.URLNotFoundError:
pass
else:
self.fail("Expected a URLNotFoundError!")
def testHTTP410Gone(self):
"""Error thrown if URL has been removed (HTTP 410)."""
try:
tumblr.parse(self.urlGone)
except tumblr.URLGoneError:
pass
else:
self.fail("Expected a URLGoneError!")
def testHTTP500ServerError(self):
"""Error thrown if HTTP 500 occurs."""
try:
tumblr.parse(self.urlServerError)
except tumblr.InternalServerError:
pass
else:
self.fail("Expected an InternalServerError!")
def testHTTP503ServiceUnavailableError(self):
"""Error thrown if HTTP 503 occurs."""
try:
tumblr.parse(self.urlServiceUnavailable)
except tumblr.ServiceUnavailableError:
pass
else:
self.fail("Expected a ServiceUnavailableError!")
def testContentTypeTextXml(self):
"""Should accept HTTP content-type text/xml."""
log = tumblr.parse(self.urlContentTypeTextXml)
# Just do anything
assert log.name == u'demo'
def testContentTypeApplicationXml(self):
"""Should accept HTTP content-type application/xml."""
log = tumblr.parse(self.urlContentTypeApplicationXml)
# Just do anything
assert log.name == u'demo'
def testContentTypeApplicationXhtmlXml(self):
"""Error thrown if content-type application/xhtml+xml used."""
try:
tumblr.parse(self.urlContentTypeApplicationXhtmlXml)
except tumblr.UnsupportedContentTypeError:
pass
else:
self.fail("Expected an UnsupportedContentTypeError!")
class XMLTestCases(unittest.TestCase):
"""Checks various XML handling scenarios."""
def setUp(self):
self.urlMalformed = 'http://chompy.net/lab/tumblrapi/tests/xml/malformed.xml'
self.urlBadAmpersand = 'http://chompy.net/lab/tumblrapi/tests/xml/ampersand.xml'
def testMalformedXML(self):
"""Error thrown if XML is not well-formed."""
try:
tumblr.parse(self.urlMalformed)
except tumblr.TumblrParseError:
pass
else:
fail("Expected a TumblrParseError for malformed XML!")
def testUnencodedAmpersand(self):
"""Error thrown if ampersand is unencoded."""
try:
tumblr.parse(self.urlBadAmpersand)
except tumblr.TumblrParseError:
pass
else:
fail("Expected a TumblrParseError for malformed XML due to unencoded ampersand!")
class TumblelogTestCases(unittest.TestCase):
"""Tests involving the tumblr API XML format."""
def setUp(self):
self.url = "http://chompy.net/lab/tumblrapi/tests/tumblelog/demo.xml"
self.log = tumblr.parse(self.url)
def testTumblelogUrl(self):
"""A tumblelog must have a URL."""
assert self.log.url == u'http://demo.tumblr.com/'
def testCname(self):
"""It's okay if tumblr.cname is or is not present."""
self.log.cname
def testTagline(self):
"""It's okay if tumblr.tagline is or is not present."""
self.log.tagline
def testFeedsNotPresent(self):
"""It's okay if tumblr.feeds is or is not present."""
self.log.feeds
class TumblelogPostTestCases(unittest.TestCase):
"""Generic tests involving posts."""
def setUp(self):
self.url = 'http://chompy.net/lab/tumblrapi/tests/tumblelog/demo.xml'
self.log = tumblr.parse(self.url)
def testPostPermalink(self):
"""post.permalink is equivalent to post.url."""
assert self.log.posts[0].permalink == self.log.posts[0].url
class TumblelogRegularPostTestCases(unittest.TestCase):
"""Tests involving Regular posts."""
def setUp(self):
self.url = "http://chompy.net/lab/tumblrapi/tests/tumblelog/regular.xml"
self.log = tumblr.parse(self.url)
def testRegularPostType(self):
"""A Regular post should have type='regular'."""
assert self.log.posts[0].type == 'regular'
def testRegularContent(self):
"""For Regular posts, post.content is equivalent to post.body."""
assert self.log.posts[0].content == self.log.posts[0].body
def testRegularDescription(self):
"""For Regular posts, post.description is equivalent to post.body."""
assert self.log.posts[0].description == self.log.posts[0].body
def testRegularNoTitle(self):
"""It's okay if a Regular post has no title."""
self.log.posts[1].title
def testRegularNoBody(self):
"""It's okay if a Regular post has no body."""
self.log.posts[2].body
class TumblelogLinkPostTestCases(unittest.TestCase):
"""Tests involving Link posts."""
def setUp(self):
self.url = "http://chompy.net/lab/tumblrapi/tests/tumblelog/link.xml"
self.log = tumblr.parse(self.url)
def testLinkPostType(self):
"""A Link post should have type='link'."""
assert self.log.posts[0].type == 'link'
def testLinkContent(self):
"""For Link posts, post.content is equivalent to post.description."""
assert self.log.posts[0].content == self.log.posts[0].description
def testLinkBody(self):
"""For Link posts, post.body is equivalent to post.description."""
assert self.log.posts[0].body == self.log.posts[0].description
def testLinkRelated(self):
"""For Link posts, post.related is equivalent to post.link_url."""
assert self.log.posts[0].related == self.log.posts[0].link_url
def testLinkNoTitle(self):
"""It's okay if a Link post has no title."""
self.log.posts[1].title
def testLinkNoDescription(self):
"""It's okay if a Link post has no description."""
self.log.posts[2].description
def testLinkNoLinkUrl(self):
"""It's okay if a Link post has no URL. Strange but true."""
self.log.posts[3].link_url
class TumblelogQuotePostTestCases(unittest.TestCase):
"""Tests involving quote posts."""
def setUp(self):
self.url = "http://chompy.net/lab/tumblrapi/tests/tumblelog/quote.xml"
self.log = tumblr.parse(self.url)
def testQuotePostType(self):
"""A Quote post should have type='quote'."""
assert self.log.posts[0].type == 'quote'
def testQuoteContent(self):
"""For Quote posts, post.content is equivalent to post.quote."""
assert self.log.posts[0].content == self.log.posts[0].quote
def testQuoteBody(self):
"""For Quote posts, post.body is equivalent to post.quote."""
assert self.log.posts[0].body == self.log.posts[0].quote
def testQuoteBody(self):
"""For Quote posts, post.description is equivalent to post.quote."""
assert self.log.posts[0].description == self.log.posts[0].quote
def testQuoteSourceNotPresent(self):
"""It's okay if a Quote post has no source."""
self.log.posts[1].source
def testQuoteTextNotPresent(self):
"""It's okay if a Quote post has no text (though it shouldn't be)."""
self.log.posts[2].quote
def testQuoteSourceEmpty(self):
"""It's okay if a Quote post's source is empty."""
assert self.log.posts[3].source == u''
class TumblelogPhotoPostTestCases(unittest.TestCase):
"""Tests involving photo posts."""
def setUp(self):
self.url = "http://chompy.net/lab/tumblrapi/tests/tumblelog/photo.xml"
self.log = tumblr.parse(self.url)
def testPhotoPostType(self):
"""A Photo post should have type='photo'."""
assert self.log.posts[0].type == 'photo'
def testPhotoBody(self):
"""For Photo posts, post.body is equivalent to post.caption."""
assert self.log.posts[0].body == self.log.posts[0].caption
def testPhotoDescription(self):
"""For Photo posts, post.description is equivalent to post.caption."""
assert self.log.posts[0].description == self.log.posts[0].caption
def testPhotoCaptionNotPresent(self):
"""It's okay if a Photo post's caption is empty."""
assert self.log.posts[3].caption == u''
def testPhotoUrls(self):
"""Photo URLs should in fact be URLs."""
for url in self.log.posts[0].urls.values():
assert isUrl(url)
class TumblelogSourceFeedsTestCases(unittest.TestCase):
"""Test inolving source feed support."""
def setUp(self):
self.url = "http://chompy.net/lab/tumblrapi/tests/tumblelog/sourcefeeds.xml"
self.log = tumblr.parse(self.url)
def testSourceFeedsPresent(self):
"""Source feed metadata should be present in post.source_feed."""
assert self.log.posts[3].source_feed is not None
def testSourceFeedId(self):
"""Source feed should have an Id."""
assert self.log.posts[3].source_feed.id == 48612
def testSourceFeedTitle(self):
"""Source feed should have a title."""
assert self.log.posts[3].source_feed.title == u'del.icio.us/mpgomez'
def testSourceFeedUrl(self):
"""Source feed should have a URL."""
assert self.log.posts[3].source_feed.url == u'http://del.icio.us/rss/mpgomez'
def testSourceFeedType(self):
"""Source feed should have a type."""
assert self.log.posts[3].source_feed.type == u'link-description'
if __name__ == '__main__':
clear_http_cache()
unittest.main()