|
| 1 | +import os |
| 2 | + |
| 3 | +from socket_basics.core.notification.ms_teams_notifier import MSTeamsNotifier |
| 4 | +from socket_basics.core.notification.manager import NotificationManager |
| 5 | + |
| 6 | + |
| 7 | +def _base_cfg(): |
| 8 | + return { |
| 9 | + "notifiers": { |
| 10 | + "msteams": { |
| 11 | + "module_path": "socket_basics.core.notification.ms_teams_notifier", |
| 12 | + "class": "MSTeamsNotifier", |
| 13 | + "parameters": [ |
| 14 | + {"name": "msteams_webhook_url", "env_variable": "INPUT_MSTEAMS_WEBHOOK_URL", "type": "str"}, |
| 15 | + ], |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + |
| 21 | +def test_msteams_notifier_reads_url_from_params(): |
| 22 | + """msteams_webhook_url param from dashboard config should populate self.webhook_url""" |
| 23 | + n = MSTeamsNotifier({"msteams_webhook_url": "https://outlook.office.com/webhook/xxx"}) |
| 24 | + assert n.webhook_url == "https://outlook.office.com/webhook/xxx" |
| 25 | + |
| 26 | + |
| 27 | +def test_msteams_notifier_url_is_none_without_config(monkeypatch): |
| 28 | + """Without any config or env var, webhook_url should be falsy""" |
| 29 | + monkeypatch.delenv("MSTEAMS_WEBHOOK_URL", raising=False) |
| 30 | + monkeypatch.delenv("INPUT_MSTEAMS_WEBHOOK_URL", raising=False) |
| 31 | + n = MSTeamsNotifier({}) |
| 32 | + assert not n.webhook_url |
| 33 | + |
| 34 | + |
| 35 | +def test_msteams_notifier_falls_back_to_env_var(monkeypatch): |
| 36 | + """INPUT_MSTEAMS_WEBHOOK_URL env var should work as fallback when params empty""" |
| 37 | + monkeypatch.setenv("INPUT_MSTEAMS_WEBHOOK_URL", "https://outlook.office.com/webhook/env") |
| 38 | + n = MSTeamsNotifier({}) |
| 39 | + assert n.webhook_url == "https://outlook.office.com/webhook/env" |
| 40 | + |
| 41 | + |
| 42 | +def test_msteams_notifier_params_take_precedence_over_env(monkeypatch): |
| 43 | + """Dashboard config (params) should take precedence over env var""" |
| 44 | + monkeypatch.setenv("INPUT_MSTEAMS_WEBHOOK_URL", "https://outlook.office.com/webhook/env") |
| 45 | + n = MSTeamsNotifier({"msteams_webhook_url": "https://outlook.office.com/webhook/dashboard"}) |
| 46 | + assert n.webhook_url == "https://outlook.office.com/webhook/dashboard" |
| 47 | + |
| 48 | + |
| 49 | +def test_msteams_enabled_via_app_config(monkeypatch): |
| 50 | + """MS Teams notifier should load and receive URL when msteams_webhook_url is in app_config""" |
| 51 | + monkeypatch.delenv("MSTEAMS_WEBHOOK_URL", raising=False) |
| 52 | + monkeypatch.delenv("INPUT_MSTEAMS_WEBHOOK_URL", raising=False) |
| 53 | + |
| 54 | + cfg = _base_cfg() |
| 55 | + nm = NotificationManager(cfg, app_config={"msteams_webhook_url": "https://outlook.office.com/webhook/dashboard"}) |
| 56 | + nm.load_from_config() |
| 57 | + |
| 58 | + msteams = next(n for n in nm.notifiers if getattr(n, "name", "") == "msteams") |
| 59 | + assert msteams.webhook_url == "https://outlook.office.com/webhook/dashboard" |
| 60 | + |
| 61 | + |
| 62 | +def test_msteams_enabled_via_env_var(monkeypatch): |
| 63 | + """MS Teams notifier should load when INPUT_MSTEAMS_WEBHOOK_URL env var is set""" |
| 64 | + monkeypatch.setenv("INPUT_MSTEAMS_WEBHOOK_URL", "https://outlook.office.com/webhook/env") |
| 65 | + |
| 66 | + cfg = _base_cfg() |
| 67 | + nm = NotificationManager(cfg, app_config={}) |
| 68 | + nm.load_from_config() |
| 69 | + |
| 70 | + msteams = next(n for n in nm.notifiers if getattr(n, "name", "") == "msteams") |
| 71 | + assert msteams.webhook_url == "https://outlook.office.com/webhook/env" |
| 72 | + |
| 73 | + |
| 74 | +def test_msteams_app_config_precedence_over_env(monkeypatch): |
| 75 | + """app_config msteams_webhook_url should take precedence over env var in manager flow""" |
| 76 | + monkeypatch.setenv("INPUT_MSTEAMS_WEBHOOK_URL", "https://outlook.office.com/webhook/env") |
| 77 | + |
| 78 | + cfg = _base_cfg() |
| 79 | + nm = NotificationManager(cfg, app_config={"msteams_webhook_url": "https://outlook.office.com/webhook/dashboard"}) |
| 80 | + nm.load_from_config() |
| 81 | + |
| 82 | + msteams = next(n for n in nm.notifiers if getattr(n, "name", "") == "msteams") |
| 83 | + assert msteams.webhook_url == "https://outlook.office.com/webhook/dashboard" |
0 commit comments