def testAddUser(self):
     self.pwd.addUser(username='******', password='******')
     # reopen file.
     pf = PasswordFile(filename=self.filename, hashPassword=poorHash)
     self.assertTrue(pf.validateUser('John', 'password'))
 def setUp(self):
     SeecrTestCase.setUp(self)
     self.filename=join(self.tempdir, 'passwd')
     self.pwd = PasswordFile(filename=self.filename, hashPassword=poorHash)
 def testReadPasswordFile(self):
     passwdHash = poorHash('password')
     userlist = {'John':passwdHash}
     jsonSave(userlist, open(self.filename, 'w'))
     pf = PasswordFile(filename=self.filename, hashPassword=poorHash)
     self.assertTrue(pf.validateUser('John', 'password'))
class PasswordFileTest(SeecrTestCase):
    def setUp(self):
        SeecrTestCase.setUp(self)
        self.filename=join(self.tempdir, 'passwd')
        self.pwd = PasswordFile(filename=self.filename, hashPassword=poorHash)

    def testReadPasswordFile(self):
        passwdHash = poorHash('password')
        userlist = {'John':passwdHash}
        jsonSave(userlist, open(self.filename, 'w'))
        pf = PasswordFile(filename=self.filename, hashPassword=poorHash)
        self.assertTrue(pf.validateUser('John', 'password'))

    def testAddUser(self):
        self.pwd.addUser(username='******', password='******')
        # reopen file.
        pf = PasswordFile(filename=self.filename, hashPassword=poorHash)
        self.assertTrue(pf.validateUser('John', 'password'))

    def testValidPassword(self):
        self.pwd.addUser(username='******', password='******')
        self.assertFalse(self.pwd.validateUser(username='******', password=''))
        self.assertFalse(self.pwd.validateUser(username='******', password='******'))
        self.assertFalse(self.pwd.validateUser(username='******', password='******'))
        self.assertTrue(self.pwd.validateUser(username='******', password='******'))
        self.assertFalse(self.pwd.validateUser(username='******', password='******'))

        self.assertFalse(self.pwd.validateUser(username='', password=''))
        self.assertFalse(self.pwd.validateUser(username='******', password=''))

    def testChangePassword(self):
        self.pwd.addUser(username='******', password='******')
        self.pwd.changePassword(username='******', oldPassword='******', newPassword='******')
        self.assertTrue(self.pwd.validateUser(username='******', password='******'))

    def testChangePasswordWithBadOldpassword(self):
        self.pwd.addUser(username='******', password='******')
        self.assertRaises(ValueError, self.pwd.changePassword, username='******', oldPassword='******', newPassword='******')
        self.assertTrue(self.pwd.validateUser(username='******', password='******'))
        self.assertFalse(self.pwd.validateUser(username='******', password='******'))

    def testChangePasswordWithBadUsername(self):
        self.pwd.addUser(username='******', password='******')
        self.assertRaises(ValueError, self.pwd.changePassword, username='******', oldPassword='******', newPassword='******')

    def testPasswordTest(self):
        self.assertTrue(self.pwd._passwordTest('something'))
        self.assertTrue(self.pwd._passwordTest('s om et hing'))
        self.assertTrue(self.pwd._passwordTest('ng'))
        self.assertTrue(self.pwd._passwordTest('SOMETHING'))
        self.assertTrue(self.pwd._passwordTest('123513'))
        self.assertFalse(self.pwd._passwordTest(''))
        self.assertFalse(self.pwd._passwordTest(' '))
        self.assertFalse(self.pwd._passwordTest('\t'))
        self.assertFalse(self.pwd._passwordTest('\t\n'))

    def testAddUserWithBadPassword(self):
        self.assertRaises(ValueError, self.pwd.addUser, username='******', password='')

    def testAddUserWithBadname(self):
        self.assertRaises(ValueError, self.pwd.addUser, username='', password='******')

    def testChangePasswordWithBadPassword(self):
        self.pwd.addUser(username='******', password='******')
        self.assertRaises(ValueError, self.pwd.changePassword, username='******', oldPassword='******', newPassword='')

    def testUsernameTest(self):
        self.assertTrue(self.pwd._usernameTest('name'))
        self.assertTrue(self.pwd._usernameTest('*****@*****.**'))
        self.assertTrue(self.pwd._usernameTest('name-1235'))
        self.assertFalse(self.pwd._usernameTest('name\t-1235'))
        self.assertFalse(self.pwd._usernameTest(''))
        self.assertFalse(self.pwd._usernameTest(' '))
        self.assertFalse(self.pwd._usernameTest(' name'))

    def testAddExistingUser(self):
        self.pwd.addUser(username='******', password='******')
        self.assertRaises(ValueError, self.pwd.addUser, username='******', password='******')

    def testAddMultipleUsers(self):
        self.pwd.addUser(username='******', password='******')
        self.pwd.addUser(username='******', password='******')
        self.pwd.addUser(username='******', password='******')
        self.assertTrue(self.pwd.validateUser('John', 'password'))
        self.assertTrue(self.pwd.validateUser('Johnny', 'password2'))
        self.assertTrue(self.pwd.validateUser('Johann', 'password3'))

    def testRemoveUser(self):
        self.pwd.addUser(username='******', password='******')
        self.assertTrue(self.pwd.validateUser('John', 'password'))
        self.pwd.removeUser(username='******')
        self.assertFalse(self.pwd.validateUser('John', 'password'))

    def testListUsernames(self):
        self.pwd.addUser(username='******', password='******')
        self.pwd.addUser(username='******', password='******')
        self.pwd.addUser(username='******', password='******')
        self.assertEquals(set(['admin', 'hank', 'graham', 'john']), set(self.pwd.listUsernames()))

    def testHasUser(self):
        self.pwd.addUser(username='******', password='******')
        self.assertTrue(self.pwd.hasUser(username='******'))
        self.assertFalse(self.pwd.hasUser(username='******'))

    def testCreateFileIfMissingWithDefaultAdmin(self):
        self.assertTrue(self.pwd.validateUser(username='******', password='******'))