Commit a86e2740 authored by David Douard's avatar David Douard
Browse files

[test] refactor a bit unittest_mboximport.py

and make it work with cw 3.22
parent e7f359dbd0dd
"""unit tests for email mbox import functionnality"""
import sys
import unittest
from os import mkdir
from os.path import join, dirname, exists, abspath
from io import BytesIO
from logilab.common.testlib import TestCase, unittest_main
from cwclientlib import cwproxy
from cubicweb.devtools.httptest import CubicWebServerTC
from cwclientlib.cwproxy import CWProxy, SignedRequestAuth
try:
from cwemail.mboximport import Importer
......@@ -18,24 +15,47 @@ except ImportError:
from cwemail.mboximport import Importer
# make this test module importable even if cw is not available,
# otherwise, just skip the tests
try:
from cubicweb.devtools.httptest import CubicWebServerTC
except ImportError:
CubicWebServerTC = object
def setUpModule():
if CubicWebServerTC is object:
raise unittest.SkipTest('cubicweb is missing')
class MBOXImporterTC(CubicWebServerTC):
token_id = u'mboximport'
test_db_id = u'mboximport'
@classmethod
def pre_setup_database(cls, cnx, config):
cnx.create_entity('AuthToken', id=cls.token_id,
enabled=True,
token_for_user=cnx.user.eid)
cnx.commit()
def setUp(self):
super(MBOXImporterTC, self).setUp()
with self.admin_access.repo_cnx() as cnx:
cnx.create_entity('AuthToken', enabled=True, id=self.token_id, token_for_user=cnx.user)
cnx.commit()
self.secret = cnx.execute('String X WHERE T token X')[0][0]
with self.admin_access.client_cnx() as cnx:
token = cnx.find('AuthToken', id=self.token_id).one()
self.admineid = cnx.user.eid
self.token = token.token
auth = SignedRequestAuth(token_id=self.token_id,
secret=self.token)
self.client = CWProxy(base_url=self.config['base-url'],
auth=auth)
def test_mbox_format(self):
mi = Importer(cwproxy.CWProxy(self.config['base-url'], auth=cwproxy.SignedRequestAuth(self.token_id, self.secret)))
mi = Importer(self.client)
mi.import_mbox(self.datapath('mbox'))
with self.admin_access.client_cnx() as cnx:
self._test_base(cnx)
def test_maildir_format(self):
mi = Importer(cwproxy.CWProxy(self.config['base-url'], auth=cwproxy.SignedRequestAuth(self.token_id, self.secret)))
mi = Importer(self.client)
maildirpath = self.datapath('maildir')
newpath = join(maildirpath, 'new')
if not exists(newpath):
......@@ -201,7 +221,7 @@ http://lists.logilab.org/mailman/listinfo/python-projects
"""
def test_no_subject(self):
mi = Importer(cwproxy.CWProxy(self.config['base-url'], auth=cwproxy.SignedRequestAuth(self.token_id, self.secret)))
mi = Importer(self.client)
mi.import_mbox_stream(BytesIO(self.NOSUBJECT))
with self.admin_access.client_cnx() as cnx:
rset = cnx.execute('Any X ORDERBY S WHERE X is Email, X subject S')
......@@ -211,7 +231,7 @@ http://lists.logilab.org/mailman/listinfo/python-projects
self.assertEqual(email.references(), set(('<xxx@blabla>',)))
def test_double_import(self):
mi = Importer(cwproxy.CWProxy(self.config['base-url'], auth=cwproxy.SignedRequestAuth(self.token_id, self.secret)))
mi = Importer(self.client)
mi.import_mbox_stream(BytesIO(self.NOSUBJECT))
mi.import_mbox_stream(BytesIO(self.NOSUBJECT))
with self.admin_access.client_cnx() as cnx:
......@@ -234,7 +254,7 @@ http://lists.logilab.org/mailman/listinfo/python-projects
#testrset = cursor.execute('Any X WHERE X identical_to Y, X canonical TRUE, Y eid %(y)s', {'y': eid1})
#self.assertEqual(testrset[0][0], eid2)
cnx.commit()
mi = Importer(cwproxy.CWProxy(self.config['base-url'], auth=cwproxy.SignedRequestAuth(self.token_id, self.secret)))
mi = Importer(self.client)
mi.import_mbox_stream(BytesIO(self.NOSUBJECT))
with self.admin_access.client_cnx() as cnx:
rset = cnx.execute('Any X ORDERBY S WHERE X is Email, X subject S')
......@@ -244,5 +264,4 @@ http://lists.logilab.org/mailman/listinfo/python-projects
if __name__ == '__main__':
unittest_main()
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment