def lsuser(): user_manager = SqliteUserManager(AUTHDBPATH, COLLECTIONPATH) try: users = user_manager.user_list() for username in users: print(username) except ValueError as error: print("Could not list users: {}".format(error), file=sys.stderr)
def passwd(username): user_manager = SqliteUserManager(AUTHDBPATH, COLLECTIONPATH) if username not in user_manager.user_list(): print("User {} doesn't exist".format(username)) return password = getpass.getpass("Enter password for {}: ".format(username)) try: user_manager.set_password_for_user(username, password) except ValueError as error: print("Could not set password for user {}: {}".format(username, error), file=sys.stderr)
class SqliteUserManagerTest(unittest.TestCase): def setUp(self): basedir = tempfile.mkdtemp(prefix=self.__class__.__name__) self.basedir = basedir self.auth_db_path = os.path.join(basedir, "auth.db") self.collection_path = os.path.join(basedir, "collections") self.user_manager = SqliteUserManager(self.auth_db_path, self.collection_path) def tearDown(self): shutil.rmtree(self.basedir) self.user_manager = None def test_auth_db_exists(self): self.assertFalse(self.user_manager.auth_db_exists()) self.user_manager.create_auth_db() self.assertTrue(self.user_manager.auth_db_exists()) os.unlink(self.auth_db_path) self.assertFalse(self.user_manager.auth_db_exists()) def test_user_list(self): username = "******" password = "******" self.user_manager.create_auth_db() self.assertEqual(self.user_manager.user_list(), []) self.user_manager.add_user(username, password) self.assertEqual(self.user_manager.user_list(), [username]) def test_user_exists(self): username = "******" password = "******" self.user_manager.create_auth_db() self.user_manager.add_user(username, password) self.assertTrue(self.user_manager.user_exists(username)) self.user_manager.del_user(username) self.assertFalse(self.user_manager.user_exists(username)) def test_del_user(self): username = "******" password = "******" collection_dir_path = os.path.join(self.collection_path, username) self.user_manager.create_auth_db() self.user_manager.add_user(username, password) self.user_manager.del_user(username) # User should be gone. self.assertFalse(self.user_manager.user_exists(username)) # User's collection dir should still be there. self.assertTrue(os.path.isdir(collection_dir_path)) def test_add_user(self): username = "******" password = "******" expected_dir_path = os.path.join(self.collection_path, username) self.user_manager.create_auth_db() self.assertFalse(os.path.exists(expected_dir_path)) self.user_manager.add_user(username, password) # User db entry and collection dir should be present. self.assertTrue(self.user_manager.user_exists(username)) self.assertTrue(os.path.isdir(expected_dir_path)) def test_add_users(self): users_data = [("my_first_username", "my_first_password"), ("my_second_username", "my_second_password")] self.user_manager.create_auth_db() self.user_manager.add_users(users_data) user_list = self.user_manager.user_list() self.assertIn("my_first_username", user_list) self.assertIn("my_second_username", user_list) self.assertTrue( os.path.isdir( os.path.join(self.collection_path, "my_first_username"))) self.assertTrue( os.path.isdir( os.path.join(self.collection_path, "my_second_username"))) def test__add_user_to_auth_db(self): username = "******" password = "******" self.user_manager.create_auth_db() self.user_manager.add_user(username, password) self.assertTrue(self.user_manager.user_exists(username)) def test_create_auth_db(self): self.assertFalse(os.path.exists(self.auth_db_path)) self.user_manager.create_auth_db() self.assertTrue(os.path.isfile(self.auth_db_path)) def test__create_user_dir(self): username = "******" expected_dir_path = os.path.join(self.collection_path, username) self.assertFalse(os.path.exists(expected_dir_path)) self.user_manager._create_user_dir(username) self.assertTrue(os.path.isdir(expected_dir_path)) def test_authenticate(self): username = "******" password = "******" self.user_manager.create_auth_db() self.user_manager.add_user(username, password) self.assertTrue(self.user_manager.authenticate(username, password)) def test_set_password_for_user(self): username = "******" password = "******" new_password = "******" self.user_manager.create_auth_db() self.user_manager.add_user(username, password) self.user_manager.set_password_for_user(username, new_password) self.assertFalse(self.user_manager.authenticate(username, password)) self.assertTrue(self.user_manager.authenticate(username, new_password))
class SqliteUserManagerTest(unittest.TestCase): def setUp(self): basedir = tempfile.mkdtemp(prefix=self.__class__.__name__) self.basedir = basedir self.auth_db_path = os.path.join(basedir, "auth.db") self.collection_path = os.path.join(basedir, "collections") self.user_manager = SqliteUserManager(self.auth_db_path, self.collection_path) def tearDown(self): shutil.rmtree(self.basedir) self.user_manager = None def test_auth_db_exists(self): self.assertFalse(self.user_manager.auth_db_exists()) self.user_manager.create_auth_db() self.assertTrue(self.user_manager.auth_db_exists()) os.unlink(self.auth_db_path) self.assertFalse(self.user_manager.auth_db_exists()) def test_user_list(self): username = "******" password = "******" self.user_manager.create_auth_db() self.assertEqual(self.user_manager.user_list(), []) self.user_manager.add_user(username, password) self.assertEqual(self.user_manager.user_list(), [username]) def test_user_exists(self): username = "******" password = "******" self.user_manager.create_auth_db() self.user_manager.add_user(username, password) self.assertTrue(self.user_manager.user_exists(username)) self.user_manager.del_user(username) self.assertFalse(self.user_manager.user_exists(username)) def test_del_user(self): username = "******" password = "******" collection_dir_path = os.path.join(self.collection_path, username) self.user_manager.create_auth_db() self.user_manager.add_user(username, password) self.user_manager.del_user(username) # User should be gone. self.assertFalse(self.user_manager.user_exists(username)) # User's collection dir should still be there. self.assertTrue(os.path.isdir(collection_dir_path)) def test_add_user(self): username = "******" password = "******" expected_dir_path = os.path.join(self.collection_path, username) self.user_manager.create_auth_db() self.assertFalse(os.path.exists(expected_dir_path)) self.user_manager.add_user(username, password) # User db entry and collection dir should be present. self.assertTrue(self.user_manager.user_exists(username)) self.assertTrue(os.path.isdir(expected_dir_path)) def test_add_users(self): users_data = [("my_first_username", "my_first_password"), ("my_second_username", "my_second_password")] self.user_manager.create_auth_db() self.user_manager.add_users(users_data) user_list = self.user_manager.user_list() self.assertIn("my_first_username", user_list) self.assertIn("my_second_username", user_list) self.assertTrue(os.path.isdir(os.path.join(self.collection_path, "my_first_username"))) self.assertTrue(os.path.isdir(os.path.join(self.collection_path, "my_second_username"))) def test__add_user_to_auth_db(self): username = "******" password = "******" self.user_manager.create_auth_db() self.user_manager.add_user(username, password) self.assertTrue(self.user_manager.user_exists(username)) def test_create_auth_db(self): self.assertFalse(os.path.exists(self.auth_db_path)) self.user_manager.create_auth_db() self.assertTrue(os.path.isfile(self.auth_db_path)) def test__create_user_dir(self): username = "******" expected_dir_path = os.path.join(self.collection_path, username) self.assertFalse(os.path.exists(expected_dir_path)) self.user_manager._create_user_dir(username) self.assertTrue(os.path.isdir(expected_dir_path)) def test_authenticate(self): username = "******" password = "******" self.user_manager.create_auth_db() self.user_manager.add_user(username, password) self.assertTrue(self.user_manager.authenticate(username, password)) def test_set_password_for_user(self): username = "******" password = "******" new_password = "******" self.user_manager.create_auth_db() self.user_manager.add_user(username, password) self.user_manager.set_password_for_user(username, new_password) self.assertFalse(self.user_manager.authenticate(username, password)) self.assertTrue(self.user_manager.authenticate(username, new_password))