1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-04 10:19:16 +02:00

a first multi-account test, no real checks yet

This commit is contained in:
holger krekel 2018-09-07 20:53:12 +02:00
parent b153089d4c
commit 823d1283ac
5 changed files with 76 additions and 21 deletions

View file

@ -1,6 +1,7 @@
import distutils.ccompiler import distutils.ccompiler
import distutils.sysconfig import distutils.sysconfig
import tempfile import tempfile
import re
from os.path import dirname, abspath from os.path import dirname, abspath
from os.path import join as joinpath from os.path import join as joinpath
@ -11,16 +12,8 @@ deltah = joinpath(dirname(dirname(dirname(here))), "src", "deltachat.h")
def read_event_defines(): def read_event_defines():
for line in open(deltah): rex = re.compile(r'#define\s+(?:DC_EVENT_|DC_CONTACT_ID_)\S+\s+(\d+).*')
if line.startswith("#define"): return filter(rex.match, open(deltah))
parts = line.split()
if len(parts) >= 3:
if parts[1].startswith("DC_EVENT"):
try:
int(parts[2])
except ValueError:
continue
yield line
def ffibuilder(): def ffibuilder():

View file

@ -6,11 +6,16 @@ from .capi import ffi
import deltachat import deltachat
def eventprinter(evt): class EventPrinter:
def __init__(self, dc_context):
self.dc_context = dc_context
self.info = str(self.dc_context).strip(">").split()[-1]
def __call__(self, evt):
evt_name, data1, data2 = evt evt_name, data1, data2 = evt
t = threading.currentThread() t = threading.currentThread()
tname = getattr(t, "name", t) tname = getattr(t, "name", t)
print("[" + tname + "]", evt_name, data1, data2) print("[%s-%s]" % (tname, self.info), evt_name, data1, data2)
class EventHandler: class EventHandler:
@ -46,11 +51,11 @@ class Contact:
@property @property
def addr(self): def addr(self):
return ffi.string(capi.lib.dc_contact_get_addr(self.dc_contact_t)) return ffi_unicode(capi.lib.dc_contact_get_addr(self.dc_contact_t))
@property @property
def display_name(self): def display_name(self):
return ffi.string(capi.lib.dc_contact_get_display_name(self.dc_contact_t)) return ffi_unicode(capi.lib.dc_contact_get_display_name(self.dc_contact_t))
@property @property
def is_blocked(self): def is_blocked(self):
@ -61,6 +66,16 @@ class Contact:
return capi.lib.dc_contact_is_verified(self.dc_contact_t) return capi.lib.dc_contact_is_verified(self.dc_contact_t)
class Chat:
def __init__(self, dc_context, chat_id):
self.dc_context = dc_context
self.id = chat_id
def send_text_message(self, msg):
msg = convert_bytes(msg)
return capi.lib.dc_send_text_msg(self.dc_context, self.id, msg)
class Account: class Account:
def __init__(self, db_path, logcallback=None, eventhandler=None): def __init__(self, db_path, logcallback=None, eventhandler=None):
self.dc_context = ctx = capi.lib.dc_context_new( self.dc_context = ctx = capi.lib.dc_context_new(
@ -69,7 +84,9 @@ class Account:
if hasattr(db_path, "encode"): if hasattr(db_path, "encode"):
db_path = db_path.encode("utf8") db_path = db_path.encode("utf8")
capi.lib.dc_open(ctx, db_path, capi.ffi.NULL) capi.lib.dc_open(ctx, db_path, capi.ffi.NULL)
self._logcallback = logcallback or eventprinter if logcallback is None:
logcallback = EventPrinter(self.dc_context)
self._logcallback = logcallback
if eventhandler is None: if eventhandler is None:
eventhandler = EventHandler(self.dc_context) eventhandler = EventHandler(self.dc_context)
self._eventhandler = eventhandler self._eventhandler = eventhandler
@ -84,12 +101,21 @@ class Account:
def get_config(self, name): def get_config(self, name):
name = name.encode("utf8") name = name.encode("utf8")
res = capi.lib.dc_get_config(self.dc_context, name, b'') res = capi.lib.dc_get_config(self.dc_context, name, b'')
return capi.ffi.string(res).decode("utf8") return ffi_unicode(res)
def get_self_contact(self):
return Contact(self.dc_context, capi.lib.DC_CONTACT_ID_SELF)
def create_contact(self, emailadr, name=ffi.NULL): def create_contact(self, emailadr, name=ffi.NULL):
name = convert_bytes(name)
emailadr = convert_bytes(emailadr)
contact_id = capi.lib.dc_create_contact(self.dc_context, name, emailadr) contact_id = capi.lib.dc_create_contact(self.dc_context, name, emailadr)
return Contact(self.dc_context, contact_id) return Contact(self.dc_context, contact_id)
def create_chat_by_contact(self, contact):
chat_id = capi.lib.dc_create_chat_by_contact_id(self.dc_context, contact.id)
return Chat(self.dc_context, chat_id)
def start(self): def start(self):
deltachat.set_context_callback(self.dc_context, self._process_event) deltachat.set_context_callback(self.dc_context, self._process_event)
capi.lib.dc_configure(self.dc_context) capi.lib.dc_configure(self.dc_context)
@ -154,3 +180,15 @@ class IOThreads:
while not self._thread_quitflag: while not self._thread_quitflag:
capi.lib.dc_perform_smtp_jobs(self.dc_context) capi.lib.dc_perform_smtp_jobs(self.dc_context)
capi.lib.dc_perform_smtp_idle(self.dc_context) capi.lib.dc_perform_smtp_idle(self.dc_context)
def convert_bytes(obj):
if obj == ffi.NULL:
return obj
if not isinstance(obj, bytes):
return obj.encode("utf8")
return obj
def ffi_unicode(obj):
return ffi.string(obj).decode("utf8")

View file

@ -8,6 +8,13 @@ except ImportError:
class TestLive: class TestLive:
def test_selfcontact(self, acfactory):
ac1 = acfactory.get_live_account(started=False)
me = ac1.get_self_contact()
assert me.display_name
# assert me.addr # xxx why is this empty?
def test_contacts(self, acfactory): def test_contacts(self, acfactory):
ac1 = acfactory.get_live_account(started=False) ac1 = acfactory.get_live_account(started=False)
contact1 = ac1.create_contact("some1@hello.com", name="some1") contact1 = ac1.create_contact("some1@hello.com", name="some1")
@ -17,6 +24,12 @@ class TestLive:
assert not contact1.is_blocked assert not contact1.is_blocked
assert not contact1.is_verified assert not contact1.is_verified
def test_chat(self, acfactory):
ac1 = acfactory.get_live_account(started=False)
contact1 = ac1.create_contact("some1@hello.com", name="some1")
chat = ac1.create_chat_by_contact(contact1)
assert chat.id
def test_basic_configure_login_ok(self, acfactory): def test_basic_configure_login_ok(self, acfactory):
q = Queue() q = Queue()
ac1 = acfactory.get_live_account(logcallback=q.put) ac1 = acfactory.get_live_account(logcallback=q.put)
@ -32,3 +45,13 @@ class TestLive:
if re.match("smtp-login.*ok.", data2.lower()): if re.match("smtp-login.*ok.", data2.lower()):
smtp_ok = True smtp_ok = True
assert ac1.get_config("mail_pw") assert ac1.get_config("mail_pw")
def test_send_message(self, acfactory):
ac1 = acfactory.get_live_account()
ac2 = acfactory.get_live_account()
c2 = ac2.get_self_contact()
chat = ac1.create_chat_by_contact(c2)
import time
time.sleep(5)
print ("sending test message")
chat.send_text_message("msg1")

View file

@ -9,3 +9,4 @@ def test_empty_context():
def test_event_defines(): def test_event_defines():
assert capi.lib.DC_EVENT_INFO == 100 assert capi.lib.DC_EVENT_INFO == 100
assert capi.lib.DC_CONTACT_ID_SELF

View file

@ -3,7 +3,7 @@ minversion = 2.0
distshare = {homedir}/.tox/distshare distshare = {homedir}/.tox/distshare
# make sure to update environment list in travis.yml and appveyor.yml # make sure to update environment list in travis.yml and appveyor.yml
envlist = envlist =
linting lint
py27 py27
py35 py35
@ -14,7 +14,7 @@ deps =
pytest pytest
pdbpp pdbpp
[testenv:linting] [testenv:lint]
skipsdist = True skipsdist = True
usedevelop = True usedevelop = True
basepython = python2.7 basepython = python2.7