2020-07-09 01:07:12 +00:00
|
|
|
# Copyright (C) 2020 by Maike <maike@systemli.org>
|
2020-09-28 22:39:32 +00:00
|
|
|
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
|
2020-07-09 01:07:12 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
2020-07-13 01:57:27 +00:00
|
|
|
import subprocess
|
2020-07-16 12:29:57 +00:00
|
|
|
from re import findall
|
|
|
|
from urllib.parse import urlparse
|
2020-07-09 01:07:12 +00:00
|
|
|
|
2020-10-13 08:35:20 +00:00
|
|
|
from fastapi import status
|
2023-04-01 12:57:41 +00:00
|
|
|
import pytest
|
2020-10-13 08:35:20 +00:00
|
|
|
|
|
|
|
from kibicara.webapi.admin import to_token
|
|
|
|
|
2020-07-09 01:07:12 +00:00
|
|
|
|
2023-04-01 12:57:41 +00:00
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
async def test_email_subscribe_unsubscribe(client, hood_id, receive_email):
|
|
|
|
response = await client.post(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/email/subscribe/".format(hood_id),
|
|
|
|
json={"email": "test@localhost"},
|
2020-07-09 01:12:46 +00:00
|
|
|
)
|
2020-07-16 12:29:57 +00:00
|
|
|
assert response.status_code == status.HTTP_202_ACCEPTED
|
|
|
|
mail = receive_email()
|
2023-03-18 17:47:02 +00:00
|
|
|
body = mail["body"]
|
2020-07-16 12:29:57 +00:00
|
|
|
confirm_url = findall(
|
2023-03-18 17:47:02 +00:00
|
|
|
r"http[s]?://"
|
|
|
|
+ r"(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+",
|
2020-07-16 12:29:57 +00:00
|
|
|
body,
|
|
|
|
)[0]
|
2023-03-18 17:47:02 +00:00
|
|
|
start = len("token=")
|
2023-04-01 13:32:10 +00:00
|
|
|
response = await client.post(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/email/subscribe/confirm/{1}".format(
|
2020-10-12 20:47:06 +00:00
|
|
|
hood_id, urlparse(confirm_url).query[start:]
|
|
|
|
)
|
2020-09-06 21:06:52 +00:00
|
|
|
)
|
2020-07-09 01:12:46 +00:00
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
2023-04-01 13:32:10 +00:00
|
|
|
response = await client.post(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/email/subscribe/confirm/{1}".format(
|
2020-10-12 20:47:06 +00:00
|
|
|
hood_id, urlparse(confirm_url).query[start:]
|
|
|
|
)
|
2020-09-06 21:06:52 +00:00
|
|
|
)
|
2020-07-16 12:29:57 +00:00
|
|
|
assert response.status_code == status.HTTP_409_CONFLICT
|
2023-03-18 17:47:02 +00:00
|
|
|
token = to_token(email=mail["to"], hood=hood_id)
|
2023-04-01 13:32:10 +00:00
|
|
|
response = await client.delete(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/email/unsubscribe/{1}".format(hood_id, token)
|
2020-10-12 20:47:06 +00:00
|
|
|
)
|
2020-07-16 12:29:57 +00:00
|
|
|
assert response.status_code == status.HTTP_204_NO_CONTENT
|
2020-07-09 01:12:46 +00:00
|
|
|
|
|
|
|
|
2023-04-01 12:57:41 +00:00
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
async def test_email_message(client, hood_id, trigger_id, email_row):
|
2020-07-11 02:28:55 +00:00
|
|
|
body = {
|
2023-03-18 17:47:02 +00:00
|
|
|
"text": "test",
|
|
|
|
"author": "test@localhost",
|
|
|
|
"secret": email_row["secret"],
|
2020-07-11 02:28:55 +00:00
|
|
|
}
|
2023-04-01 13:32:10 +00:00
|
|
|
response = await client.post(
|
2023-04-01 12:57:41 +00:00
|
|
|
"/api/hoods/{0}/email/messages/".format(hood_id), json=body
|
|
|
|
)
|
2020-07-11 02:28:55 +00:00
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
|
|
|
|
|
|
|
|
2023-04-01 12:57:41 +00:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_email_send_mda(trigger_id, email_row):
|
|
|
|
pytest.skip("Only works if kibicara is listening on port 8000, and only sometimes")
|
2020-07-13 01:57:27 +00:00
|
|
|
mail = """From test@example.com Tue Jun 16 15:33:19 2020
|
2020-07-12 18:29:28 +00:00
|
|
|
Return-path: <test@example.com>
|
|
|
|
Envelope-to: hood@localhost
|
|
|
|
Delivery-date: Tue, 16 Jun 2020 15:33:19 +0200
|
|
|
|
Received: from [23.143.35.123] (helo=example.com)
|
|
|
|
by example.com with smtp (Exim 4.89)
|
|
|
|
(envelope-from <test@example.com>)
|
|
|
|
id 1jlC1e-0005ro-PL
|
|
|
|
for hood@localhost; Tue, 16 Jun 2020 15:33:19 +0200
|
|
|
|
Message-ID: <B5F50812.F55DFD8B@example.com>
|
|
|
|
Date: Tue, 16 Jun 2020 06:53:19 -0700
|
|
|
|
Reply-To: "Test" <test@example.com>
|
|
|
|
From: "Test" <test@example.com>
|
|
|
|
MIME-Version: 1.0
|
|
|
|
To: <hood@localhost>
|
|
|
|
Subject: Chat: test
|
2020-07-13 01:57:27 +00:00
|
|
|
Content-Type: multipart/mixed; boundary="AqNPlAX243a8sip3B7kXv8UKD8wuti"
|
|
|
|
|
|
|
|
|
|
|
|
--AqNPlAX243a8sip3B7kXv8UKD8wuti
|
|
|
|
Content-Type: text/plain; charset=utf-8
|
2020-07-12 18:29:28 +00:00
|
|
|
|
2020-07-13 01:57:27 +00:00
|
|
|
test
|
|
|
|
|
|
|
|
--AqNPlAX243a8sip3B7kXv8UKD8wuti--
|
|
|
|
"""
|
|
|
|
proc = subprocess.run(
|
2023-03-18 17:47:02 +00:00
|
|
|
["kibicara_mda", "hood"], stdout=subprocess.PIPE, input=mail, encoding="ascii"
|
2020-07-13 01:57:27 +00:00
|
|
|
)
|
|
|
|
assert proc.returncode == 0
|