From 23726002a3caae8dca11f6a4c7541cc065c06dac Mon Sep 17 00:00:00 2001
From: Julian Rother <julian@cccv.de>
Date: Tue, 29 Mar 2022 17:34:06 +0200
Subject: [PATCH] Fix group/role update command clearing description

The group and role update subcommands set the description to an empty string
if the "--description" option was ommitted.

Fixes #156
---
 tests/test_role.py     | 12 ++++++++++++
 tests/test_user.py     |  7 +++++++
 uffd/role/cli.py       |  2 +-
 uffd/user/cli_group.py |  2 +-
 4 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/tests/test_role.py b/tests/test_role.py
index 7186f790..6fe4125a 100644
--- a/tests/test_role.py
+++ b/tests/test_role.py
@@ -398,6 +398,18 @@ class TestRoleCLI(UffdTestCase):
 			self.assertEqual(role.is_default, True)
 			self.assertEqual(set(self.get_user().groups), {self.get_access_group()})
 
+	# Regression test for https://git.cccv.de/uffd/uffd/-/issues/156
+	def test_update_without_description(self):
+		with self.app.test_request_context():
+			role = Role.query.filter_by(name='test').first()
+			role.description = 'Test description'
+			db.session.commit()
+		result = self.app.test_cli_runner().invoke(args=['role', 'update', 'test', '--clear-groups'])
+		self.assertEqual(result.exit_code, 0)
+		with self.app.test_request_context():
+			role = Role.query.filter_by(name='test').first()
+			self.assertEqual(role.description, 'Test description')
+
 	def test_delete(self):
 		with self.app.test_request_context():
 			self.assertIsNotNone(Role.query.filter_by(name='test').first())
diff --git a/tests/test_user.py b/tests/test_user.py
index d641eb22..6a92ab6b 100644
--- a/tests/test_user.py
+++ b/tests/test_user.py
@@ -715,6 +715,13 @@ class TestGroupCLI(UffdTestCase):
 			group = Group.query.filter_by(name='users').first()
 			self.assertEqual(group.description, 'New description')
 
+	def test_update_without_description(self):
+		result = self.app.test_cli_runner().invoke(args=['group', 'update', 'users']) # Should not change anything
+		self.assertEqual(result.exit_code, 0)
+		with self.app.test_request_context():
+			group = Group.query.filter_by(name='users').first()
+			self.assertEqual(group.description, 'Base group for all users')
+
 	def test_delete(self):
 		with self.app.test_request_context():
 			self.assertIsNotNone(Group.query.filter_by(name='users').first())
diff --git a/uffd/role/cli.py b/uffd/role/cli.py
index 54d69c35..6e94f5f1 100644
--- a/uffd/role/cli.py
+++ b/uffd/role/cli.py
@@ -101,7 +101,7 @@ def create(name, description, default, moderator_group, add_group, add_role):
 
 @role_cli.command(help='Update role attributes')
 @click.argument('name')
-@click.option('--description', default='', help='Set description text.')
+@click.option('--description', help='Set description text.')
 @click.option('--default/--no-default', default=None, help='Mark role as default or not. Non-service users are auto-added to default roles.')
 @click.option('--moderator-group', metavar='GROUP_NAME', help='Set moderator group.')
 @click.option('--no-moderator-group', is_flag=True, flag_value=True, default=False, help='Clear moderator group setting.')
diff --git a/uffd/user/cli_group.py b/uffd/user/cli_group.py
index 3627f0a0..0dd80e53 100644
--- a/uffd/user/cli_group.py
+++ b/uffd/user/cli_group.py
@@ -48,7 +48,7 @@ def create(name, description):
 
 @group_cli.command(help='Update group attributes')
 @click.argument('name')
-@click.option('--description', default='', help='Set description text.')
+@click.option('--description', help='Set description text.')
 def update(name, description):
 	with current_app.test_request_context():
 		group = Group.query.filter_by(name=name).one_or_none()
-- 
GitLab