Browse Source

Replace example by failing tests

master
juga 1 year ago
parent
commit
34e4bb6e71
2 changed files with 124 additions and 194 deletions
  1. +0
    -194
      pygnunetrest/example.py
  2. +124
    -0
      tests/test_gnunet.py

+ 0
- 194
pygnunetrest/example.py View File

@ -1,194 +0,0 @@
# Examples for the Python GNUnet REST Connector
import gnunet
# make instance of connector,
# without arguments it defaults to localhost, port 7776
print("\nMaking instance of connector")
conn = gnunet.GNUnetRest()
# it is also possible to specify host and port, for example:
# conn=gnunet.GNUnetRest("localhost","7771")
# test if connection is possible
print("\nTesting Connector")
if conn.test() == True:
print("Connection established")
else:
print("Connection not possible")
quit()
# IDENTITIES#
# ==========#
# first we create an identity named alice.
# the function createIdentity returns a boolean for success or failure.
print("\nCreating some identities")
idname = "alice"
success = conn.createIdentity(idname)
if success == True:
print("Identity ", idname, " created")
else:
print("Error: Identity not created")
# then we create two more identities, bob and carol.
idname = "bob"
success = conn.createIdentity(idname)
if success == True:
print("Identity ", idname, " created")
else:
print("Error: Identity not created")
idname = "carol"
success = conn.createIdentity(idname)
if success == True:
print("Identity ", idname, " created")
else:
print("Error: Identity not created")
# now we retrieve a list of all existing identities.
# the function returns a list of dictionaries.
print("\nRetrieving identities")
ids = conn.getIdentityList()
for x in ids:
print("Name: ", x["name"], "\nPublic Key: ", x["pubkey"], "\n")
# to retrieve the public key of specific identity, we use this:
pubkey_alice = conn.getIdentityPubkey("alice")
print("Public key of alice: ", pubkey_alice)
# to get the identity name corresponding to a public key, we use this:
name_alice = conn.getIdentityName(pubkey_alice)
print("Name for public key ", pubkey_alice, ": ", name_alice)
# next we change the name of identity "bob" to "dorothy".
print("\Changing name of identity")
from_id = "bob"
to_id = "dorothy"
success = conn.changeIdentityName("bob", "dorothy")
if success == True:
print("Changed identity ", from_id, " to ", to_id)
else:
print("Error: Identity not changed")
# in the end, we delete all created identities.
print("\nDeleting identities")
if conn.deleteIdentity("alice"):
print("deleted alice")
if conn.deleteIdentity("dorothy"):
print("deleted dorothy")
if conn.deleteIdentity("carol"):
print("deleted carol")
# NAMESTORE#
# =========#
# to use the namestore subsystem, we first need an identity (also called 'ego')
print("\nCreating identity for namestore")
idname = "emily"
success = conn.createIdentity(idname)
if success == True:
print("Identity ", idname, " created")
else:
print("Error: Identity not created")
# then, we have to make the created identity the default ego of namestore subsystem.
# note: the name of that identity (or ego) is also the name of the zone
print("\nMake identity the namestore default ego")
success = conn.setNamestoreIdentity(idname)
if success == True:
print(idname, " is now the namestore default identity.")
else:
print("Setting namestore default identity failed")
# now we add some namestore entries to the zone.
# syntax is: addNamestoreEntry(zone,name,value,type)
print("\nAdding some namestore entries")
zonename = idname
success1 = conn.addNamestoreEntry(zonename, "google", "8.8.8.8", "A")
success2 = conn.addNamestoreEntry(
zonename, "pepfoundation", "95.128.36.146", "A"
)
success3 = conn.addNamestoreEntry(zonename, "gnunet", "131.159.74.67", "A")
if success1 and success2 and success3:
print("3 records created")
else:
print("failure creating records")
# we can change existing namestore entries.
print("\nChanging namestore entry")
success = conn.changeNamestoreEntry(zonename, "google", "6.6.6.6", "A")
if success:
print("Namestore entry changed")
else:
print("failure changing namestore entry")
# now we show all existing namestore entries.
print("\nRetrieving namestore entries")
entries = conn.getNamestoreEntries(zonename)
for x in entries:
print("\n")
print("Name: ", x["record_name"])
for y in x["data"]:
print("Value: ", y["value"], " Type:", y["record_type"])
# delete all the namestore entries
print("\nDeleting all namestore entries")
success1 = conn.deleteNamestoreEntry(zonename, "google")
success2 = conn.deleteNamestoreEntry(zonename, "pepfoundation")
success3 = conn.deleteNamestoreEntry(zonename, "gnunet")
if success1 and success2 and success2:
print("Namestore entries successfully deleted")
else:
print("Error deleting namestore entries")
# GNS#
# ===#
# first we create a test record with 3 namestore entries.
print("\nCreating namestore entries")
zonename = idname
success1 = conn.addNamestoreEntry(zonename, "testrecord", "95.128.36.146", "A")
success2 = conn.addNamestoreEntry(
zonename, "testrecord", "Notes for the record", "TXT"
)
success3 = conn.addNamestoreEntry(
zonename, "testrecord", "Further notes for the record", "TXT"
)
# success3=True
# forgot why i did this
if success1 and success2 and success3:
print("3 records created")
else:
print("failure creating records")
# then we retrieve all entries for the test record
print("\nRetrieving GNS records by name")
entries = conn.getGNSValuesOfName(zonename, "testrecord")
print("\n")
print("Record name: ", entries["record_name"])
for x in entries["data"]:
print("Type: ", x["record_type"], " Value: ", x["value"])
# now we only retrieve entries for the test of type "TXT"
print("\nRetrieving GNS records by name and type")
entries = conn.getGNSValuesOfNameAndType(zonename, "testrecord", "TXT")
print("\n")
print("Record name: ", entries["record_name"])
for x in entries["data"]:
print("Type: ", x["record_type"], " Value: ", x["value"])
# in the end, we delete the test record.
print("\nDeleting test record")
success = conn.deleteNamestoreEntry(zonename, "testrecord")
if success:
print("Namestore entry successfully deleted")
else:
print("Error deleting namestore entries")

+ 124
- 0
tests/test_gnunet.py View File

@ -0,0 +1,124 @@
# Examples for the Python GNUnet REST Connector
from pygnunetrest import gnunetrest
def test_connect():
# make instance of connector,
# without arguments it defaults to localhost, port 7776
conn = gnunetrest.GNUnetRest()
# it is also possible to specify host and port, for example:
# conn=gnunetrest.GNUnetRest("localhost","7771")
# test if connection is possible
print("\nTesting Connector")
assert conn
# IDENTITIES#
# ==========#
def test_create_identity():
print("\nCreating some identities")
conn = gnunetrest.GNUnetRest()
# The request works, but doesn't create the identity
assert conn.create_identity("alice")
def test_list_identities():
conn = gnunetrest.GNUnetRest()
conn.create_identity("alice")
conn.create_identity("bob")
identities = conn.identity_list()
print(identities)
# This fail!
assert 2 == len(identities)
def test_fetch_identity_hash():
conn = gnunetrest.GNUnetRest()
conn.create_identity("alice")
r = conn.fetch_identity_hash("alice")
assert 52 == len(r)
# def test_fetch_identity_name():
# conn = gnunetrest.GNUnetRest()
# conn.create_identity("alice")
# r = conn.fetch_identity_name("alice")
def test_change_identity_name():
conn = gnunetrest.GNUnetRest()
conn.create_identity("alice")
assert conn.change_identity_name("alice", "carol")
r = conn.fetch_identity_hash("carol")
assert 52 == len(r)
def test_delete_identity():
conn = gnunetrest.GNUnetRest()
conn.create_identity("alice")
assert conn.delete_identity("alice")
assert not conn.fetch_identity_hash("alice")[0]
# # NAMESTORE#
# # =========#
def test_set_namestore_identity():
conn = gnunetrest.GNUnetRest()
conn.create_identity("alice")
assert conn.set_namestore_identity("alice")
def test_list_namestore_entry():
conn = gnunetrest.GNUnetRest()
assert conn.set_namestore_identity("alice")
r = conn.list_namestore_entries("alice")
print(r)
def test_add_namestore_entry():
conn = gnunetrest.GNUnetRest()
assert conn.add_namestore_entry(
"alice",
"pepfoundation",
"95.128.36.146",
"A"
)
def test_change_namestore_entry():
conn = gnunetrest.GNUnetRest()
assert conn.change_namestore_entry(
"alice",
"pepfoundation",
"6.6.6.6",
"A"
)
def test_delete_namestore_entry():
conn = gnunetrest.GNUnetRest()
assert conn.delete_namestore_entry("alice", "pepfoundation")
# # GNS#
# # ===#
def test_list_gns_record():
conn = gnunetrest.GNUnetRest()
r = conn.list_gns_record("alice", "pepfoundation")
print(r)
assert r[0]
assert r['record_name'] == 'pepfoundation.alice'
assert r['data']
def test_list_gns_record_type():
conn = gnunetrest.GNUnetRest()
r = conn.list_gns_record_type("alice", "pepfoundation", "A")
print(r)
assert r[0]
assert r['record_name'] == 'pepfoundation.alice'
assert r['data']

Loading…
Cancel
Save