63 lines
2.8 KiB
Python
63 lines
2.8 KiB
Python
import base64
|
|
import pathlib
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import unittest.mock
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from atlassian import client
|
|
from failure import Failure
|
|
|
|
SEARCH = "https://acme.atlassian.net/rest/api/3/search/jql"
|
|
AUTHORIZATION = "Basic " + base64.b64encode(b"me@example.com:s3cret").decode()
|
|
|
|
|
|
class Credentials(unittest.TestCase):
|
|
def test_builds_basic_auth_from_the_env_files_email_and_token(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
env = pathlib.Path(temp, "acme.env")
|
|
env.write_text("ATLASSIAN_EMAIL=me@example.com\nATLASSIAN_API_TOKEN=s3cret\n")
|
|
env.chmod(0o600)
|
|
self.assertEqual(client.basic_auth(env), AUTHORIZATION)
|
|
|
|
def test_a_missing_env_file_fails(self):
|
|
self.assertIsInstance(client.basic_auth(pathlib.Path("/nonexistent/acme.env")), Failure)
|
|
|
|
|
|
class Jira(unittest.TestCase):
|
|
def test_sends_each_request_through_the_shared_client_with_jiras_error_messages(self):
|
|
with unittest.mock.patch.object(client.json_request, "http_json", return_value={"issues": []}) as sent:
|
|
self.assertEqual(client.jira_json("POST", SEARCH, AUTHORIZATION, {"jql": "x"}), {"issues": []})
|
|
sent.assert_called_once_with("POST", SEARCH, AUTHORIZATION, {"jql": "x"}, 30, client.jira_errors)
|
|
|
|
def test_reads_jiras_messages_from_an_error_answer(self):
|
|
cases = (
|
|
({"errorMessages": ["Issue does not exist or you do not have permission to see it."], "errors": {}}, "Issue does not exist or you do not have permission to see it."),
|
|
({"errorMessages": ["One."], "errors": {"jql": "Two."}}, "One.; Two."),
|
|
({"errorMessages": "One.", "errors": ["Two."]}, ""),
|
|
({"errorMessages": [7, "One."]}, "One."),
|
|
(["One."], ""),
|
|
)
|
|
for answer, message in cases:
|
|
with self.subTest(answer=answer):
|
|
self.assertEqual(client.jira_errors(answer), message)
|
|
|
|
|
|
class Confluence(unittest.TestCase):
|
|
def test_sends_each_request_through_the_shared_client_with_confluences_error_messages(self):
|
|
with unittest.mock.patch.object(client.json_request, "http_json", return_value={}) as sent:
|
|
self.assertEqual(client.confluence_json("GET", "https://acme.atlassian.net/wiki/api/v2/pages/1", AUTHORIZATION, None), {})
|
|
sent.assert_called_once_with("GET", "https://acme.atlassian.net/wiki/api/v2/pages/1", AUTHORIZATION, None, 30, client.confluence_errors)
|
|
|
|
def test_reads_confluences_messages_from_an_error_answer(self):
|
|
self.assertEqual(client.confluence_errors({"errors": [{"status": 409, "title": "Version must be incremented"}, {"title": "and more"}, "odd"]}), "Version must be incremented; and more")
|
|
self.assertEqual(client.confluence_errors({"message": "Not Found"}), "Not Found")
|
|
self.assertEqual(client.confluence_errors({"errors": "odd"}), "")
|
|
self.assertEqual(client.confluence_errors(["odd"]), "")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|