tests: improve coverage
This commit is contained in:
parent
6b5d6e6e87
commit
cda8898019
1 changed files with 76 additions and 18 deletions
|
@ -8,27 +8,31 @@ import nio
|
||||||
from .start import BOT_URL, FULL_ID, MATRIX_ID, MATRIX_PW, MATRIX_URL
|
from .start import BOT_URL, FULL_ID, MATRIX_ID, MATRIX_PW, MATRIX_URL
|
||||||
|
|
||||||
SHA256 = "fd7522672889385736be8ffc86d1f8de2e15668864f49af729b5c63e5e0698c4"
|
SHA256 = "fd7522672889385736be8ffc86d1f8de2e15668864f49af729b5c63e5e0698c4"
|
||||||
EXAMPLE_GITHUB_REQUEST_HEADERS = {
|
|
||||||
|
|
||||||
|
def headers(sha256=SHA256, event="push"):
|
||||||
|
"""Mock headers from github webhooks."""
|
||||||
|
return {
|
||||||
# 'Request URL': 'https://bot.saurel.me/room?formatter=github',
|
# 'Request URL': 'https://bot.saurel.me/room?formatter=github',
|
||||||
# 'Request method': 'POST',
|
# 'Request method': 'POST',
|
||||||
"Accept": "*/*",
|
"Accept": "*/*",
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
"User-Agent": "GitHub-Hookshot/8d33975",
|
"User-Agent": "GitHub-Hookshot/8d33975",
|
||||||
"X-GitHub-Delivery": "636b9b1c-0761-11ec-8a8a-5e435c5ac4f4",
|
"X-GitHub-Delivery": "636b9b1c-0761-11ec-8a8a-5e435c5ac4f4",
|
||||||
"X-GitHub-Event": "push",
|
"X-GitHub-Event": event,
|
||||||
"X-GitHub-Hook-ID": "311845633",
|
"X-GitHub-Hook-ID": "311845633",
|
||||||
"X-GitHub-Hook-Installation-Target-ID": "171114171",
|
"X-GitHub-Hook-Installation-Target-ID": "171114171",
|
||||||
"X-GitHub-Hook-Installation-Target-Type": "repository",
|
"X-GitHub-Hook-Installation-Target-Type": "repository",
|
||||||
"X-Hub-Signature": "sha1=ea68fdfcb2f328aaa8f50d176f355e5d4fc95d94",
|
"X-Hub-Signature": "sha1=ea68fdfcb2f328aaa8f50d176f355e5d4fc95d94",
|
||||||
"X-Hub-Signature-256": f"sha256={SHA256}",
|
"X-Hub-Signature-256": f"sha256={sha256}",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class GithubFormatterTest(unittest.IsolatedAsyncioTestCase):
|
class GithubFormatterTest(unittest.IsolatedAsyncioTestCase):
|
||||||
"""Github formatter test class."""
|
"""Github formatter test class."""
|
||||||
|
|
||||||
async def test_github_body(self):
|
async def test_github_notification(self):
|
||||||
"""Send a markdown message, and check the result."""
|
"""Send a mock github webhook, and check the result."""
|
||||||
messages = []
|
messages = []
|
||||||
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
|
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
|
||||||
|
|
||||||
|
@ -37,7 +41,6 @@ class GithubFormatterTest(unittest.IsolatedAsyncioTestCase):
|
||||||
|
|
||||||
with open("tests/example_github_push.json", "rb") as f:
|
with open("tests/example_github_push.json", "rb") as f:
|
||||||
example_github_push = f.read().strip()
|
example_github_push = f.read().strip()
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
httpx.post(
|
httpx.post(
|
||||||
f"{BOT_URL}/{room.room_id}",
|
f"{BOT_URL}/{room.room_id}",
|
||||||
|
@ -45,7 +48,40 @@ class GithubFormatterTest(unittest.IsolatedAsyncioTestCase):
|
||||||
"formatter": "github",
|
"formatter": "github",
|
||||||
},
|
},
|
||||||
content=example_github_push,
|
content=example_github_push,
|
||||||
headers=EXAMPLE_GITHUB_REQUEST_HEADERS,
|
headers=headers(event="something else"),
|
||||||
|
).json(),
|
||||||
|
{"status": 200, "ret": "OK"},
|
||||||
|
)
|
||||||
|
|
||||||
|
sync = await client.sync()
|
||||||
|
messages = await client.room_messages(room.room_id, sync.next_batch)
|
||||||
|
await client.close()
|
||||||
|
|
||||||
|
message = messages.chunk[0]
|
||||||
|
self.assertEqual(message.sender, FULL_ID)
|
||||||
|
self.assertEqual(
|
||||||
|
message.formatted_body,
|
||||||
|
"<p>notification from github</p>",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def test_github_push(self):
|
||||||
|
"""Send a mock github push webhook, and check the result."""
|
||||||
|
messages = []
|
||||||
|
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
|
||||||
|
|
||||||
|
await client.login(MATRIX_PW)
|
||||||
|
room = await client.room_create()
|
||||||
|
|
||||||
|
with open("tests/example_github_push.json", "rb") as f:
|
||||||
|
example_github_push = f.read().strip()
|
||||||
|
self.assertEqual(
|
||||||
|
httpx.post(
|
||||||
|
f"{BOT_URL}/{room.room_id}",
|
||||||
|
params={
|
||||||
|
"formatter": "github",
|
||||||
|
},
|
||||||
|
content=example_github_push,
|
||||||
|
headers=headers(),
|
||||||
).json(),
|
).json(),
|
||||||
{"status": 200, "ret": "OK"},
|
{"status": 200, "ret": "OK"},
|
||||||
)
|
)
|
||||||
|
@ -69,3 +105,25 @@ class GithubFormatterTest(unittest.IsolatedAsyncioTestCase):
|
||||||
message.formatted_body,
|
message.formatted_body,
|
||||||
expected,
|
expected,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def test_github_wrong_digest(self):
|
||||||
|
"""Send a mock github push webhook with a wrong digest."""
|
||||||
|
client = nio.AsyncClient(MATRIX_URL, MATRIX_ID)
|
||||||
|
|
||||||
|
await client.login(MATRIX_PW)
|
||||||
|
room = await client.room_create()
|
||||||
|
|
||||||
|
with open("tests/example_github_push.json", "rb") as f:
|
||||||
|
example_github_push = f.read().strip()
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
httpx.post(
|
||||||
|
f"{BOT_URL}/{room.room_id}",
|
||||||
|
params={
|
||||||
|
"formatter": "github",
|
||||||
|
},
|
||||||
|
content=example_github_push,
|
||||||
|
headers=headers("wrong digest"),
|
||||||
|
).json(),
|
||||||
|
{"status": 401, "ret": "Invalid SHA-256 HMAC digest"},
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue