Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / news / test / test_nntp.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 from twisted.trial import unittest
5 from twisted.news import database
6 from twisted.news import nntp
7 from twisted.protocols import loopback
8 from twisted.test import proto_helpers
9
10 ALL_GROUPS = ('alt.test.nntp', 0, 1, 'y'),
11 GROUP = ('0', '1', '0', 'alt.test.nntp', 'group', 'selected')
12 SUBSCRIPTIONS = ['alt.test.nntp', 'news.testgroup']
13
14 POST_STRING = """Path: not-for-mail
15 From: <exarkun@somehost.domain.com>
16 Subject: a test
17 Newsgroups: alt.test.nntp
18 Organization: 
19 Summary: 
20 Keywords: 
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 ..
26 ...
27 lala
28 moo
29 -- 
30 "One World, one Web, one Program." - Microsoft(R) promotional ad
31 "Ein Volk, ein Reich, ein Fuhrer." - Adolf Hitler
32 --
33  10:56pm up 4 days, 4:42, 1 user, load average: 0.08, 0.08, 0.12
34 """
35
36 class TestNNTPClient(nntp.NNTPClient):
37     def __init__(self):
38         nntp.NNTPClient.__init__(self)
39
40     def assertEqual(self, foo, bar):
41         if foo != bar: raise AssertionError("%r != %r!" % (foo, bar))
42
43     def connectionMade(self):
44         nntp.NNTPClient.connectionMade(self)
45         self.fetchSubscriptions()
46
47
48     def gotSubscriptions(self, subscriptions):
49         self.assertEqual(len(subscriptions), len(SUBSCRIPTIONS))
50         for s in subscriptions:
51             assert s in SUBSCRIPTIONS
52
53         self.fetchGroups()
54
55     def gotAllGroups(self, info):
56         self.assertEqual(len(info), len(ALL_GROUPS))
57         self.assertEqual(info[0], ALL_GROUPS[0])
58
59         self.fetchGroup('alt.test.nntp')
60
61
62     def getAllGroupsFailed(self, error):
63         raise AssertionError("fetchGroups() failed: %s" % (error,))
64
65
66     def gotGroup(self, info):
67         self.assertEqual(len(info), 6)
68         self.assertEqual(info, GROUP)
69
70         self.postArticle(POST_STRING)
71
72
73     def getSubscriptionsFailed(self, error):
74         raise AssertionError("fetchSubscriptions() failed: %s" % (error,))
75
76
77     def getGroupFailed(self, error):
78         raise AssertionError("fetchGroup() failed: %s" % (error,))
79
80
81     def postFailed(self, error):
82         raise AssertionError("postArticle() failed: %s" % (error,))
83
84
85     def postedOk(self):
86         self.fetchArticle(1)
87
88
89     def gotArticle(self, info):
90         origBody = POST_STRING.split('\n\n')[1]
91         newBody = info.split('\n\n', 1)[1]
92
93         self.assertEqual(origBody, newBody)
94
95         # We're done
96         self.transport.loseConnection()
97
98
99     def getArticleFailed(self, error):
100         raise AssertionError("fetchArticle() failed: %s" % (error,))
101
102
103 class NNTPTestCase(unittest.TestCase):
104     def setUp(self):
105         self.server = nntp.NNTPServer()
106         self.server.factory = self
107         self.backend = database.NewsShelf(None, 'news.db')
108         self.backend.addGroup('alt.test.nntp', 'y')
109
110         for s in SUBSCRIPTIONS:
111             self.backend.addSubscription(s)
112
113         self.transport = proto_helpers.StringTransport()
114         self.server.makeConnection(self.transport)
115         self.client = TestNNTPClient()
116
117     def testLoopback(self):
118         return loopback.loopbackAsync(self.server, self.client)
119
120         # XXX This test is woefully incomplete.  It tests the single
121         # most common code path and nothing else.  Expand it and the
122         # test fairy will leave you a surprise.
123
124         #         reactor.iterate(1) # fetchGroups()
125         #         reactor.iterate(1) # fetchGroup()
126         #         reactor.iterate(1) # postArticle()
127
128
129     def test_connectionMade(self):
130         """
131         When L{NNTPServer} is connected, it sends a server greeting to the
132         client.
133         """
134         self.assertEqual(
135             self.transport.value().split('\r\n'), [
136                 '200 server ready - posting allowed',
137                 ''])
138
139
140     def test_LIST(self):
141         """
142         When L{NTTPServer} receives a I{LIST} command, it sends a list of news
143         groups to the client (RFC 3977, section 7.6.1.1).
144         """
145         self.transport.clear()
146         self.server.do_LIST()
147         self.assertEqual(
148             self.transport.value().split('\r\n'), [
149                 '215 newsgroups in form "group high low flags"',
150                 'alt.test.nntp 0 1 y',
151                 '.',
152                 ''])
153
154
155     def test_GROUP(self):
156         """
157         When L{NNTPServer} receives a I{GROUP} command, it sends a line of
158         information about that group to the client (RFC 3977, section 6.1.1.1).
159         """
160         self.transport.clear()
161         self.server.do_GROUP('alt.test.nntp')
162         self.assertEqual(
163             self.transport.value().split('\r\n'), [
164                 '211 0 1 0 alt.test.nntp group selected',
165                 ''])
166
167
168     def test_LISTGROUP(self):
169         """
170         When L{NNTPServer} receives a I{LISTGROUP} command, it sends a list of
171         message numbers for the messages in a particular group (RFC 3977,
172         section 6.1.2.1).
173         """
174         self.transport.clear()
175         self.server.do_LISTGROUP('alt.test.nntp')
176         self.assertEqual(
177             self.transport.value().split('\r\n'), [
178                 '211 list of article numbers follow',
179                 '.',
180                 ''])
181
182
183     def test_XROVER(self):
184         """
185         When L{NTTPServer} receives a I{XROVER} command, it sends a list of
186         I{References} header values for the messages in a particular group (RFC
187         2980, section 2.11).
188         """
189         self.server.do_GROUP('alt.test.nntp')
190         self.transport.clear()
191
192         self.server.do_XROVER()
193         self.assertEqual(
194             self.transport.value().split('\r\n'), [
195                 '221 Header follows',
196                 '.',
197                 ''])