test/python_tests/test_handshake.py
author Volker Birk <vb@pep.foundation>
Fri, 05 Jan 2018 21:54:50 +0100
changeset 2358 a40432f11e35
parent 2357 test/python_tests/test_1.py@5d93322dfd2e
child 2361 6c6698ef4e34
permissions -rw-r--r--
handshake
     1 # -*- coding: utf-8 -*-
     2 
     3 # this file is under GNU General Public License 3.0
     4 # Copyleft 2017, p≡p foundation
     5 
     6 
     7 from transport import *
     8 
     9 
    10 def setup_gnupg():
    11     assert os.environ["GNUPGHOME"] != ""
    12 
    13 
    14 mydir = os.path.dirname(os.path.realpath(__file__))
    15 
    16 
    17 class Test1:
    18 
    19     def setup_method(self):
    20         arthome = "test1"
    21 
    22         os.environ["HOME"] = os.path.join(mydir, arthome)
    23         os.environ["GNUPGHOME"] = os.path.join(mydir, arthome, '.gnupg')
    24 
    25         os.chdir(os.path.join(mydir, arthome))
    26 
    27     @property
    28     def me(self):
    29         # because of flaws of py.test these two statements are necessary
    30         setup_gnupg()   # work around a bug with initializing os.environ
    31         import pEp      # after that import pEp module, not before
    32 
    33         i = pEp.Identity()
    34         i.address = "test1@peptest.ch"
    35         i.username = "Alice One"
    36         pEp.myself(i)
    37         return i
    38 
    39     @property
    40     def you(self):
    41         setup_gnupg() ; import pEp
    42 
    43         i = pEp.Identity()
    44         i.address = "test2@peptest.ch"
    45         i.username = "Bob Two"
    46         pEp.update_identity(i)
    47         return i
    48 
    49     def test_send_and_recv_message(self):
    50         setup_gnupg() ; import pEp
    51 
    52         msg = pEp.Message()
    53         msg.from_ = self.me
    54         msg.to = [self.you]
    55         msg.shortmsg = "Subject line"
    56         msg.longmsg = "Message Text\n"
    57 
    58         enc = msg.encrypt()
    59         send_message("test2", str(enc))
    60 
    61         txt = wait_for_message()
    62         enc = pEp.Message(txt)
    63         assert enc.from_.address == "test2@peptest.ch"
    64         inc, keys, rating, consumed, flags = enc.decrypt()
    65         assert rating == 6
    66 
    67 
    68 class Test2:
    69 
    70     def setup_method(self):
    71         arthome = "test2"
    72 
    73         os.environ["HOME"] = os.path.join(mydir, arthome)
    74         os.environ["GNUPGHOME"] = os.path.join(mydir, arthome, '.gnupg')
    75 
    76         os.chdir(os.path.join(mydir, arthome))
    77 
    78     @property
    79     def me(self):
    80         # because of flaws of py.test these two statements are necessary
    81         setup_gnupg()   # work around a bug with initializing os.environ
    82         import pEp      # after that import pEp module, not before
    83 
    84         i = pEp.Identity()
    85         i.address = "test2@peptest.ch"
    86         i.username = "Bob Two"
    87         pEp.myself(i)
    88         return i
    89 
    90     @property
    91     def you(self):
    92         setup_gnupg() ; import pEp
    93 
    94         i = pEp.Identity()
    95         i.address = "test1@peptest.ch"
    96         i.username = "Alice One"
    97         pEp.update_identity(i)
    98         return i
    99 
   100     def test_recv_and_send_message(self):
   101         setup_gnupg() ; import pEp
   102 
   103         txt = wait_for_message()
   104         msg = pEp.Message(txt)
   105         msg.decrypt()
   106         assert msg.from_.address == self.you.address
   107 
   108         out = pEp.Message(1)
   109         out.from_ = self.me
   110         out.to = [self.you]
   111         out.shortmsg = "Subject Back"
   112         out.longmsg = "Text Back\n"
   113 
   114         enc = out.encrypt()
   115         send_message("test1", str(enc))
   116