def describe(self, channel, action): """ Strike a pose. @type channel: Destination, Hostmask or str @param channel: The user or channel to perform an action in. @type action: str @param action: The action to preform. """ if isinstance(channel, Destination): self._log.debug('Implicitly converting Destination to raw format for action performance: %s --> %s', repr(channel), channel.raw) channel = channel.raw if isinstance(channel, Hostmask): self._log.debug('Implicitly converting Hostmask to nick format for action performance: %s --> %s', repr(channel), channel.nick) channel = channel.nick IRCClient.describe(self, channel, action)
class ClientTests(TestCase): """ Tests for the protocol-level behavior of IRCClient methods intended to be called by application code. """ def setUp(self): """ Create and connect a new L{IRCClient} to a new L{StringTransport}. """ self.transport = StringTransport() self.protocol = IRCClient() self.protocol.performLogin = False self.protocol.makeConnection(self.transport) # Sanity check - we don't want anything to have happened at this # point, since we're not in a test yet. self.assertEquals(self.transport.value(), "") def getLastLine(self, transport): """ Return the last IRC message in the transport buffer. """ return transport.value().split('\r\n')[-2] def test_away(self): """ L{IRCCLient.away} sends an AWAY command with the specified message. """ message = "Sorry, I'm not here." self.protocol.away(message) expected = [ 'AWAY :%s' % (message, ), '', ] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_back(self): """ L{IRCClient.back} sends an AWAY command with an empty message. """ self.protocol.back() expected = [ 'AWAY :', '', ] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_whois(self): """ L{IRCClient.whois} sends a WHOIS message. """ self.protocol.whois('alice') self.assertEquals(self.transport.value().split('\r\n'), ['WHOIS alice', '']) def test_whoisWithServer(self): """ L{IRCClient.whois} sends a WHOIS message with a server name if a value is passed for the C{server} parameter. """ self.protocol.whois('alice', 'example.org') self.assertEquals(self.transport.value().split('\r\n'), ['WHOIS example.org alice', '']) def test_register(self): """ L{IRCClient.register} sends NICK and USER commands with the username, name, hostname, server name, and real name specified. """ username = '******' hostname = 'testhost' servername = 'testserver' self.protocol.realname = 'testname' self.protocol.password = None self.protocol.register(username, hostname, servername) expected = [ 'NICK %s' % (username, ), 'USER %s %s %s :%s' % (username, hostname, servername, self.protocol.realname), '' ] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_registerWithPassword(self): """ If the C{password} attribute of L{IRCClient} is not C{None}, the C{register} method also sends a PASS command with it as the argument. """ username = '******' hostname = 'testhost' servername = 'testserver' self.protocol.realname = 'testname' self.protocol.password = '******' self.protocol.register(username, hostname, servername) expected = [ 'PASS %s' % (self.protocol.password, ), 'NICK %s' % (username, ), 'USER %s %s %s :%s' % (username, hostname, servername, self.protocol.realname), '' ] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_registerWithTakenNick(self): """ Verify that the client repeats the L{IRCClient.setNick} method with a new value when presented with an C{ERR_NICKNAMEINUSE} while trying to register. """ username = '******' hostname = 'testhost' servername = 'testserver' self.protocol.realname = 'testname' self.protocol.password = '******' self.protocol.register(username, hostname, servername) self.protocol.irc_ERR_NICKNAMEINUSE('prefix', ['param']) lastLine = self.getLastLine(self.transport) self.assertNotEquals(lastLine, 'NICK %s' % (username, )) # Keep chaining underscores for each collision self.protocol.irc_ERR_NICKNAMEINUSE('prefix', ['param']) lastLine = self.getLastLine(self.transport) self.assertEquals(lastLine, 'NICK %s' % (username + '__', )) def test_overrideAlterCollidedNick(self): """ L{IRCClient.alterCollidedNick} determines how a nickname is altered upon collision while a user is trying to change to that nickname. """ nick = 'foo' self.protocol.alterCollidedNick = lambda nick: nick + '***' self.protocol.register(nick) self.protocol.irc_ERR_NICKNAMEINUSE('prefix', ['param']) lastLine = self.getLastLine(self.transport) self.assertEquals(lastLine, 'NICK %s' % (nick + '***', )) def test_nickChange(self): """ When a NICK command is sent after signon, C{IRCClient.nickname} is set to the new nickname I{after} the server sends an acknowledgement. """ oldnick = 'foo' newnick = 'bar' self.protocol.register(oldnick) self.protocol.irc_RPL_WELCOME('prefix', ['param']) self.protocol.setNick(newnick) self.assertEquals(self.protocol.nickname, oldnick) self.protocol.irc_NICK('%s!quux@qux' % (oldnick, ), [newnick]) self.assertEquals(self.protocol.nickname, newnick) def test_erroneousNick(self): """ Trying to register an illegal nickname results in the default legal nickname being set, and trying to change a nickname to an illegal nickname results in the old nickname being kept. """ # Registration case: change illegal nickname to erroneousNickFallback badnick = 'foo' self.assertEquals(self.protocol._registered, False) self.protocol.register(badnick) self.protocol.irc_ERR_ERRONEUSNICKNAME('prefix', ['param']) lastLine = self.getLastLine(self.transport) self.assertEquals(lastLine, 'NICK %s' % (self.protocol.erroneousNickFallback, )) self.protocol.irc_RPL_WELCOME('prefix', ['param']) self.assertEquals(self.protocol._registered, True) self.protocol.setNick(self.protocol.erroneousNickFallback) self.assertEquals(self.protocol.nickname, self.protocol.erroneousNickFallback) # Illegal nick change attempt after registration. Fall back to the old # nickname instead of erroneousNickFallback. oldnick = self.protocol.nickname self.protocol.setNick(badnick) self.protocol.irc_ERR_ERRONEUSNICKNAME('prefix', ['param']) lastLine = self.getLastLine(self.transport) self.assertEquals(lastLine, 'NICK %s' % (badnick, )) self.assertEquals(self.protocol.nickname, oldnick) def test_describe(self): """ L{IRCClient.desrcibe} sends a CTCP ACTION message to the target specified. """ target = 'foo' channel = '#bar' action = 'waves' self.protocol.describe(target, action) self.protocol.describe(channel, action) expected = [ 'PRIVMSG %s :\01ACTION %s\01' % (target, action), 'PRIVMSG %s :\01ACTION %s\01' % (channel, action), '' ] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_me(self): """ L{IRCClient.me} sends a CTCP ACTION message to the target channel specified. If the target does not begin with a standard channel prefix, '#' is prepended. """ target = 'foo' channel = '#bar' action = 'waves' self.protocol.me(target, action) self.protocol.me(channel, action) expected = [ 'PRIVMSG %s :\01ACTION %s\01' % ('#' + target, action), 'PRIVMSG %s :\01ACTION %s\01' % (channel, action), '' ] self.assertEquals(self.transport.value().split('\r\n'), expected) warnings = self.flushWarnings(offendingFunctions=[self.test_me]) self.assertEquals( warnings[0]['message'], "me() is deprecated since Twisted 9.0. Use IRCClient.describe().") self.assertEquals(warnings[0]['category'], DeprecationWarning) self.assertEquals(len(warnings), 2)
class ClientTests(TestCase): """ Tests for the protocol-level behavior of IRCClient methods intended to be called by application code. """ def setUp(self): """ Create and connect a new L{IRCClient} to a new L{StringTransport}. """ self.transport = StringTransport() self.protocol = IRCClient() self.protocol.performLogin = False self.protocol.makeConnection(self.transport) # Sanity check - we don't want anything to have happened at this # point, since we're not in a test yet. self.assertEquals(self.transport.value(), "") def test_away(self): """ L{IRCCLient.away} sends an AWAY command with the specified message. """ message = "Sorry, I'm not here." self.protocol.away(message) expected = [ 'AWAY :%s' % (message,), '', ] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_back(self): """ L{IRCClient.back} sends an AWAY command with an empty message. """ self.protocol.back() expected = [ 'AWAY :', '', ] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_whois(self): """ L{IRCClient.whois} sends a WHOIS message. """ self.protocol.whois('alice') self.assertEquals( self.transport.value().split('\r\n'), ['WHOIS alice', '']) def test_whoisWithServer(self): """ L{IRCClient.whois} sends a WHOIS message with a server name if a value is passed for the C{server} parameter. """ self.protocol.whois('alice', 'example.org') self.assertEquals( self.transport.value().split('\r\n'), ['WHOIS example.org alice', '']) def test_register(self): """ L{IRCClient.register} sends NICK and USER commands with the username, name, hostname, server name, and real name specified. """ username = '******' hostname = 'testhost' servername = 'testserver' self.protocol.realname = 'testname' self.protocol.password = None self.protocol.register(username, hostname, servername) expected = [ 'NICK %s' % (username,), 'USER %s %s %s :%s' % ( username, hostname, servername, self.protocol.realname), ''] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_registerWithPassword(self): """ If the C{password} attribute of L{IRCClient} is not C{None}, the C{register} method also sends a PASS command with it as the argument. """ username = '******' hostname = 'testhost' servername = 'testserver' self.protocol.realname = 'testname' self.protocol.password = '******' self.protocol.register(username, hostname, servername) expected = [ 'PASS %s' % (self.protocol.password,), 'NICK %s' % (username,), 'USER %s %s %s :%s' % ( username, hostname, servername, self.protocol.realname), ''] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_describe(self): """ L{IRCClient.desrcibe} sends a CTCP ACTION message to the target specified. """ target = 'foo' channel = '#bar' action = 'waves' self.protocol.describe(target, action) self.protocol.describe(channel, action) expected = [ 'PRIVMSG %s :\01ACTION %s\01' % (target, action), 'PRIVMSG %s :\01ACTION %s\01' % (channel, action), ''] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_me(self): """ L{IRCClient.me} sends a CTCP ACTION message to the target channel specified. If the target does not begin with a standard channel prefix, '#' is prepended. """ target = 'foo' channel = '#bar' action = 'waves' self.protocol.me(target, action) self.protocol.me(channel, action) expected = [ 'PRIVMSG %s :\01ACTION %s\01' % ('#' + target, action), 'PRIVMSG %s :\01ACTION %s\01' % (channel, action), ''] self.assertEquals(self.transport.value().split('\r\n'), expected) warnings = self.flushWarnings( offendingFunctions=[self.test_me]) self.assertEquals( warnings[0]['message'], "me() is deprecated since Twisted 9.0. Use IRCClient.describe().") self.assertEquals(warnings[0]['category'], DeprecationWarning) self.assertEquals(len(warnings), 2)
class ClientTests(TestCase): """ Tests for the protocol-level behavior of IRCClient methods intended to be called by application code. """ def setUp(self): """ Create and connect a new L{IRCClient} to a new L{StringTransport}. """ self.transport = StringTransport() self.protocol = IRCClient() self.protocol.performLogin = False self.protocol.makeConnection(self.transport) # Sanity check - we don't want anything to have happened at this # point, since we're not in a test yet. self.assertEquals(self.transport.value(), "") def getLastLine(self, transport): """ Return the last IRC message in the transport buffer. """ return transport.value().split('\r\n')[-2] def test_away(self): """ L{IRCCLient.away} sends an AWAY command with the specified message. """ message = "Sorry, I'm not here." self.protocol.away(message) expected = [ 'AWAY :%s' % (message,), '', ] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_back(self): """ L{IRCClient.back} sends an AWAY command with an empty message. """ self.protocol.back() expected = [ 'AWAY :', '', ] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_whois(self): """ L{IRCClient.whois} sends a WHOIS message. """ self.protocol.whois('alice') self.assertEquals( self.transport.value().split('\r\n'), ['WHOIS alice', '']) def test_whoisWithServer(self): """ L{IRCClient.whois} sends a WHOIS message with a server name if a value is passed for the C{server} parameter. """ self.protocol.whois('alice', 'example.org') self.assertEquals( self.transport.value().split('\r\n'), ['WHOIS example.org alice', '']) def test_register(self): """ L{IRCClient.register} sends NICK and USER commands with the username, name, hostname, server name, and real name specified. """ username = '******' hostname = 'testhost' servername = 'testserver' self.protocol.realname = 'testname' self.protocol.password = None self.protocol.register(username, hostname, servername) expected = [ 'NICK %s' % (username,), 'USER %s %s %s :%s' % ( username, hostname, servername, self.protocol.realname), ''] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_registerWithPassword(self): """ If the C{password} attribute of L{IRCClient} is not C{None}, the C{register} method also sends a PASS command with it as the argument. """ username = '******' hostname = 'testhost' servername = 'testserver' self.protocol.realname = 'testname' self.protocol.password = '******' self.protocol.register(username, hostname, servername) expected = [ 'PASS %s' % (self.protocol.password,), 'NICK %s' % (username,), 'USER %s %s %s :%s' % ( username, hostname, servername, self.protocol.realname), ''] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_registerWithTakenNick(self): """ Verify that the client repeats the L{IRCClient.setNick} method with a new value when presented with an C{ERR_NICKNAMEINUSE} while trying to register. """ username = '******' hostname = 'testhost' servername = 'testserver' self.protocol.realname = 'testname' self.protocol.password = '******' self.protocol.register(username, hostname, servername) self.protocol.irc_ERR_NICKNAMEINUSE('prefix', ['param']) lastLine = self.getLastLine(self.transport) self.assertNotEquals(lastLine, 'NICK %s' % (username,)) # Keep chaining underscores for each collision self.protocol.irc_ERR_NICKNAMEINUSE('prefix', ['param']) lastLine = self.getLastLine(self.transport) self.assertEquals(lastLine, 'NICK %s' % (username + '__',)) def test_overrideAlterCollidedNick(self): """ L{IRCClient.alterCollidedNick} determines how a nickname is altered upon collision while a user is trying to change to that nickname. """ nick = 'foo' self.protocol.alterCollidedNick = lambda nick: nick + '***' self.protocol.register(nick) self.protocol.irc_ERR_NICKNAMEINUSE('prefix', ['param']) lastLine = self.getLastLine(self.transport) self.assertEquals( lastLine, 'NICK %s' % (nick + '***',)) def test_nickChange(self): """ When a NICK command is sent after signon, C{IRCClient.nickname} is set to the new nickname I{after} the server sends an acknowledgement. """ oldnick = 'foo' newnick = 'bar' self.protocol.register(oldnick) self.protocol.irc_RPL_WELCOME('prefix', ['param']) self.protocol.setNick(newnick) self.assertEquals(self.protocol.nickname, oldnick) self.protocol.irc_NICK('%s!quux@qux' % (oldnick,), [newnick]) self.assertEquals(self.protocol.nickname, newnick) def test_erroneousNick(self): """ Trying to register an illegal nickname results in the default legal nickname being set, and trying to change a nickname to an illegal nickname results in the old nickname being kept. """ # Registration case: change illegal nickname to erroneousNickFallback badnick = 'foo' self.assertEquals(self.protocol._registered, False) self.protocol.register(badnick) self.protocol.irc_ERR_ERRONEUSNICKNAME('prefix', ['param']) lastLine = self.getLastLine(self.transport) self.assertEquals( lastLine, 'NICK %s' % (self.protocol.erroneousNickFallback,)) self.protocol.irc_RPL_WELCOME('prefix', ['param']) self.assertEquals(self.protocol._registered, True) self.protocol.setNick(self.protocol.erroneousNickFallback) self.assertEquals( self.protocol.nickname, self.protocol.erroneousNickFallback) # Illegal nick change attempt after registration. Fall back to the old # nickname instead of erroneousNickFallback. oldnick = self.protocol.nickname self.protocol.setNick(badnick) self.protocol.irc_ERR_ERRONEUSNICKNAME('prefix', ['param']) lastLine = self.getLastLine(self.transport) self.assertEquals( lastLine, 'NICK %s' % (badnick,)) self.assertEquals(self.protocol.nickname, oldnick) def test_describe(self): """ L{IRCClient.desrcibe} sends a CTCP ACTION message to the target specified. """ target = 'foo' channel = '#bar' action = 'waves' self.protocol.describe(target, action) self.protocol.describe(channel, action) expected = [ 'PRIVMSG %s :\01ACTION %s\01' % (target, action), 'PRIVMSG %s :\01ACTION %s\01' % (channel, action), ''] self.assertEquals(self.transport.value().split('\r\n'), expected) def test_me(self): """ L{IRCClient.me} sends a CTCP ACTION message to the target channel specified. If the target does not begin with a standard channel prefix, '#' is prepended. """ target = 'foo' channel = '#bar' action = 'waves' self.protocol.me(target, action) self.protocol.me(channel, action) expected = [ 'PRIVMSG %s :\01ACTION %s\01' % ('#' + target, action), 'PRIVMSG %s :\01ACTION %s\01' % (channel, action), ''] self.assertEquals(self.transport.value().split('\r\n'), expected) warnings = self.flushWarnings( offendingFunctions=[self.test_me]) self.assertEquals( warnings[0]['message'], "me() is deprecated since Twisted 9.0. Use IRCClient.describe().") self.assertEquals(warnings[0]['category'], DeprecationWarning) self.assertEquals(len(warnings), 2)