Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxy apps command line interface #1003

Merged
merged 18 commits into from
Jan 6, 2024
173 changes: 173 additions & 0 deletions tests/unit_tests/test_tethys_cli/test_proxyapps_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
from tethys_apps.models import ProxyApp

from unittest import mock
from tethys_cli.proxyapps_commands import (
add_proxyapp,
update_proxyapp,
list_proxyapps,
)

import unittest


class TestProxyAppsCommand(unittest.TestCase):
def setUp(self):
self.app_name = "My_Proxy_App_for_Testing"
self.endpoint = "http://foo.example.com/my-proxy-app"
self.back_url = "http://bar.example.com/apps/"
self.logo = "http://foo.example.com/my-proxy-app/logo.png"
self.description = "This is an app that is not here."
self.tags = '"Water","Earth","Fire","Air"'
self.open_in_new_tab = True

self.proxy_app = ProxyApp(
name=self.app_name,
endpoint=self.endpoint,
logo_url=self.logo,
back_url=self.back_url,
description=self.description,
tags=self.tags,
open_in_new_tab=self.open_in_new_tab,
)
self.proxy_app.save()

def tearDown(self):
self.proxy_app.delete()

@mock.patch("tethys_cli.proxyapps_commands.write_info")
@mock.patch("tethys_cli.proxyapps_commands.print")
def test_list_proxy_apps(self, mock_print, mock_write_info):
mock_args = mock.Mock()
list_proxyapps(mock_args)
rts_call_args = mock_print.call_args_list
check_list = []

for i in range(len(rts_call_args)):
check_list.append(rts_call_args[i][0][0])

mock_write_info.assert_called_with("Proxy Apps:")
self.assertIn(" My_Proxy_App_for_Testing", check_list)

@mock.patch("tethys_cli.proxyapps_commands.write_error")
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_update_proxy_apps_no_app(self, mock_exit, mock_write_error):
mock_args = mock.Mock()
mock_args.proxy_app_name = "non_existing_proxy_app"
mock_args.proxy_app_key = "non_existing_key"
mock_args.proxy_app_key_value = "https://fake.com"

self.assertRaises(
SystemExit,
update_proxyapp,
mock_args,
)

mock_write_error.assert_called_with(
"Proxy app non_existing_proxy_app does not exits"
)
mock_exit.assert_called_with(1)

@mock.patch("tethys_cli.proxyapps_commands.write_error")
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_update_proxy_apps_no_correct_key(self, mock_exit, mock_write_error):
mock_args = mock.Mock()
mock_args.proxy_app_name = self.app_name
mock_args.proxy_app_key = "non_existing_key"
mock_args.proxy_app_key_value = "https://fake.com"

self.assertRaises(
SystemExit,
update_proxyapp,
mock_args,
)

mock_write_error.assert_called_with(
f"Attribute non_existing_key does not exists in Proxy app {self.app_name}"
)
mock_exit.assert_called_with(1)

@mock.patch("tethys_cli.proxyapps_commands.write_success")
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_update_proxy_apps(self, mock_exit, mock_write_success):
mock_args = mock.Mock()
mock_args.proxy_app_name = self.app_name
mock_args.proxy_app_key = "logo_url"
mock_args.proxy_app_key_value = "https://fake.com"
self.assertRaises(
SystemExit,
update_proxyapp,
mock_args,
)
mock_write_success.assert_called_with(f"Proxy app {self.app_name} was updated")
mock_exit.assert_called_with(0)

@mock.patch("tethys_cli.proxyapps_commands.write_error")
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_add_proxy_apps_with_existing_proxy_app(self, mock_exit, mock_write_error):
mock_args = mock.Mock()
mock_args.proxy_app_name = self.app_name
mock_args.proxy_app_endpoint = "http://foo.example.com/my-proxy-app"

self.assertRaises(
SystemExit,
add_proxyapp,
mock_args,
)
mock_write_error.assert_called_with(
f"There is already a proxy app with that name: {self.app_name}"
)
mock_exit.assert_called_with(1)

@mock.patch("tethys_cli.proxyapps_commands.write_error")
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_add_proxyapp_integrity_error(self, mock_exit, mock_write_error):
app_name_mock = "My_Proxy_App_for_Testing_2"
mock_args = mock.Mock()
mock_args.proxy_app_name = app_name_mock
mock_args.proxy_app_endpoint = "http://foo.example.com/my-proxy-app"
mock_args.proxy_app_description = None
mock_args.proxy_app_logo_url = None
mock_args.proxy_app_tags = None
mock_args.proxy_app_enabled = None
mock_args.proxy_app_show_in_apps_library = None
mock_args.proxy_app_back_url = None
mock_args.proxy_app_open_new_tab = None
mock_args.proxy_app_display_external_icon = None
mock_args.proxy_app_order = None

self.assertRaises(
SystemExit,
add_proxyapp,
mock_args,
)
mock_write_error.assert_called_with(
f"Not possible to add the proxy app: {app_name_mock}"
)
mock_exit.assert_called_with(1)

@mock.patch("tethys_cli.proxyapps_commands.write_success")
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_add_proxyapp_success(self, mock_exit, mock_write_success):
romer8 marked this conversation as resolved.
Show resolved Hide resolved
app_name_mock = "My_Proxy_App_for_Testing_2"
mock_args = mock.Mock()
mock_args.proxy_app_name = app_name_mock
mock_args.proxy_app_endpoint = "http://foo.example.com/my-proxy-app"
mock_args.proxy_app_description = ""
mock_args.proxy_app_logo_url = ""
mock_args.proxy_app_tags = ""
mock_args.proxy_app_enabled = True
mock_args.proxy_app_show_in_apps_library = True
mock_args.proxy_app_back_url = ""
mock_args.proxy_app_open_new_tab = True
mock_args.proxy_app_display_external_icon = False
mock_args.proxy_app_order = 0

self.assertRaises(
SystemExit,
add_proxyapp,
mock_args,
)
new_proxy_app = ProxyApp.objects.get(name=app_name_mock)
new_proxy_app.delete()
mock_write_success.assert_called_with(f"Proxy app {app_name_mock} added")
mock_exit.assert_called_with(0)
3 changes: 2 additions & 1 deletion tethys_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from tethys_cli.test_command import add_test_parser
from tethys_cli.install_commands import add_install_parser
from tethys_cli.uninstall_command import add_uninstall_parser
from tethys_cli.proxyapps_commands import add_proxyapps_parser


def tethys_command_parser():
Expand Down Expand Up @@ -55,7 +56,7 @@ def tethys_command_parser():
add_site_parser(subparsers)
add_syncstores_parser(subparsers)
add_test_parser(subparsers)

add_proxyapps_parser(subparsers)
return parser


Expand Down
186 changes: 186 additions & 0 deletions tethys_cli/proxyapps_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
from tethys_cli.cli_colors import (
write_error,
write_success,
write_info,
)

from tethys_cli.cli_helpers import setup_django

from django.db import IntegrityError


def add_proxyapps_parser(subparsers):
# Setup list command
proxyapps_parser = subparsers.add_parser(
"proxyapp", help="Add proxy apps and list proxy apps into the Tethys Platform"
)
proxyapps_subparsers = proxyapps_parser.add_subparsers(title="Options")

proxyapps_list_parser = proxyapps_subparsers.add_parser(
"list", help="list available proxy apps in the current tethys installation"
romer8 marked this conversation as resolved.
Show resolved Hide resolved
)
proxyapps_list_parser.set_defaults(func=list_proxyapps)

proxyapps_add_parser = proxyapps_subparsers.add_parser(
"add",
help="Add a new proxy app. Arguments: proxy_app_name endpoint [description] [logo_url] [tags] [enabled] [show_in_apps_library] [back_url] [open_new_tab] [display_external_icon] [app_order]",
romer8 marked this conversation as resolved.
Show resolved Hide resolved
)
proxyapps_add_parser.add_argument(
"proxy_app_name",
romer8 marked this conversation as resolved.
Show resolved Hide resolved
help="The proxy app name",
swainn marked this conversation as resolved.
Show resolved Hide resolved
)
proxyapps_add_parser.add_argument(
"proxy_app_endpoint",
help="The proxy app endpoint",
)
proxyapps_add_parser.add_argument(
"proxy_app_description", help="The proxy app description", nargs="?", default=""
)
proxyapps_add_parser.add_argument(
"proxy_app_logo_url", help="The proxy app logo url", nargs="?", default=""
romer8 marked this conversation as resolved.
Show resolved Hide resolved
)

proxyapps_add_parser.add_argument(
"proxy_app_tags", help="The proxy app tags", nargs="?", default=""
)
proxyapps_add_parser.add_argument(
"proxy_app_enabled",
help="Defines if the proxy app is enabled or not",
nargs="?",
default=True,
)
proxyapps_add_parser.add_argument(
"proxy_app_show_in_apps_library",
help="Defines if the proxy app is enabled or not",
nargs="?",
default=True,
)
proxyapps_add_parser.add_argument(
"proxy_app_back_url",
help="Defines a custom back url for the proxy app",
nargs="?",
default="",
)
proxyapps_add_parser.add_argument(
"proxy_app_open_new_tab",
help="Defines if the proxy app opens in a new tab",
nargs="?",
default=True,
)
proxyapps_add_parser.add_argument(
"proxy_app_display_external_icon",
help="Defines if the proxy app opens in a new tab",
romer8 marked this conversation as resolved.
Show resolved Hide resolved
nargs="?",
default=False,
)
proxyapps_add_parser.add_argument(
"proxy_app_order",
help="Defines if the proxy app opens in a new tab",
romer8 marked this conversation as resolved.
Show resolved Hide resolved
nargs="?",
default=0,
)

proxyapps_add_parser.set_defaults(func=add_proxyapp)

proxyapps_update_parser = proxyapps_subparsers.add_parser(
"update",
help="Update a new proxy app. Arguments: proxy_app_name key_value new_value",
)
proxyapps_update_parser.add_argument(
"proxy_app_name",
help="The proxy app name",
)
proxyapps_update_parser.add_argument(
"proxy_app_key",
help="The proxy app key that needs to be changed: [name] [endpoint] [description] [logo_url] [tags] [enabled] [show_in_apps_library] [back_url] [open_new_tab] [display_external_icon] [app_order] ",
)
proxyapps_update_parser.add_argument(
"proxy_app_key_value",
help="Th new value for the proxy app's key",
)
romer8 marked this conversation as resolved.
Show resolved Hide resolved

proxyapps_update_parser.set_defaults(func=update_proxyapp)


def list_proxyapps(args):
setup_django()
from tethys_apps.models import ProxyApp

proxy_apps = ProxyApp.objects.all()

write_info("Proxy Apps:")
for proxy_app in proxy_apps:
print(f" {proxy_app.name}")
romer8 marked this conversation as resolved.
Show resolved Hide resolved


def update_proxyapp(args):
setup_django()
from tethys_apps.models import ProxyApp

app_name = args.proxy_app_name
app_key = args.proxy_app_key
app_value = args.proxy_app_key_value

try:
proxy_app = ProxyApp.objects.get(name=app_name)
# breakpoint()
romer8 marked this conversation as resolved.
Show resolved Hide resolved
if not hasattr(proxy_app, app_key):
write_error(f"Attribute {app_key} does not exists in Proxy app {app_name}")
romer8 marked this conversation as resolved.
Show resolved Hide resolved
exit(1)

setattr(proxy_app, app_key, app_value)
proxy_app.save()
write_success(f"Proxy app {app_name} was updated")
romer8 marked this conversation as resolved.
Show resolved Hide resolved
exit(0)

except ProxyApp.DoesNotExist:
write_error(f"Proxy app {app_name} does not exits")
romer8 marked this conversation as resolved.
Show resolved Hide resolved
exit(1)


def add_proxyapp(args):
"""
Add Proxy app
"""
setup_django()

from tethys_apps.models import ProxyApp

app_name = args.proxy_app_name
app_endpoint = args.proxy_app_endpoint
app_description = args.proxy_app_description
app_logo_url = args.proxy_app_logo_url
app_tags = args.proxy_app_tags
app_enabled = args.proxy_app_enabled
app_show_in_app_library = args.proxy_app_show_in_apps_library
app_back_url = args.proxy_app_back_url
app_open_new_tab = args.proxy_app_open_new_tab
app_display_external_icon = args.proxy_app_display_external_icon
app_order = args.proxy_app_order
try:
proxy_app = ProxyApp.objects.get(name=app_name)
write_error(f"There is already a proxy app with that name: {app_name}")
exit(1)

except ProxyApp.DoesNotExist:
try:
proxy_app = ProxyApp(
name=app_name,
endpoint=app_endpoint,
logo_url=app_logo_url,
back_url=app_back_url,
description=app_description,
tags=app_tags,
show_in_apps_library=app_show_in_app_library,
enabled=app_enabled,
open_in_new_tab=app_open_new_tab,
display_external_icon=app_display_external_icon,
order=app_order,
)

proxy_app.save()
write_success(f"Proxy app {app_name} added")
exit(0)
except IntegrityError:
write_error(f"Not possible to add the proxy app: {app_name}")
romer8 marked this conversation as resolved.
Show resolved Hide resolved
exit(1)
Loading