import unittest from datetime import datetime from base64 import b64encode from pathlib import Path from tempfile import NamedTemporaryFile from hashlib import sha256 from slixmpp.test import SlixTest from slixmpp.plugins.xep_0082 import format_datetime class TestStatelessFileSharing(SlixTest): def setUp(self): self.stream_start( mode="component", jid="whatevs.shakespeare.lit", plugins={"xep_0447"} ) def test_set_file(self): with NamedTemporaryFile("wb+") as f: n = 10 size = 0 for i in range(n): size += len(bytes(i)) f.write(bytes(i)) f.seek(0) h = b64encode(sha256(f.read()).digest()).decode() sfs = self.xmpp["xep_0447"].get_sfs( Path(f.name), ["https://xxx.com"], media_type="MEDIA", desc="DESCRIPTION", ) self.check( sfs, f""" MEDIA {Path(f.name).name} {size} {h} DESCRIPTION {format_datetime(datetime.fromtimestamp(Path(f.name).stat().st_mtime))} """, use_values=False, ) suite = unittest.TestLoader().loadTestsFromTestCase(TestStatelessFileSharing)