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.
 
 
 

139 lines
3.7 KiB

"""Unit tests for the GNUnet REST client.
.. note:: Each of them depends on the state of the previous one.
"""
import os
from pygnunetrest import gnunetrest
# To be able to test against gnunet running in other machine or container.
HOST = os.environ.get('HOST', 'localhost')
PORT = os.environ.get('PORT', 7776)
REST_CLIENT = gnunetrest.GNUnetRest(HOST, PORT)
def test_connect():
# test if connection is possible
print("\nTesting Connector")
assert REST_CLIENT
# IDENTITIES#
# ==========#
def test_create_identity():
print("\nCreating some identities")
r = REST_CLIENT.create_identity("alice")
assert r is True
r = REST_CLIENT.create_identity("bob")
assert r is True
def test_list_identities():
REST_CLIENT.create_identity("bob")
identities = REST_CLIENT.identity_list()
assert 2 == len(identities)
assert "alice" == identities[0]['name']
assert "bob" == identities[1]['name']
def test_fetch_identity_keyhash():
r = REST_CLIENT.fetch_identity_keyhash("alice")
assert 52 == len(r)
def test_fetch_identity_name():
# First fetch the hash, since we don't know it.
keyhash = REST_CLIENT.fetch_identity_keyhash("alice")
r = REST_CLIENT.fetch_identity_name(keyhash)
assert "alice" == r
def test_change_identity_name():
assert REST_CLIENT.change_identity_name("bob", "carol")
r = REST_CLIENT.fetch_identity_keyhash("carol")
assert 52 == len(r)
def test_delete_identity():
assert REST_CLIENT.delete_identity("carol") is True
assert REST_CLIENT.fetch_identity_name("carol")[0] is False
# # NAMESTORE#
# # =========#
def test_set_namestore_default_identity():
assert REST_CLIENT.set_namestore_default_identity("alice") is True
def test_fetch_namestore_default_identity():
r = REST_CLIENT.fetch_namestore_default_identity()
assert "alice" == r
def test_add_namestore_entry():
r = REST_CLIENT.add_namestore_entry(
"alice",
"pepfoundation",
"95.128.36.146",
"A"
)
assert r is True
def test_list_namestore_entry():
r = REST_CLIENT.list_namestore_entries("alice")
assert 1 == len(r)
assert 'pepfoundation' == r[0]['record_name']
# There could be more than one `RecordData`, so iterate through them.
assert '95.128.36.146' in [record['value'] for record in r[0]['data']]
def test_change_namestore_entry():
assert REST_CLIENT.change_namestore_entry(
"alice",
"pepfoundation",
"6.6.6.6",
"A"
)
r = REST_CLIENT.list_namestore_entries("alice")
assert 1 == len(r)
assert 'pepfoundation' == r[0]['record_name']
# It would modify any record that match all the flags, ending with only
# one, even for repeated records.
assert '6.6.6.6' == [record['value'] for record in r[0]['data']][0]
def test_delete_namestore_entry():
r = REST_CLIENT.delete_namestore_entry("alice", "pepfoundation")
assert r is True
r = REST_CLIENT.list_namestore_entries("alice")
# There should not be anymore records with that record name
assert 0 == len(r)
# # GNS#
# # ===#
def test_list_gns_record():
# Add an entry again, since it was deleted
r = REST_CLIENT.add_namestore_entry(
"alice",
"pepfoundation",
"95.128.36.146",
"A"
)
assert r is True
r = REST_CLIENT.list_gns_record("alice", "pepfoundation")
assert isinstance(r, dict)
assert 'pepfoundation.alice' == r['record_name']
assert '95.128.36.146' == r["data"][0]["value"]
def test_list_gns_record_type():
r = REST_CLIENT.list_gns_record_type("alice", "pepfoundation", "A")
assert isinstance(r, dict)
assert 'pepfoundation.alice' == r['record_name']
# Even if there're several records, all should be type A
assert 'A' == r['data'][0]['record_type']