Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IdentifierResourceEnd: rename alias, events, and delete aid #218

Merged
merged 4 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions src/keria/app/aiding.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def loadEnds(app, agency, authn):
app.add_route("/identifiers", aidsEnd)
aidEnd = IdentifierResourceEnd()
app.add_route("/identifiers/{name}", aidEnd)
app.add_route("/identifiers/{name}/events", aidEnd)

aidOOBIsEnd = IdentifierOOBICollectionEnd()
app.add_route("/identifiers/{name}/oobis", aidOOBIsEnd)
Expand Down Expand Up @@ -456,23 +457,73 @@ def on_get(req, rep, name):
rep.data = json.dumps(data).encode("utf-8")

def on_put(self, req, rep, name):
""" Identifier UPDATE endpoint
""" Identifier rename endpoint

Parameters:
req (Request): falcon.Request HTTP request object
rep (Response): falcon.Response HTTP response object
name (str): human readable name for Hab to rename

"""
if not name:
raise falcon.HTTPBadRequest(description="name is required")
agent = req.context.agent
hab = agent.hby.habByName(name)

if hab is None:
raise falcon.HTTPNotFound(title=f"No AID with name {name} found")
body = req.get_media()
newName = body.get("name")
habord = hab.db.habs.get(keys=name)
hab.db.habs.put(keys=newName,
val=habord)
hab.db.habs.rem(keys=name)
hab.name = newName
hab = agent.hby.habByName(newName)
data = info(hab, agent.mgr, full=True)
rep.status = falcon.HTTP_200
rep.content_type = "application/json"
rep.data = json.dumps(data).encode("utf-8")

def on_delete(self, req, rep, name):
""" Identifier delete endpoint

Parameters:
req (Request): falcon.Request HTTP request object
rep (Response): falcon.Response HTTP response object
name (str): human readable name for Hab to delete

"""
if not name:
raise falcon.HTTPBadRequest(description="name is required")
agent = req.context.agent
hab = agent.hby.habByName(name)
if hab is None:
raise falcon.HTTPNotFound(title=f"No AID with name {name} found")
hab.db.habs.rem(keys=name)
rep.status = falcon.HTTP_200

def on_post(self, req, rep, name):
""" Identifier events endpoint

Parameters:
req (Request): falcon.Request HTTP request object
rep (Response): falcon.Response HTTP response object
name (str): human readable name for Hab to rotate or interact

"""
if not name:
raise falcon.HTTPBadRequest(description="name is required")
agent = req.context.agent
try:
body = req.get_media()
typ = Ilks.ixn if req.params.get("type") == "ixn" else Ilks.rot

if typ in (Ilks.rot,):
if body.get("rot") is not None:
op = self.rotate(agent, name, body)
else:
elif body.get("ixn") is not None:
op = self.interact(agent, name, body)
else:
raise falcon.HTTPBadRequest(title="invalid request",
description=f"required field 'rot' or 'ixn' missing from request")

rep.status = falcon.HTTP_200
rep.content_type = "application/json"
Expand Down
44 changes: 30 additions & 14 deletions tests/app/test_aiding.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def test_identifier_collection_end(helpers):
resend = aiding.IdentifierResourceEnd()
app.add_route("/identifiers", end)
app.add_route("/identifiers/{name}", resend)
app.add_route("/identifiers/{name}/events", resend)

groupEnd = aiding.GroupMemberCollectionEnd()
app.add_route("/identifiers/{name}/members", groupEnd)
Expand Down Expand Up @@ -365,7 +366,7 @@ def test_identifier_collection_end(helpers):
'salty': {'stem': 'signify:aid', 'pidx': 0, 'tier': 'low', 'sxlt': sxlt, 'transferable': True, 'kidx': 1,
'icodes': [MtrDex.Ed25519_Seed], 'ncodes': [MtrDex.Ed25519_Seed]}
}
res = client.simulate_put(path="/identifiers/aid1", body=json.dumps(body))
res = client.simulate_post(path="/identifiers/aid1/events", body=json.dumps(body))
assert res.status_code == 200

# Try with missing arguments
Expand Down Expand Up @@ -470,8 +471,21 @@ def test_identifier_collection_end(helpers):
'salty': {'stem': 'signify:aid', 'pidx': 0, 'tier': 'low', 'sxlt': sxlt, 'transferable': True, 'kidx': 3,
'icodes': [MtrDex.Ed25519_Seed], 'ncodes': [MtrDex.Ed25519_Seed]}
}
res = client.simulate_put(path="/identifiers/aid3", body=json.dumps(body))
res = client.simulate_post(path="/identifiers/aid3/events", body=json.dumps(body))
assert res.status_code == 200

# rename aid3
res = client.simulate_put(path="/identifiers/aid3", body=json.dumps({"name": "aid3Renamed"}))
assert res.status_code == 200
aid = res.json
assert aid["name"] == "aid3Renamed"

# delete aid3renamed
res = client.simulate_delete(path="/identifiers/aid3Renamed")
assert res.status_code == 200
res = client.simulate_get(path="/identifiers")
assert res.status_code == 200
assert len(res.json) == 2

# create member habs for group AID
p1 = p1hby.makeHab(name="p1")
Expand Down Expand Up @@ -599,8 +613,8 @@ def test_identifier_collection_end(helpers):

res = client.simulate_get(path="/identifiers")
assert res.status_code == 200
assert len(res.json) == 4
aid = res.json[3]
assert len(res.json) == 3
aid = res.json[2]
assert aid["name"] == "multisig"
assert aid["prefix"] == serder.pre
group = aid["group"]
Expand Down Expand Up @@ -654,6 +668,7 @@ def test_identifier_collection_end(helpers):
resend = aiding.IdentifierResourceEnd()
app.add_route("/identifiers", end)
app.add_route("/identifiers/{name}", resend)
app.add_route("/identifiers/{name}/events", resend)
eventsEnd = agenting.KeyEventCollectionEnd()
app.add_route("/events", eventsEnd)

Expand Down Expand Up @@ -722,7 +737,7 @@ def test_identifier_collection_end(helpers):
"transferable": True,
}
}
res = client.simulate_put(path="/identifiers/randy1", body=json.dumps(body))
res = client.simulate_post(path="/identifiers/randy1/events", body=json.dumps(body))
assert res.status_code == 200
assert res.json["response"] == serder.ked
res = client.simulate_get(path="/identifiers")
Expand All @@ -740,7 +755,7 @@ def test_identifier_collection_end(helpers):
body = {'ixn': serder.ked,
'sigs': sigers
}
res = client.simulate_put(path="/identifiers/randy1?type=ixn", body=json.dumps(body))
res = client.simulate_post(path="/identifiers/randy1/events", body=json.dumps(body))
assert res.status_code == 200
assert res.json["response"] == serder.ked

Expand All @@ -751,17 +766,17 @@ def test_identifier_collection_end(helpers):
assert events[2] == serder.ked

# Bad interactions
res = client.simulate_put(path="/identifiers/badrandy?type=ixn", body=json.dumps(body))
res = client.simulate_post(path="/identifiers/badrandy/events", body=json.dumps(body))
assert res.status_code == 404
assert res.json == {'title': 'No AID badrandy found'}

body = {'sigs': sigers}
res = client.simulate_put(path="/identifiers/randy1?type=ixn", body=json.dumps(body))
res = client.simulate_post(path="/identifiers/randy1/events", body=json.dumps(body))
assert res.status_code == 400
assert res.json == {'description': "required field 'ixn' missing from request", 'title': 'invalid interaction'}
assert res.json == {'description': "required field 'rot' or 'ixn' missing from request", 'title': 'invalid request'}

body = {'ixn': serder.ked}
res = client.simulate_put(path="/identifiers/randy1?type=ixn", body=json.dumps(body))
res = client.simulate_post(path="/identifiers/randy1/events", body=json.dumps(body))
assert res.status_code == 400
assert res.json == {'description': "required field 'sigs' missing from request", 'title': 'invalid interaction'}

Expand All @@ -774,7 +789,7 @@ def test_identifier_collection_end(helpers):
"transferable": True,
}
}
res = client.simulate_put(path="/identifiers/randybad?type=rot", body=json.dumps(body))
res = client.simulate_post(path="/identifiers/randybad/events", body=json.dumps(body))
assert res.status_code == 404
assert res.json == {'title': 'No AID with name randybad found'}

Expand All @@ -786,9 +801,9 @@ def test_identifier_collection_end(helpers):
"transferable": True,
}
}
res = client.simulate_put(path="/identifiers/randy1", body=json.dumps(body))
res = client.simulate_post(path="/identifiers/randy1/events", body=json.dumps(body))
assert res.status_code == 400
assert res.json == {'description': "required field 'rot' missing from request", 'title': 'invalid rotation'}
assert res.json == {'description': "required field 'rot' or 'ixn' missing from request", 'title': 'invalid request'}

# rotate to unknown witness
serder = eventing.rotate(keys=keys,
Expand All @@ -812,7 +827,7 @@ def test_identifier_collection_end(helpers):
"transferable": True,
}
}
res = client.simulate_put(path="/identifiers/randy1", body=json.dumps(body))
res = client.simulate_post(path="/identifiers/randy1/events", body=json.dumps(body))
assert res.status_code == 400
assert res.json == {'description': "unknown witness EJJR2nmwyYAZAoTNZH3ULvaU6Z-i0d8fSVPzhzS6b5CM",
'title': '400 Bad Request'}
Expand All @@ -823,6 +838,7 @@ def test_identifier_collection_end(helpers):
resend = aiding.IdentifierResourceEnd()
app.add_route("/identifiers", end)
app.add_route("/identifiers/{name}", resend)
app.add_route("/identifiers/{name}/events", resend)
eventsEnd = agenting.KeyEventCollectionEnd()
app.add_route("/events", eventsEnd)

Expand Down
Loading
Loading