summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_logger.py90
1 files changed, 84 insertions, 6 deletions
diff --git a/test/test_logger.py b/test/test_logger.py
index 09ba720e..d583733e 100644
--- a/test/test_logger.py
+++ b/test/test_logger.py
@@ -2,8 +2,76 @@
Test the functions in the `logger` module
"""
import datetime
-from poezio.logger import LogMessage, parse_log_line, parse_log_lines, build_log_message
-from poezio.common import get_utc_time, get_local_time
+from pathlib import Path
+from random import sample
+from shutil import rmtree
+from string import hexdigits
+from poezio import logger
+from poezio.logger import (
+ LogMessage, parse_log_line, parse_log_lines, build_log_message
+)
+from poezio.ui.types import Message
+from poezio.common import get_utc_time
+import pytest
+
+
+class ConfigShim:
+ def __init__(self, value):
+ self.value = value
+
+ def get_by_tabname(self, name, *args, **kwargs):
+ return self.value
+
+
+logger.config = ConfigShim(True)
+
+
+@pytest.fixture
+def log_dir():
+ name = 'tmplog-' + ''.join(sample(hexdigits, 16))
+ path = Path('/tmp', name)
+ try:
+ yield path
+ finally:
+ rmtree(path, ignore_errors=True)
+
+
+def read_file(logger, name):
+ if '/' in name:
+ name = name.replace('/', '\\')
+ filename = logger.log_dir / f'{name}'
+ with open(filename) as fd:
+ return fd.read()
+
+
+def test_log_roster(log_dir):
+ instance = logger.Logger()
+ instance.log_dir = log_dir
+ instance.log_roster_change('toto@example.com', 'test test')
+ content = read_file(instance, 'roster.log')
+ assert content[:3] == 'MI '
+ assert content[-32:] == ' 000 toto@example.com test test\n'
+
+
+def test_log_message(log_dir):
+ instance = logger.Logger()
+ instance.log_dir = log_dir
+ msg = Message('content', 'toto')
+ instance.log_message('toto@example.com', msg)
+ content = read_file(instance, 'toto@example.com')
+ line = parse_log_lines(content.split('\n'), '')[0]
+ assert line['nickname'] == 'toto'
+ assert line['txt'] == 'content'
+ msg2 = Message('content\ncontent2', 'titi')
+ instance.log_message('toto@example.com', msg2)
+ content = read_file(instance, 'toto@example.com')
+ lines = parse_log_lines(content.split('\n'), '')
+
+ assert lines[0]['nickname'] == 'toto'
+ assert lines[0]['txt'] == 'content'
+ assert lines[1]['nickname'] == 'titi'
+ assert lines[1]['txt'] == 'content\ncontent2'
+
def test_parse_message():
line = 'MR 20170909T09:09:09Z 000 <nick>  body'
@@ -17,17 +85,27 @@ def test_parse_message():
def test_log_and_parse_messages():
- msg1 = {'nick': 'toto', 'msg': 'coucou', 'date': datetime.datetime.now().replace(microsecond=0)}
+ msg1 = {
+ 'nick': 'toto',
+ 'msg': 'coucou',
+ 'date': datetime.datetime.now().replace(microsecond=0),
+ 'prefix': 'MR',
+ }
msg1_utc = get_utc_time(msg1['date'])
built_msg1 = build_log_message(**msg1)
assert built_msg1 == 'MR %s 000 <toto>  coucou\n' % (msg1_utc.strftime('%Y%m%dT%H:%M:%SZ'))
- msg2 = {'nick': 'toto', 'msg': 'coucou\ncoucou', 'date': datetime.datetime.now().replace(microsecond=0)}
+ msg2 = {
+ 'nick': 'toto',
+ 'msg': 'coucou\ncoucou',
+ 'date': datetime.datetime.now().replace(microsecond=0),
+ 'prefix': 'MR',
+ }
built_msg2 = build_log_message(**msg2)
msg2_utc = get_utc_time(msg2['date'])
assert built_msg2 == 'MR %s 001 <toto>  coucou\n coucou\n' % (msg2_utc.strftime('%Y%m%dT%H:%M:%SZ'))
assert parse_log_lines((built_msg1 + built_msg2).split('\n'), 'user@domain') == [
- {'time': msg1['date'], 'history': True, 'txt': '\x195,-1}coucou', 'nickname': 'toto'},
- {'time': msg2['date'], 'history': True, 'txt': '\x195,-1}coucou\ncoucou', 'nickname': 'toto'},
+ {'time': msg1['date'], 'history': True, 'txt': 'coucou', 'nickname': 'toto'},
+ {'time': msg2['date'], 'history': True, 'txt': 'coucou\ncoucou', 'nickname': 'toto'},
]