Answer architecture round 1: 4 🔴, 5 🟡 (0 misread); one-way plan packages with a plan core and an Atlassian client, one parse and print of an item's cells, a pure chunk-start decision, a settings table, one plan read and one standing per pick, one config load in chunk-pr, notifier backends in notifier, and the bin regroup filed as item 392
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import unittest.mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from notifier import slack as dm
|
||||
from failure import Failure
|
||||
|
||||
CONFIG = dm.Config("xoxb-secret", "U0123ABCD")
|
||||
|
||||
|
||||
class Slack:
|
||||
"""Slack's answer to every request, recording each one it gets."""
|
||||
|
||||
def __init__(self, answer: object) -> None:
|
||||
self.answer = answer
|
||||
self.requests: list[tuple[str, str, str, object]] = []
|
||||
|
||||
def __call__(self, method: str, url: str, token: str, body: object) -> object:
|
||||
self.requests.append((method, url, token, body))
|
||||
return self.answer
|
||||
|
||||
|
||||
def env_file(folder: str, text: str, mode: int = 0o600) -> pathlib.Path:
|
||||
path = pathlib.Path(folder) / "slack.env"
|
||||
path.write_text(text)
|
||||
path.chmod(mode)
|
||||
return path
|
||||
|
||||
|
||||
class ReadConfig(unittest.TestCase):
|
||||
def test_reads_the_token_and_the_recipient_skipping_comments_and_quotes(self):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
path = env_file(folder, '# acme\nSLACK_TOKEN="xoxb-secret"\nSLACK_RECIPIENT = U0123ABCD\nOTHER=1\n')
|
||||
self.assertEqual(dm.read_config(path), CONFIG)
|
||||
|
||||
def test_names_each_key_missing_and_refuses_a_file_others_can_read(self):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
path = env_file(folder, "SLACK_TOKEN=xoxb-secret\n")
|
||||
self.assertEqual(dm.read_config(path), Failure(f"{path} lacks SLACK_RECIPIENT"))
|
||||
path = env_file(folder, "SLACK_TOKEN=t\nSLACK_RECIPIENT=r\n", 0o640)
|
||||
self.assertEqual(dm.read_config(path), Failure(f"{path} holds the bot's token, so only its owner may read it; run chmod 600 {path}"))
|
||||
|
||||
|
||||
class Send(unittest.TestCase):
|
||||
def test_posts_the_message_to_the_recipients_member_id_with_the_token(self):
|
||||
slack = Slack({"ok": True, "ts": "1.2"})
|
||||
self.assertIsNone(dm.send(CONFIG, "Which one?", slack))
|
||||
self.assertEqual(slack.requests, [("POST", "https://slack.com/api/chat.postMessage", "xoxb-secret", {"channel": "U0123ABCD", "text": "Which one?"})])
|
||||
|
||||
def test_escapes_the_three_characters_slack_reads_as_control_characters(self):
|
||||
slack = Slack({"ok": True})
|
||||
self.assertIsNone(dm.send(CONFIG, "Use <b> & [<setting>]?", slack))
|
||||
self.assertEqual(slack.requests[0][3], {"channel": "U0123ABCD", "text": "Use <b> & [<setting>]?"})
|
||||
|
||||
def test_leaves_quotes_as_they_are_since_slack_reads_only_three_characters_as_control(self):
|
||||
slack = Slack({"ok": True})
|
||||
self.assertIsNone(dm.send(CONFIG, "Use \"a\" or 'b'?", slack))
|
||||
self.assertEqual(slack.requests[0][3]["text"], "Use \"a\" or 'b'?")
|
||||
|
||||
def test_fails_naming_slacks_error_code_where_slack_answers_ok_false(self):
|
||||
self.assertEqual(dm.send(CONFIG, "hi", Slack({"ok": False, "error": "invalid_auth"})), Failure("POST /api/chat.postMessage refused the message: invalid_auth"))
|
||||
|
||||
def test_fails_on_an_answer_that_says_neither_ok_nor_an_error(self):
|
||||
for answer in ({}, ["ok"], {"ok": "true"}, {"ok": False, "error": 7}):
|
||||
with self.subTest(answer=answer):
|
||||
self.assertEqual(dm.send(CONFIG, "hi", Slack(answer)), Failure("POST /api/chat.postMessage refused the message: no error code"))
|
||||
|
||||
def test_passes_on_the_failure_of_the_request_itself(self):
|
||||
failed = Failure("cannot reach https://slack.com: timed out")
|
||||
self.assertEqual(dm.send(CONFIG, "hi", Slack(failed)), failed)
|
||||
|
||||
|
||||
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)
|
||||
for answer, code in (({"error": "ratelimited"}, "ratelimited"), ({"error": 7}, ""), ({}, ""), (["error"], "")):
|
||||
with self.subTest(answer=answer):
|
||||
self.assertEqual(dm.error_message(answer), code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user