Skip to content

Commit 7ce9d1b

Browse files
fix: revert changes in non-relevant files
1 parent d222ea7 commit 7ce9d1b

4 files changed

Lines changed: 310 additions & 556 deletions

File tree

openedx_authz/management/commands/enforcement.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ def handle(self, *args, **options):
7878
Raises:
7979
CommandError: If model or policy files are not found or enforcer creation fails.
8080
"""
81-
model_file_path = (
82-
self._get_file_path("model.conf") or options["model_file_path"]
83-
)
81+
model_file_path = self._get_file_path("model.conf") or options["model_file_path"]
8482
policy_file_path = options["policy_file_path"]
8583

8684
if not os.path.isfile(model_file_path):
@@ -95,9 +93,7 @@ def handle(self, *args, **options):
9593

9694
try:
9795
enforcer = casbin.Enforcer(model_file_path, policy_file_path)
98-
self.stdout.write(
99-
self.style.SUCCESS("Casbin enforcer created successfully")
100-
)
96+
self.stdout.write(self.style.SUCCESS("Casbin enforcer created successfully"))
10197

10298
policies = enforcer.get_policy()
10399
roles = enforcer.get_grouping_policy()
@@ -160,9 +156,7 @@ def _run_interactive_mode(self, enforcer: casbin.Enforcer) -> None:
160156
self.stdout.write(self.style.ERROR("Exiting interactive mode..."))
161157
break
162158

163-
def _test_interactive_request(
164-
self, enforcer: casbin.Enforcer, user_input: str
165-
) -> None:
159+
def _test_interactive_request(self, enforcer: casbin.Enforcer, user_input: str) -> None:
166160
"""Process and test a single enforcement request from user input.
167161
168162
Parses the input string, validates the format, executes the enforcement
@@ -180,11 +174,7 @@ def _test_interactive_request(
180174
try:
181175
parts = [part.strip() for part in user_input.split()]
182176
if len(parts) != 3:
183-
self.stdout.write(
184-
self.style.ERROR(
185-
f"✗ Invalid format. Expected 3 parts, got {len(parts)}"
186-
)
187-
)
177+
self.stdout.write(self.style.ERROR(f"✗ Invalid format. Expected 3 parts, got {len(parts)}"))
188178
self.stdout.write("Format: subject action scope")
189179
self.stdout.write("Example: user:alice act:read org:OpenedX")
190180
return
@@ -193,13 +183,9 @@ def _test_interactive_request(
193183
result = enforcer.enforce(subject, action, scope)
194184

195185
if result:
196-
self.stdout.write(
197-
self.style.SUCCESS(f"✓ ALLOWED: {subject} {action} {scope}")
198-
)
186+
self.stdout.write(self.style.SUCCESS(f"✓ ALLOWED: {subject} {action} {scope}"))
199187
else:
200-
self.stdout.write(
201-
self.style.ERROR(f"✗ DENIED: {subject} {action} {scope}")
202-
)
188+
self.stdout.write(self.style.ERROR(f"✗ DENIED: {subject} {action} {scope}"))
203189

204190
except (ValueError, IndexError, TypeError) as e:
205191
self.stdout.write(self.style.ERROR(f"✗ Error processing request: {str(e)}"))

openedx_authz/tests/test_commands.py

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ def test_requires_policy_file_argument(self):
4141
with self.assertRaises(CommandError) as ctx:
4242
call_command("enforcement")
4343

44-
self.assertEqual(
45-
"Error: the following arguments are required: --policy-file-path",
46-
str(ctx.exception),
47-
)
44+
self.assertEqual("Error: the following arguments are required: --policy-file-path", str(ctx.exception))
4845

4946
def test_policy_file_not_found_raises(self):
5047
"""Test that command errors when the provided policy file does not exist."""
@@ -55,18 +52,13 @@ def test_policy_file_not_found_raises(self):
5552

5653
self.assertEqual(f"Policy file not found: {non_existent}", str(ctx.exception))
5754

58-
@patch.object(
59-
EnforcementCommand, "_get_file_path", return_value="invalid/path/model.conf"
60-
)
55+
@patch.object(EnforcementCommand, "_get_file_path", return_value="invalid/path/model.conf")
6156
def test_model_file_not_found_raises(self, mock_get_file_path: Mock):
6257
"""Test that command errors when the provided model file does not exist."""
6358
with self.assertRaises(CommandError) as ctx:
6459
call_command("enforcement", policy_file_path=self.policy_file_path.name)
6560

66-
self.assertEqual(
67-
f"Model file not found: {mock_get_file_path.return_value}",
68-
str(ctx.exception),
69-
)
61+
self.assertEqual(f"Model file not found: {mock_get_file_path.return_value}", str(ctx.exception))
7062

7163
@patch("openedx_authz.management.commands.enforcement.casbin.Enforcer")
7264
def test_error_creating_enforcer_raises(self, mock_enforcer_cls: Mock):
@@ -76,16 +68,11 @@ def test_error_creating_enforcer_raises(self, mock_enforcer_cls: Mock):
7668
with self.assertRaises(CommandError) as ctx:
7769
call_command("enforcement", policy_file_path=self.policy_file_path.name)
7870

79-
self.assertEqual(
80-
"Error creating Casbin enforcer: Enforcer creation error",
81-
str(ctx.exception),
82-
)
71+
self.assertEqual("Error creating Casbin enforcer: Enforcer creation error", str(ctx.exception))
8372

8473
@patch("openedx_authz.management.commands.enforcement.casbin.Enforcer")
8574
@patch.object(EnforcementCommand, "_run_interactive_mode")
86-
def test_successful_run_prints_summary(
87-
self, mock_run_interactive: Mock, mock_enforcer_cls: Mock
88-
):
75+
def test_successful_run_prints_summary(self, mock_run_interactive: Mock, mock_enforcer_cls: Mock):
8976
"""
9077
Test successful command execution with policy file and interactive mode.
9178
When files exist, command should create enforcer, print counts, and call interactive loop.
@@ -102,11 +89,7 @@ def test_successful_run_prints_summary(
10289
mock_enforcer.get_named_grouping_policy.return_value = action_grouping
10390
mock_enforcer_cls.return_value = mock_enforcer
10491

105-
call_command(
106-
"enforcement",
107-
policy_file_path=self.policy_file_path.name,
108-
stdout=self.buffer,
109-
)
92+
call_command("enforcement", policy_file_path=self.policy_file_path.name, stdout=self.buffer)
11093

11194
output = self.buffer.getvalue()
11295
self.assertIn("Casbin Interactive Enforcement", output)
@@ -122,17 +105,10 @@ def test_run_interactive_mode_displays_help(self):
122105
self.command._run_interactive_mode(self.enforcer)
123106

124107
self.assertIn("Interactive Mode", self.buffer.getvalue())
125-
self.assertIn(
126-
"Test custom enforcement requests interactively.", self.buffer.getvalue()
127-
)
128-
self.assertIn(
129-
"Enter 'quit', 'exit', or 'q' to exit the interactive mode.",
130-
self.buffer.getvalue(),
131-
)
108+
self.assertIn("Test custom enforcement requests interactively.", self.buffer.getvalue())
109+
self.assertIn("Enter 'quit', 'exit', or 'q' to exit the interactive mode.", self.buffer.getvalue())
132110
self.assertIn("Format: subject action scope", self.buffer.getvalue())
133-
self.assertIn(
134-
"Example: user:alice act:read org:OpenedX", self.buffer.getvalue()
135-
)
111+
self.assertIn("Example: user:alice act:read org:OpenedX", self.buffer.getvalue())
136112

137113
def test_run_interactive_mode_maintains_interactive_loop(self):
138114
"""Test that the interactive mode maintains the interactive loop."""
@@ -211,9 +187,7 @@ def test_interactive_request_error(self, exception: Exception):
211187
"""Test that `_test_interactive_request` handles processing errors."""
212188
self.enforcer.enforce.side_effect = exception
213189

214-
self.command._test_interactive_request(
215-
self.enforcer, "user:alice act:read org:OpenedX"
216-
)
190+
self.command._test_interactive_request(self.enforcer, "user:alice act:read org:OpenedX")
217191

218192
error_output = self.buffer.getvalue()
219193
self.assertIn(f"✗ Error processing request: {str(exception)}", error_output)

0 commit comments

Comments
 (0)