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.

126 lines
3.4 KiB

2 years ago
2 years ago
  1. # -*- coding: utf-8 -*-
  2. # This file is under GNU Affero General Public License 3.0
  3. # see LICENSE.txt
  4. from . import utils
  5. import pytest
  6. @pytest.fixture()
  7. def model():
  8. "Returns the whole data model"
  9. return Model()
  10. identities = \
  11. {
  12. "alice": {
  13. "name": "alice",
  14. "user_id": "23",
  15. "accounts":
  16. {
  17. "work": {
  18. "addr": "alice_work@peptest.org",
  19. "fpr": "3E45175EE953EBBEB948F11A6A03DB2A17FB9D15"
  20. },
  21. "home": {
  22. "addr": "alice@peptest.org",
  23. "fpr": "2D35731B9C754564CBAD15D2D18F7444594F2283"
  24. }
  25. }
  26. },
  27. "bob": {
  28. "name": "bob",
  29. "user_id": "uuid:1-2-3-4",
  30. "accounts": \
  31. {
  32. "work": {
  33. "addr": "bob_work@peptest.org",
  34. "fpr": "1A97F263D8319D6885F638C5AA81E1B5457A2B40"
  35. },
  36. "home": {
  37. "addr": "bob@peptest.org",
  38. "fpr": "6A9835699EF1215F1558A496D9C1D4B0984094E5"
  39. }
  40. }
  41. }
  42. }
  43. class Identity:
  44. """
  45. An Identity class that is:
  46. - can represent pEp.Identity
  47. - is read-only (const)
  48. """
  49. def __init__(self, name="", user_id="", addr="", fpr="", key_sec="", key_pub=""):
  50. self.__name = name
  51. self.__user_id = user_id
  52. self.__addr = addr
  53. self.__fpr = fpr
  54. self.__key_sec = key_sec
  55. self.__key_pub = key_pub
  56. @property
  57. def name(self):
  58. return self.__name
  59. @property
  60. def user_id(self):
  61. return self.__user_id
  62. @property
  63. def addr(self):
  64. return self.__addr
  65. @property
  66. def fpr(self):
  67. return self.__fpr
  68. @property
  69. def key_sec(self):
  70. return self.__key_sec
  71. @property
  72. def key_pub(self):
  73. return self.__key_pub
  74. def debug(self) -> str:
  75. ret = "name:" + self.__name
  76. ret +="user_id:" + self.__user_id
  77. ret +="addr:" + self.__addr
  78. ret +="fpr:" + self.__fpr
  79. ret +="key_sec:" + self.__key_sec[0:255]
  80. ret +="key_pub:" + self.__key_pub[0:255]
  81. return ret
  82. def __str__(self):
  83. return "{} {}".format(self.name, "<{}>".format(self.addr))
  84. # The Data Model
  85. class Model:
  86. alice = None
  87. alice_work = None
  88. bob = None
  89. bob_work = None
  90. def getIdentity(self, name, account) -> Identity:
  91. # fetch keys for ident from data folder
  92. key_sec = utils.data_file_contents(identities[name]['accounts'][account]['fpr'] + ".sec.asc")
  93. key_pub = utils.data_file_contents(identities[name]['accounts'][account]['fpr'] + ".pub.asc")
  94. ident = Identity(name=identities[name]['name'],
  95. user_id=identities[name]['user_id'],
  96. addr=identities[name]['accounts'][account]['addr'],
  97. fpr=identities[name]['accounts'][account]['fpr'],
  98. key_pub=key_pub,
  99. key_sec=key_sec
  100. )
  101. return ident
  102. def __init__(self):
  103. self.alice = self.getIdentity("alice", "home")
  104. self.alice_work = self.getIdentity("alice", "work")
  105. self.bob = self.getIdentity("bob", "home")
  106. self.bob_work = self.getIdentity("bob", "work")