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