9 dbm "github.com/tendermint/tmlibs/db"
11 "github.com/bytom/errors"
14 func TestCreate(t *testing.T) {
15 testDB := dbm.NewDB("testdb", "leveldb", "temp")
16 defer os.RemoveAll("temp")
17 cs := NewStore(testDB)
18 ctx := context.Background()
25 {"b", "network", nil},
26 {"", "client", ErrBadID},
27 {"bad:id", "client", ErrBadID},
28 {"a", "network", ErrDuplicateID}, // this aborts the transaction, so no tests can follow
31 for _, c := range cases {
32 _, err := cs.Create(ctx, c.id, c.typ)
33 if errors.Root(err) != c.want {
34 t.Errorf("Create(%s, %s) error = %s want %s", c.id, c.typ, err, c.want)
39 func TestList(t *testing.T) {
40 ctx := context.Background()
41 testDB := dbm.NewDB("testdb", "leveldb", "temp")
42 defer os.RemoveAll("temp")
43 cs := NewStore(testDB)
45 tokenMap := make(map[string]*Token)
46 tokenMap["ab"] = mustCreateToken(ctx, t, cs, "ab", "test")
47 tokenMap["bc"] = mustCreateToken(ctx, t, cs, "bc", "test")
48 tokenMap["cd"] = mustCreateToken(ctx, t, cs, "cd", "test")
50 got, err := cs.List(ctx)
52 t.Errorf("List errored: get list error")
55 if len(got) != len(tokenMap) {
56 t.Error("List errored: get invalid length")
58 for _, v := range got {
59 if m := strings.Compare(v.Token, tokenMap[v.ID].Token); m != 0 {
60 t.Errorf("List error: ID: %s, expected token: %s, DB token: %s", v.ID, *tokenMap[v.ID], v.Token)
66 func TestCheck(t *testing.T) {
67 ctx := context.Background()
68 testDB := dbm.NewDB("testdb", "leveldb", "temp")
69 defer os.RemoveAll("temp")
70 cs := NewStore(testDB)
72 token := mustCreateToken(ctx, t, cs, "x", "client")
73 tokenParts := strings.Split(token.Token, ":")
75 valid, err := cs.Check(ctx, tokenParts[0], tokenParts[1])
80 t.Fatal("expected token and secret to be valid")
83 valid, err = cs.Check(ctx, "x", "badsecret")
88 t.Fatal("expected bad secret to not be valid")
92 func TestDelete(t *testing.T) {
93 ctx := context.Background()
94 testDB := dbm.NewDB("testdb", "leveldb", "temp")
95 defer os.RemoveAll("temp")
96 cs := NewStore(testDB)
99 mustCreateToken(ctx, t, cs, id, "client")
101 err := cs.Delete(ctx, id)
106 value := cs.DB.Get([]byte(id))
108 t.Fatal("delete fail")
112 func TestDeleteWithInvalidId(t *testing.T) {
113 ctx := context.Background()
114 testDB := dbm.NewDB("testdb", "leveldb", "temp")
115 defer os.RemoveAll("temp")
116 cs := NewStore(testDB)
118 err := cs.Delete(ctx, "@")
119 if errors.Root(err) != ErrBadID {
120 t.Errorf("Deletion with invalid id success, while it should not")
124 func mustCreateToken(ctx context.Context, t *testing.T, cs *CredentialStore, id, typ string) *Token {
125 token, err := cs.Create(ctx, id, typ)