~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/words/test/test_irc_service.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2009 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
"""
 
5
Tests for IRC portions of L{twisted.words.service}.
 
6
"""
 
7
 
 
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
 
13
 
 
14
class IRCUserTestCase(unittest.TestCase):
 
15
    """
 
16
    Isolated tests for L{IRCUser}
 
17
    """
 
18
 
 
19
    def setUp(self):
 
20
        """
 
21
        Sets up a Realm, Portal, Factory, IRCUser, Transport, and Connection
 
22
        for our tests.
 
23
        """
 
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)
 
31
 
 
32
 
 
33
    def test_sendMessage(self):
 
34
        """
 
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".
 
37
        """
 
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())
 
43
 
 
44
 
 
45
    def response(self):
 
46
        """
 
47
        Grabs our responses and then clears the transport
 
48
        """
 
49
        response = self.ircUser.transport.value().splitlines()
 
50
        self.ircUser.transport.clear()
 
51
        return map(irc.parsemsg, response)
 
52
 
 
53
 
 
54
    def scanResponse(self, response, messageType):
 
55
        """
 
56
        Gets messages out of a response
 
57
        
 
58
        @param response: The parsed IRC messages of the response, as returned
 
59
        by L{IRCServiceTestCase.response}
 
60
        
 
61
        @param messageType: The string type of the desired messages.
 
62
        
 
63
        @return: An iterator which yields 2-tuples of C{(index, ircMessage)}
 
64
        """
 
65
        for n, message in enumerate(response):
 
66
            if (message[1] == messageType):
 
67
                yield n, message
 
68
 
 
69
 
 
70
    def test_sendNickSendsGreeting(self):
 
71
        """
 
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.
 
75
        """
 
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.']))])
 
84
 
 
85
 
 
86
    def test_fullLogin(self):
 
87
        """
 
88
        Receiving USER, PASS, NICK will log in the user, and transmit the
 
89
        appropriate response messages.
 
90
        """
 
91
        self.ircUser.irc_USER("", ["john doe"])
 
92
        self.ircUser.irc_PASS("", ["pass"])
 
93
        self.ircUser.irc_NICK("", ["john"])
 
94
 
 
95
        version = ('Your host is example.com, running version %s' %
 
96
            (self.factory._serverInfo["serviceVersion"],))
 
97
 
 
98
        creation = ('This server was created on %s' %
 
99
            (self.factory._serverInfo["creationDate"],))
 
100
 
 
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"],
 
110
               'w', 'n'])])