def testLoginSameUserMultEmail(self):
        UserData.onLogin("uid", "user name", "*****@*****.**", "test")
        user = UserData.onLogin("uid", "user name", "*****@*****.**", "test")
        
        self.assertSerialEquals(user, UserData.read(user.getId()))

        self.assertEquals(["*****@*****.**", "*****@*****.**"], user.getAllEmails())
        self.assertEquals([":uid", "test:uid"], user.getAllUserIDs())
        
        #Check stats
        
        def stat(name):
            return user.getPrefValue(UserData.PREFS_STATS, name, "")
        
        self.assertEquals("test", stat("LastLoginAuth"))
        self.assertEquals("uid", stat("LastLoginID"))
        self.assertEquals("*****@*****.**", stat("LastLoginEmail"))
        
        #Check auth prefs
        
        def auth(name):
            prefs = user.getPrefValue(UserData.PREFS_AUTH, "test", {})
            return prefs.get(name, "")
        
        self.assertEquals("uid", auth("LastID"))
        self.assertEquals("*****@*****.**", auth("LastEmail"))
        self.assertEquals("user name", auth("UserName"))
        
        self.assertEquals(["*****@*****.**", "*****@*****.**"], auth("Emails"))
        self.assertEquals(["uid"], auth("UserIDs"))        
    def testLoginSameUserMultEmail(self):
        UserData.onLogin("uid", "user name", "*****@*****.**", "test")
        user = UserData.onLogin("uid", "user name", "*****@*****.**", "test")

        self.assertSerialEquals(user, UserData.read(user.getId()))

        self.assertEquals(["*****@*****.**", "*****@*****.**"],
                          user.getAllEmails())
        self.assertEquals([":uid", "test:uid"], user.getAllUserIDs())

        #Check stats

        def stat(name):
            return user.getPrefValue(UserData.PREFS_STATS, name, "")

        self.assertEquals("test", stat("LastLoginAuth"))
        self.assertEquals("uid", stat("LastLoginID"))
        self.assertEquals("*****@*****.**", stat("LastLoginEmail"))

        #Check auth prefs

        def auth(name):
            prefs = user.getPrefValue(UserData.PREFS_AUTH, "test", {})
            return prefs.get(name, "")

        self.assertEquals("uid", auth("LastID"))
        self.assertEquals("*****@*****.**", auth("LastEmail"))
        self.assertEquals("user name", auth("UserName"))

        self.assertEquals(["*****@*****.**", "*****@*****.**"], auth("Emails"))
        self.assertEquals(["uid"], auth("UserIDs"))
    def testSnarfMissingUserData(self):
        #Login/create user, then update with no name or email
        user = UserData.onLogin("uid", "user name 1", "*****@*****.**", "test")
        user.setUserName("")
        user.setEmail("")
        user.save()

        #Should grab the new stuff on login
        user = UserData.onLogin("uid", "user name 2", "*****@*****.**", "test")
        self.assertSerialEquals(user, UserData.read(user.getId()))

        self.assertEquals("user name 2", user.getUserName())
        self.assertEquals("*****@*****.**", user.getEmail())
 def testSnarfMissingUserData(self):
     #Login/create user, then update with no name or email
     user = UserData.onLogin("uid", "user name 1", "*****@*****.**", "test")
     user.setUserName("")
     user.setEmail("")
     user.save()
     
     #Should grab the new stuff on login
     user = UserData.onLogin("uid", "user name 2", "*****@*****.**", "test")
     self.assertSerialEquals(user, UserData.read(user.getId()))
     
     self.assertEquals("user name 2", user.getUserName())
     self.assertEquals("*****@*****.**", user.getEmail())
    def testLoginExisting(self):
        first = UserData.onLogin("uid", "user name", "*****@*****.**", "test")
        self.assertSerialEquals(first, UserData.read(first.getId()))

        first.addRole("role1")
        first.save()
        self.assertSerialEquals(first, UserData.read(first.getId()))

        self.assertEquals(['role1'], first.getRoles())

        self.assertEquals(["*****@*****.**"], first.getAllEmails())

        import time
        time.sleep(1)

        last = UserData.onLogin("uid", "user name", "*****@*****.**", "test")
        self.assertEquals(first.getUserID(), last.getUserID())
        self.assertEquals(first.getUserName(), last.getUserName())
        self.assertEquals(first.getEmail(), last.getEmail())
        self.assertEquals(first.getRoles(), last.getRoles())

        self.assertEquals(["*****@*****.**"], last.getAllEmails())
        self.assertEquals([":uid", "test:uid"], last.getAllUserIDs())

        #Check stats

        def stat(name):
            return last.getPrefValue(UserData.PREFS_STATS, name, "")

        self.assertTrue(len(stat("LastLoginTime")) > 1)
        self.assertEquals("test", stat("LastLoginAuth"))
        self.assertEquals("uid", stat("LastLoginID"))
        self.assertEquals("*****@*****.**", stat("LastLoginEmail"))

        #Check auth prefs

        #Only one auth section - our "test" from above
        self.assertEquals(1, len(last.getPrefs(UserData.PREFS_AUTH)))

        def auth(name):
            prefs = last.getPrefValue(UserData.PREFS_AUTH, "test", {})
            return prefs.get(name, "")

        self.assertTrue(len(auth("LastLogin")) > 1)
        self.assertEquals("uid", auth("LastID"))
        self.assertEquals("*****@*****.**", auth("LastEmail"))
        self.assertEquals("user name", auth("UserName"))

        self.assertEquals(["*****@*****.**"], auth("Emails"))
        self.assertEquals(["uid"], auth("UserIDs"))
 def testLoginExisting(self):
     first = UserData.onLogin("uid", "user name", "*****@*****.**", "test")
     self.assertSerialEquals(first, UserData.read(first.getId()))
     
     first.addRole("role1")
     first.save()
     self.assertSerialEquals(first, UserData.read(first.getId()))
     
     self.assertEquals(['role1'], first.getRoles())
     
     self.assertEquals(["*****@*****.**"], first.getAllEmails())
     
     import time
     time.sleep(1)
     
     last = UserData.onLogin("uid", "user name", "*****@*****.**", "test")
     self.assertEquals(first.getUserID(), last.getUserID())
     self.assertEquals(first.getUserName(), last.getUserName())
     self.assertEquals(first.getEmail(), last.getEmail())
     self.assertEquals(first.getRoles(), last.getRoles())
     
     self.assertEquals(["*****@*****.**"], last.getAllEmails())
     self.assertEquals([":uid", "test:uid"], last.getAllUserIDs())
     
     #Check stats
     
     def stat(name):
         return last.getPrefValue(UserData.PREFS_STATS, name, "")
     
     self.assertTrue(len(stat("LastLoginTime")) > 1)
     self.assertEquals("test", stat("LastLoginAuth"))
     self.assertEquals("uid", stat("LastLoginID"))
     self.assertEquals("*****@*****.**", stat("LastLoginEmail"))
     
     #Check auth prefs
     
     #Only one auth section - our "test" from above
     self.assertEquals(1, len(last.getPrefs(UserData.PREFS_AUTH)))
     
     def auth(name):
         prefs = last.getPrefValue(UserData.PREFS_AUTH, "test", {})
         return prefs.get(name, "")
     
     self.assertTrue(len(auth("LastLogin")) > 1)
     self.assertEquals("uid", auth("LastID"))
     self.assertEquals("*****@*****.**", auth("LastEmail"))
     self.assertEquals("user name", auth("UserName"))
     
     self.assertEquals(["*****@*****.**"], auth("Emails"))
     self.assertEquals(["uid"], auth("UserIDs"))
    def testReadWriteRetrieveUsers(self):
        u1 = UserData.onLogin("user1", "User One", "*****@*****.**",
                              "testing")
        u2 = UserData.onLogin("user2", "User Two", "*****@*****.**",
                              "testing")
        self.assertNotEquals(u1.getId(), u2.getId())

        ctx = UserContext("WithUsers", [u1.getId(), u2.getId()], {
            "u1Name": u1.getUserName(),
            "u2Name": u2.getUserName()
        })

        self.assertReadWrite(ctx)

        #If read/write succeeded, check the indexes
        self.assertEquals([ctx], UserContext.objects(users=u1.getId()))
        self.assertEquals([ctx], UserContext.objects(users=u2.getId()))
        self.assertEquals([], UserContext.objects(users='notAPerson'))
        self.assertEquals([ctx], UserContext.objects(name='WithUsers'))
        self.assertEquals([], UserContext.objects(name='notAContext'))

        idList = sorted([u1.getId(), u2.getId()])

        #Test users changes that shouldn't affect state
        copy = UserContext.read(ctx.getId())
        self.assertSerialEquals(ctx, copy)
        ctx.addUser(u1.getId())
        ctx.removeUser("user3")
        self.assertSerialEquals(ctx, copy)
        self.assertEquals(idList, copy.getUsers())

        #Check user DB read
        users = ctx.readDBUsers()
        self.assertEquals(idList, sorted([u.getId() for u in users]))

        #What about empty users (via removal and then when hitting the DB)
        ctx.removeUser(u1.getId())
        ctx.removeUser(u2.getId())
        self.assertEquals([], ctx.getUsers())
        self.assertEquals([], ctx.readDBUsers())
 def testReadWriteRetrieveUsers(self):
     u1 = UserData.onLogin("user1", "User One", "*****@*****.**", "testing")
     u2 = UserData.onLogin("user2", "User Two", "*****@*****.**", "testing")
     self.assertNotEquals(u1.getId(), u2.getId())
     
     ctx = UserContext(
         "WithUsers", 
         [u1.getId(), u2.getId()],
         {"u1Name": u1.getUserName(), "u2Name": u2.getUserName()}
     )
     
     self.assertReadWrite(ctx)
     
     #If read/write succeeded, check the indexes
     self.assertEquals([ctx], UserContext.objects(users=u1.getId()))
     self.assertEquals([ctx], UserContext.objects(users=u2.getId()))
     self.assertEquals([],    UserContext.objects(users='notAPerson'))
     self.assertEquals([ctx], UserContext.objects(name='WithUsers'))
     self.assertEquals([],    UserContext.objects(name='notAContext'))
     
     idList = sorted([u1.getId(), u2.getId()])
     
     #Test users changes that shouldn't affect state
     copy = UserContext.read(ctx.getId())
     self.assertSerialEquals(ctx, copy)
     ctx.addUser(u1.getId())
     ctx.removeUser("user3")
     self.assertSerialEquals(ctx, copy)
     self.assertEquals(idList, copy.getUsers())
     
     #Check user DB read
     users = ctx.readDBUsers()
     self.assertEquals(idList, sorted([u.getId() for u in users]))
     
     #What about empty users (via removal and then when hitting the DB)
     ctx.removeUser(u1.getId())
     ctx.removeUser(u2.getId())
     self.assertEquals([], ctx.getUsers())
     self.assertEquals([], ctx.readDBUsers())
 def testAnon(self):
     anon1 = UserData.onLogin(None, None, None, None)
     
     anon2 = UserData()
     
     anon3 = UserData("bob")
     anon3.becomeAnon()
     
     self.assertSerialEquals(anon1, anon2)
     self.assertSerialEquals(anon2, anon3)
     self.assertSerialEquals(anon1, anon3)
     
     print anon1.getId(), anon1
     anon1.save()
     print UserData.read(anon1.getId())
     
     self.assertReadWrite(anon1)
     self.assertReadWrite(anon2)
     self.assertReadWrite(anon3)
    def testAnon(self):
        anon1 = UserData.onLogin(None, None, None, None)

        anon2 = UserData()

        anon3 = UserData("bob")
        anon3.becomeAnon()

        self.assertSerialEquals(anon1, anon2)
        self.assertSerialEquals(anon2, anon3)
        self.assertSerialEquals(anon1, anon3)

        print anon1.getId(), anon1
        anon1.save()
        print UserData.read(anon1.getId())

        self.assertReadWrite(anon1)
        self.assertReadWrite(anon2)
        self.assertReadWrite(anon3)
 def testUpdateMissingAuth(self):
     orig = UserData.onLogin("uid", "user name", "*****@*****.**", None)
     self.assertSerialEquals(orig, UserData.read(orig.getId()))
     
     UA = UserData.UNKNOWN_AUTH #Make the code a little more concise
     
     self.assertEquals("uid", orig.getUserID())
     self.assertEquals("user name", orig.getUserName())
     self.assertEquals("*****@*****.**", orig.getEmail())
     self.assertEquals([], orig.getRoles())
     
     self.assertEquals(["*****@*****.**"], orig.getAllEmails())
     self.assertEquals([":uid", UA+":uid"], orig.getAllUserIDs())
     
     #Check stats
     
     def stat(name):
         return orig.getPrefValue(UserData.PREFS_STATS, name, "")
     
     self.assertTrue(len(stat("LastLoginTime")) > 1)
     self.assertEquals(UA, stat("LastLoginAuth"))
     self.assertEquals("uid", stat("LastLoginID"))
     self.assertEquals("*****@*****.**", stat("LastLoginEmail"))
     
     #Check auth prefs
     
     #Only one auth section - our "test" from above
     self.assertEquals(1, len(orig.getPrefs(UserData.PREFS_AUTH)))
     
     def auth(name):
         prefs = orig.getPrefValue(UserData.PREFS_AUTH, UA, {})
         return prefs.get(name, "")
     
     self.assertTrue(len(auth("LastLogin")) > 1)
     self.assertEquals("uid", auth("LastID"))
     self.assertEquals("*****@*****.**", auth("LastEmail"))
     self.assertEquals("user name", auth("UserName"))
     
     self.assertEquals(["*****@*****.**"], auth("Emails"))
     self.assertEquals(["uid"], auth("UserIDs"))
    def testUpdateMissingAuth(self):
        orig = UserData.onLogin("uid", "user name", "*****@*****.**", None)
        self.assertSerialEquals(orig, UserData.read(orig.getId()))

        UA = UserData.UNKNOWN_AUTH  #Make the code a little more concise

        self.assertEquals("uid", orig.getUserID())
        self.assertEquals("user name", orig.getUserName())
        self.assertEquals("*****@*****.**", orig.getEmail())
        self.assertEquals([], orig.getRoles())

        self.assertEquals(["*****@*****.**"], orig.getAllEmails())
        self.assertEquals([":uid", UA + ":uid"], orig.getAllUserIDs())

        #Check stats

        def stat(name):
            return orig.getPrefValue(UserData.PREFS_STATS, name, "")

        self.assertTrue(len(stat("LastLoginTime")) > 1)
        self.assertEquals(UA, stat("LastLoginAuth"))
        self.assertEquals("uid", stat("LastLoginID"))
        self.assertEquals("*****@*****.**", stat("LastLoginEmail"))

        #Check auth prefs

        #Only one auth section - our "test" from above
        self.assertEquals(1, len(orig.getPrefs(UserData.PREFS_AUTH)))

        def auth(name):
            prefs = orig.getPrefValue(UserData.PREFS_AUTH, UA, {})
            return prefs.get(name, "")

        self.assertTrue(len(auth("LastLogin")) > 1)
        self.assertEquals("uid", auth("LastID"))
        self.assertEquals("*****@*****.**", auth("LastEmail"))
        self.assertEquals("user name", auth("UserName"))

        self.assertEquals(["*****@*****.**"], auth("Emails"))
        self.assertEquals(["uid"], auth("UserIDs"))
 def testRoles(self):
     user = UserData.onLogin("uid", "user name", "*****@*****.**", "test")
     self.assertSerialEquals(user, UserData.read(user.getId()))
     
     #Add role
     user.addRole("role1")
     self.assertEquals(["role1"], user.getRoles())
     
     #Maintain alphabetical order
     user.addRole("role0")
     self.assertEquals(["role0", "role1"], user.getRoles())
     
     #Save/read with multiple roles
     self.assertReadWrite(user)
     
     #Ignore dups
     user.addRole("role0")
     user.addRole("role1")
     self.assertEquals(["role0", "role1"], user.getRoles())
     
     self.assertFalse(user.needRole("role0"))
     self.assertFalse(user.needRole("role1"))
     self.assertTrue(user.needRole("role2"))
     self.assertEquals(["role0", "role1", "role2"], user.getRoles())
    def testRoles(self):
        user = UserData.onLogin("uid", "user name", "*****@*****.**", "test")
        self.assertSerialEquals(user, UserData.read(user.getId()))

        #Add role
        user.addRole("role1")
        self.assertEquals(["role1"], user.getRoles())

        #Maintain alphabetical order
        user.addRole("role0")
        self.assertEquals(["role0", "role1"], user.getRoles())

        #Save/read with multiple roles
        self.assertReadWrite(user)

        #Ignore dups
        user.addRole("role0")
        user.addRole("role1")
        self.assertEquals(["role0", "role1"], user.getRoles())

        self.assertFalse(user.needRole("role0"))
        self.assertFalse(user.needRole("role1"))
        self.assertTrue(user.needRole("role2"))
        self.assertEquals(["role0", "role1", "role2"], user.getRoles())
 def testPrefChanges(self):
     user = UserData.onLogin("uid", "user name", "*****@*****.**", "test")
     
     #Empty prefs
     self.assertEquals(user.getPrefs("NOPE"), {})
     self.assertEquals(user.getPrefs(""), {})
     
     #Simple optional preferences
     user.setPrefValue("opt", "K1", "V2")
     self.assertEquals("V2", user.getPrefValue("opt", "K1"))
     user.setPrefValue("opt", "K1", "V1")
     self.assertEquals("V1", user.getPrefValue("opt", "K1"))
     
     #Missing pref value
     self.assertEquals("MISS", user.getPrefValue("opt", "KX", "MISS"))
     self.assertEquals("MISS", user.getPrefValue("optNo", "K1", "MISS"))
     
     #pop value (including bad pref and key)
     user.setPrefValue("opt", "K2", "V2")
     self.assertEquals("V2", user.getPrefValue("opt", "K2"))
     self.assertEquals("MISS", user.popPrefValue("optNo", "K2", "MISS"))
     self.assertEquals("V2", user.popPrefValue("opt", "K2", "MISS"))
     self.assertEquals("MISS", user.popPrefValue("opt", "K2", "MISS"))
     self.assertEquals("MISS", user.popPrefValue("optNo", "K2", "MISS"))
     
     self.assertEquals(["*****@*****.**"], user.getAllEmails())
     self.assertEquals([":uid", "test:uid"], user.getAllUserIDs())
     
     #Cheat a little on the timestamp
     ts = user.getPrefValue(UserData.PREFS_STATS, "LastLoginTime")
             
     self.assertEquals(user.getAllPrefs(), {
         UserData.PREFS_STATS: {
             "LastLoginTime": ts,
             "LastLoginAuth": "test",
             "LastLoginID": "uid",
             "LastLoginEmail": "*****@*****.**",
         },
         UserData.PREFS_AUTH: {
             "test": {
                 "LastLogin": ts,
                 "LastID": "uid",
                 "LastEmail": "*****@*****.**",
                 "UserName": "******",
                 "Emails": ["*****@*****.**"],
                 "UserIDs": ["uid"],
             }
         },
         "opt" : {
             "K1": "V1"
         }
     })
     
     #Check other prefs
     
     self.assertEquals(user.getPrefs("opt"), {"K1": "V1"})
     
     #Check stats
     
     self.assertEquals(user.getPrefs(UserData.PREFS_STATS), {
         "LastLoginTime": ts,
         "LastLoginAuth": "test",
         "LastLoginID": "uid",
         "LastLoginEmail": "*****@*****.**",
     })
     
     #Check auths
     
     self.assertEquals(user.getPrefs(UserData.PREFS_AUTH), {
         "test": {
             "LastLogin": ts,
             "LastID": "uid",
             "LastEmail": "*****@*****.**",
             "UserName": "******",
             "Emails": ["*****@*****.**"],
             "UserIDs": ["uid"],
         }
     })
    def testLoginMultiple(self):
        logins = [
            ("uidA1", "user name 1", "*****@*****.**", "testA"),
            ("uidA2", "user name 2", "*****@*****.**",
             "testA"),  #Same email, different user
            ("uidA1", "user name 3", "*****@*****.**",
             "testA"),  #Same UID, different email
            ("uidB1", "user name 4", "*****@*****.**", "testB"
             ),  #New auth provider - but should match with previous email
            ("uidB1", "user name 5", "*****@*****.**",
             "testB"),  #Same UID, different email (from other auth)
            ("uidB2", "user name 6", "*****@*****.**",
             "testB"),  #Same email, different UID
            ("uidB2", "user name 7", "*****@*****.**",
             "testB"),  #Same UID, different email
        ]

        all_emails = []
        all_uids = []

        user = None
        for uid, uname, email, auth in logins:
            all_emails.append(email)
            all_uids.append(auth + ':' + uid)
            user = UserData.onLogin(uid, uname, email, auth)

        self.assertIsNotNone(user)

        #First login created the user - so that's the "main" ID
        self.assertSerialEquals(user, UserData.read(user.getId()))

        all_emails = sorted(set(all_emails))
        print "ALL EMAILS: %s" % str(all_emails)
        self.assertEquals(4, len(all_emails))
        self.assertEquals(all_emails, user.getAllEmails())

        all_uids.append(":uidA1")
        all_uids = sorted(set(all_uids))
        print "ALL UIDS: %s" % str(all_uids)
        self.assertEquals(5, len(all_uids))  #4 + top-level ID
        self.assertEquals(all_uids, user.getAllUserIDs())

        #Check stats

        def stat(name):
            return user.getPrefValue(UserData.PREFS_STATS, name, "")

        self.assertEquals("testB", stat("LastLoginAuth"))
        self.assertEquals("uidB2", stat("LastLoginID"))
        self.assertEquals("*****@*****.**", stat("LastLoginEmail"))

        #Check auth prefs

        def auth(auth, name):
            prefs = user.getPrefValue(UserData.PREFS_AUTH, auth, {})
            return prefs.get(name, "")

        self.assertEquals("uidA1", auth("testA", "LastID"))
        self.assertEquals("*****@*****.**", auth("testA", "LastEmail"))
        self.assertEquals("user name 3", auth("testA", "UserName"))
        self.assertEquals(["*****@*****.**", "*****@*****.**"],
                          auth("testA", "Emails"))
        self.assertEquals(["uidA1", "uidA2"], auth("testA", "UserIDs"))

        self.assertEquals("uidB2", auth("testB", "LastID"))
        self.assertEquals("*****@*****.**", auth("testB", "LastEmail"))
        self.assertEquals("user name 7", auth("testB", "UserName"))
        self.assertEquals(
            ["*****@*****.**", "*****@*****.**", "*****@*****.**"],
            auth("testB", "Emails"))
        self.assertEquals(["uidB1", "uidB2"], auth("testB", "UserIDs"))
 def testLoginMultiple(self):
     logins = [
         ("uidA1", "user name 1", "*****@*****.**", "testA"),
         ("uidA2", "user name 2", "*****@*****.**", "testA"), #Same email, different user
         ("uidA1", "user name 3", "*****@*****.**", "testA"), #Same UID, different email
         ("uidB1", "user name 4", "*****@*****.**", "testB"), #New auth provider - but should match with previous email
         ("uidB1", "user name 5", "*****@*****.**", "testB"), #Same UID, different email (from other auth)
         ("uidB2", "user name 6", "*****@*****.**", "testB"), #Same email, different UID
         ("uidB2", "user name 7", "*****@*****.**", "testB"), #Same UID, different email
     ]
     
     all_emails = []
     all_uids = []
     
     user = None
     for uid, uname, email, auth in logins:
         all_emails.append(email)
         all_uids.append(auth + ':' + uid)
         user = UserData.onLogin(uid, uname, email, auth)
     
     self.assertIsNotNone(user)
     
     #First login created the user - so that's the "main" ID
     self.assertSerialEquals(user, UserData.read(user.getId()))
     
     all_emails = sorted(set(all_emails))
     print "ALL EMAILS: %s" % str(all_emails)
     self.assertEquals(4, len(all_emails))
     self.assertEquals(all_emails, user.getAllEmails())
     
     all_uids.append(":uidA1")
     all_uids = sorted(set(all_uids))
     print "ALL UIDS: %s" % str(all_uids)
     self.assertEquals(5, len(all_uids)) #4 + top-level ID
     self.assertEquals(all_uids, user.getAllUserIDs())
     
     #Check stats
     
     def stat(name):
         return user.getPrefValue(UserData.PREFS_STATS, name, "")
     
     self.assertEquals("testB", stat("LastLoginAuth"))
     self.assertEquals("uidB2", stat("LastLoginID"))
     self.assertEquals("*****@*****.**", stat("LastLoginEmail"))
     
     #Check auth prefs
     
     def auth(auth, name):
         prefs = user.getPrefValue(UserData.PREFS_AUTH, auth, {})
         return prefs.get(name, "")
     
     self.assertEquals("uidA1", auth("testA", "LastID"))
     self.assertEquals("*****@*****.**", auth("testA", "LastEmail"))
     self.assertEquals("user name 3", auth("testA", "UserName"))
     self.assertEquals(["*****@*****.**", "*****@*****.**"], auth("testA", "Emails"))
     self.assertEquals(["uidA1", "uidA2"], auth("testA", "UserIDs"))
     
     self.assertEquals("uidB2", auth("testB", "LastID"))
     self.assertEquals("*****@*****.**", auth("testB", "LastEmail"))
     self.assertEquals("user name 7", auth("testB", "UserName"))
     self.assertEquals(["*****@*****.**", "*****@*****.**", "*****@*****.**"], auth("testB", "Emails"))
     self.assertEquals(["uidB1", "uidB2"], auth("testB", "UserIDs"))
    def testPrefChanges(self):
        user = UserData.onLogin("uid", "user name", "*****@*****.**", "test")

        #Empty prefs
        self.assertEquals(user.getPrefs("NOPE"), {})
        self.assertEquals(user.getPrefs(""), {})

        #Simple optional preferences
        user.setPrefValue("opt", "K1", "V2")
        self.assertEquals("V2", user.getPrefValue("opt", "K1"))
        user.setPrefValue("opt", "K1", "V1")
        self.assertEquals("V1", user.getPrefValue("opt", "K1"))

        #Missing pref value
        self.assertEquals("MISS", user.getPrefValue("opt", "KX", "MISS"))
        self.assertEquals("MISS", user.getPrefValue("optNo", "K1", "MISS"))

        #pop value (including bad pref and key)
        user.setPrefValue("opt", "K2", "V2")
        self.assertEquals("V2", user.getPrefValue("opt", "K2"))
        self.assertEquals("MISS", user.popPrefValue("optNo", "K2", "MISS"))
        self.assertEquals("V2", user.popPrefValue("opt", "K2", "MISS"))
        self.assertEquals("MISS", user.popPrefValue("opt", "K2", "MISS"))
        self.assertEquals("MISS", user.popPrefValue("optNo", "K2", "MISS"))

        self.assertEquals(["*****@*****.**"], user.getAllEmails())
        self.assertEquals([":uid", "test:uid"], user.getAllUserIDs())

        #Cheat a little on the timestamp
        ts = user.getPrefValue(UserData.PREFS_STATS, "LastLoginTime")

        self.assertEquals(
            user.getAllPrefs(), {
                UserData.PREFS_STATS: {
                    "LastLoginTime": ts,
                    "LastLoginAuth": "test",
                    "LastLoginID": "uid",
                    "LastLoginEmail": "*****@*****.**",
                },
                UserData.PREFS_AUTH: {
                    "test": {
                        "LastLogin": ts,
                        "LastID": "uid",
                        "LastEmail": "*****@*****.**",
                        "UserName": "******",
                        "Emails": ["*****@*****.**"],
                        "UserIDs": ["uid"],
                    }
                },
                "opt": {
                    "K1": "V1"
                }
            })

        #Check other prefs

        self.assertEquals(user.getPrefs("opt"), {"K1": "V1"})

        #Check stats

        self.assertEquals(
            user.getPrefs(UserData.PREFS_STATS), {
                "LastLoginTime": ts,
                "LastLoginAuth": "test",
                "LastLoginID": "uid",
                "LastLoginEmail": "*****@*****.**",
            })

        #Check auths

        self.assertEquals(
            user.getPrefs(UserData.PREFS_AUTH), {
                "test": {
                    "LastLogin": ts,
                    "LastID": "uid",
                    "LastEmail": "*****@*****.**",
                    "UserName": "******",
                    "Emails": ["*****@*****.**"],
                    "UserIDs": ["uid"],
                }
            })