1
# Copyright (c) 2009 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
Tests for IRC portions of L{twisted.words.service}.
8
from twisted.trial import unittest
9
from twisted.test import proto_helpers
10
from twisted.words.service import InMemoryWordsRealm, IRCFactory
11
from twisted.words.protocols import irc
12
from twisted.cred import checkers, portal
14
class IRCUserTestCase(unittest.TestCase):
16
Isolated tests for L{IRCUser}
21
Sets up a Realm, Portal, Factory, IRCUser, Transport, and Connection
24
self.wordsRealm = InMemoryWordsRealm("example.com")
25
self.portal = portal.Portal(self.wordsRealm,
26
[checkers.InMemoryUsernamePasswordDatabaseDontUse(john="pass")])
27
self.factory = IRCFactory(self.wordsRealm, self.portal)
28
self.ircUser = self.factory.buildProtocol(None)
29
self.stringTransport = proto_helpers.StringTransport()
30
self.ircUser.makeConnection(self.stringTransport)
33
def test_sendMessage(self):
35
Sending a message to a user after they have sent NICK, but before they
36
have authenticated, results in a message from "example.com".
38
self.ircUser.irc_NICK("", ["mynick"])
39
self.stringTransport.clear()
40
self.ircUser.sendMessage("foo")
41
self.assertEquals(":example.com foo mynick\r\n",
42
self.stringTransport.value())
47
Grabs our responses and then clears the transport
49
response = self.ircUser.transport.value().splitlines()
50
self.ircUser.transport.clear()
51
return map(irc.parsemsg, response)
54
def scanResponse(self, response, messageType):
56
Gets messages out of a response
58
@param response: The parsed IRC messages of the response, as returned
59
by L{IRCServiceTestCase.response}
61
@param messageType: The string type of the desired messages.
63
@return: An iterator which yields 2-tuples of C{(index, ircMessage)}
65
for n, message in enumerate(response):
66
if (message[1] == messageType):
70
def test_sendNickSendsGreeting(self):
72
Receiving NICK without authenticating sends the MOTD Start and MOTD End
73
messages, which is required by certain popular IRC clients (such as
74
Pidgin) before a connection is considered to be fully established.
76
self.ircUser.irc_NICK("", ["mynick"])
77
response = self.response()
78
start = list(self.scanResponse(response, irc.RPL_MOTDSTART))
79
end = list(self.scanResponse(response, irc.RPL_ENDOFMOTD))
80
self.assertEquals(start,
81
[(0, ('example.com', '375', ['mynick', '- example.com Message of the Day - ']))])
82
self.assertEquals(end,
83
[(1, ('example.com', '376', ['mynick', 'End of /MOTD command.']))])
86
def test_fullLogin(self):
88
Receiving USER, PASS, NICK will log in the user, and transmit the
89
appropriate response messages.
91
self.ircUser.irc_USER("", ["john doe"])
92
self.ircUser.irc_PASS("", ["pass"])
93
self.ircUser.irc_NICK("", ["john"])
95
version = ('Your host is example.com, running version %s' %
96
(self.factory._serverInfo["serviceVersion"],))
98
creation = ('This server was created on %s' %
99
(self.factory._serverInfo["creationDate"],))
101
self.assertEquals(self.response(),
102
[('example.com', '375',
103
['john', '- example.com Message of the Day - ']),
104
('example.com', '376', ['john', 'End of /MOTD command.']),
105
('example.com', '001', ['john', 'connected to Twisted IRC']),
106
('example.com', '002', ['john', version]),
107
('example.com', '003', ['john', creation]),
108
('example.com', '004',
109
['john', 'example.com', self.factory._serverInfo["serviceVersion"],