mirror of
https://github.com/chatmail/relay.git
synced 2026-05-19 20:38:05 +00:00
make notifier take a directory
This commit is contained in:
@@ -23,13 +23,17 @@ DICTPROXY_TRANSACTION_CHARS = "SBC"
|
|||||||
|
|
||||||
|
|
||||||
class Notifier:
|
class Notifier:
|
||||||
def __init__(self):
|
def __init__(self, metadata_dir):
|
||||||
|
self.metadata_dir = metadata_dir
|
||||||
self.guid2token = {}
|
self.guid2token = {}
|
||||||
self.to_notify_queue = Queue()
|
self.to_notify_queue = Queue()
|
||||||
|
|
||||||
def set_token(self, guid, token):
|
def set_token(self, guid, token):
|
||||||
self.guid2token[guid] = token
|
self.guid2token[guid] = token
|
||||||
|
|
||||||
|
def get_token(self, guid):
|
||||||
|
return self.guid2token.get(guid)
|
||||||
|
|
||||||
def new_message_for_guid(self, guid):
|
def new_message_for_guid(self, guid):
|
||||||
self.to_notify_queue.put(guid)
|
self.to_notify_queue.put(guid)
|
||||||
|
|
||||||
@@ -40,7 +44,7 @@ class Notifier:
|
|||||||
|
|
||||||
def thread_run_one(self, requests_session):
|
def thread_run_one(self, requests_session):
|
||||||
guid = self.to_notify_queue.get()
|
guid = self.to_notify_queue.get()
|
||||||
token = self.guid2token.get(guid)
|
token = self.get_token(guid)
|
||||||
if token:
|
if token:
|
||||||
response = requests_session.post(
|
response = requests_session.post(
|
||||||
"https://notifications.delta.chat/notify",
|
"https://notifications.delta.chat/notify",
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import io
|
import io
|
||||||
|
import pytest
|
||||||
|
|
||||||
from chatmaild.metadata import (
|
from chatmaild.metadata import (
|
||||||
handle_dovecot_request,
|
handle_dovecot_request,
|
||||||
@@ -7,14 +8,20 @@ from chatmaild.metadata import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_handle_dovecot_request_lookup_fails():
|
@pytest.fixture
|
||||||
notifier = Notifier()
|
def notifier(tmp_path):
|
||||||
|
metadata_dir = tmp_path.joinpath("metadata")
|
||||||
|
if not metadata_dir.exists():
|
||||||
|
metadata_dir.mkdir()
|
||||||
|
return Notifier(metadata_dir)
|
||||||
|
|
||||||
|
|
||||||
|
def test_handle_dovecot_request_lookup_fails(notifier):
|
||||||
res = handle_dovecot_request("Lpriv/123/chatmail", {}, notifier)
|
res = handle_dovecot_request("Lpriv/123/chatmail", {}, notifier)
|
||||||
assert res == "N\n"
|
assert res == "N\n"
|
||||||
|
|
||||||
|
|
||||||
def test_handle_dovecot_request_happy_path():
|
def test_handle_dovecot_request_happy_path(notifier):
|
||||||
notifier = Notifier()
|
|
||||||
transactions = {}
|
transactions = {}
|
||||||
|
|
||||||
# lookups return the same NOTFOUND result
|
# lookups return the same NOTFOUND result
|
||||||
@@ -52,7 +59,7 @@ def test_handle_dovecot_request_happy_path():
|
|||||||
assert not transactions
|
assert not transactions
|
||||||
|
|
||||||
|
|
||||||
def test_handle_dovecot_protocol_set_devicetoken():
|
def test_handle_dovecot_protocol_set_devicetoken(notifier):
|
||||||
rfile = io.BytesIO(
|
rfile = io.BytesIO(
|
||||||
b"\n".join(
|
b"\n".join(
|
||||||
[
|
[
|
||||||
@@ -64,13 +71,12 @@ def test_handle_dovecot_protocol_set_devicetoken():
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
wfile = io.BytesIO()
|
wfile = io.BytesIO()
|
||||||
notifier = Notifier()
|
|
||||||
handle_dovecot_protocol(rfile, wfile, notifier)
|
handle_dovecot_protocol(rfile, wfile, notifier)
|
||||||
assert notifier.guid2token["guid00"] == "01234"
|
assert notifier.guid2token["guid00"] == "01234"
|
||||||
assert wfile.getvalue() == b"O\n"
|
assert wfile.getvalue() == b"O\n"
|
||||||
|
|
||||||
|
|
||||||
def test_handle_dovecot_protocol_iterate():
|
def test_handle_dovecot_protocol_iterate(notifier):
|
||||||
rfile = io.BytesIO(
|
rfile = io.BytesIO(
|
||||||
b"\n".join(
|
b"\n".join(
|
||||||
[
|
[
|
||||||
@@ -80,12 +86,11 @@ def test_handle_dovecot_protocol_iterate():
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
wfile = io.BytesIO()
|
wfile = io.BytesIO()
|
||||||
notifier = Notifier()
|
|
||||||
handle_dovecot_protocol(rfile, wfile, notifier)
|
handle_dovecot_protocol(rfile, wfile, notifier)
|
||||||
assert wfile.getvalue() == b"\n"
|
assert wfile.getvalue() == b"\n"
|
||||||
|
|
||||||
|
|
||||||
def test_handle_dovecot_protocol_messagenew():
|
def test_handle_dovecot_protocol_messagenew(notifier):
|
||||||
rfile = io.BytesIO(
|
rfile = io.BytesIO(
|
||||||
b"\n".join(
|
b"\n".join(
|
||||||
[
|
[
|
||||||
@@ -97,14 +102,13 @@ def test_handle_dovecot_protocol_messagenew():
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
wfile = io.BytesIO()
|
wfile = io.BytesIO()
|
||||||
notifier = Notifier()
|
|
||||||
handle_dovecot_protocol(rfile, wfile, notifier)
|
handle_dovecot_protocol(rfile, wfile, notifier)
|
||||||
assert wfile.getvalue() == b"O\n"
|
assert wfile.getvalue() == b"O\n"
|
||||||
assert notifier.to_notify_queue.get() == "guid00"
|
assert notifier.to_notify_queue.get() == "guid00"
|
||||||
assert notifier.to_notify_queue.qsize() == 0
|
assert notifier.to_notify_queue.qsize() == 0
|
||||||
|
|
||||||
|
|
||||||
def test_notifier_thread_run():
|
def test_notifier_thread_run(notifier):
|
||||||
requests = []
|
requests = []
|
||||||
|
|
||||||
class ReqMock:
|
class ReqMock:
|
||||||
@@ -116,7 +120,6 @@ def test_notifier_thread_run():
|
|||||||
|
|
||||||
return Result()
|
return Result()
|
||||||
|
|
||||||
notifier = Notifier()
|
|
||||||
notifier.set_token("guid00", "01234")
|
notifier.set_token("guid00", "01234")
|
||||||
notifier.new_message_for_guid("guid00")
|
notifier.new_message_for_guid("guid00")
|
||||||
notifier.thread_run_one(ReqMock())
|
notifier.thread_run_one(ReqMock())
|
||||||
@@ -125,7 +128,7 @@ def test_notifier_thread_run():
|
|||||||
assert len(notifier.guid2token) == 1
|
assert len(notifier.guid2token) == 1
|
||||||
|
|
||||||
|
|
||||||
def test_notifier_thread_run_gone_removes_token():
|
def test_notifier_thread_run_gone_removes_token(notifier):
|
||||||
requests = []
|
requests = []
|
||||||
|
|
||||||
class ReqMock:
|
class ReqMock:
|
||||||
@@ -137,7 +140,6 @@ def test_notifier_thread_run_gone_removes_token():
|
|||||||
|
|
||||||
return Result()
|
return Result()
|
||||||
|
|
||||||
notifier = Notifier()
|
|
||||||
notifier.set_token("guid00", "01234")
|
notifier.set_token("guid00", "01234")
|
||||||
notifier.new_message_for_guid("guid00")
|
notifier.new_message_for_guid("guid00")
|
||||||
assert notifier.guid2token["guid00"] == "01234"
|
assert notifier.guid2token["guid00"] == "01234"
|
||||||
|
|||||||
Reference in New Issue
Block a user