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.

161 lines
4.4 KiB

  1. """Unit tests for the GNUnet REST client.
  2. .. note:: Each of them depends on the state of the previous one.
  3. """
  4. import os
  5. from pygnunetrest import gnunetrest
  6. # To be able to test against gnunet running in other machine or container.
  7. HOST = os.environ.get('HOST', 'localhost')
  8. PORT = os.environ.get('PORT', 7776)
  9. REST_CLIENT = gnunetrest.GNUnetRest(HOST, PORT)
  10. def test_connect():
  11. # test if connection is possible
  12. print("\nTesting Connector")
  13. assert REST_CLIENT
  14. # IDENTITIES#
  15. # ==========#
  16. def test_create_identity():
  17. print("\nCreating some identities")
  18. r = REST_CLIENT.create_identity("alice")
  19. assert r is True
  20. r = REST_CLIENT.create_identity("bob")
  21. assert r is True
  22. def test_list_identities():
  23. REST_CLIENT.create_identity("bob")
  24. identities = REST_CLIENT.identity_list()
  25. assert 2 == len(identities)
  26. assert "alice" == identities[0]['name']
  27. assert "bob" == identities[1]['name']
  28. def test_fetch_identity_keyhash():
  29. r = REST_CLIENT.fetch_identity_keyhash("alice")
  30. # XXX: sometimes it's 58 and others 52
  31. assert 52 <= len(r)
  32. def test_fetch_identity_name():
  33. # First fetch the hash, since we don't know it.
  34. keyhash = REST_CLIENT.fetch_identity_keyhash("alice")
  35. r = REST_CLIENT.fetch_identity_name(keyhash)
  36. assert "alice" == r
  37. def test_change_identity_name():
  38. assert REST_CLIENT.change_identity_name("bob", "carol")
  39. r = REST_CLIENT.fetch_identity_keyhash("carol")
  40. assert 52 <= len(r)
  41. def test_delete_identity():
  42. assert REST_CLIENT.delete_identity("carol") is True
  43. assert REST_CLIENT.fetch_identity_name("carol")[0] is False
  44. # # NAMESTORE#
  45. # # =========#
  46. def test_set_namestore_default_identity():
  47. assert REST_CLIENT.set_namestore_default_identity("alice") is True
  48. def test_fetch_namestore_default_identity():
  49. r = REST_CLIENT.fetch_namestore_default_identity()
  50. assert "alice" == r
  51. def test_add_namestore_entry():
  52. r = REST_CLIENT.add_namestore_entry(
  53. "alice",
  54. "pepfoundation",
  55. "95.128.36.146",
  56. "A"
  57. )
  58. assert r is True
  59. def test_list_namestore_entry():
  60. r = REST_CLIENT.list_namestore_entries("alice")
  61. assert 1 == len(r)
  62. assert 'pepfoundation' == r[0]['record_name']
  63. # There could be more than one `RecordData`, so iterate through them.
  64. assert '95.128.36.146' in [record['value'] for record in r[0]['data']]
  65. def test_change_namestore_entry():
  66. assert REST_CLIENT.change_namestore_entry(
  67. "alice",
  68. "pepfoundation",
  69. "6.6.6.6",
  70. "A"
  71. )
  72. r = REST_CLIENT.list_namestore_entries("alice")
  73. assert 1 == len(r)
  74. assert 'pepfoundation' == r[0]['record_name']
  75. # It would modify any record that match all the flags, ending with only
  76. # one, even for repeated records.
  77. assert '6.6.6.6' == [record['value'] for record in r[0]['data']][0]
  78. def test_delete_namestore_entry():
  79. r = REST_CLIENT.delete_namestore_entry("alice", "pepfoundation")
  80. assert r is True
  81. r = REST_CLIENT.list_namestore_entries("alice")
  82. # There should not be anymore records with that record name
  83. assert 0 == len(r)
  84. # # GNS#
  85. # # ===#
  86. def test_list_gns_record():
  87. # Add an entry again, since it was deleted
  88. r = REST_CLIENT.add_namestore_entry(
  89. "alice",
  90. "pepfoundation",
  91. "95.128.36.146",
  92. "A"
  93. )
  94. assert r is True
  95. r = REST_CLIENT.list_gns_record("alice", "pepfoundation")
  96. assert isinstance(r, dict)
  97. assert 'pepfoundation.alice' == r['record_name']
  98. assert '95.128.36.146' == r["data"][0]["value"]
  99. def test_list_gns_record_type():
  100. r = REST_CLIENT.list_gns_record_type("alice", "pepfoundation", "A")
  101. assert isinstance(r, dict)
  102. assert 'pepfoundation.alice' == r['record_name']
  103. # Even if there're several records, all should be type A
  104. assert 'A' == r['data'][0]['record_type']
  105. def test_add_cert_entry():
  106. """
  107. Test that is not possible to add a record type ``CERT`` via REST.
  108. Even if it is implement in GNS:
  109. https://git.gnunet.org/gnunet.git/tree/src/gnsrecord/plugin_gnsrecord_dns.c#n130
  110. """
  111. r = REST_CLIENT.add_namestore_entry(
  112. "alice",
  113. "pepfoundation",
  114. "foo",
  115. "CERT"
  116. )
  117. assert r[0] is False
  118. # In gnunet logs it can be seen the line:
  119. # ``gnunet-rest-server-12 ERROR Value invalid for record type```
  120. # But the REST API only returns this.
  121. assert "Internal Server Error" == r[1]