mirror of
https://github.com/chatmail/relay.git
synced 2026-05-19 12:28:06 +00:00
write a happy path test
This commit is contained in:
@@ -11,28 +11,58 @@ import os
|
|||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
DICTPROXY_LOOKUP_CHAR = "L"
|
||||||
|
DICTPROXY_SET_CHAR = "S"
|
||||||
|
DICTPROXY_BEGIN_TRANSACTION_CHAR = "B"
|
||||||
|
DICTPROXY_COMMIT_TRANSACTION_CHAR = "C"
|
||||||
|
DICTPROXY_TRANSACTION_CHARS = "SBC"
|
||||||
|
|
||||||
|
|
||||||
def handle_dovecot_protocol(rfile, wfile, tokens, requests_session, config: Config):
|
def handle_dovecot_protocol(rfile, wfile, tokens, requests_session, config: Config):
|
||||||
# HELLO message, ignored.
|
# HELLO message, ignored.
|
||||||
msg = rfile.readline().strip().decode()
|
msg = rfile.readline().strip().decode()
|
||||||
|
|
||||||
transactions = {}
|
def post_with_token(token):
|
||||||
|
response = requests_session.post(
|
||||||
|
"https://notifications.delta.chat/notify",
|
||||||
|
data=token,
|
||||||
|
timeout=60,
|
||||||
|
)
|
||||||
|
if response.status_code == 410:
|
||||||
|
# 410 Gone status code
|
||||||
|
# means the token is no longer valid.
|
||||||
|
del tokens[guid]
|
||||||
|
|
||||||
|
transactions = {}
|
||||||
while True:
|
while True:
|
||||||
msg = rfile.readline().strip().decode()
|
msg = rfile.readline().strip().decode()
|
||||||
if not msg:
|
if not msg:
|
||||||
break
|
break
|
||||||
|
|
||||||
res = handle_dovecot_request(msg, tokens, requests_session, config)
|
res = handle_dovecot_request(msg, transactions, tokens, post_with_token)
|
||||||
if res:
|
if res:
|
||||||
wfile.write(res.encode("ascii"))
|
wfile.write(res.encode("ascii"))
|
||||||
wfile.flush()
|
wfile.flush()
|
||||||
|
|
||||||
|
|
||||||
def handle_dovecot_request(msg, tokens, requests_session, config: Config):
|
def handle_dovecot_request(msg, transactions, tokens, post_with_token):
|
||||||
|
# see https://doc.dovecot.org/3.0/developer_manual/design/dict_protocol/
|
||||||
short_command = msg[0]
|
short_command = msg[0]
|
||||||
if short_command == "L":
|
parts = msg[1:].split("\t")
|
||||||
|
if short_command == DICTPROXY_LOOKUP_CHAR:
|
||||||
return b"N\n"
|
return b"N\n"
|
||||||
elif short_command == "S":
|
|
||||||
|
if short_command not in (DICTPROXY_TRANSACTION_CHARS):
|
||||||
|
return
|
||||||
|
|
||||||
|
transaction_id = parts[0]
|
||||||
|
|
||||||
|
if short_command == DICTPROXY_BEGIN_TRANSACTION_CHAR:
|
||||||
|
transactions[transaction_id] = b"O\n"
|
||||||
|
elif short_command == DICTPROXY_COMMIT_TRANSACTION_CHAR:
|
||||||
|
# returns whether it failed or succeeded.
|
||||||
|
return transactions.pop(transaction_id, b"N\n")
|
||||||
|
elif short_command == DICTPROXY_SET_CHAR:
|
||||||
# See header of
|
# See header of
|
||||||
# <https://github.com/dovecot/core/blob/5e7965632395793d9355eb906b173bf28d2a10ca/src/lib-storage/mailbox-attribute.h>
|
# <https://github.com/dovecot/core/blob/5e7965632395793d9355eb906b173bf28d2a10ca/src/lib-storage/mailbox-attribute.h>
|
||||||
# for the documentation on the structure of the key.
|
# for the documentation on the structure of the key.
|
||||||
@@ -45,8 +75,6 @@ def handle_dovecot_request(msg, tokens, requests_session, config: Config):
|
|||||||
# results in
|
# results in
|
||||||
# priv/dd72550f05eadc65542a1200cac67ad7/vendor/vendor.dovecot/pvt/server/chatmail
|
# priv/dd72550f05eadc65542a1200cac67ad7/vendor/vendor.dovecot/pvt/server/chatmail
|
||||||
|
|
||||||
parts = msg[1:].split("\t")
|
|
||||||
transaction_id = parts[0]
|
|
||||||
keyname = parts[1].split("/")
|
keyname = parts[1].split("/")
|
||||||
value = parts[2] if len(parts) > 2 else ""
|
value = parts[2] if len(parts) > 2 else ""
|
||||||
if keyname[0] == "priv" and keyname[2] == "devicetoken":
|
if keyname[0] == "priv" and keyname[2] == "devicetoken":
|
||||||
@@ -55,26 +83,10 @@ def handle_dovecot_request(msg, tokens, requests_session, config: Config):
|
|||||||
guid = keyname[1]
|
guid = keyname[1]
|
||||||
token = tokens.get(guid)
|
token = tokens.get(guid)
|
||||||
if token:
|
if token:
|
||||||
response = requests_session.post(
|
post_with_token(token)
|
||||||
"https://notifications.delta.chat/notify",
|
|
||||||
data=token,
|
|
||||||
timeout=60,
|
|
||||||
)
|
|
||||||
if response.status_code == 410:
|
|
||||||
# 410 Gone status code
|
|
||||||
# means the token is no longer valid.
|
|
||||||
del tokens[guid]
|
|
||||||
else:
|
else:
|
||||||
# Transaction failed.
|
# Transaction failed.
|
||||||
transactions[transaction_id] = b"F\n"
|
transactions[transaction_id] = b"F\n"
|
||||||
elif short_command == "B":
|
|
||||||
# Begin transaction.
|
|
||||||
transaction_id = msg[1:].split("\t")[0]
|
|
||||||
transactions[transaction_id] = b"O\n"
|
|
||||||
elif short_command == "C":
|
|
||||||
# Commit transaction.
|
|
||||||
transaction_id = msg[1:].split("\t")[0]
|
|
||||||
return transactions.pop(transaction_id, b"N\n")
|
|
||||||
|
|
||||||
|
|
||||||
class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer):
|
class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer):
|
||||||
@@ -84,6 +96,8 @@ class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer):
|
|||||||
def main():
|
def main():
|
||||||
socket, username, config = sys.argv[1:]
|
socket, username, config = sys.argv[1:]
|
||||||
passwd_entry = pwd.getpwnam(username)
|
passwd_entry = pwd.getpwnam(username)
|
||||||
|
|
||||||
|
# XXX config is not currently used
|
||||||
config = read_config(config)
|
config = read_config(config)
|
||||||
tokens = {}
|
tokens = {}
|
||||||
requests_session = requests.Session()
|
requests_session = requests.Session()
|
||||||
|
|||||||
41
chatmaild/src/chatmaild/tests/test_metadata.py
Normal file
41
chatmaild/src/chatmaild/tests/test_metadata.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
from chatmaild.metadata import (
|
||||||
|
handle_dovecot_request,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_handle_dovecot_request_happy_path():
|
||||||
|
tokens = {}
|
||||||
|
transactions = {}
|
||||||
|
|
||||||
|
# lookups return the same NOTFOUND result
|
||||||
|
res = handle_dovecot_request("Lpriv/123/chatmail", transactions, tokens, None)
|
||||||
|
assert res == b"N\n"
|
||||||
|
assert not tokens and not transactions
|
||||||
|
|
||||||
|
# set device token in a transaction
|
||||||
|
tx = "1111"
|
||||||
|
msg = f"B{tx}\tuser"
|
||||||
|
res = handle_dovecot_request(msg, transactions, tokens, None)
|
||||||
|
assert not res and not tokens
|
||||||
|
assert transactions == {tx: b"O\n"}
|
||||||
|
|
||||||
|
msg = f"S{tx}\tpriv/guid00/devicetoken\t01234"
|
||||||
|
res = handle_dovecot_request(msg, transactions, tokens, None)
|
||||||
|
assert not res
|
||||||
|
assert len(transactions) == 1
|
||||||
|
assert len(tokens) == 1 and tokens["guid00"] == "01234"
|
||||||
|
|
||||||
|
msg = f"C{tx}"
|
||||||
|
res = handle_dovecot_request(msg, transactions, tokens, None)
|
||||||
|
assert res == b"O\n"
|
||||||
|
assert len(transactions) == 0
|
||||||
|
assert tokens["guid00"] == "01234"
|
||||||
|
|
||||||
|
# trigger notification for incoming message
|
||||||
|
assert handle_dovecot_request(f"B{tx}\tuser", transactions, tokens, None) is None
|
||||||
|
msg = f"S{tx}\tpriv/guid00/messagenew"
|
||||||
|
requests = []
|
||||||
|
assert handle_dovecot_request(msg, transactions, tokens, requests.append) is None
|
||||||
|
assert requests == ["01234"]
|
||||||
|
assert handle_dovecot_request(f"C{tx}\tuser", transactions, tokens, None) == b"O\n"
|
||||||
|
assert not transactions
|
||||||
Reference in New Issue
Block a user