Browse Source

change the Python API to follow the engine API after ENGINE-959

The decrypt method of the message class now returns a tuple of three elements
instead of four:
* message
* keys
* flags
There is no more rating among the results, as the rating is now a property of
the message -- and can be easily accessed as message.rating .

Update examples and tests.
lucasaiu/message-struct-change
Luca Saiu 4 years ago
parent
commit
b41ca61b91
  1. 4
      examples/simple_xml_msgformat.py
  2. 2
      src/pEp/_pEp/message_api.cc
  3. 1
      src/pEp/_pEp/pEpmodule.cc
  4. 2
      test/codec_doctest.py
  5. 4
      test/pyadpt-81.py
  6. 4
      test/sync_handshake.py
  7. 8
      tests/test_basic.py
  8. 8
      tests/test_message.py
  9. 2
      utils/pEp

4
examples/simple_xml_msgformat.py

@ -127,5 +127,5 @@ def parse_serialized_message(transport_message):
pEp_attachs.append( pEp_attachs.append(
pEp.Blob(attach.text.encode(), TAG2CT[tagname])) pEp.Blob(attach.text.encode(), TAG2CT[tagname]))
msg1.attachments = pEp_attachs msg1.attachments = pEp_attachs
msg2, keys, rating, flags = msg1.decrypt() msg2, keys, flags = msg1.decrypt()
return msg2, rating return msg2, msg2.rating

2
src/pEp/_pEp/message_api.cc

@ -62,7 +62,7 @@ namespace pEp {
Message dst = _dst ? Message(_dst) : Message(src); Message dst = _dst ? Message(_dst) : Message(src);
PEP_rating _rating = dst.rating (); PEP_rating _rating = dst.rating ();
return boost::python::make_tuple(dst, keylist, _rating, _flags); return boost::python::make_tuple(dst, keylist, _flags);
} }
PEP_color _color(int rating) { PEP_color _color(int rating) {

1
src/pEp/_pEp/pEpmodule.cc

@ -514,7 +514,6 @@ namespace pEp {
"\n" "\n"
" msg the decrypted p≡p message\n" " msg the decrypted p≡p message\n"
" keys a list of keys being used\n" " keys a list of keys being used\n"
" rating the rating of the message as integer\n"
" flags flags set while decryption\n" " flags flags set while decryption\n"
) )
.add_property("outgoing_rating", &Message::outgoing_rating, "rating outgoing message will have") .add_property("outgoing_rating", &Message::outgoing_rating, "rating outgoing message will have")

2
test/codec_doctest.py

@ -4,7 +4,7 @@
""" """
>>> import pEp >>> import pEp
>>> def message_to_send(msg): >>> def message_to_send(msg):
... m, keys, rating, flags = msg.decrypt() ... m, keys, flags = msg.decrypt()
... try: ... try:
... m.attachments[0].decode() ... m.attachments[0].decode()
... print("decode successfull") ... print("decode successfull")

4
test/pyadpt-81.py

@ -6,7 +6,7 @@ import time
def message_to_send(msg): def message_to_send(msg):
print("User defined message_to_send() called") print("User defined message_to_send() called")
m, keys, rating, flags = msg.decrypt() m, keys, flags = msg.decrypt()
try: try:
print(m.attachments[0].decode()) print(m.attachments[0].decode())
except UnicodeDecodeError as e: except UnicodeDecodeError as e:
@ -44,4 +44,4 @@ start_stop_sync(1)
# print("is_sync_active: {}".format(pEp.is_sync_active())) # print("is_sync_active: {}".format(pEp.is_sync_active()))
# time.sleep(3) # time.sleep(3)
# pEp.key_reset_all_own_keys() # pEp.key_reset_all_own_keys()
# time.sleep(3) # time.sleep(3)

4
test/sync_handshake.py

@ -103,7 +103,7 @@ def messageImapToSend(msg):
def add_debug_info(msg): def add_debug_info(msg):
if msg.enc_format: if msg.enc_format:
m, keys, rating, flags = msg.decrypt(DONT_TRIGGER_SYNC) m, keys, flags = msg.decrypt(DONT_TRIGGER_SYNC)
else: else:
m = msg m = msg
try: try:
@ -212,7 +212,7 @@ def run(name, color=None, imap=False, own_ident=1, leave=False):
msg = pEp.Message(m) msg = pEp.Message(m)
output("*** Reading") output("*** Reading")
print_msg(msg) print_msg(msg)
msg2, keys, rating, flags = msg.decrypt() msg2, keys, flags = msg.decrypt()
except KeyboardInterrupt: except KeyboardInterrupt:
shutdown_sync() shutdown_sync()

8
tests/test_basic.py

@ -20,8 +20,8 @@ import pytest
# >>> m.longmsg = "Please call me back" # >>> m.longmsg = "Please call me back"
# >>> m2 = m.encrypt() # >>> m2 = m.encrypt()
# >>> print(m2) # >>> print(m2)
# >>> m3, keys, rating, flags = m2.decrypt() # >>> m3, keys, flags = m2.decrypt()
# >>> rating # >>> m3.rating
# pEp.rating.reliable # pEp.rating.reliable
# """ # """
# #
@ -50,6 +50,6 @@ def test_basic(pEp, model):
m.longmsg = constants.BODY m.longmsg = constants.BODY
#TODO: encrypt needs to return message type #TODO: encrypt needs to return message type
m2 = m.encrypt() m2 = m.encrypt()
m3, keys, rating, flags = m2.decrypt() m3, keys, flags = m2.decrypt()
#TODO: fix pEp.rating #TODO: fix pEp.rating
# assert rating == pEp. # assert m3.rating == pEp.

8
tests/test_message.py

@ -35,13 +35,13 @@ def test_msg_enc_dec_roundtrip(pEp, model, import_ident_alice_as_own_ident, impo
assert enc_msg.longmsg == "this message was encrypted with p≡p https://pEp-project.org" assert enc_msg.longmsg == "this message was encrypted with p≡p https://pEp-project.org"
# Decrypt message. # Decrypt message.
dec_msg, key_list, rating, r = enc_msg.decrypt() dec_msg, key_list, r = enc_msg.decrypt()
assert r == 0 assert r == 0
# pEp version 2.2 throws this error: # pEp version 2.2 throws this error:
# AttributeError: module 'pEp' has no attribute 'PEP_rating' # AttributeError: module 'pEp' has no attribute 'PEP_rating'
# assert rating == pEp.PEP_rating.PEP_rating_reliable # assert dec_msg.rating == pEp.PEP_rating.PEP_rating_reliable
# It seems to have changed to the following. # It seems to have changed to the following.
assert rating == pEp._pEp.rating.reliable assert dec_msg.rating == pEp._pEp.rating.reliable
# The first 2 keys are Alice's ones, the last is Bob's one. # The first 2 keys are Alice's ones, the last is Bob's one.
assert key_list[0] == key_list[1] == model.alice.fpr assert key_list[0] == key_list[1] == model.alice.fpr
@ -123,7 +123,7 @@ def test_dec_msg_len(pEp, import_ident_alice_as_own_ident, import_ident_bob):
enc_msg = msg.encrypt() enc_msg = msg.encrypt()
# Decrypt message. # Decrypt message.
dec_msg, _key_list, _rating, _r = enc_msg.decrypt() dec_msg, _key_list, _r = enc_msg.decrypt()
dec_msg_len = len(str(dec_msg)) dec_msg_len = len(str(dec_msg))
assert dec_msg.longmsg.replace("\r", "") == constants.BODY # msg.longmsg assert dec_msg.longmsg.replace("\r", "") == constants.BODY # msg.longmsg

2
utils/pEp

@ -77,7 +77,7 @@ try:
dst = src.encrypt() dst = src.encrypt()
else: else:
src.dir = 0 src.dir = 0
dst, keys, rating, flags = src.decrypt() dst, keys, flags = src.decrypt()
print(str(dst)) print(str(dst))

Loading…
Cancel
Save