keystore_test.gno
2.52 Kb ยท 78 lines
1package keystore
2
3import (
4 "std"
5 "strings"
6 "testing"
7
8 "gno.land/p/demo/testutils"
9 "gno.land/p/demo/uassert"
10 "gno.land/p/demo/ufmt"
11)
12
13func TestRender(t *testing.T) {
14 var (
15 author1 std.Address = testutils.TestAddress("author1")
16 author2 std.Address = testutils.TestAddress("author2")
17 )
18
19 tt := []struct {
20 caller std.Address
21 owner std.Address
22 ps []string
23 exp string
24 }{
25 // can set database if the owner is the caller
26 {author1, author1, []string{"set", "hello", "gno"}, StatusOK},
27 {author1, author1, []string{"size"}, "1"},
28 {author1, author1, []string{"set", "hello", "world"}, StatusOK},
29 {author1, author1, []string{"size"}, "1"},
30 {author1, author1, []string{"set", "hi", "gno"}, StatusOK},
31 {author1, author1, []string{"size"}, "2"},
32 // only owner can remove
33 {author1, author1, []string{"remove", "hi"}, StatusOK},
34 {author1, author1, []string{"get", "hi"}, StatusNotFound},
35 {author1, author1, []string{"size"}, "1"},
36 // add back
37 {author1, author1, []string{"set", "hi", "gno"}, StatusOK},
38 {author1, author1, []string{"size"}, "2"},
39
40 // different owner has different database
41 {author2, author2, []string{"set", "hello", "universe"}, StatusOK},
42 // either author can get the other info
43 {author1, author2, []string{"get", "hello"}, "universe"},
44 // either author can get the other info
45 {author2, author1, []string{"get", "hello"}, "world"},
46 {author1, author2, []string{"get", "hello"}, "universe"},
47 // anyone can view the databases
48 {author1, author2, []string{}, `- [g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6](/r/demo/keystore:g1v96hg6r0wgc47h6lta047h6lta047h6lm33tq6) (2 keys)
49- [g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00](/r/demo/keystore:g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00) (1 keys)`},
50 // anyone can view the keys in a database
51 {author1, author2, []string{""}, `# g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00 database
52
53- 0 [hello](/r/demo/keystore:g1v96hg6r0wge97h6lta047h6lta047h6lyz7c00:get:hello)`},
54 }
55 for _, tc := range tt {
56 p := ""
57 if len(tc.ps) > 0 {
58 p = tc.owner.String()
59 for _, psv := range tc.ps {
60 p += ":" + psv
61 }
62 }
63 p = strings.TrimSuffix(p, ":")
64 t.Run(p, func(t *testing.T) {
65 testing.SetOriginCaller(tc.caller)
66 var act string
67 if len(tc.ps) > 0 && tc.ps[0] == "set" {
68 act = strings.TrimSpace(Set(tc.ps[1], tc.ps[2]))
69 } else if len(tc.ps) > 0 && tc.ps[0] == "remove" {
70 act = strings.TrimSpace(Remove(tc.ps[1]))
71 } else {
72 act = strings.TrimSpace(Render(p))
73 }
74
75 uassert.Equal(t, tc.exp, act, ufmt.Sprintf("%v -> '%s'", tc.ps, p))
76 })
77 }
78}