Files
roboluke/tests/test_config_2.py
Luke Tainton d5a366c4f2
Some checks failed
Build Docker / Create Docker Image (release) Failing after 4m0s
Release / Tag release (push) Successful in 37s
Release / Create Release (push) Successful in 7s
Release / Publish Docker Images (push) Failing after 2m57s
Snyk / security (push) Successful in 8m22s
feat(config): update n8n webhook handling (#366)
Okay, I will explain the code changes presented in the diff.  The primary focus of this pull request appears to be refactoring the N8N webhook URL configuration to use separate URLs for GET and POST requests. Additionally, several files in the `.github` directory have been moved to an `.archive/.github` directory.

**1. Directory Changes:**

*   A number of files related to GitHub configuration (CODEOWNERS, issue templates, dependabot configuration, renovate configuration, and workflow files) have been moved from the `.github` directory to `.archive/.github`. This suggests that these configurations are no longer actively used or are being superseded by other configurations.

**2. Environment Variable Changes (.env.default):**

```diff
--- a/.env.default
+++ b/.env.default
@@ -5,5 +5,6 @@
  APPROVED_DOMAINS="example.com,hello.com"
  APPROVED_ROOMS="abc123,def456"
  APPROVED_USERS="bob@example.com,john@me.com"
  BOT_NAME=""
 -N8N_WEBHOOK_URL=""
 +N8N_GET_WEBHOOK_URL=""
 +N8N_POST_WEBHOOK_URL=""
  WEBEX_API_KEY=""
```

*   The `.env.default` file, which provides default values for environment variables, has been modified.
*   The single `N8N_WEBHOOK_URL` environment variable has been removed.
*   Two new environment variables, `N8N_GET_WEBHOOK_URL` and `N8N_POST_WEBHOOK_URL`, have been added. This indicates the application now requires separate webhook URLs for GET and POST requests to the N8N service.

**3. Configuration Class Changes (app/utils/config.py):**

```diff
--- a/app/utils/config.py
+++ b/app/utils/config.py
@@ -42,9 +42,14 @@ class Config:
          return os.environ["ADMIN_EMAIL"].split(",")

      @property
-    def n8n_webhook_url(self) -> str:
-        """Returns the n8n webhook URL."""
-        return os.environ["N8N_WEBHOOK_URL"]
+    def n8n_get_webhook_url(self) -> str:
+        """Returns the n8n GET webhook URL."""
+        return os.environ["N8N_GET_WEBHOOK_URL"]
+
+    @property
+    def n8n_post_webhook_url(self) -> str:
+        """Returns the n8n POST webhook URL."""
+        return os.environ["N8N_POST_WEBHOOK_URL"]

      @property
      def approved_users(self) -> list:
```

*   The `Config` class in `app/utils/config.py` has been updated to reflect the environment variable changes.
*   The `n8n_webhook_url` property has been removed.
*   Two new properties, `n8n_get_webhook_url` and `n8n_post_webhook_url`, have been added.  These properties retrieve the corresponding environment variables and provide access to the individual webhook URLs.

**4. N8N Utility Changes (app/utils/n8n.py):**

```diff
--- a/app/utils/n8n.py
+++ b/app/utils/n8n.py
@@ -16,7 +16,7 @@ def __n8n_post(data: dict) -> bool:
      """
      headers: dict = {"Content-Type": "application/json"}
      resp: requests.Response = requests.post(
-        url=config.n8n_webhook_url,
+        url=config.n8n_post_webhook_url,
          headers=headers,
          json=data,
          timeout=10,
@@ -58,7 +58,7 @@ def get_tasks(requestor) -> bool:
      """
      headers: dict = {"Content-Type": "application/json"}
      resp: requests.Response = requests.get(
-        url=config.n8n_webhook_url,
+        url=config.n8n_get_webhook_url,
          headers=headers,
          timeout=10,
          verify=True,
```

*   The `app/utils/n8n.py` file has been modified to use the new configuration properties.
*   In the `__n8n_post` function, the `url` parameter in the `requests.post` call now uses `config.n8n_post_webhook_url` instead of the old `config.n8n_webhook_url`.
*   Similarly, in the `get_tasks` function, the `url` parameter in the `requests.get` call now uses `config.n8n_get_webhook_url`.

**5. Test Changes (tests/test\_config\_1.py and tests/test\_config\_2.py):**

```diff
--- a/tests/test_config_1.py
+++ b/tests/test_config_1.py
@@ -16,7 +16,8 @@ def test_config() -> None:
          "WEBEX_API_KEY": "testing",
          "ADMIN_FIRST_NAME": "Test",
          "ADMIN_EMAIL": "test@test.com",
-        "N8N_WEBHOOK_URL": "https://n8n.test.com/webhook/abcdefg",
+        "N8N_GET_WEBHOOK_URL": "https://n8n.test.com/webhook/abc",
+        "N8N_POST_WEBHOOK_URL": "https://n8n.test.com/webhook/def",
          "APPROVED_USERS": "test@test.com",
          "APPROVED_DOMAINS": "test.com",
          "APPROVED_ROOMS": "test",
@@ -34,7 +35,8 @@ def test_config() -> None:
      assert config.approved_rooms == config_vars["APPROVED_ROOMS"].split(",")
      assert config.approved_users == config_vars["APPROVED_USERS"].split(",")
      assert config.bot_name == config_vars["BOT_NAME"]
-    assert config.n8n_webhook_url == config_vars["N8N_WEBHOOK_URL"]
+    assert config.n8n_get_webhook_url == config_vars["N8N_GET_WEBHOOK_URL"]
+    assert config.n8n_post_webhook_url == config_vars["N8N_POST_WEBHOOK_URL"]
      assert config.version == config_vars["APP_VERSION"]
      assert config.webex_token == config_vars["WEBEX_API_KEY"]
```

*   The test files have been updated to use the new environment variables and configuration properties.  The old `N8N_WEBHOOK_URL` is removed and `N8N_GET_WEBHOOK_URL` and `N8N_POST_WEBHOOK_URL` are added and asserted.

**Summary:**

This pull request introduces a separation of N8N webhook URLs for GET and POST requests. This likely allows for more specific control and potentially different workflows depending on the HTTP method used to interact with the N8N service. The `.github` directory changes suggest cleaning up the repository by archiving old or unused configuration files. The tests are updated to align with the new configuration structure.

Co-authored-by: Luke Tainton <ltainton@cisco.com>
Reviewed-on: #366
2025-08-04 09:59:52 +02:00

42 lines
1.4 KiB
Python

#!/usr/bin/env python3
# ruff: noqa: E402 pylint: disable=wrong-import-position
"""Provides test cases for app/utils/config.py."""
import os
def test_config_no_admin_vars() -> None:
"""Test config module."""
config_vars: dict = {
"APP_VERSION": "dev",
"BOT_NAME": "TestBot",
"WEBEX_API_KEY": "testing",
"ADMIN_FIRST_NAME": "Test",
"ADMIN_EMAIL": "test@test.com",
"N8N_GET_WEBHOOK_URL": "https://n8n.test.com/webhook/abc",
"N8N_POST_WEBHOOK_URL": "https://n8n.test.com/webhook/def",
}
for config_var, value in config_vars.items():
os.environ[config_var] = value
# needs to be imported AFTER environment variables are set
from app.utils.config import config # pragma: no cover
assert config.approved_domains == []
assert config.approved_rooms == []
assert config.approved_users == []
assert config.admin_emails == config_vars["ADMIN_EMAIL"].split(",")
assert config.admin_first_name == config_vars["ADMIN_FIRST_NAME"]
assert config.bot_name == config_vars["BOT_NAME"]
assert config.n8n_get_webhook_url == config_vars["N8N_GET_WEBHOOK_URL"]
assert config.n8n_post_webhook_url == config_vars["N8N_POST_WEBHOOK_URL"]
assert config.version == config_vars["APP_VERSION"]
assert config.webex_token == config_vars["WEBEX_API_KEY"]
for config_var in config_vars:
os.environ.pop(config_var, None)