Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions authserver/mailauth/management/commands/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ def _create(self, **kwargs: Any) -> None:
def _list(self, **kwargs: Any) -> None:
clients = [] # type: List[oauth2_models.Application]
if kwargs["search_client_id"]:
try:
clients = list(appmodel.objects.filter(client_id__ilike=kwargs["search_client_id"]))
except appmodel.DoesNotExist:
clients = list(appmodel.objects.filter(client_id__icontains=kwargs["search_client_id"]))
if not clients:
self.stderr.write(self.style.ERROR("Client ID not found %s" % kwargs["search_client_id"]))
return

elif kwargs["search_client_name"]:
try:
clients = list(appmodel.objects.file(name__ilike=kwargs["search_client_name"]))
except appmodel.DoesNotExist:
clients = list(appmodel.objects.filter(name__icontains=kwargs["search_client_name"]))
if not clients:
self.stderr.write(self.style.ERROR("Client name not found %s" % kwargs["search_client_name"]))
return

else:
clients = list(appmodel.objects.all())
Expand Down
37 changes: 37 additions & 0 deletions authserver/mailauth/tests/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,40 @@ def test_list_can_filter_by_name_in_json_output(self) -> None:
payload = json.loads(out.getvalue())
self.assertEqual(1, len(payload))
self.assertEqual(self.permission.permission_name, payload[0]["permission_name"])


class OAuth2CommandTests(TestCase):
def setUp(self) -> None:
self.app = models.MNApplication.objects.create(
name="testclient",
client_type=models.MNApplication.CLIENT_CONFIDENTIAL,
authorization_grant_type=models.MNApplication.GRANT_AUTHORIZATION_CODE,
redirect_uris="https://client.example.com/callback",
)

def test_list_all(self) -> None:
out = StringIO()
with redirect_stdout(out):
call_command("oauth2", "list")
self.assertIn("testclient", out.getvalue())
self.assertIn(self.app.client_id, out.getvalue())

def test_list_search_by_client_name(self) -> None:
out = StringIO()
with redirect_stdout(out):
call_command("oauth2", "list", "--search-client-name", "testclient")
self.assertIn(self.app.client_id, out.getvalue())

def test_list_search_by_client_id(self) -> None:
out = StringIO()
with redirect_stdout(out):
call_command("oauth2", "list", "--search-client-id", self.app.client_id)
self.assertIn("testclient", out.getvalue())

def test_list_search_by_client_name_not_found(self) -> None:
out = StringIO()
err = StringIO()
with redirect_stdout(out), redirect_stderr(err):
call_command("oauth2", "list", "--search-client-name", "does-not-exist")
self.assertNotIn(self.app.client_id, out.getvalue())
self.assertIn("Client name not found", err.getvalue())
Loading