|
| 1 | +import io |
| 2 | +import re |
| 3 | +from functools import wraps |
| 4 | +from typing import Any, Callable, Dict, List, Optional, Union |
| 5 | +from unittest import TestCase |
| 6 | +from unittest.mock import DEFAULT, patch |
| 7 | + |
| 8 | +from integrations.clickup import zulip_clickup |
| 9 | +from integrations.clickup.zulip_clickup import ClickUpAPIHandler |
| 10 | + |
| 11 | +MOCK_WEBHOOK_URL = ( |
| 12 | + "https://YourZulipApp.com/api/v1/external/clickup?api_key=TJ9DnIiNqt51bpfyPll5n2uT4iYxMBW9" |
| 13 | +) |
| 14 | + |
| 15 | +MOCK_AUTH_CODE = "332KKA3321NNAK3MADS" |
| 16 | +MOCK_AUTH_CODE_URL = f"https://YourZulipApp.com/?code={MOCK_AUTH_CODE}" |
| 17 | +MOCK_API_KEY = "X" * 32 |
| 18 | + |
| 19 | +SCRIPT_PATH = "integrations.clickup.zulip_clickup" |
| 20 | + |
| 21 | +MOCK_CREATED_WEBHOOK_ID = "13-13-13-13-1313-13" |
| 22 | +MOCK_DELETE_WEBHOOK_ID = "12-12-12-12-12" |
| 23 | +MOCK_GET_WEBHOOK_IDS = {"endpoint": MOCK_WEBHOOK_URL, "id": MOCK_DELETE_WEBHOOK_ID} |
| 24 | + |
| 25 | +CLICKUP_TEAM_ID = "teamid123" |
| 26 | +CLICKUP_CLIENT_ID = "clientid321" |
| 27 | +CLICKUP_CLIENT_SECRET = "clientsecret322" # noqa: S105 |
| 28 | + |
| 29 | + |
| 30 | +def make_clickup_request_side_effect( |
| 31 | + path: str, query: Dict[str, Union[str, List[str]]], method: str |
| 32 | +) -> Optional[Dict[str, Any]]: |
| 33 | + clickup_api = ClickUpAPIHandler(CLICKUP_CLIENT_ID, CLICKUP_CLIENT_SECRET, CLICKUP_TEAM_ID) |
| 34 | + api_data_mapper: Dict[str, Dict[str, Dict[str, Any]]] = { # path -> method -> response |
| 35 | + clickup_api.ENDPOINTS["oauth"]: { |
| 36 | + "POST": {"access_token": MOCK_API_KEY}, |
| 37 | + }, |
| 38 | + clickup_api.ENDPOINTS["team"]: { |
| 39 | + "POST": {"id": MOCK_CREATED_WEBHOOK_ID}, |
| 40 | + "GET": {"webhooks": [MOCK_GET_WEBHOOK_IDS]}, |
| 41 | + }, |
| 42 | + clickup_api.ENDPOINTS["webhook"].format(webhook_id=MOCK_DELETE_WEBHOOK_ID): {"DELETE": {}}, |
| 43 | + } |
| 44 | + return api_data_mapper.get(path, {}).get(method, DEFAULT) |
| 45 | + |
| 46 | + |
| 47 | +def mock_script_args() -> Callable[[Any], Callable[..., Any]]: |
| 48 | + def decorator(func: Callable[..., Any]) -> Callable[..., Any]: |
| 49 | + @wraps(func) |
| 50 | + def wrapper(*args: Any, **kwargs: Any) -> Any: |
| 51 | + mock_user_inputs = [ |
| 52 | + MOCK_WEBHOOK_URL, # input for 1st step |
| 53 | + MOCK_AUTH_CODE_URL, # input for 3rd step |
| 54 | + "1,2,3,4,5", # third input for 4th step |
| 55 | + ] |
| 56 | + with patch( |
| 57 | + "sys.argv", |
| 58 | + [ |
| 59 | + "zulip_clickup.py", |
| 60 | + "--clickup-team-id", |
| 61 | + CLICKUP_TEAM_ID, |
| 62 | + "--clickup-client-id", |
| 63 | + CLICKUP_CLIENT_ID, |
| 64 | + "--clickup-client-secret", |
| 65 | + CLICKUP_CLIENT_SECRET, |
| 66 | + "--zulip-webhook-url", |
| 67 | + MOCK_WEBHOOK_URL, |
| 68 | + ], |
| 69 | + ), patch("os.system"), patch("time.sleep"), patch("sys.exit"), patch( |
| 70 | + "builtins.input", side_effect=mock_user_inputs |
| 71 | + ), patch( |
| 72 | + SCRIPT_PATH + ".ClickUpAPIHandler.make_clickup_request", |
| 73 | + side_effect=make_clickup_request_side_effect, |
| 74 | + ): |
| 75 | + result = func(*args, **kwargs) |
| 76 | + |
| 77 | + return result |
| 78 | + |
| 79 | + return wrapper |
| 80 | + |
| 81 | + return decorator |
| 82 | + |
| 83 | + |
| 84 | +class ZulipClickUpScriptTest(TestCase): |
| 85 | + @mock_script_args() |
| 86 | + def test_valid_arguments(self) -> None: |
| 87 | + with patch(SCRIPT_PATH + ".run") as mock_run, patch( |
| 88 | + "sys.stdout", new=io.StringIO() |
| 89 | + ) as mock_stdout: |
| 90 | + zulip_clickup.main() |
| 91 | + self.assertRegex(mock_stdout.getvalue(), r"Running Zulip Clickup Integration...") |
| 92 | + mock_run.assert_called_once_with( |
| 93 | + CLICKUP_CLIENT_ID, CLICKUP_CLIENT_SECRET, CLICKUP_TEAM_ID, MOCK_WEBHOOK_URL |
| 94 | + ) |
| 95 | + |
| 96 | + def test_missing_arguments(self) -> None: |
| 97 | + with self.assertRaises(SystemExit) as cm: |
| 98 | + with patch("sys.stderr", new=io.StringIO()) as mock_stderr: |
| 99 | + zulip_clickup.main() |
| 100 | + self.assertEqual(cm.exception.code, 2) |
| 101 | + self.assertRegex( |
| 102 | + mock_stderr.getvalue(), |
| 103 | + r"""the following arguments are required: --clickup-team-id, --clickup-client-id, --clickup-client-secret, --zulip-webhook-url\n""", |
| 104 | + ) |
| 105 | + |
| 106 | + @mock_script_args() |
| 107 | + def test_redirect_to_auth_page(self) -> None: |
| 108 | + with patch("webbrowser.open") as mock_open, patch( |
| 109 | + "sys.stdout", new=io.StringIO() |
| 110 | + ) as mock_stdout: |
| 111 | + zulip_clickup.main() |
| 112 | + redirect_uri = "https://YourZulipApp.com" |
| 113 | + mock_open.assert_called_once_with( |
| 114 | + f"https://app.clickup.com/api?client_id={CLICKUP_CLIENT_ID}&redirect_uri={redirect_uri}" |
| 115 | + ) |
| 116 | + expected_output = ( |
| 117 | + r"STEP 1[\s\S]*" |
| 118 | + r"ClickUp authorization page will open in your browser\.[\s\S]*" |
| 119 | + r"Please authorize your workspace\(s\)\.[\s\S]*" |
| 120 | + r"Click 'Connect Workspace' on the page to proceed\.\.\." |
| 121 | + ) |
| 122 | + |
| 123 | + self.assertRegex( |
| 124 | + mock_stdout.getvalue(), |
| 125 | + expected_output, |
| 126 | + ) |
| 127 | + |
| 128 | + @mock_script_args() |
| 129 | + def test_query_for_auth_code(self) -> None: |
| 130 | + with patch("webbrowser.open"), patch("sys.stdout", new=io.StringIO()) as mock_stdout: |
| 131 | + zulip_clickup.main() |
| 132 | + expected_output = ( |
| 133 | + "STEP 2\n----\nAfter you've authorized your workspace,\n" |
| 134 | + "you should be redirected to your home URL.\n" |
| 135 | + "Please copy your home URL and paste it below.\n" |
| 136 | + "It should contain a code, and look similar to this:\n\n" |
| 137 | + "e.g. " + re.escape(MOCK_AUTH_CODE_URL) |
| 138 | + ) |
| 139 | + self.assertRegex( |
| 140 | + mock_stdout.getvalue(), |
| 141 | + expected_output, |
| 142 | + ) |
| 143 | + |
| 144 | + @mock_script_args() |
| 145 | + def test_select_clickup_events(self) -> None: |
| 146 | + with patch("webbrowser.open"), patch("sys.stdout", new=io.StringIO()) as mock_stdout: |
| 147 | + zulip_clickup.main() |
| 148 | + expected_output = r""" |
| 149 | +STEP 3 |
| 150 | +---- |
| 151 | +Please select which ClickUp event notification\(s\) you'd |
| 152 | +like to receive in your Zulip app\. |
| 153 | +EVENT CODES: |
| 154 | + 1 = task |
| 155 | + 2 = list |
| 156 | + 3 = folder |
| 157 | + 4 = space |
| 158 | + 5 = goals |
| 159 | +
|
| 160 | + Or, enter \* to subscribe to all events\. |
| 161 | +
|
| 162 | +Here's an example input if you intend to only receive notifications |
| 163 | +related to task, list and folder: 1,2,3 |
| 164 | +""" |
| 165 | + self.assertRegex( |
| 166 | + mock_stdout.getvalue(), |
| 167 | + expected_output, |
| 168 | + ) |
| 169 | + |
| 170 | + @mock_script_args() |
| 171 | + def test_success_message(self) -> None: |
| 172 | + with patch("webbrowser.open"), patch("sys.stdout", new=io.StringIO()) as mock_stdout: |
| 173 | + zulip_clickup.main() |
| 174 | + expected_output = r"SUCCESS: Completed integrating your Zulip app with ClickUp!\s*webhook_id: \d+-\d+-\d+-\d+-\d+-\d+\s*You may delete this script or run it again to reconfigure\s*your integration\." |
| 175 | + self.assertRegex(mock_stdout.getvalue(), expected_output) |
0 commit comments