Organized
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from redmine_ticket_attachments import (
|
||||
attachments_from_issue,
|
||||
pick_attachment,
|
||||
safe_filename,
|
||||
upload_file_token,
|
||||
)
|
||||
from redmine_tickets import build_parser, inject_default_sync_command
|
||||
|
||||
|
||||
class AttachmentHelpersTest(unittest.TestCase):
|
||||
def test_pick_and_safe(self):
|
||||
atts = [{"id": 1, "filename": "a.pdf"}, {"id": 2, "filename": "b.pdf"}]
|
||||
self.assertEqual(2, pick_attachment(atts, 2)["id"])
|
||||
with self.assertRaises(LookupError):
|
||||
pick_attachment(atts, None)
|
||||
self.assertEqual(1, pick_attachment([atts[0]], None)["id"])
|
||||
self.assertEqual("foo_bar.pdf", safe_filename("foo/bar.pdf"))
|
||||
|
||||
def test_attachments_from_issue(self):
|
||||
self.assertEqual([], attachments_from_issue({}))
|
||||
self.assertEqual(1, len(attachments_from_issue({"attachments": [{"id": 9}]})))
|
||||
|
||||
def test_cli_parse(self):
|
||||
args = build_parser().parse_args(
|
||||
inject_default_sync_command(
|
||||
[
|
||||
"--project",
|
||||
"customer-service",
|
||||
"attachments",
|
||||
"list",
|
||||
"--issue-id",
|
||||
"1",
|
||||
"--refresh",
|
||||
]
|
||||
)
|
||||
)
|
||||
self.assertEqual("attachments", args.command)
|
||||
self.assertEqual("list", args.attachments_command)
|
||||
self.assertTrue(args.refresh)
|
||||
|
||||
args2 = build_parser().parse_args(
|
||||
inject_default_sync_command(
|
||||
[
|
||||
"attachments",
|
||||
"upload",
|
||||
"--issue-id",
|
||||
"2",
|
||||
"--file",
|
||||
"/tmp/x.pdf",
|
||||
"--apply",
|
||||
]
|
||||
)
|
||||
)
|
||||
self.assertEqual("upload", args2.attachments_command)
|
||||
self.assertTrue(args2.apply)
|
||||
|
||||
def test_upload_token_mock(self):
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
path = Path(td) / "note.txt"
|
||||
path.write_text("hello", encoding="utf-8")
|
||||
|
||||
class FakeResp:
|
||||
def read(self):
|
||||
return b'{"upload":{"token":"tok.123","id":1}}'
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *a):
|
||||
return False
|
||||
|
||||
with patch("redmine_ticket_attachments.urllib_request.urlopen", return_value=FakeResp()):
|
||||
upload = upload_file_token(
|
||||
base_url="http://192.168.50.170",
|
||||
api_key="x",
|
||||
path=path,
|
||||
)
|
||||
self.assertEqual("tok.123", upload["token"])
|
||||
self.assertEqual("note.txt", upload["filename"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user