| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
import sys, types |
|---|
| 5 |
from pprint import pformat |
|---|
| 6 |
|
|---|
| 7 |
from twisted.trial import unittest |
|---|
| 8 |
from twisted.news import database |
|---|
| 9 |
from twisted.internet import reactor |
|---|
| 10 |
|
|---|
| 11 |
MESSAGE_ID = "f83ba57450ed0fd8ac9a472b847e830e" |
|---|
| 12 |
|
|---|
| 13 |
POST_STRING = """Path: not-for-mail |
|---|
| 14 |
From: <exarkun@somehost.domain.com> |
|---|
| 15 |
Subject: a test |
|---|
| 16 |
Newsgroups: alt.test.nntp |
|---|
| 17 |
Organization: |
|---|
| 18 |
Summary: |
|---|
| 19 |
Keywords: |
|---|
| 20 |
Message-Id: %s |
|---|
| 21 |
User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.4.17 (i686)) |
|---|
| 22 |
|
|---|
| 23 |
this is a test |
|---|
| 24 |
... |
|---|
| 25 |
lala |
|---|
| 26 |
moo |
|---|
| 27 |
-- |
|---|
| 28 |
"One World, one Web, one Program." - Microsoft(R) promotional ad |
|---|
| 29 |
"Ein Volk, ein Reich, ein Fuhrer." - Adolf Hitler |
|---|
| 30 |
-- |
|---|
| 31 |
10:56pm up 4 days, 4:42, 1 user, load average: 0.08, 0.08, 0.12 |
|---|
| 32 |
""" % (MESSAGE_ID) |
|---|
| 33 |
|
|---|
| 34 |
class NewsTestCase(unittest.TestCase): |
|---|
| 35 |
def setUp(self): |
|---|
| 36 |
self.backend = database.NewsShelf(None, 'news2.db') |
|---|
| 37 |
self.backend.addGroup('alt.test.nntp', 'y') |
|---|
| 38 |
self.backend.postRequest(POST_STRING.replace('\n', '\r\n')) |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
def testArticleExists(self): |
|---|
| 42 |
d = self.backend.articleExistsRequest(MESSAGE_ID) |
|---|
| 43 |
d.addCallback(self.failUnless) |
|---|
| 44 |
return d |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
def testArticleRequest(self): |
|---|
| 48 |
d = self.backend.articleRequest(None, None, MESSAGE_ID) |
|---|
| 49 |
|
|---|
| 50 |
def cbArticle(result): |
|---|
| 51 |
self.failUnless(isinstance(result, tuple), |
|---|
| 52 |
'callback result is wrong type: ' + str(result)) |
|---|
| 53 |
self.assertEquals(len(result), 3, |
|---|
| 54 |
'callback result list should have three entries: ' + |
|---|
| 55 |
str(result)) |
|---|
| 56 |
self.assertEquals(result[1], MESSAGE_ID, |
|---|
| 57 |
"callback result Message-Id doesn't match: %s vs %s" % |
|---|
| 58 |
(MESSAGE_ID, result[1])) |
|---|
| 59 |
body = result[2].read() |
|---|
| 60 |
self.failIfEqual(body.find('\r\n\r\n'), -1, |
|---|
| 61 |
"Can't find \\r\\n\\r\\n between header and body") |
|---|
| 62 |
return result |
|---|
| 63 |
|
|---|
| 64 |
d.addCallback(cbArticle) |
|---|
| 65 |
return d |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
def testHeadRequest(self): |
|---|
| 69 |
d = self.testArticleRequest() |
|---|
| 70 |
|
|---|
| 71 |
def cbArticle(result): |
|---|
| 72 |
index = result[0] |
|---|
| 73 |
|
|---|
| 74 |
d = self.backend.headRequest("alt.test.nntp", index) |
|---|
| 75 |
d.addCallback(cbHead) |
|---|
| 76 |
return d |
|---|
| 77 |
|
|---|
| 78 |
def cbHead(result): |
|---|
| 79 |
self.assertEquals(result[1], MESSAGE_ID, |
|---|
| 80 |
"callback result Message-Id doesn't match: %s vs %s" % |
|---|
| 81 |
(MESSAGE_ID, result[1])) |
|---|
| 82 |
|
|---|
| 83 |
self.assertEquals(result[2][-2:], '\r\n', |
|---|
| 84 |
"headers must be \\r\\n terminated.") |
|---|
| 85 |
|
|---|
| 86 |
d.addCallback(cbArticle) |
|---|
| 87 |
return d |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
def testBodyRequest(self): |
|---|
| 91 |
d = self.testArticleRequest() |
|---|
| 92 |
|
|---|
| 93 |
def cbArticle(result): |
|---|
| 94 |
index = result[0] |
|---|
| 95 |
|
|---|
| 96 |
d = self.backend.bodyRequest("alt.test.nntp", index) |
|---|
| 97 |
d.addCallback(cbBody) |
|---|
| 98 |
return d |
|---|
| 99 |
|
|---|
| 100 |
def cbBody(result): |
|---|
| 101 |
body = result[2].read() |
|---|
| 102 |
self.assertEquals(body[0:4], 'this', |
|---|
| 103 |
"message body has been altered: " + |
|---|
| 104 |
pformat(body[0:4])) |
|---|
| 105 |
|
|---|
| 106 |
d.addCallback(cbArticle) |
|---|
| 107 |
return d |
|---|