Commits

Mike Steder committed 9e14c89

More tests around models and registration.

  • Participants
  • Parent commits db3415a

Comments (0)

Files changed (6)

         newnick = message.parameters[0]
         if newnick not in db.nicks:
             if not len(newnick) > irc.MAX_NICK_LENGTH:
-                db.nicks.add(newnick)
-                db.nickToUserId[newnick] = userId
-                user.nick = newnick
+                db.setNick(userId, newnick)
                 po.send([userId], ":%s NICK %s"%(user.nick, user.nick))
             else:
                 po.send([userId], irc.ERR_ERRONEUSNICKNAME)
 @registerCommand("USER")
 def user(db, po, userId, message):
     user = db.getUser(userId)
-    if len(message.parameters) == 4 and not user.user:
-        user.user = message.parameters
+    if len(message.parameters) >= 4 and not user.user:
         username = message.parameters[0]
         hostname = message.parameters[1]
         servername = message.parameters[2]
-        realname = message.parameters[3]
+        realname = " ".join(message.parameters[3:])
+        if realname[0] == ":":
+            realname = realname[1:]
+
+        user.user = (username, hostname, servername, realname)
+
         po.send([userId], "NOTICE you are now registered as %s!%s@%s (%s)"%(
             username, hostname, servername, realname))
-    elif len(message.parameters) == 4 and user.user:
+    elif len(message.parameters) >= 4 and user.user:
         po.send([userId], irc.ERR_ALREADYREGISTRED)
     else:
         po.send([userId], irc.ERR_NEEDMOREPARAMS)
     def addUser(self, socket, address):
         u = User(socket,
                  address)
+        self.socketToUser[socket] = u
         self.users[u.userId] = u
         return u.userId
 
     def getUser(self, userId):
         return self.users[userId]
 
+    def setNick(self, userId, newnick):
+        self.nicks.add(newnick)
+        self.nickToUserId[newnick] = userId
+        self.users[userId].nick = newnick
+
     def joinChannel(self, channelName):
         channelName = channelName.upper()
         if channelName not in self.channelByName:

noir/test/commands/test_registration.py

         self.command(self.db, self.po, self.u, m)
         self.po.send.assert_called_with([self.u], ":bingo NICK bingo")
 
+    def test_set_nick_data(self):
+        """Normal nick message without user prefix
+
+        So we're initially setting the users nick as part
+        of the registration process
+
+        """
+        m = messages.Message(None, "NICK", ["bingo"])
+        self.command(self.db, self.po, self.u, m)
+        user = self.db.getUser(self.u)
+        self.assertEqual(user.nick, "bingo")
+
     def test_set_nick_w_hopcount(self):
         """Hopcount is used to tell servers about nicknames on
         other servers.  The server will ignore the hopcount
         self.po.send.assert_called_with([self.u], irc.ERR_ERRONEUSNICKNAME)
 
 
+class TestUSER(unittest2.TestCase):
+    def setUp(self):
+        self.db = models.DB()
+        socket, address = "Fake", "127.0.0.1"
+        self.u = self.db.addUser(socket, address)
+        self.po = mocks.MockPo()
+        self.command = commands.COMMANDS["USER"]
+
+    def test_needMoreParameters_none(self):
+        m = messages.Message(None, "USER", [])
+        self.command(self.db, self.po, self.u, m)
+        self.po.send.assert_called_with([self.u], irc.ERR_NEEDMOREPARAMS)
+        
+    def test_needMoreParameters_3(self):
+        m = messages.Message(None, "USER", ["a", "b", "c"])
+        self.command(self.db, self.po, self.u, m)
+        self.po.send.assert_called_with([self.u], irc.ERR_NEEDMOREPARAMS)
+
+    def test_register_shortName(self):
+        m = messages.Message(None, "USER", ["guest", "hostname", "servername", "RealName" ])
+        self.command(self.db, self.po, self.u, m)
+        u = self.db.getUser(self.u)
+        self.assertEqual(u.user, ("guest", "hostname", "servername", "RealName"))
+
+    def test_register_alreadyRegistered(self):
+        m = messages.Message(None, "USER", ["guest", "hostname", "servername", "RealName" ])
+        self.command(self.db, self.po, self.u, m)
+        self.po.send.assert_called_with([self.u], "NOTICE you are now registered as %s!%s@%s (%s)"%("guest", "hostname", "servername", "RealName"))
+        self.command(self.db, self.po, self.u, m)
+        self.po.send.assert_called_with([self.u], irc.ERR_ALREADYREGISTRED)
+
+    def test_register_nameWithSpaces(self):
+        m = messages.Message(None, "USER", ["guest", "hostname", "servername", ":My", "Real", "Name", "With", "Spaces"])
+        self.command(self.db, self.po, self.u, m)
+        self.po.send.assert_called_with([self.u], "NOTICE you are now registered as %s!%s@%s (%s)"%("guest", "hostname", "servername", "My Real Name With Spaces"))
+        u = self.db.getUser(self.u)
+        self.assertEqual(u.user, ('guest', 'hostname', 'servername', 'My Real Name With Spaces'))
+

noir/test/commands/test_registration.pyc

Binary file removed.

noir/test/commands/test_sending.py

     ERR_NOSUCHNICK
     RPL_AWAY
     """
+    def setUp(self):
+        self.db = models.DB()
+        socket, address = "Fake", "127.0.0.1"
+        self.u = self.db.addUser(socket, address)
+        self.po = mocks.MockPo()
+        self.command = commands.COMMANDS["PRIVMSG"]
     
 
 class TestNOTICE(unittest2.TestCase):

noir/test/test_models.py

+"""
+"""
+
+
+import unittest2
+
+
+from noir import models
+
+
+class TestAddingUsers(unittest2.TestCase):
+    def setUp(self):
+        self.db = models.DB()
+        self.socket, self.address = "fake socket", "localhost"
+        self.newUserId = self.db.addUser(self.socket, self.address)
+
+    def test_getUser(self):
+        user = self.db.getUser(self.newUserId)
+        self.assertEqual(user.userId, self.newUserId)
+        self.assertEqual(user.socket, "fake socket")
+        self.assertEqual(user.address, "localhost")
+
+    def test_getUserBySocket(self):
+        user = self.db.socketToUser[self.socket]
+        self.assertEqual(user.userId, self.newUserId)
+
+    def test_numberOfUsers(self):
+        self.assertEqual(len(self.db.users), 1)
+
+class TestSetNick(unittest2.TestCase):
+    def setUp(self):
+        self.db = models.DB()
+        self.socket, self.address = "fake socket", "localhost"
+        self.newUserId = self.db.addUser(self.socket, self.address)
+        self.db.setNick(self.newUserId, "bingo")        
+
+    def test_includedInNicksSet(self):
+        self.assertEqual(self.db.nicks, set(["bingo"]))
+
+    def test_mappingToUserId(self):
+        self.assertEqual(self.db.nickToUserId["bingo"], self.newUserId)
+
+    def test_userNick(self):
+        user = self.db.getUser(self.newUserId)
+        self.assertEqual(user.nick, "bingo")