Browse Source

structuring fixtures...

master
heck 5 years ago
parent
commit
5b12e284c8
  1. 41
      tests/conftest.py
  2. 11
      tests/test_identity.py
  3. 32
      tests/test_message.py

41
tests/conftest.py

@ -6,7 +6,7 @@ import pytest
from . import constants
# Static Data
@pytest.fixture()
def datadir(request):
"""Get, read, open test files from the tests "data" directory."""
@ -29,47 +29,50 @@ def datadir(request):
return f.readlines()
return D(request.fspath.dirpath("data"))
@pytest.fixture()
def ctx_init(tmpdir_factory, request):
"""Create a tmp dir for the tests"""
base = str(abs(hash(request.node.nodeid)))[:3]
bn = tmpdir_factory.mktemp(base)
print(bn)
import os
os.environ["PEP_HOME"] = str(bn)
os.environ["HOME"] = str(bn)
@pytest.fixture()
def alice_key_sec(datadir):
key_data = datadir.read('alice@openpgp.example.sec.asc')
return key_data
@pytest.fixture()
def bob_key_pub(datadir):
key_data = datadir.read('bob@openpgp.example.pub.asc')
return key_data
# Init
@pytest.fixture()
def env_init(tmpdir_factory, request):
"""Create a tmp dir for the tests"""
base = str(abs(hash(request.node.nodeid)))[:3]
bn = tmpdir_factory.mktemp(base)
print(bn)
import os
os.environ["PEP_HOME"] = str(bn)
os.environ["HOME"] = str(bn)
@pytest.fixture()
def import_identity_alice(ctx_init, alice_key_sec):
def pEp(env_init):
import pEp
return pEp
# Identities
@pytest.fixture()
def import_ident_alice(pEp, alice_key_sec):
pEp.import_key(alice_key_sec)
alice = pEp.Identity(
constants.ALICE_ADDRESS, constants.ALICE_NAME,
constants.ALICE_NAME_ADDR, constants.ALICE_FPR, 0, ''
)
pEp.set_own_key(alice, constants.ALICE_FPR)
return alice
@pytest.fixture()
def import_identity_bob(ctx_init, bob_key_pub):
import pEp
def import_ident_alice_as_own_ident(pEp, import_ident_alice):
alice = import_ident_alice
pEp.set_own_key(alice, constants.ALICE_FPR)
return alice
@pytest.fixture()
def import_ident_bob(pEp, bob_key_pub):
pEp.import_key(bob_key_pub)
bob = pEp.Identity(
constants.BOB_ADDRESS, constants.BOB_NAME, '',

11
tests/test_identity.py

@ -5,9 +5,8 @@ import os
from . import constants
def test_create_one_identity_succeed(ctx_init):
def test_create_one_identity_succeed(pEp):
# It has to be imported here to get the management db initialized,
import pEp
alice = pEp.Identity()
alice.address = constants.ALICE_ADDRESS
alice.username = constants.ALICE_NAME
@ -44,9 +43,7 @@ def test_create_one_identity_succeed(ctx_init):
assert alice.flags == expected_alice.flags
def test_two_identities_succeed(ctx_init, bob_key_pub):
import pEp
def test_two_identities_succeed(pEp, bob_key_pub):
alice = pEp.Identity(
constants.ALICE_ADDRESS, constants.ALICE_NAME, '',
constants.ALICE_FPR, 0, ''
@ -88,9 +85,7 @@ def test_two_identities_succeed(ctx_init, bob_key_pub):
assert bob.flags == 0
def test_set_own_key(ctx_init, alice_key_sec):
import pEp
def test_set_own_key(pEp, alice_key_sec):
pEp.import_key(alice_key_sec)
alice = pEp.Identity()
alice.address = constants.ALICE_ADDRESS

32
tests/test_message.py

@ -4,11 +4,9 @@
from . import constants
def test_msg_enc_dec_roundtrip(import_identity_alice, import_identity_bob):
import pEp
alice = import_identity_alice
bob = import_identity_bob
def test_msg_enc_dec_roundtrip(pEp, import_ident_alice_as_own_ident, import_ident_bob):
alice = import_ident_alice_as_own_ident
bob = import_ident_bob
msg = pEp.Message(constants.OUTGOING_MSG, alice)
msg.to = [bob]
@ -72,7 +70,7 @@ Hi world!
assert dec_lines[7:] == expected_dec_lines[7:]
def test_msg_len_changes(import_identity_alice, import_identity_bob):
def test_msg_len_changes(pEp, import_ident_alice_as_own_ident, import_ident_bob):
"""Test that the original message is modified after encryption.
Headers are added and therefore the modified unencrypted message length
@ -82,10 +80,8 @@ def test_msg_len_changes(import_identity_alice, import_identity_bob):
extra headers and modify the subject returning a new message.
"""
import pEp
alice = import_identity_alice
bob = import_identity_bob
alice = import_ident_alice_as_own_ident
bob = import_ident_bob
msg = pEp.outgoing_message(alice)
msg.to = [bob]
@ -105,17 +101,15 @@ def test_msg_len_changes(import_identity_alice, import_identity_bob):
assert msg_after_encrypt_len != msg_len
def test_dec_msg_len(import_identity_alice, import_identity_bob):
def test_dec_msg_len(pEp, import_ident_alice_as_own_ident, import_ident_bob):
"""
Test that the decrypted message length is different from the original.
Because it adds extra headers.
"""
import pEp
alice = import_identity_alice
bob = import_identity_bob
alice = import_ident_alice_as_own_ident
bob = import_ident_bob
msg = pEp.outgoing_message(alice)
msg.to = [bob]
@ -155,12 +149,10 @@ Hi world!\r
assert dec_msg_len - len_extra_headers == msg_len
def test_null_char_rmed(import_identity_alice, import_identity_bob):
def test_null_char_rmed(pEp, import_ident_alice_as_own_ident, import_ident_bob):
"""Test that null characters and anything after them is removed."""
import pEp
alice = import_identity_alice
bob = import_identity_bob
alice = import_ident_alice_as_own_ident
bob = import_ident_bob
msg = pEp.outgoing_message(alice)
msg.to = [bob]

Loading…
Cancel
Save