diff --git a/supysonic/cli.py b/supysonic/cli.py index 75c2db6..8def64e 100755 --- a/supysonic/cli.py +++ b/supysonic/cli.py @@ -224,13 +224,11 @@ class SupysonicCLI(cmd.Cmd): self.write_error_line(UserManager.error_str(status)) def user_delete(self, name): - user = self.__store.find(User, User.name == name).one() - if not user: - self.write_error_line('No such user') + ret = UserManager.delete_by_name(self.__store, name) + if ret != UserManager.SUCCESS: + self.write_error_line(UserManager.error_str(ret)) else: - self.__store.remove(user) - self.__store.commit() - self.write_line("User '{}' deleted".format(name)) + self.write_line("Deleted user '{}'".format(name)) def user_setadmin(self, name, off): user = self.__store.find(User, User.name == name).one() diff --git a/supysonic/managers/user.py b/supysonic/managers/user.py index e9042c1..5e43d44 100644 --- a/supysonic/managers/user.py +++ b/supysonic/managers/user.py @@ -83,6 +83,13 @@ class UserManager: return UserManager.SUCCESS + @staticmethod + def delete_by_name(store, name): + user = store.find(User, User.name == name).one() + if not user: + return UserManager.NO_SUCH_USER + return UserManager.delete(store, user.id) + @staticmethod def try_auth(store, name, password): user = store.find(User, User.name == name).one() diff --git a/tests/managers/test_manager_user.py b/tests/managers/test_manager_user.py index a50ea32..daf282c 100644 --- a/tests/managers/test_manager_user.py +++ b/tests/managers/test_manager_user.py @@ -112,6 +112,15 @@ class UserManagerTestCase(unittest.TestCase): self.assertIsNone(self.store.get(db.User, user.id)) self.assertEqual(self.store.find(db.User).count(), 0) + def test_delete_by_name(self): + # Delete existing users + for name in ['alice', 'bob', 'charlie']: + self.assertEqual(UserManager.delete_by_name(self.store, name), UserManager.SUCCESS) + self.assertEqual(self.store.find(db.User, db.User.name == name).count(), 0) + + # Delete non-existent user + self.assertEqual(UserManager.delete_by_name(self.store, 'null'), UserManager.NO_SUCH_USER) + def test_try_auth(self): # Test authentication for name in ['alice', 'bob', 'charlie']: