Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
"""Sends a direct message from a Mattermost bot to one user, through Mattermost's REST API v4. The `notifier` setting `mattermost` picks it."""
|
||||
import dataclasses
|
||||
import pathlib
|
||||
import typing
|
||||
import urllib.parse
|
||||
|
||||
from failure import Failure
|
||||
from rest_client.json_request import Http
|
||||
from rest_client.json_request import Http, MessageOf
|
||||
from rest_client import json_request
|
||||
from rest_client.env_file import read_env
|
||||
|
||||
@@ -53,9 +54,10 @@ def id_of(answer: object, request: str) -> str | Failure:
|
||||
return found if isinstance(found, str) and found else Failure(f"{request} answered without an id")
|
||||
|
||||
|
||||
def http_json(method: str, url: str, token: str, body: object) -> object:
|
||||
# request is json_request.http_json, but in the test of what this hands it.
|
||||
def http_json(method: str, url: str, token: str, body: object, request: typing.Callable[[str, str, str, object, int, MessageOf], object] = json_request.http_json) -> object:
|
||||
"""The JSON that url answers to the bot's request, None where the answer has no body, or a Failure saying why there is none."""
|
||||
return json_request.http_json(method, url, f"Bearer {token}", body, TIMEOUT, error_message)
|
||||
return request(method, url, f"Bearer {token}", body, TIMEOUT, error_message)
|
||||
|
||||
|
||||
def error_message(answer: object) -> str:
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
import dataclasses
|
||||
import html
|
||||
import pathlib
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
from rest_client.json_request import Http
|
||||
from rest_client.json_request import Http, MessageOf
|
||||
from rest_client import json_request
|
||||
from rest_client.env_file import read_env
|
||||
|
||||
@@ -41,9 +42,10 @@ def send(config: Config, message: str, http: Http) -> Failure | None:
|
||||
return Failure(f"POST /api/chat.postMessage refused the message: {error if isinstance(error, str) and error else 'no error code'}")
|
||||
|
||||
|
||||
def http_json(method: str, url: str, token: str, body: object) -> object:
|
||||
# request is json_request.http_json, but in the test of what this hands it.
|
||||
def http_json(method: str, url: str, token: str, body: object, request: typing.Callable[[str, str, str, object, int, MessageOf], object] = json_request.http_json) -> object:
|
||||
"""The JSON that url answers to the bot's request, None where the answer has no body, or a Failure saying why there is none."""
|
||||
return json_request.http_json(method, url, f"Bearer {token}", body, TIMEOUT, error_message)
|
||||
return request(method, url, f"Bearer {token}", body, TIMEOUT, error_message)
|
||||
|
||||
|
||||
def error_message(answer: object) -> str:
|
||||
|
||||
@@ -2,7 +2,6 @@ import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import unittest.mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
@@ -102,9 +101,9 @@ class Send(unittest.TestCase):
|
||||
|
||||
class HttpJson(unittest.TestCase):
|
||||
def test_sends_the_token_as_a_bearer_and_reads_mattermosts_message_from_an_error(self):
|
||||
with unittest.mock.patch.object(dm.json_request, "http_json", return_value={"id": "post-id"}) as sent:
|
||||
self.assertEqual(dm.http_json("POST", "https://chat.example/api/v4/posts", "secret-token", {"message": "hi"}), {"id": "post-id"})
|
||||
sent.assert_called_once_with("POST", "https://chat.example/api/v4/posts", "Bearer secret-token", {"message": "hi"}, dm.TIMEOUT, dm.error_message)
|
||||
sent = []
|
||||
self.assertEqual(dm.http_json("POST", "https://chat.example/api/v4/posts", "secret-token", {"message": "hi"}, lambda *request: sent.append(request) or {"id": "post-id"}), {"id": "post-id"})
|
||||
self.assertEqual(sent, [("POST", "https://chat.example/api/v4/posts", "Bearer secret-token", {"message": "hi"}, dm.TIMEOUT, dm.error_message)])
|
||||
self.assertEqual(dm.TIMEOUT, 10)
|
||||
|
||||
def test_reads_mattermosts_message_from_an_error_answer(self):
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import contextlib
|
||||
import dataclasses
|
||||
import io
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -73,14 +74,14 @@ class Notify(Project):
|
||||
self.name_notifier("slack", SLACK_KEYS)
|
||||
slack = Slack({"ok": True})
|
||||
with contextlib.chdir(self.checkout):
|
||||
self.assertIsNone(system_effects(through("slack", slack)).dm("Which one?", None))
|
||||
self.assertIsNone(system_effects(self.home, os.environ, through("slack", slack)).dm("Which one?", None))
|
||||
self.assertEqual(slack.requests, [SLACK_POST])
|
||||
|
||||
def test_a_dm_that_names_a_directory_goes_through_the_notifier_for_the_project_holding_it_from_any_current_directory(self):
|
||||
self.name_notifier("slack", SLACK_KEYS)
|
||||
slack = Slack({"ok": True})
|
||||
with contextlib.chdir(self.home):
|
||||
self.assertIsNone(system_effects(through("slack", slack)).dm("Which one?", str(self.checkout)))
|
||||
self.assertIsNone(system_effects(self.home, os.environ, through("slack", slack)).dm("Which one?", str(self.checkout)))
|
||||
self.assertEqual(slack.requests, [SLACK_POST])
|
||||
|
||||
def test_skips_the_notice_for_a_project_that_names_no_notifier_even_where_its_env_file_holds_the_keys(self):
|
||||
|
||||
@@ -2,7 +2,6 @@ import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import unittest.mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
@@ -76,9 +75,9 @@ class Send(unittest.TestCase):
|
||||
|
||||
class HttpJson(unittest.TestCase):
|
||||
def test_sends_the_token_as_a_bearer_and_reads_slacks_code_from_an_error(self):
|
||||
with unittest.mock.patch.object(dm.json_request, "http_json", return_value={"ok": True}) as sent:
|
||||
self.assertEqual(dm.http_json("POST", "https://slack.com/api/chat.postMessage", "xoxb-secret", {"text": "hi"}), {"ok": True})
|
||||
sent.assert_called_once_with("POST", "https://slack.com/api/chat.postMessage", "Bearer xoxb-secret", {"text": "hi"}, dm.TIMEOUT, dm.error_message)
|
||||
sent = []
|
||||
self.assertEqual(dm.http_json("POST", "https://slack.com/api/chat.postMessage", "xoxb-secret", {"text": "hi"}, lambda *request: sent.append(request) or {"ok": True}), {"ok": True})
|
||||
self.assertEqual(sent, [("POST", "https://slack.com/api/chat.postMessage", "Bearer xoxb-secret", {"text": "hi"}, dm.TIMEOUT, dm.error_message)])
|
||||
for answer, code in (({"error": "ratelimited"}, "ratelimited"), ({"error": 7}, ""), ({}, ""), (["error"], "")):
|
||||
with self.subTest(answer=answer):
|
||||
self.assertEqual(dm.error_message(answer), code)
|
||||
|
||||
Reference in New Issue
Block a user