119 lines
3.4 KiB
Python
119 lines
3.4 KiB
Python
"""Tests for small helpers in slack_export and server."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import json
|
|
import zipfile
|
|
|
|
import pytest
|
|
|
|
from slack_export import (
|
|
SlackExport,
|
|
load_json,
|
|
resolve_paths,
|
|
slack_ts_to_iso,
|
|
user_display,
|
|
)
|
|
|
|
|
|
def test_slack_ts_to_iso():
|
|
assert slack_ts_to_iso("1476210563.000002") is not None
|
|
assert "2016" in slack_ts_to_iso("1476210563.000002")
|
|
assert slack_ts_to_iso(None) is None
|
|
assert slack_ts_to_iso("not-a-ts") is None
|
|
|
|
|
|
def test_user_display_fallbacks():
|
|
assert user_display(None) == "unknown"
|
|
assert user_display({"name": "bot", "is_bot": True}) == "bot"
|
|
assert user_display({"profile": {"display_name": "Ilia"}, "name": "jedi"}) == "Ilia"
|
|
|
|
|
|
def test_resolve_paths_custom_root(fixture_export_path):
|
|
paths = resolve_paths(fixture_export_path)
|
|
assert paths.root == fixture_export_path.resolve()
|
|
assert paths.channels_json.name == "channels.json"
|
|
|
|
|
|
def test_load_json_roundtrip(tmp_path):
|
|
p = tmp_path / "x.json"
|
|
p.write_text('{"a": 1}', encoding="utf-8")
|
|
assert load_json(p) == {"a": 1}
|
|
|
|
|
|
def test_server_manifest_helpers(tmp_path, fixture_export_path):
|
|
import server
|
|
|
|
path = tmp_path / "m.json"
|
|
default = server.default_manifest(str(fixture_export_path))
|
|
assert default["version"] == 1
|
|
|
|
loaded = server.load_manifest(path, str(fixture_export_path))
|
|
assert loaded["export_path"] == str(fixture_export_path)
|
|
|
|
path.write_text("{not json", encoding="utf-8")
|
|
with pytest.raises(json.JSONDecodeError):
|
|
server.load_manifest(path, str(fixture_export_path))
|
|
|
|
server.save_manifest(path, {"version": 1, "channels": {}})
|
|
assert path.is_file()
|
|
assert "updated_at" in load_json(path)
|
|
|
|
|
|
def test_file_preview_payload_bytes_text_and_html():
|
|
import server
|
|
|
|
txt = server.file_preview_payload_bytes(b"hello world", "notes.txt")
|
|
assert txt["ok"] and "hello" in txt["html"]
|
|
|
|
html = server.file_preview_payload_bytes(
|
|
b"<html><body><h1>Title</h1></body></html>", "page.html"
|
|
)
|
|
assert html["ok"] and html["kind"] == "html"
|
|
|
|
bad = server.file_preview_payload_bytes(b"\x00\x01\xff", "binary.dat")
|
|
assert not bad["ok"]
|
|
|
|
|
|
def test_file_preview_payload_bytes_docx(tmp_path):
|
|
import server
|
|
|
|
buf = io.BytesIO()
|
|
with zipfile.ZipFile(buf, "w") as zf:
|
|
zf.writestr(
|
|
"word/document.xml",
|
|
'<?xml version="1.0"?>'
|
|
'<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">'
|
|
"<w:p><w:r><w:t>Lease terms</w:t></w:r></w:p></w:document>",
|
|
)
|
|
raw = buf.getvalue()
|
|
out = server.file_preview_payload_bytes(raw, "lease.docx")
|
|
assert out["ok"]
|
|
assert "Lease" in out["html"]
|
|
|
|
path = tmp_path / "lease.docx"
|
|
path.write_bytes(raw)
|
|
assert "Lease" in server.file_preview_payload(path)["html"]
|
|
|
|
|
|
def test_docx_to_html_file(tmp_path):
|
|
import server
|
|
|
|
buf = io.BytesIO()
|
|
with zipfile.ZipFile(buf, "w") as zf:
|
|
zf.writestr(
|
|
"word/document.xml",
|
|
'<?xml version="1.0"?>'
|
|
'<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">'
|
|
"<w:p><w:r><w:t>Line one</w:t></w:r></w:p></w:document>",
|
|
)
|
|
path = tmp_path / "t.docx"
|
|
path.write_bytes(buf.getvalue())
|
|
assert "Line one" in server.docx_to_html(path)
|
|
|
|
|
|
def test_export_missing_root_raises():
|
|
with pytest.raises(FileNotFoundError):
|
|
SlackExport("/nonexistent/export/path")
|