slightly reorga
authorVolker Birk <vb@pep.foundation>
Fri, 05 Jan 2018 19:39:46 +0100
changeset 2352c28e9e3c7fcb
parent 2351 b59bc7d1baa3
child 2353 88c29a4816bb
slightly reorga
test/python_tests/conftest.py
test/python_tests/setup_test.py
test/python_tests/test1.py
test/python_tests/test_1.py
test/python_tests/test_setup.py
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/python_tests/conftest.py	Fri Jan 05 19:39:46 2018 +0100
     1.3 @@ -0,0 +1,11 @@
     1.4 +# -*- coding: utf-8 -*-
     1.5 +
     1.6 +# this file is under GNU General Public License 3.0
     1.7 +# Copyleft 2017, p≡p foundation
     1.8 +
     1.9 +
    1.10 +from setup_test import create_homes
    1.11 +
    1.12 +def pytest_runtest_setup(item):
    1.13 +    create_homes()
    1.14 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/python_tests/setup_test.py	Fri Jan 05 19:39:46 2018 +0100
     2.3 @@ -0,0 +1,109 @@
     2.4 +#!/usr/bin/env python3
     2.5 +# -*- coding: utf-8 -*-
     2.6 +
     2.7 +# this file is under GNU General Public License 3.0
     2.8 +# Copyleft 2017, p≡p foundation
     2.9 +
    2.10 +#import vimpdb; vimpdb.set_trace()
    2.11 +
    2.12 +
    2.13 +"""
    2.14 +This script is setting up the test environment by creating two home directories
    2.15 +for simulation of message exchange between two p≡p parties.
    2.16 +
    2.17 +The directories are created in the current directory.
    2.18 +"""
    2.19 +
    2.20 +
    2.21 +import os
    2.22 +import shutil
    2.23 +from multiprocessing import Process
    2.24 +
    2.25 +
    2.26 +realhome = os.path.expanduser("~")
    2.27 +mydir = os.path.abspath(os.path.curdir)
    2.28 +
    2.29 +
    2.30 +def link_if_exists(dirname, arthome):
    2.31 +    "link directory from home to artificial home"
    2.32 +
    2.33 +    orig = os.path.join(realhome, dirname)
    2.34 +    if os.path.exists(orig):
    2.35 +        if not os.path.exists(dirname):
    2.36 +            os.symlink(orig, dirname, True)
    2.37 +
    2.38 +
    2.39 +def create_own_identities(mydir, arthome, username):
    2.40 +    "create own identities as part of the test setup"
    2.41 +
    2.42 +    os.environ["HOME"] = os.path.join(mydir, arthome)
    2.43 +    os.environ["GNUPGHOME"] = os.path.join(mydir, arthome, '.gnupg')
    2.44 +
    2.45 +    import pEp
    2.46 +    me = pEp.Identity()
    2.47 +    me.address = arthome + "@peptest.ch"
    2.48 +    me.username = username
    2.49 +
    2.50 +    pEp.myself(me)
    2.51 +    print(repr(me))
    2.52 +
    2.53 +
    2.54 +def link_file(filename):
    2.55 +    "sym-link file to version in parent directory"
    2.56 +
    2.57 +    src = os.path.join(os.pardir, filename)
    2.58 +    if not os.path.exists(filename):
    2.59 +        os.symlink(src, filename, False)
    2.60 +
    2.61 +
    2.62 +def create_home(mydir, arthome, username):
    2.63 +    "create an artificial home directory for testing"
    2.64 +
    2.65 +    os.chdir(mydir)
    2.66 +    os.makedirs(arthome, exist_ok=True)
    2.67 +
    2.68 +    os.chdir(arthome)
    2.69 +
    2.70 +    link_if_exists("bin", arthome)
    2.71 +    link_if_exists("include", arthome)
    2.72 +    link_if_exists("lib", arthome)
    2.73 +    link_if_exists("share", arthome)
    2.74 +    link_if_exists(".local", arthome)
    2.75 +    link_if_exists("Library", arthome) # this may exist on macOS
    2.76 +
    2.77 +    p = Process(target=create_own_identities, args=(mydir, arthome, username))
    2.78 +    p.start()
    2.79 +    p.join()
    2.80 +
    2.81 +
    2.82 +def create_homes():
    2.83 +    "create two artificial home directories for the two parties"
    2.84 +
    2.85 +    create_home(mydir, "test1", "Alice One")
    2.86 +    create_home(mydir, "test2", "Bob Two")
    2.87 +
    2.88 +
    2.89 +def remove_homes():
    2.90 +    """remove formerly created artificial home directories including their
    2.91 +    contents"""
    2.92 +
    2.93 +    os.chdir(mydir)
    2.94 +    shutil.rmtree("test1", ignore_errors=True)
    2.95 +    shutil.rmtree("test2", ignore_errors=True)
    2.96 +    shutil.rmtree("__pycache__", ignore_errors=True)
    2.97 +
    2.98 +
    2.99 +if __name__ == "__main__":
   2.100 +    import argparse
   2.101 +
   2.102 +    parser = argparse.ArgumentParser(description=__doc__)
   2.103 +    parser.add_argument('-r', '--remove', action="store_true",
   2.104 +            help=remove_homes.__doc__)
   2.105 +
   2.106 +    args = parser.parse_args()
   2.107 +
   2.108 +    if args.remove:
   2.109 +        remove_homes()
   2.110 +    else:
   2.111 +        create_homes()
   2.112 +
     3.1 --- a/test/python_tests/test1.py	Fri Jan 05 15:25:16 2018 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,57 +0,0 @@
     3.4 -# -*- coding: utf-8 -*-
     3.5 -
     3.6 -# this file is under GNU General Public License 3.0
     3.7 -# Copyleft 2017, p≡p foundation
     3.8 -
     3.9 -
    3.10 -from transport import *
    3.11 -
    3.12 -
    3.13 -class Test1:
    3.14 -
    3.15 -    def setup_class(self):
    3.16 -        arthome = "test1"
    3.17 -
    3.18 -        self.mydir = os.path.abspath(os.path.curdir)
    3.19 -
    3.20 -        os.environ["HOME"] = os.path.join(self.mydir, arthome)
    3.21 -        os.environ["GNUPGHOME"] = os.path.join(self.mydir, arthome, '.gnupg')
    3.22 -
    3.23 -        os.chdir(os.path.join(self.mydir, arthome))
    3.24 -
    3.25 -    @property
    3.26 -    def me(self):
    3.27 -        # because of flaws of py.test these two lines are necessary
    3.28 -        assert os.environ["GNUPGHOME"] != ""
    3.29 -        import pEp
    3.30 -
    3.31 -        i = pEp.Identity()
    3.32 -        i.address = "test1@peptest.ch"
    3.33 -        i.username = "Alice One"
    3.34 -        pEp.myself(i)
    3.35 -        return i
    3.36 -
    3.37 -    @property
    3.38 -    def you(self):
    3.39 -        assert os.environ["GNUPGHOME"] != ""
    3.40 -        import pEp
    3.41 -
    3.42 -        i = pEp.Identity()
    3.43 -        i.address = "test2@peptest.ch"
    3.44 -        i.username = "Bob Two"
    3.45 -        pEp.update_identity(i)
    3.46 -        return i
    3.47 -
    3.48 -    def test_send_message(self):
    3.49 -        assert os.environ["GNUPGHOME"] != ""
    3.50 -        import pEp
    3.51 -
    3.52 -        msg = pEp.Message()
    3.53 -        msg.from_ = self.me
    3.54 -        msg.to = [self.you]
    3.55 -        msg.shortmsg = "Subject line"
    3.56 -        msg.longmsg = "Message Text\n"
    3.57 -
    3.58 -        enc = msg.encrypt()
    3.59 -        send_message("test2", str(enc))
    3.60 -
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/python_tests/test_1.py	Fri Jan 05 19:39:46 2018 +0100
     4.3 @@ -0,0 +1,104 @@
     4.4 +# -*- coding: utf-8 -*-
     4.5 +
     4.6 +# this file is under GNU General Public License 3.0
     4.7 +# Copyleft 2017, p≡p foundation
     4.8 +
     4.9 +
    4.10 +from transport import *
    4.11 +
    4.12 +
    4.13 +def setup_gnupg():
    4.14 +    assert os.environ["GNUPGHOME"] != ""
    4.15 +
    4.16 +
    4.17 +mydir = os.path.dirname(os.path.realpath(__file__))
    4.18 +
    4.19 +
    4.20 +class Test1:
    4.21 +
    4.22 +    def setup_method(self):
    4.23 +        arthome = "test1"
    4.24 +
    4.25 +        os.environ["HOME"] = os.path.join(mydir, arthome)
    4.26 +        os.environ["GNUPGHOME"] = os.path.join(mydir, arthome, '.gnupg')
    4.27 +
    4.28 +        os.chdir(os.path.join(mydir, arthome))
    4.29 +
    4.30 +    @property
    4.31 +    def me(self):
    4.32 +        # because of flaws of py.test these two statements are necessary
    4.33 +        setup_gnupg()   # work around a bug with initializing os.environ
    4.34 +        import pEp      # after that import pEp module, not before
    4.35 +
    4.36 +        i = pEp.Identity()
    4.37 +        i.address = "test1@peptest.ch"
    4.38 +        i.username = "Alice One"
    4.39 +        pEp.myself(i)
    4.40 +        return i
    4.41 +
    4.42 +    @property
    4.43 +    def you(self):
    4.44 +        setup_gnupg() ; import pEp
    4.45 +
    4.46 +        i = pEp.Identity()
    4.47 +        i.address = "test2@peptest.ch"
    4.48 +        i.username = "Bob Two"
    4.49 +        pEp.update_identity(i)
    4.50 +        return i
    4.51 +
    4.52 +    def test_send_message(self):
    4.53 +        setup_gnupg() ; import pEp
    4.54 +
    4.55 +        msg = pEp.Message()
    4.56 +        msg.from_ = self.me
    4.57 +        msg.to = [self.you]
    4.58 +        msg.shortmsg = "Subject line"
    4.59 +        msg.longmsg = "Message Text\n"
    4.60 +
    4.61 +        enc = msg.encrypt()
    4.62 +        send_message("test2", str(enc))
    4.63 +
    4.64 +class Test2:
    4.65 +
    4.66 +    def setup_method(self):
    4.67 +        arthome = "test2"
    4.68 +
    4.69 +        os.environ["HOME"] = os.path.join(mydir, arthome)
    4.70 +        os.environ["GNUPGHOME"] = os.path.join(mydir, arthome, '.gnupg')
    4.71 +
    4.72 +        os.chdir(os.path.join(mydir, arthome))
    4.73 +
    4.74 +    @property
    4.75 +    def me(self):
    4.76 +        # because of flaws of py.test these two statements are necessary
    4.77 +        setup_gnupg()   # work around a bug with initializing os.environ
    4.78 +        import pEp      # after that import pEp module, not before
    4.79 +
    4.80 +        i = pEp.Identity()
    4.81 +        i.address = "test2@peptest.ch"
    4.82 +        i.username = "Bob Two"
    4.83 +        pEp.myself(i)
    4.84 +        return i
    4.85 +
    4.86 +    @property
    4.87 +    def you(self):
    4.88 +        setup_gnupg() ; import pEp
    4.89 +
    4.90 +        i = pEp.Identity()
    4.91 +        i.address = "test1@peptest.ch"
    4.92 +        i.username = "Alice One"
    4.93 +        pEp.update_identity(i)
    4.94 +        return i
    4.95 +
    4.96 +    def test_send_message(self):
    4.97 +        setup_gnupg() ; import pEp
    4.98 +
    4.99 +        msg = pEp.Message()
   4.100 +        msg.from_ = self.me
   4.101 +        msg.to = [self.you]
   4.102 +        msg.shortmsg = "Subject line"
   4.103 +        msg.longmsg = "Message Text\n"
   4.104 +
   4.105 +        enc = msg.encrypt()
   4.106 +        send_message("test1", str(enc))
   4.107 +
     5.1 --- a/test/python_tests/test_setup.py	Fri Jan 05 15:25:16 2018 +0100
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,108 +0,0 @@
     5.4 -#!/usr/bin/env python3
     5.5 -# -*- coding: utf-8 -*-
     5.6 -
     5.7 -# this file is under GNU General Public License 3.0
     5.8 -# Copyleft 2017, p≡p foundation
     5.9 -
    5.10 -#import vimpdb; vimpdb.set_trace()
    5.11 -
    5.12 -
    5.13 -"""
    5.14 -This script is setting up the test environment by creating two home directories
    5.15 -for simulation of message exchange between two p≡p parties.
    5.16 -
    5.17 -The directories are created in the current directory.
    5.18 -"""
    5.19 -
    5.20 -
    5.21 -import os
    5.22 -import shutil
    5.23 -from multiprocessing import Process
    5.24 -
    5.25 -
    5.26 -realhome = os.path.expanduser("~")
    5.27 -mydir = os.path.abspath(os.path.curdir)
    5.28 -
    5.29 -
    5.30 -def link_if_exists(dirname, arthome):
    5.31 -    "link directory from home to artificial home"
    5.32 -
    5.33 -    orig = os.path.join(realhome, dirname)
    5.34 -    if os.path.exists(orig):
    5.35 -        if not os.path.exists(dirname):
    5.36 -            os.symlink(orig, dirname, True)
    5.37 -
    5.38 -
    5.39 -def create_own_identities(mydir, arthome, username):
    5.40 -    "create own identities as part of the test setup"
    5.41 -
    5.42 -    os.environ["HOME"] = os.path.join(mydir, arthome)
    5.43 -    os.environ["GNUPGHOME"] = os.path.join(mydir, arthome, '.gnupg')
    5.44 -
    5.45 -    import pEp
    5.46 -    me = pEp.Identity()
    5.47 -    me.address = arthome + "@peptest.ch"
    5.48 -    me.username = username
    5.49 -
    5.50 -    pEp.myself(me)
    5.51 -    print(repr(me))
    5.52 -
    5.53 -
    5.54 -def link_file(filename):
    5.55 -    "sym-link file to version in parent directory"
    5.56 -
    5.57 -    src = os.path.join(os.pardir, filename)
    5.58 -    if not os.path.exists(filename):
    5.59 -        os.symlink(src, filename, False)
    5.60 -
    5.61 -
    5.62 -def create_home(mydir, arthome, username):
    5.63 -    "create an artificial home directory for testing"
    5.64 -
    5.65 -    os.chdir(mydir)
    5.66 -    os.makedirs(arthome, exist_ok=True)
    5.67 -
    5.68 -    os.chdir(arthome)
    5.69 -
    5.70 -    link_if_exists("bin", arthome)
    5.71 -    link_if_exists("include", arthome)
    5.72 -    link_if_exists("lib", arthome)
    5.73 -    link_if_exists("share", arthome)
    5.74 -    link_if_exists(".local", arthome)
    5.75 -    link_if_exists("Library", arthome) # this may exist on macOS
    5.76 -
    5.77 -    p = Process(target=create_own_identities, args=(mydir, arthome, username))
    5.78 -    p.start()
    5.79 -    p.join()
    5.80 -
    5.81 -
    5.82 -def create_homes():
    5.83 -    "create two artificial home directories for the two parties"
    5.84 -
    5.85 -    create_home(mydir, "test1", "Alice One")
    5.86 -    create_home(mydir, "test2", "Bob Two")
    5.87 -
    5.88 -
    5.89 -def remove_homes():
    5.90 -    """remove formerly created artificial home directories including their
    5.91 -    contents"""
    5.92 -
    5.93 -    os.chdir(mydir)
    5.94 -    shutil.rmtree("test1", ignore_errors=True)
    5.95 -    shutil.rmtree("test2", ignore_errors=True)
    5.96 -
    5.97 -
    5.98 -if __name__ == "__main__":
    5.99 -    import argparse
   5.100 -
   5.101 -    parser = argparse.ArgumentParser(description=__doc__)
   5.102 -    parser.add_argument('-r', '--remove', action="store_true",
   5.103 -            help=remove_homes.__doc__)
   5.104 -
   5.105 -    args = parser.parse_args()
   5.106 -
   5.107 -    if args.remove:
   5.108 -        remove_homes()
   5.109 -    else:
   5.110 -        create_homes()
   5.111 -