p≡p for Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.9 KiB

  1. """pytest configuration for the unit tests."""
  2. import os
  3. import pytest
  4. from . import constants
  5. @pytest.fixture()
  6. def datadir(request):
  7. """Get, read, open test files from the tests "data" directory."""
  8. class D:
  9. def __init__(self, basepath):
  10. self.basepath = basepath
  11. def open(self, name, mode="r"):
  12. return self.basepath.join(name).open(mode)
  13. def join(self, name):
  14. return self.basepath.join(name).strpath
  15. def read(self, name):
  16. with self.open(name, "r") as f:
  17. return f.read()
  18. def readlines(self, name):
  19. with self.open(name, "r") as f:
  20. return f.readlines()
  21. return D(request.fspath.dirpath("data"))
  22. @pytest.fixture(scope='function')
  23. def tmpdir(tmpdir_factory, request):
  24. """Create a tmp dir for the tests"""
  25. base = str(hash(request.node.nodeid))[:3]
  26. bn = tmpdir_factory.mktemp(base)
  27. return bn
  28. @pytest.fixture()
  29. def alice_sec_key_data(datadir):
  30. key_data = datadir.read('alice@openpgp.example.sec.asc')
  31. return key_data
  32. @pytest.fixture()
  33. def bob_pub_key_data(datadir):
  34. key_data = datadir.read('bob@openpgp.example.pub.asc')
  35. return key_data
  36. @pytest.fixture()
  37. def create_alice_identity(tmpdir, alice_sec_key_data, bob_pub_key_data):
  38. os.environ["HOME"] = str(tmpdir)
  39. import pEp
  40. pEp.import_key(alice_sec_key_data)
  41. alice = pEp.Identity(
  42. constants.ALICE_ADDRESS, constants.ALICE_NAME,
  43. constants.ALICE_NAME_ADDR, constants.ALICE_FP, 0, ''
  44. )
  45. pEp.set_own_key(alice, constants.ALICE_FP)
  46. return alice
  47. @pytest.fixture()
  48. def create_bob_identity(tmpdir, bob_pub_key_data):
  49. os.environ["HOME"] = str(tmpdir)
  50. import pEp
  51. pEp.import_key(bob_pub_key_data)
  52. bob = pEp.Identity(
  53. constants.BOB_ADDRESS, constants.BOB_NAME, '',
  54. constants.BOB_FP, 56, ''
  55. )
  56. return bob