You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

128 lines
3.3 KiB

import unittest
from .context import ptaimport
from ptaimport import passwords as pwd
class TestPasswords( unittest.TestCase ):
"""
Tests the passwords module functions
"""
def setUp( self ):
# no manager name given
self.config_none = {
}
# unknown manager name given
self.config_unknown = {
"manager": "unknown"
}
def test_verify( self ):
"""
Tests the passwords.verify() function
"""
(ok, err) = pwd.verify( self.config_none )
self.assertFalse(ok)
self.assertNotEqual( len(err), 0 )
(ok, err) = pwd.verify( self.config_unknown )
self.assertFalse(ok)
self.assertNotEqual( len(err), 0 )
def test_factory( self ):
"""
Tests the passwords.Manager() function
"""
self.assertRaises(ValueError, pwd.Manager, self.config_none )
self.assertRaises(ValueError, pwd.Manager, self.config_unknown )
#-------------------------------------------------------------------------------
class TestPlainTextPasswords( unittest.TestCase ):
"""
Tests the PlainTextPasswordManager class
"""
def setUp( self ):
# no passwords given
self.config_none = {
"manager": "plaintext"
}
# not dict "passwords" section
self.config_wrong = {
"manager": "plaintext",
"passwords": "foo"
}
# one password given
self.test_pass_name = "my_password"
self.test_pass_val = "qwerty1234"
self.config_pass = {
"manager": "plaintext",
"passwords": {
self.test_pass_name: self.test_pass_val
}
}
def test_verify(self):
"""
Tests the PlainTextPasswordManager.verify() method
"""
(ok, err) = pwd.verify( self.config_none )
self.assertFalse(ok)
self.assertNotEqual( len(err), 0 )
(ok, err) = pwd.verify( self.config_wrong )
self.assertFalse(ok)
self.assertNotEqual( len(err), 0 )
(ok, err) = pwd.verify( self.config_pass )
self.assertTrue(ok)
self.assertEqual( len(err), 0 )
def test_pass(self):
"""
Tests the PlainTextPasswordManager.get() method
"""
manager = pwd.Manager( self.config_pass )
pwd_val = manager.get( self.test_pass_name )
self.assertIsNotNone( pwd_val )
self.assertEqual( pwd_val, pwd.Password(self.test_pass_val) )
#-------------------------------------------------------------------------------
class TestGNUPassPasswords( unittest.TestCase ):
"""
Tests the GNUPassPasswordManager class
Unfortunately, not much can be tested automatically, as any password
retrieval would trigger user input (and anyway, we cannot know the ground
truth for tests)
"""
def setUp( self ):
self.config = {
"manager": "pass"
}
def test_create( self ):
"""
Tests the creation of a GNU pass manager
"""
(ok, err) = pwd.verify( self.config )
self.assertTrue(ok)
self.assertEqual( len(err), 0 )
pwd.Manager( self.config )
#-------------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()