def deluser(username): user_manager = SqliteUserManager(AUTHDBPATH, COLLECTIONPATH) try: user_manager.del_user(username) except ValueError as error: print("Could not delete user {}: {}".format(username, error), file=sys.stderr)
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 setUp(self): monkeypatch_db() # Create temporary files and dirs the server will use. self.server_paths = helpers.server_utils.create_server_paths() # Add a test user to the temp auth db the server will use. self.user_manager = SqliteUserManager( self.server_paths['auth_db_path'], self.server_paths['data_root']) self.user_manager.add_user('testuser', 'testpassword') # Get absolute path to development ini file. script_dir = os.path.dirname(os.path.realpath(__file__)) ini_file_path = os.path.join(script_dir, "assets", "test.conf") # Create SyncApp instance using the dev ini file and the temporary # paths. self.server_app = helpers.server_utils.create_sync_app( self.server_paths, ini_file_path) # Wrap the SyncApp object in TestApp instance for testing. self.server_test_app = TestApp(self.server_app) # MockRemoteServer instance needed for testing normal collection # syncing and for retrieving hkey for other tests. self.mock_remote_server = MockRemoteServer( hkey=None, server_test_app=self.server_test_app)
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 adduser(username): if username: password = getpass.getpass("Enter password for {}: ".format(username)) user_manager = SqliteUserManager(AUTHDBPATH, COLLECTIONPATH) user_manager.add_user(username, password) else: usage()
class SyncAppFunctionalTestBase(unittest.TestCase): @classmethod def setUpClass(cls): cls.colutils = CollectionUtils() @classmethod def tearDownClass(cls): cls.colutils.clean_up() cls.colutils = None def setUp(self): monkeypatch_db() # Create temporary files and dirs the server will use. self.server_paths = server_utils.create_server_paths() # Add a test user to the temp auth db the server will use. self.user_manager = SqliteUserManager( self.server_paths["auth_db_path"], self.server_paths["data_root"] ) self.user_manager.add_user("testuser", "testpassword") # Get absolute path to development ini file. script_dir = os.path.dirname(os.path.realpath(__file__)) ini_file_path = os.path.join(script_dir, "assets", "test.conf") # Create SyncApp instance using the dev ini file and the temporary # paths. self.config = helpers.server_utils.create_config( self.server_paths, ini_file_path ) self.sync_app = SyncApp(self.config["sync_app"]) self.flask_app = create_app(sync_app=self.sync_app, config=self.config) self.flask_app.config["TESTING"] = True # Wrap the SyncApp object in TestApp instance for testing. self.server_test_app = TestApp(self.flask_app) # MockRemoteServer instance needed for testing normal collection # syncing and for retrieving hkey for other tests. self.mock_remote_server = MockRemoteServer( hkey=None, server_test_app=self.server_test_app ) def tearDown(self): self.server_paths = {} self.user_manager = None # Shut down server. self.sync_app.collection_manager.shutdown() self.flask_app = None self.sync_app = None self.client_server_connection = None unpatch_db()
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)
def passwd(username): if os.path.isfile(AUTHDBPATH): password = getpass.getpass("Enter password for {}: ".format(username)) user_manager = SqliteUserManager(AUTHDBPATH, COLLECTIONPATH) try: user_manager.set_password_for_user(username, password) except ValueError as error: print("Could not set password for user {}: {}".format( username, error.message), file=sys.stderr) else: print("{}: Database file does not exist".format(sys.argv[0]), file=sys.stderr)
def deluser(username): if username and os.path.isfile(AUTHDBPATH): user_manager = SqliteUserManager(AUTHDBPATH, COLLECTIONPATH) try: user_manager.del_user(username) except ValueError as error: print("Could not delete user {}: {}".format( username, error.message), file=sys.stderr) elif not username: usage() else: print("{}: Database file does not exist".format(sys.argv[0]), file=sys.stderr)
def __init__(self, config): from ankisyncd.thread import getCollectionManager self.data_root = os.path.abspath(config['data_root']) self.base_url = config['base_url'] self.base_media_url = config['base_media_url'] self.setup_new_collection = None self.prehooks = {} self.posthooks = {} if "session_db_path" in config: self.session_manager = SqliteSessionManager( config['session_db_path']) else: self.session_manager = SimpleSessionManager() if "auth_db_path" in config: self.user_manager = SqliteUserManager(config['auth_db_path']) else: logging.warn( "auth_db_path not set, ankisyncd will accept any password") self.user_manager = SimpleUserManager() self.collection_manager = getCollectionManager() # make sure the base_url has a trailing slash if not self.base_url.endswith('/'): self.base_url += '/' if not self.base_media_url.endswith('/'): self.base_media_url += '/'
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 setup_method(self, method): monkeypatch_db() # Create temporary files and dirs the server will use. self.server_paths = create_server_paths() # Add a test user to the temp auth db the server will use. self.user_manager = SqliteUserManager( self.server_paths["auth_db_path"], self.server_paths["data_root"]) self.user_manager.add_user("testuser", "testpassword") # Get absolute path to development ini file. script_dir = os.path.dirname(os.path.realpath(__file__)) ini_file_path = os.path.join(script_dir, "assets", "test.conf") self.config = create_config(self.server_paths, ini_file_path) self.sync_app = SyncApp(self.config["sync_app"]) self.flask_app = create_app(self.config, self.sync_app, user_manager=self.user_manager) self.flask_app.config["TESTING"] = True self.client = self.flask_app.test_client()
def __init__(self, config): from ankisyncd.thread import getCollectionManager self.data_root = os.path.abspath(config['data_root']) self.base_url = config['base_url'] self.base_media_url = config['base_media_url'] self.setup_new_collection = None self.prehooks = {} self.posthooks = {} if "session_db_path" in config: self.session_manager = SqliteSessionManager( config['session_db_path']) else: self.session_manager = SimpleSessionManager() if "auth_db_path" in config: self.user_manager = SqliteUserManager(config['auth_db_path']) else: logging.warn( "auth_db_path not set, ankisyncd will accept any password") self.user_manager = SimpleUserManager() self.collection_manager = getCollectionManager() # make sure the base_url has a trailing slash if not self.base_url.endswith('/'): self.base_url += '/' if not self.base_media_url.endswith('/'): self.base_media_url += '/' # convert base URLs to regexes, to handle Anki desktop's hostNum protocol self.base_url = re.compile(r'%s(?P<path>.*)' % self.base_url.replace('${hostNum}', r'\d*')) self.base_media_url = re.compile( r'%s(?P<path>.*)' % self.base_media_url.replace('${hostNum}', r'\d*'))
class TestAPI: @classmethod def setup_class(cls): cls.colutils = CollectionUtils() @classmethod def teardown_class(cls): cls.colutils.clean_up() cls.colutils = None def setup_method(self, method): monkeypatch_db() # Create temporary files and dirs the server will use. self.server_paths = create_server_paths() # Add a test user to the temp auth db the server will use. self.user_manager = SqliteUserManager( self.server_paths["auth_db_path"], self.server_paths["data_root"]) self.user_manager.add_user("testuser", "testpassword") # Get absolute path to development ini file. script_dir = os.path.dirname(os.path.realpath(__file__)) ini_file_path = os.path.join(script_dir, "assets", "test.conf") self.config = create_config(self.server_paths, ini_file_path) self.sync_app = SyncApp(self.config["sync_app"]) self.flask_app = create_app(self.config, self.sync_app, user_manager=self.user_manager) self.flask_app.config["TESTING"] = True self.client = self.flask_app.test_client() def teardown_method(self): self.server_paths = {} self.user_manager = None # Shut down server. self.sync_app.collection_manager.shutdown() self.flask_app = None self.sync_app = None self.client_server_connection = None unpatch_db() def test_no_login(self): rv = self.client.get("/api/decks") assert rv.status_code == 401 assert json.loads(rv.data)["error"] == "Unauthorized" def test_good_login(self): header_value = "Basic %s" % base64.b64encode(b"testusername" + b":" + b"testpassword") rv = self.client.open("/api/decks", method="GET", headers={"Authorization": header_value}) assert rv.status_code == 401 assert json.loads(rv.data)["error"] == "Unauthorized" def test_bad_password(self): header_value = "Basic %s" % base64.b64encode(b"testusername" + b":" + b"badpassword") rv = self.client.open("/api/decks", method="GET", headers={"Authorization": header_value}) assert rv.status_code == 401 assert json.loads(rv.data)["error"] == "Unauthorized"
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))