Skip to content

Commit

Permalink
Render is read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Sep 14, 2023
1 parent 61079bb commit dd50037
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
16 changes: 1 addition & 15 deletions examples/gno.land/r/demo/keystore/keystore.gno
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,11 @@ func size(owner string) string {
return ufmt.Sprintf("%d", keystore.Data.Size())
}

// Render provides url access to the functions of the keystore
// Render provides read-only url access to the functions of the keystore
// "" -> show all keystores listed by owner
// "owner" -> show all keys for that owner's keystore
// "owner:size" -> returns size of owner's keystore
// "owner:get:key" -> show value for that key in owner's keystore
// "owner:set:key:value" -> sets a key-value pair for owner's keystore (owner must be caller)
// "owner:remove:key" -> removes key from owner keystore (owner must be caller)
func Render(p string) string {
var response string
args := strings.Split(p, ":")
Expand Down Expand Up @@ -175,18 +173,6 @@ func Render(p string) string {
key := args[2]
if cmd == "get" {
return get(owner, key)
} else if cmd == "remove" {
// remove will only work if caller is owner
return remove(owner, key)
}
} else if numArgs == 4 {
owner := args[0]
cmd := args[1]
key := args[2]
val := args[3]
if cmd == "set" {
// set only works if caller is owner
return set(owner, key, val)
}
}
return response
Expand Down
11 changes: 8 additions & 3 deletions examples/gno.land/r/demo/keystore/keystore_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ func TestRender(t *testing.T) {
{author1, author2, []string{"get", "hello"}, "universe"},
// either author can get the other info
{author2, author1, []string{"get", "hello"}, "world"},
// cannot perform write operations is owner (author2) is not caller (author1)
{author1, author2, []string{"set", "hello", "should not work"}, StatusNoWriteAccess},
{author1, author2, []string{"get", "hello"}, "universe"},
// anyone can view the databases
{author1, author2, []string{}, `- [g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6](/r/demo/keystore:g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6) (2 keys)
Expand All @@ -64,7 +62,14 @@ func TestRender(t *testing.T) {
p = strings.TrimSuffix(p, ":")
t.Run(p, func(t *testing.T) {
std.TestSetOrigCaller(tc.caller)
act := strings.TrimSpace(Render(p))
var act string
if len(tc.ps) > 0 && tc.ps[0] == "set" {
act = strings.TrimSpace(Set(tc.ps[1], tc.ps[2]))
} else if len(tc.ps) > 0 && tc.ps[0] == "remove" {
act = strings.TrimSpace(Remove(tc.ps[1]))
} else {
act = strings.TrimSpace(Render(p))
}
if act != tc.exp {
t.Errorf("%v -> '%s', got '%s', wanted '%s'", tc.ps, p, act, tc.exp)
}
Expand Down

0 comments on commit dd50037

Please sign in to comment.