Browse Source

Rebuild and fix pytest based tests in new dir tests_new. To work with defined env for engine using separate process per test.

master
heck 5 years ago
parent
commit
e3c53a0d3c
  1. 3
      Makefile
  2. 8
      pyproject.toml
  3. 3
      setup.cfg
  4. 20
      tests_new/conftest.py
  5. 3
      tests_new/constants.py
  6. 0
      tests_new/data/README.md
  7. 0
      tests_new/data/alice@openpgp.example.pub.asc
  8. 0
      tests_new/data/alice@openpgp.example.sec.asc
  9. 0
      tests_new/data/bob@openpgp.example.pub.asc
  10. 0
      tests_new/data/bob@openpgp.example.sec.asc
  11. 16
      tests_new/test_identity.py
  12. 9
      tests_new/test_message.py
  13. 2
      tests_new/test_pep.py

3
Makefile

@ -57,8 +57,9 @@ envtest:
install-test: compile
pip3 install .[test]
# TODO: maybe use setup.py test?
test: install-test
pytest
pytest --forked
# Development

8
pyproject.toml

@ -1,3 +1,11 @@
[build-system]
requires = ["setuptools >=39.2.0", "setuptools_scm >= 4.1.2", "wheel >= 0.35.1"]
build-backend = "setuptools.build_meta"
[tool.pytest.ini_options]
minversion = "6.0"
addopts = ""
testpaths = [
"tests_new",
]

3
setup.cfg

@ -28,7 +28,7 @@ classifiers =
zip_safe = false
include_package_data = true
python_requires = >= 3.6
test_suite = tests
test_suite = tests_new
install_requires =
# deprecated/redundant with pyproject.toml, but lets keep both ways around for now
setup_requires =
@ -39,4 +39,5 @@ setup_requires =
# To install these dependencies, run pip install .[test]
test =
pytest
pytest-forked
doc = sphinx

20
tests/conftest.py → tests_new/conftest.py

@ -1,4 +1,5 @@
"""pytest configuration for the unit tests."""
import os
import pytest
@ -28,14 +29,15 @@ def datadir(request):
return D(request.fspath.dirpath("data"))
@pytest.fixture(scope='function')
def tmpdir(tmpdir_factory, request):
@pytest.fixture()
def ctx_init(tmpdir_factory, request):
"""Create a tmp dir for the tests"""
base = str(hash(request.node.nodeid))[:3]
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)
return bn
@pytest.fixture()
@ -51,27 +53,25 @@ def bob_pub_key_data(datadir):
@pytest.fixture()
def create_alice_identity(tmpdir, alice_sec_key_data, bob_pub_key_data):
os.environ["HOME"] = str(tmpdir)
def create_alice_identity(ctx_init, alice_sec_key_data):
import pEp
pEp.import_key(alice_sec_key_data)
alice = pEp.Identity(
constants.ALICE_ADDRESS, constants.ALICE_NAME,
constants.ALICE_NAME_ADDR, constants.ALICE_FP, 0, ''
)
)
pEp.set_own_key(alice, constants.ALICE_FP)
return alice
@pytest.fixture()
def create_bob_identity(tmpdir, bob_pub_key_data):
os.environ["HOME"] = str(tmpdir)
def create_bob_identity(ctx_init, bob_pub_key_data):
import pEp
pEp.import_key(bob_pub_key_data)
bob = pEp.Identity(
constants.BOB_ADDRESS, constants.BOB_NAME, '',
constants.BOB_FP, 56, ''
)
)
return bob

3
tests/constants.py → tests_new/constants.py

@ -16,8 +16,7 @@ BOB_NAME_ADDR = NAME_ADDR.format(BOB_NAME, ANGLE_ADDR.format(BOB_ADDRESS))
ALICE_NAME = "Alice Lovelace"
ALICE_ADDRESS = "alice@openpgp.example"
ALICE_FP = "EB85BB5FA33A75E15E944E63F231550C4F47E38E"
ALICE_NAME_ADDR = NAME_ADDR.format(ALICE_NAME,
ANGLE_ADDR.format(ALICE_ADDRESS))
ALICE_NAME_ADDR = NAME_ADDR.format(ALICE_NAME, ANGLE_ADDR.format(ALICE_ADDRESS))
SUBJECT = "This is a subject"
BODY = "Hi world!\n"

0
tests/data/README.md → tests_new/data/README.md

0
tests/data/alice@openpgp.example.pub.asc → tests_new/data/alice@openpgp.example.pub.asc

0
tests/data/alice@openpgp.example.sec.asc → tests_new/data/alice@openpgp.example.sec.asc

0
tests/data/bob@openpgp.example.pub.asc → tests_new/data/bob@openpgp.example.pub.asc

0
tests/data/bob@openpgp.example.sec.asc → tests_new/data/bob@openpgp.example.sec.asc

16
tests/test_identity.py → tests_new/test_identity.py

@ -4,7 +4,7 @@ import os
from . import constants
def test_create_one_identity_succeed(tmpdir):
def test_create_one_identity_succeed(ctx_init):
# It has to be imported here to get the management db initialized,
import pEp
alice = pEp.Identity()
@ -15,7 +15,7 @@ def test_create_one_identity_succeed(tmpdir):
expected_alice = pEp.Identity(
constants.ALICE_ADDRESS, constants.ALICE_NAME, '',
constants.ALICE_FP, 0, ''
)
)
# XXX: Can't compare objects
# assert alice == expected_alice
@ -43,14 +43,13 @@ def test_create_one_identity_succeed(tmpdir):
assert alice.flags == expected_alice.flags
def test_two_identities_succeed(tmpdir, bob_pub_key_data):
os.environ["HOME"] = str(tmpdir)
def test_two_identities_succeed(ctx_init, bob_pub_key_data):
import pEp
alice = pEp.Identity(
constants.ALICE_ADDRESS, constants.ALICE_NAME, '',
constants.ALICE_FP, 0, ''
)
)
assert alice.address == constants.ALICE_ADDRESS
assert alice.username == constants.ALICE_NAME
assert alice.fpr == constants.ALICE_FP
@ -67,7 +66,7 @@ def test_two_identities_succeed(tmpdir, bob_pub_key_data):
expected_bob = pEp.Identity(
constants.BOB_ADDRESS, constants.BOB_NAME, '',
constants.BOB_FP, 56, ''
)
)
assert str(bob) == constants.BOB_NAME_ADDR
assert bob.address == expected_bob.address
@ -88,8 +87,7 @@ def test_two_identities_succeed(tmpdir, bob_pub_key_data):
assert bob.flags == 0
def test_set_own_key(tmpdir, alice_sec_key_data):
os.environ["HOME"] = str(tmpdir)
def test_set_own_key(ctx_init, alice_sec_key_data):
import pEp
pEp.import_key(alice_sec_key_data)
@ -102,7 +100,7 @@ def test_set_own_key(tmpdir, alice_sec_key_data):
expected_alice = pEp.Identity(
constants.ALICE_ADDRESS, constants.ALICE_NAME, '',
constants.ALICE_FP, 0, ''
)
)
pEp.set_own_key(alice, alice.fpr)
# assert str(alice) == constants.ALICE_NAME_ADDR

9
tests/test_message.py → tests_new/test_message.py

@ -30,8 +30,7 @@ def test_msg_enc_dec_roundtrip(create_alice_identity, create_bob_identity):
assert str(enc_msg.from_) == constants.ALICE_NAME_ADDR
assert str(enc_msg.to[0]) == constants.BOB_NAME_ADDR
assert enc_msg.shortmsg == "p≡p"
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.
dec_msg, key_list, rating, r = enc_msg.decrypt()
@ -54,7 +53,8 @@ def test_msg_enc_dec_roundtrip(create_alice_identity, create_bob_identity):
# Content-Transfer-Encoding: doesn't print `quoted-printable` anymore.
# Content-Disposition: is not present anymore.
# `!` is not replaced by `=21` anymore.
expected_dec_lines = """From: Alice Lovelace <alice@openpgp.example>
expected_dec_lines = \
"""From: Alice Lovelace <alice@openpgp.example>
To: Bob Babagge <bob@openpgp.example>
Subject: This is a subject
X-pEp-Version: 2.1
@ -129,7 +129,8 @@ def test_dec_msg_len(create_alice_identity, create_bob_identity):
dec_msg_len = len(str(dec_msg))
assert dec_msg.longmsg.replace("\r", "") == constants.BODY # msg.longmsg
expected_dec_msg = """From: Alice Lovelace <alice@openpgp.example>\r
expected_dec_msg = \
"""From: Alice Lovelace <alice@openpgp.example>\r
To: Bob Babagge <bob@openpgp.example>\r
Subject: This is a subject\r
X-pEp-Version: 2.1\r

2
tests/test_pep.py → tests_new/test_pep.py

@ -1,10 +1,10 @@
"""Unit test for pEp package, not for subpackages or modules."""
from pEp import __version__
def test_pep_version():
""" Test that __version__ is not None or empty and is not 0.0.0."""
from pEp import __version__
# We could also test that match the regex, but that is already a test in
# setuptools_scm itself.
assert __version__
Loading…
Cancel
Save