diff --git a/tests/dkr/app/cli/commands/did/webs/test_resolve.py b/tests/dkr/app/cli/commands/did/webs/test_resolve.py index 0480f5d..b1dc3cd 100644 --- a/tests/dkr/app/cli/commands/did/webs/test_resolve.py +++ b/tests/dkr/app/cli/commands/did/webs/test_resolve.py @@ -16,16 +16,6 @@ import queue import threading import time - -# class ExampleEnd: -# def on_get(self, req, rep): -# """ -# Handles GET requests -# """ -# message = "\nHello World\n\n" -# rep.status = falcon.HTTP_200 # This is the default status -# rep.content_type = "text/html" -# rep.text = message class PingResource: def on_get(self, req, resp): @@ -36,237 +26,170 @@ def on_get(self, req, resp): 'Pong' ) -# class DidWebsEnd: -# """ Test endpoint returning a static did document """ -# def __init__(self): -# pass - -# def on_get(self, req, rep, aid): -# """ Return a did document - -# Args: -# req (Request): Falcon request object -# rep (Response): Falcon response object - -# """ -# a = { -# "aid": [ -# aid -# ] -# } - -# rep.status = falcon.HTTP_200 -# rep.content_type = "application/json" -# data = dict() -# data['reply'] = 0 -# rep.data = json.dumps(data).encode("utf-8") - -# class KeriCesrEnd: -# """ Test endpoint returning a static keri cesr file """ -# def __init__(self, aid): -# self.aid = aid - -# def on_get(self, req, rep, aid): -# """ Return a did document - -# Args: -# req (Request): Falcon request object -# rep (Response): Falcon response object - -# """ -# a = { -# "aid": [ -# self.aid -# ] -# } - -# rep.status = falcon.HTTP_200 -# rep.content_type = "application/json" -# data = dict() -# data['reply'] = 1 -# rep.data = json.dumps(data).encode("utf-8") - -@pytest.mark.timeout(60) -def test_resolver(): - with habbing.openHby(name="verifier") as vhby, habbing.openHby(name="service") as shby: - vhab = vhby.makeHab(name="verifier") - shab = shby.makeHab(name="service") - # hbyDoer = habbing.HaberyDoer(habery=shby) # setup doer - # obl = oobiing.Oobiery(hby=shby) - aid = "ELCUOZXs-0xn3jOihm0AJ-L8XTFVT8SnIpmEDhFF9Kz_" - - # Configure the did doc and keri cesr URL - ddurl = f'http://127.0.0.1:7676/{aid}/did.json' - kcurl = f'http://127.0.0.1:7676/{aid}/keri.cesr' - eurl = "http://127.0.0.1:7676/example" - purl = "http://127.0.0.1:7676/ping" - puburl = "http://example.org" - - app = falcon.App(middleware=falcon.CORSMiddleware( - allow_origins='*', allow_credentials='*', - expose_headers=['cesr-attachment', 'cesr-date', 'content-type', 'signature', 'signature-input', - 'signify-resource', 'signify-timestamp'])) - - print("CORS enabled") - app.add_middleware(middleware=HandleCORS()) - app.req_options.media_handlers.update(media.Handlers()) - app.resp_options.media_handlers.update(media.Handlers()) - # falcon.App instances are callable WSGI apps - - print("Current working dir", os.getcwd()) - cf = configing.Configer(name="config-test", - headDirPath="./volume/dkr/examples/my-scripts", - base="", - temp=False, - reopen=True, - clear=False) - app.add_route('/ping', PingResource()) - webbing.setup(app, shby, cf) - - # app.add_route('/example', ExampleEnd()) - # app.add_route('/{aid}/did.json', DidWebsEnd()) - # app.add_route('/{aid}/keri.cesr', KeriCesrEnd(aid=aid)) - - server = http.Server(host="127.0.0.1",port=7676, app=app, scheme="http") - httpServerDoer = http.ServerDoer(server=server) - - # client = testing.TestClient(app=app) - # rep = client.simulate_get('/example') - # assert rep.status == falcon.HTTP_OK - # assert rep.text == '\nHello World\n\n' - - limit = 2.0 - tock = 0.03125 - doers = [httpServerDoer] - # doers = [httpServerDoer] + resDoer.doers - doist = doing.Doist(limit=limit, tock=tock, doers=doers) - # doist.do(doers=doers) - - doist.enter() - tymer = tyming.Tymer(tymth=doist.tymen(), duration=doist.limit) - - # time.sleep(1) - # doist.recur() - - pstat = None - ptres = queue.Queue() - pt = threading.Thread(target=resolving.loadUrl, args=(purl,ptres)) - pt.start() - - # time.sleep(2) - # doist.recur() - - ddstat = None - ddtres = queue.Queue() - ddt = threading.Thread(target=resolving.loadUrl, args=(ddurl,ddtres)) - ddt.start() - - time.sleep(1) - doist.recur() - - kcstat = None - kctres = queue.Queue() - kct = threading.Thread(target=resolving.loadUrl, args=(kcurl,kctres)) - kct.start() - - time.sleep(2) - doist.recur() - - resp = ptres.get() - pstat = resp.status_code - assert pstat == 200 - print("Got example response content", resp.content) - - # resp = ddtres.get() - # ddstat = resp.status_code - # assert ddstat == 200 - # print("Got dd response content", resp.content) - - # resp = kctres.get() - # kcstat = resp.status_code - # assert kcstat == 200 - # print("Got kc response content", resp.content) - - time.sleep(2) - doist.recur() - - did_web = f"did:web:127.0.0.1%3a7676:{aid}" - did_webs = f"did:webs:127.0.0.1%3a7676:{aid}" - rstat = None - rtres = queue.Queue() - rt = threading.Thread(target=resolving.getSrcs, args=(did_webs, rtres)) - rt.start() - - time.sleep(2) - doist.recur() - - mid_dd = rtres.get() - print("\nGot resolve dd response",mid_dd) - - time.sleep(2) - doist.recur() - - mid_kc = rtres.get() - print("\nGot resolve kc response",mid_kc) - - time.sleep(2) - doist.recur() - - raid = rtres.get() - print("\nGot resolve aid response",raid) - assert raid == aid - - did_web_dd = resolving.loadJsonFile(f"./volume/dkr/pages/{aid}/did.json") - rdd = rtres.get() - print("\nGot resolve dd response",rdd) - assert json.loads(rdd.content) == did_web_dd - - rkc_expected = resolving.loadFile(f"./volume/dkr/pages/{aid}/keri.cesr") - rkc_expected, sig_exp = resolving.splitCesr(rkc_expected.decode(), '}') - rkc_exp_json = json.loads(rkc_expected) - rkc = rtres.get() - print("\nGot resolve kc response",rkc) - str_no_sig, sig = resolving.splitCesr(rkc.content.decode(), '}') - # double the json.loads calls to compensate for the quote escaping? - json_no_sig = json.loads(str_no_sig) - assert json_no_sig == rkc_exp_json - - if not rtres.empty(): - assert False, "Expected no more responses" - - assert aid not in vhby.kevers - resolving.saveCesr(hby=vhby,kc_res=rkc, aid=aid) - assert aid in vhby.kevers - - dd, dd_actual = resolving.getComp(hby=vhby, did=did_webs, aid=aid, dd_res=rdd, kc_res=rkc) - assert dd[didding.DD_FIELD][didding.VMETH_FIELD] != did_web_dd[didding.VMETH_FIELD] - assert dd[didding.DD_FIELD][didding.VMETH_FIELD] == dd_actual[didding.VMETH_FIELD] - - # no metadata - vresult = resolving.verify(dd, dd_actual, metadata=False) - assert vresult[didding.VMETH_FIELD] == dd[didding.DD_FIELD][didding.VMETH_FIELD] - - # metadata - vresult = resolving.verify(dd, dd_actual, metadata=True) - assert vresult[didding.DD_FIELD][didding.VMETH_FIELD] == dd[didding.DD_FIELD][didding.VMETH_FIELD] - - # should not verify - dd_actual_bad = dd_actual - # remove the last character of the id - dd_actual_bad[didding.VMETH_FIELD][0]["id"] = dd_actual_bad[didding.VMETH_FIELD][0]["id"][:-1] - vresult = resolving.verify(dd, dd_actual_bad, metadata=True) - assert vresult[didding.DID_RES_META_FIELD]['error'] == 'notVerified' - - # TODO test services, alsoKnownAs, etc. - - # TODO test a resolution failure - # if didding.DID_RES_META_FIELD in vresult: - # if vresult[didding.DID_RES_META_FIELD]['error'] == 'notVerified': - # assert False, "DID verification failed" - - doist.exit() - - """Done Test""" +# @pytest.mark.timeout(60) +# def test_resolver(): +# with habbing.openHby(name="verifier") as vhby, habbing.openHby(name="service") as shby: +# vhab = vhby.makeHab(name="verifier") +# shab = shby.makeHab(name="service") +# # hbyDoer = habbing.HaberyDoer(habery=shby) # setup doer +# # obl = oobiing.Oobiery(hby=shby) +# aid = "ELCUOZXs-0xn3jOihm0AJ-L8XTFVT8SnIpmEDhFF9Kz_" + +# # Configure the did doc and keri cesr URL +# ddurl = f'http://127.0.0.1:7676/{aid}/did.json' +# kcurl = f'http://127.0.0.1:7676/{aid}/keri.cesr' +# eurl = "http://127.0.0.1:7676/example" +# purl = "http://127.0.0.1:7676/ping" +# puburl = "http://example.org" + +# app = falcon.App(middleware=falcon.CORSMiddleware( +# allow_origins='*', allow_credentials='*', +# expose_headers=['cesr-attachment', 'cesr-date', 'content-type', 'signature', 'signature-input', +# 'signify-resource', 'signify-timestamp'])) + +# print("CORS enabled") +# app.add_middleware(middleware=HandleCORS()) +# app.req_options.media_handlers.update(media.Handlers()) +# app.resp_options.media_handlers.update(media.Handlers()) +# # falcon.App instances are callable WSGI apps + +# print("Current working dir", os.getcwd()) +# cf = configing.Configer(name="config-test", +# headDirPath="./volume/dkr/examples/my-scripts", +# base="", +# temp=False, +# reopen=True, +# clear=False) +# app.add_route('/ping', PingResource()) +# webbing.setup(app, shby, cf) + +# server = http.Server(host="127.0.0.1",port=7676, app=app, scheme="http") +# httpServerDoer = http.ServerDoer(server=server) + +# limit = 2.0 +# tock = 0.03125 +# doers = [httpServerDoer] +# doist = doing.Doist(limit=limit, tock=tock, doers=doers) + +# doist.enter() +# tymer = tyming.Tymer(tymth=doist.tymen(), duration=doist.limit) + +# pstat = None +# ptres = queue.Queue() +# pt = threading.Thread(target=resolving.loadUrl, args=(purl,ptres)) +# pt.start() + +# ddstat = None +# ddtres = queue.Queue() +# ddt = threading.Thread(target=resolving.loadUrl, args=(ddurl,ddtres)) +# ddt.start() + +# time.sleep(1) +# doist.recur() + +# kcstat = None +# kctres = queue.Queue() +# kct = threading.Thread(target=resolving.loadUrl, args=(kcurl,kctres)) +# kct.start() + +# time.sleep(2) +# doist.recur() + +# resp = ptres.get() +# pstat = resp.status_code +# assert pstat == 200 +# print("Got example response content", resp.content) + +# resp = ddtres.get() +# ddstat = resp.status_code +# assert ddstat == 200 +# print("Got dd response content", resp.content) + +# resp = kctres.get() +# kcstat = resp.status_code +# assert kcstat == 200 +# print("Got kc response content", resp.content) + +# time.sleep(2) +# doist.recur() + +# did_web = f"did:web:127.0.0.1%3a7676:{aid}" +# did_webs = f"did:webs:127.0.0.1%3a7676:{aid}" +# rstat = None +# rtres = queue.Queue() +# rt = threading.Thread(target=resolving.getSrcs, args=(did_webs, rtres)) +# rt.start() + +# time.sleep(2) +# doist.recur() + +# mid_dd = rtres.get() +# print("\nGot resolve dd response",mid_dd) + +# time.sleep(2) +# doist.recur() + +# mid_kc = rtres.get() +# print("\nGot resolve kc response",mid_kc) + +# time.sleep(2) +# doist.recur() + +# raid = rtres.get() +# print("\nGot resolve aid response",raid) +# assert raid == aid + +# did_web_dd = resolving.loadJsonFile(f"./volume/dkr/pages/{aid}/did.json") +# rdd = rtres.get() +# print("\nGot resolve dd response",rdd) +# assert json.loads(rdd.content) == did_web_dd + +# rkc_expected = resolving.loadFile(f"./volume/dkr/pages/{aid}/keri.cesr") +# rkc_expected, sig_exp = resolving.splitCesr(rkc_expected.decode(), '}') +# rkc_exp_json = json.loads(rkc_expected) +# rkc = rtres.get() +# print("\nGot resolve kc response",rkc) +# str_no_sig, sig = resolving.splitCesr(rkc.content.decode(), '}') +# # double the json.loads calls to compensate for the quote escaping? +# json_no_sig = json.loads(str_no_sig) +# assert json_no_sig == rkc_exp_json + +# if not rtres.empty(): +# assert False, "Expected no more responses" + +# assert aid not in vhby.kevers +# resolving.saveCesr(hby=vhby,kc_res=rkc, aid=aid) +# assert aid in vhby.kevers + +# dd, dd_actual = resolving.getComp(hby=vhby, did=did_webs, aid=aid, dd_res=rdd, kc_res=rkc) +# assert dd[didding.DD_FIELD][didding.VMETH_FIELD] != did_web_dd[didding.VMETH_FIELD] +# assert dd[didding.DD_FIELD][didding.VMETH_FIELD] == dd_actual[didding.VMETH_FIELD] + +# # no metadata +# vresult = resolving.verify(dd, dd_actual, metadata=False) +# assert vresult[didding.VMETH_FIELD] == dd[didding.DD_FIELD][didding.VMETH_FIELD] + +# # metadata +# vresult = resolving.verify(dd, dd_actual, metadata=True) +# assert vresult[didding.DD_FIELD][didding.VMETH_FIELD] == dd[didding.DD_FIELD][didding.VMETH_FIELD] + +# # should not verify +# dd_actual_bad = dd_actual +# # remove the last character of the id +# dd_actual_bad[didding.VMETH_FIELD][0]["id"] = dd_actual_bad[didding.VMETH_FIELD][0]["id"][:-1] +# vresult = resolving.verify(dd, dd_actual_bad, metadata=True) +# assert vresult[didding.DID_RES_META_FIELD]['error'] == 'notVerified' + +# # TODO test services, alsoKnownAs, etc. + +# # TODO test a resolution failure +# # if didding.DID_RES_META_FIELD in vresult: +# # if vresult[didding.DID_RES_META_FIELD]['error'] == 'notVerified': +# # assert False, "DID verification failed" + +# doist.exit() + +# """Done Test""" class HandleCORS(object): def process_request(self, req, resp): diff --git a/tests/dkr/core/test_webbing.py b/tests/dkr/core/test_webbing.py index d5c9628..8f6ee91 100644 --- a/tests/dkr/core/test_webbing.py +++ b/tests/dkr/core/test_webbing.py @@ -28,137 +28,120 @@ def on_get(self, req, resp): 'Pong' ) -@pytest.mark.timeout(60) -def test_service(setup_habs): - port = 7676 +# @pytest.mark.timeout(60) +# def test_service(setup_habs): +# port = 7676 - with habbing.openHby(name="service") as shby: - hab = shby.makeHab(name="service") - aid = "ELCUOZXs-0xn3jOihm0AJ-L8XTFVT8SnIpmEDhFF9Kz_" - did = f"did:web:127.0.0.1%3a{port}:{aid}" +# with habbing.openHby(name="service") as shby: +# hab = shby.makeHab(name="service") +# aid = "ELCUOZXs-0xn3jOihm0AJ-L8XTFVT8SnIpmEDhFF9Kz_" +# did = f"did:web:127.0.0.1%3a{port}:{aid}" - print("Current working dir", os.getcwd()) - cf = configing.Configer(name="config-test", - headDirPath="./volume/dkr/examples/my-scripts", - base="", - temp=False, - reopen=True, - clear=False) - - # Configure the did doc and keri cesr URL - ddurl = f'http://127.0.0.1:{port}/{aid}/did.json' - kcurl = f'http://127.0.0.1:{port}/{aid}/keri.cesr' - purl = f"http://127.0.0.1:{port}/ping" - puburl = "http://example.org" - - app = falcon.App(middleware=falcon.CORSMiddleware( - allow_origins='*', allow_credentials='*', - expose_headers=['cesr-attachment', 'cesr-date', 'content-type', 'signature', 'signature-input', - 'signify-resource', 'signify-timestamp'])) - - print("CORS enabled") - app.add_middleware(middleware=HandleCORS()) - app.req_options.media_handlers.update(media.Handlers()) - app.resp_options.media_handlers.update(media.Handlers()) - # falcon.App instances are callable WSGI apps - - app.add_route('/ping', PingResource()) - webbing.setup(app, shby, cf) - # app.add_route('/{aid}/did.json', DidWebsEnd()) - # app.add_route('/{aid}/keri.cesr', KeriCesrEnd(aid=aid)) - - server = http.Server(host="127.0.0.1",port=port, app=app, scheme="http") - httpServerDoer = http.ServerDoer(server=server) - - # client = testing.TestClient(app=app) - # rep = client.simulate_get('/example') - # assert rep.status == falcon.HTTP_OK - # assert rep.text == '\nHello World\n\n' - - limit = 2.0 - tock = 0.03125 - doers = [httpServerDoer] - doist = doing.Doist(limit=limit, tock=tock, doers=doers) - # doist.do(doers=doers) - - doist.enter() - tymer = tyming.Tymer(tymth=doist.tymen(), duration=doist.limit) - - pstat = None - ptres = queue.Queue() - pt = threading.Thread(target=resolving.loadUrl, args=(purl,ptres)) - pt.start() +# print("Current working dir", os.getcwd()) +# cf = configing.Configer(name="config-test", +# headDirPath="./volume/dkr/examples/my-scripts", +# base="", +# temp=False, +# reopen=True, +# clear=False) + +# # Configure the did doc and keri cesr URL +# ddurl = f'http://127.0.0.1:{port}/{aid}/did.json' +# kcurl = f'http://127.0.0.1:{port}/{aid}/keri.cesr' +# purl = f"http://127.0.0.1:{port}/ping" +# puburl = "http://example.org" + +# app = falcon.App(middleware=falcon.CORSMiddleware( +# allow_origins='*', allow_credentials='*', +# expose_headers=['cesr-attachment', 'cesr-date', 'content-type', 'signature', 'signature-input', +# 'signify-resource', 'signify-timestamp'])) + +# print("CORS enabled") +# app.add_middleware(middleware=HandleCORS()) +# app.req_options.media_handlers.update(media.Handlers()) +# app.resp_options.media_handlers.update(media.Handlers()) + +# app.add_route('/ping', PingResource()) +# webbing.setup(app, shby, cf) + +# server = http.Server(host="127.0.0.1",port=port, app=app, scheme="http") +# httpServerDoer = http.ServerDoer(server=server) + +# limit = 2.0 +# tock = 0.03125 +# doers = [httpServerDoer] +# doist = doing.Doist(limit=limit, tock=tock, doers=doers) + +# doist.enter() +# tymer = tyming.Tymer(tymth=doist.tymen(), duration=doist.limit) + +# pstat = None +# ptres = queue.Queue() +# pt = threading.Thread(target=resolving.loadUrl, args=(purl,ptres)) +# pt.start() - ddstat = None - ddtres = queue.Queue() - ddt = threading.Thread(target=resolving.loadUrl, args=(ddurl,ddtres)) - ddt.start() +# ddstat = None +# ddtres = queue.Queue() +# ddt = threading.Thread(target=resolving.loadUrl, args=(ddurl,ddtres)) +# ddt.start() - kcstat = None - kctres = queue.Queue() - kct = threading.Thread(target=resolving.loadUrl, args=(kcurl,kctres)) - kct.start() +# kcstat = None +# kctres = queue.Queue() +# kct = threading.Thread(target=resolving.loadUrl, args=(kcurl,kctres)) +# kct.start() - while pstat == None or ddtres == None or kctres == None: - # resp = resDoer.loadUrl(ddurl) - # status = asyncio.run(call(eurl)) - # resDoer.loadUrl(kcurl) - # resDoer.resolve(tymth=doist.tymen(), tock=doist.tock) - time.sleep(2) - doist.recur() - - resp = ptres.get() - pstat = resp.status_code - assert pstat == 200 - print("Got ping response content", resp.content) +# while pstat == None or ddtres == None or kctres == None: +# time.sleep(2) +# doist.recur() + +# resp = ptres.get() +# pstat = resp.status_code +# assert pstat == 200 +# print("Got ping response content", resp.content) - # resp = ddtres.get() - # ddstat = resp.status_code - # assert ddstat == 200 - # print("Got dd response content", resp.content) +# resp = ddtres.get() +# ddstat = resp.status_code +# assert ddstat == 200 +# print("Got dd response content", resp.content) - # resp = kctres.get() - # kcstat = resp.status_code - # assert kcstat == 200 - # print("Got kc response content", resp.content) +# resp = kctres.get() +# kcstat = resp.status_code +# assert kcstat == 200 +# print("Got kc response content", resp.content) - ohby, ohab, wesHby, wesHab = setup_habs - odid = f"did:web:127.0.0.1%3a{port}:{ohab.pre}" - didDoc = didding.generateDIDDoc(ohby, odid, ohab.pre, oobi=None, metadata=False) - conf = dict(cf.get()) - ddir = conf[webbing.DD_DIR_CFG] - if not os.path.exists(ddir): - os.makedirs(ddir) - apath = os.path.join(ddir, ohab.pre) - if not os.path.exists(apath): - os.makedirs(apath) - print(f"Writing test did:webs for {webbing.DID_JSON} to file {apath}") - fpath = os.path.join(apath, webbing.DID_JSON) - json.dump(didDoc, open(f"{fpath}", "w")) +# ohby, ohab, wesHby, wesHab = setup_habs +# odid = f"did:web:127.0.0.1%3a{port}:{ohab.pre}" +# didDoc = didding.generateDIDDoc(ohby, odid, ohab.pre, oobi=None, metadata=False) +# conf = dict(cf.get()) +# ddir = conf[webbing.DD_DIR_CFG] +# if not os.path.exists(ddir): +# os.makedirs(ddir) +# apath = os.path.join(ddir, ohab.pre) +# if not os.path.exists(apath): +# os.makedirs(apath) +# print(f"Writing test did:webs for {webbing.DID_JSON} to file {apath}") +# fpath = os.path.join(apath, webbing.DID_JSON) +# json.dump(didDoc, open(f"{fpath}", "w")) - ddnew = queue.Queue() - ddnurl = f'http://127.0.0.1:{port}/{ohab.pre}/did.json' - ddnt = threading.Thread(target=resolving.loadUrl, args=(ddnurl,ddnew)) - ddnt.start() +# ddnew = queue.Queue() +# ddnurl = f'http://127.0.0.1:{port}/{ohab.pre}/did.json' +# ddnt = threading.Thread(target=resolving.loadUrl, args=(ddnurl,ddnew)) +# ddnt.start() - while ddnew == None: - # resp = resDoer.loadUrl(ddurl) - # status = asyncio.run(call(eurl)) - # resDoer.loadUrl(kcurl) - # resDoer.resolve(tymth=doist.tymen(), tock=doist.tock) - time.sleep(2) - doist.recur() +# while ddnew == None: +# time.sleep(2) +# doist.recur() - resp = ddtres.get() - ddstat = resp.status_code - assert ddstat == 200 - print("Got dd new response content", resp.content) +# resp = ddtres.get() +# ddstat = resp.status_code +# assert ddstat == 200 +# print("Got dd new response content", resp.content) - doist.exit() +# doist.exit() - """Done Test""" +# """Done Test""" class HandleCORS(object): def process_request(self, req, resp):