Open Media Library Platform

This commit is contained in:
j 2013-10-11 19:28:32 +02:00
commit 411ad5b16f
5849 changed files with 1778641 additions and 0 deletions

View file

@ -0,0 +1 @@
"""News Tests"""

View file

@ -0,0 +1,224 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for L{twisted.news.database}.
"""
__metaclass__ = type
from email.Parser import Parser
from socket import gethostname
from twisted.trial.unittest import TestCase
from twisted.internet.defer import succeed
from twisted.mail.smtp import messageid
from twisted.news.database import Article, PickleStorage, NewsShelf
class ModerationTestsMixin:
"""
Tests for the moderation features of L{INewsStorage} implementations.
"""
def setUp(self):
self._email = []
def sendmail(self, smtphost, from_addr, to_addrs, msg,
senderDomainName=None, port=25):
"""
Fake of L{twisted.mail.smtp.sendmail} which records attempts to send
email and immediately pretends success.
Subclasses should arrange for their storage implementation to call this
instead of the real C{sendmail} function.
"""
self._email.append((
smtphost, from_addr, to_addrs, msg, senderDomainName, port))
return succeed(None)
_messageTemplate = """\
From: some dude
To: another person
Subject: activities etc
Message-ID: %(articleID)s
Newsgroups: %(newsgroup)s
%(approved)s
Body of the message is such.
""".replace('\n', '\r\n')
def getApprovedMessage(self, articleID, group):
"""
Return a C{str} containing an RFC 2822 formatted message including an
I{Approved} header indicating it has passed through moderation.
"""
return self._messageTemplate % {
'articleID': articleID,
'newsgroup': group,
'approved': 'Approved: yup\r\n'}
def getUnapprovedMessage(self, articleID, group):
"""
Return a C{str} containing an RFC 2822 formatted message with no
I{Approved} header indicating it may require moderation.
"""
return self._messageTemplate % {
'articleID': articleID,
'newsgroup': group,
'approved': '\r\n'}
def getStorage(self, groups, moderators, mailhost, sender):
"""
Override in a subclass to return a L{INewsStorage} provider to test for
correct moderation behavior.
@param groups: A C{list} of C{str} naming the groups which should exist
in the resulting storage object.
@param moderators: A C{dict} mapping C{str} each group name to a C{list}
of C{str} giving moderator email (RFC 2821) addresses.
"""
raise NotImplementedError()
def test_postApproved(self):
"""
L{INewsStorage.postRequest} posts the message if it includes an
I{Approved} header.
"""
group = "example.group"
moderator = "alice@example.com"
mailhost = "127.0.0.1"
sender = "bob@example.org"
articleID = messageid()
storage = self.getStorage(
[group], {group: [moderator]}, mailhost, sender)
message = self.getApprovedMessage(articleID, group)
result = storage.postRequest(message)
def cbPosted(ignored):
self.assertEqual(self._email, [])
exists = storage.articleExistsRequest(articleID)
exists.addCallback(self.assertTrue)
return exists
result.addCallback(cbPosted)
return result
def test_postModerated(self):
"""
L{INewsStorage.postRequest} forwards a message to the moderator if it
does not include an I{Approved} header.
"""
group = "example.group"
moderator = "alice@example.com"
mailhost = "127.0.0.1"
sender = "bob@example.org"
articleID = messageid()
storage = self.getStorage(
[group], {group: [moderator]}, mailhost, sender)
message = self.getUnapprovedMessage(articleID, group)
result = storage.postRequest(message)
def cbModerated(ignored):
self.assertEqual(len(self._email), 1)
self.assertEqual(self._email[0][0], mailhost)
self.assertEqual(self._email[0][1], sender)
self.assertEqual(self._email[0][2], [moderator])
self._checkModeratorMessage(
self._email[0][3], sender, moderator, group, message)
self.assertEqual(self._email[0][4], None)
self.assertEqual(self._email[0][5], 25)
exists = storage.articleExistsRequest(articleID)
exists.addCallback(self.assertFalse)
return exists
result.addCallback(cbModerated)
return result
def _checkModeratorMessage(self, messageText, sender, moderator, group, postingText):
p = Parser()
msg = p.parsestr(messageText)
headers = dict(msg.items())
del headers['Message-ID']
self.assertEqual(
headers,
{'From': sender,
'To': moderator,
'Subject': 'Moderate new %s message: activities etc' % (group,),
'Content-Type': 'message/rfc822'})
posting = p.parsestr(postingText)
attachment = msg.get_payload()[0]
for header in ['from', 'to', 'subject', 'message-id', 'newsgroups']:
self.assertEqual(posting[header], attachment[header])
self.assertEqual(posting.get_payload(), attachment.get_payload())
class PickleStorageTests(ModerationTestsMixin, TestCase):
"""
Tests for L{PickleStorage}.
"""
def getStorage(self, groups, moderators, mailhost, sender):
"""
Create and return a L{PickleStorage} instance configured to require
moderation.
"""
storageFilename = self.mktemp()
storage = PickleStorage(
storageFilename, groups, moderators, mailhost, sender)
storage.sendmail = self.sendmail
self.addCleanup(PickleStorage.sharedDBs.pop, storageFilename)
return storage
class NewsShelfTests(ModerationTestsMixin, TestCase):
"""
Tests for L{NewsShelf}.
"""
def getStorage(self, groups, moderators, mailhost, sender):
"""
Create and return a L{NewsShelf} instance configured to require
moderation.
"""
storageFilename = self.mktemp()
shelf = NewsShelf(mailhost, storageFilename, sender)
for name in groups:
shelf.addGroup(name, 'm') # Dial 'm' for moderator
for address in moderators.get(name, []):
shelf.addModerator(name, address)
shelf.sendmail = self.sendmail
return shelf
def test_notifyModerator(self):
"""
L{NewsShelf.notifyModerator} sends a moderation email to a single
moderator.
"""
shelf = NewsShelf('example.com', self.mktemp(), 'alice@example.com')
shelf.sendmail = self.sendmail
shelf.notifyModerator('bob@example.org', Article('Foo: bar', 'Some text'))
self.assertEqual(len(self._email), 1)
def test_defaultSender(self):
"""
If no sender is specified to L{NewsShelf.notifyModerators}, a default
address based on the system hostname is used for both the envelope and
RFC 2822 sender addresses.
"""
shelf = NewsShelf('example.com', self.mktemp())
shelf.sendmail = self.sendmail
shelf.notifyModerators(['bob@example.org'], Article('Foo: bar', 'Some text'))
self.assertEqual(self._email[0][1], 'twisted-news@' + gethostname())
self.assertIn('From: twisted-news@' + gethostname(), self._email[0][3])

View file

@ -0,0 +1,105 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
from pprint import pformat
from twisted.trial import unittest
from twisted.news import database
MESSAGE_ID = "f83ba57450ed0fd8ac9a472b847e830e"
POST_STRING = """Path: not-for-mail
From: <exarkun@somehost.domain.com>
Subject: a test
Newsgroups: alt.test.nntp
Organization:
Summary:
Keywords:
Message-Id: %s
User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.4.17 (i686))
this is a test
...
lala
moo
--
"One World, one Web, one Program." - Microsoft(R) promotional ad
"Ein Volk, ein Reich, ein Fuhrer." - Adolf Hitler
--
10:56pm up 4 days, 4:42, 1 user, load average: 0.08, 0.08, 0.12
""" % (MESSAGE_ID)
class NewsTestCase(unittest.TestCase):
def setUp(self):
self.backend = database.NewsShelf(None, 'news2.db')
self.backend.addGroup('alt.test.nntp', 'y')
self.backend.postRequest(POST_STRING.replace('\n', '\r\n'))
def testArticleExists(self):
d = self.backend.articleExistsRequest(MESSAGE_ID)
d.addCallback(self.failUnless)
return d
def testArticleRequest(self):
d = self.backend.articleRequest(None, None, MESSAGE_ID)
def cbArticle(result):
self.failUnless(isinstance(result, tuple),
'callback result is wrong type: ' + str(result))
self.assertEqual(len(result), 3,
'callback result list should have three entries: ' +
str(result))
self.assertEqual(result[1], MESSAGE_ID,
"callback result Message-Id doesn't match: %s vs %s" %
(MESSAGE_ID, result[1]))
body = result[2].read()
self.failIfEqual(body.find('\r\n\r\n'), -1,
"Can't find \\r\\n\\r\\n between header and body")
return result
d.addCallback(cbArticle)
return d
def testHeadRequest(self):
d = self.testArticleRequest()
def cbArticle(result):
index = result[0]
d = self.backend.headRequest("alt.test.nntp", index)
d.addCallback(cbHead)
return d
def cbHead(result):
self.assertEqual(result[1], MESSAGE_ID,
"callback result Message-Id doesn't match: %s vs %s" %
(MESSAGE_ID, result[1]))
self.assertEqual(result[2][-2:], '\r\n',
"headers must be \\r\\n terminated.")
d.addCallback(cbArticle)
return d
def testBodyRequest(self):
d = self.testArticleRequest()
def cbArticle(result):
index = result[0]
d = self.backend.bodyRequest("alt.test.nntp", index)
d.addCallback(cbBody)
return d
def cbBody(result):
body = result[2].read()
self.assertEqual(body[0:4], 'this',
"message body has been altered: " +
pformat(body[0:4]))
d.addCallback(cbArticle)
return d

View file

@ -0,0 +1,197 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
from twisted.trial import unittest
from twisted.news import database
from twisted.news import nntp
from twisted.protocols import loopback
from twisted.test import proto_helpers
ALL_GROUPS = ('alt.test.nntp', 0, 1, 'y'),
GROUP = ('0', '1', '0', 'alt.test.nntp', 'group', 'selected')
SUBSCRIPTIONS = ['alt.test.nntp', 'news.testgroup']
POST_STRING = """Path: not-for-mail
From: <exarkun@somehost.domain.com>
Subject: a test
Newsgroups: alt.test.nntp
Organization:
Summary:
Keywords:
User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.4.17 (i686))
this is a test
.
..
...
lala
moo
--
"One World, one Web, one Program." - Microsoft(R) promotional ad
"Ein Volk, ein Reich, ein Fuhrer." - Adolf Hitler
--
10:56pm up 4 days, 4:42, 1 user, load average: 0.08, 0.08, 0.12
"""
class TestNNTPClient(nntp.NNTPClient):
def __init__(self):
nntp.NNTPClient.__init__(self)
def assertEqual(self, foo, bar):
if foo != bar: raise AssertionError("%r != %r!" % (foo, bar))
def connectionMade(self):
nntp.NNTPClient.connectionMade(self)
self.fetchSubscriptions()
def gotSubscriptions(self, subscriptions):
self.assertEqual(len(subscriptions), len(SUBSCRIPTIONS))
for s in subscriptions:
assert s in SUBSCRIPTIONS
self.fetchGroups()
def gotAllGroups(self, info):
self.assertEqual(len(info), len(ALL_GROUPS))
self.assertEqual(info[0], ALL_GROUPS[0])
self.fetchGroup('alt.test.nntp')
def getAllGroupsFailed(self, error):
raise AssertionError("fetchGroups() failed: %s" % (error,))
def gotGroup(self, info):
self.assertEqual(len(info), 6)
self.assertEqual(info, GROUP)
self.postArticle(POST_STRING)
def getSubscriptionsFailed(self, error):
raise AssertionError("fetchSubscriptions() failed: %s" % (error,))
def getGroupFailed(self, error):
raise AssertionError("fetchGroup() failed: %s" % (error,))
def postFailed(self, error):
raise AssertionError("postArticle() failed: %s" % (error,))
def postedOk(self):
self.fetchArticle(1)
def gotArticle(self, info):
origBody = POST_STRING.split('\n\n')[1]
newBody = info.split('\n\n', 1)[1]
self.assertEqual(origBody, newBody)
# We're done
self.transport.loseConnection()
def getArticleFailed(self, error):
raise AssertionError("fetchArticle() failed: %s" % (error,))
class NNTPTestCase(unittest.TestCase):
def setUp(self):
self.server = nntp.NNTPServer()
self.server.factory = self
self.backend = database.NewsShelf(None, 'news.db')
self.backend.addGroup('alt.test.nntp', 'y')
for s in SUBSCRIPTIONS:
self.backend.addSubscription(s)
self.transport = proto_helpers.StringTransport()
self.server.makeConnection(self.transport)
self.client = TestNNTPClient()
def testLoopback(self):
return loopback.loopbackAsync(self.server, self.client)
# XXX This test is woefully incomplete. It tests the single
# most common code path and nothing else. Expand it and the
# test fairy will leave you a surprise.
# reactor.iterate(1) # fetchGroups()
# reactor.iterate(1) # fetchGroup()
# reactor.iterate(1) # postArticle()
def test_connectionMade(self):
"""
When L{NNTPServer} is connected, it sends a server greeting to the
client.
"""
self.assertEqual(
self.transport.value().split('\r\n'), [
'200 server ready - posting allowed',
''])
def test_LIST(self):
"""
When L{NTTPServer} receives a I{LIST} command, it sends a list of news
groups to the client (RFC 3977, section 7.6.1.1).
"""
self.transport.clear()
self.server.do_LIST()
self.assertEqual(
self.transport.value().split('\r\n'), [
'215 newsgroups in form "group high low flags"',
'alt.test.nntp 0 1 y',
'.',
''])
def test_GROUP(self):
"""
When L{NNTPServer} receives a I{GROUP} command, it sends a line of
information about that group to the client (RFC 3977, section 6.1.1.1).
"""
self.transport.clear()
self.server.do_GROUP('alt.test.nntp')
self.assertEqual(
self.transport.value().split('\r\n'), [
'211 0 1 0 alt.test.nntp group selected',
''])
def test_LISTGROUP(self):
"""
When L{NNTPServer} receives a I{LISTGROUP} command, it sends a list of
message numbers for the messages in a particular group (RFC 3977,
section 6.1.2.1).
"""
self.transport.clear()
self.server.do_LISTGROUP('alt.test.nntp')
self.assertEqual(
self.transport.value().split('\r\n'), [
'211 list of article numbers follow',
'.',
''])
def test_XROVER(self):
"""
When L{NTTPServer} receives a I{XROVER} command, it sends a list of
I{References} header values for the messages in a particular group (RFC
2980, section 2.11).
"""
self.server.do_GROUP('alt.test.nntp')
self.transport.clear()
self.server.do_XROVER()
self.assertEqual(
self.transport.value().split('\r\n'), [
'221 Header follows',
'.',
''])