9 dbm "github.com/tendermint/tmlibs/db"
11 "github.com/vapor/errors"
14 func TestCreate(t *testing.T) {
15 testDB := dbm.NewDB("testdb", "leveldb", "temp")
16 defer os.RemoveAll("temp")
17 cs := NewStore(testDB)
24 {"b", "network", nil},
25 {"", "client", ErrBadID},
26 {"bad:id", "client", ErrBadID},
27 {"a", "network", ErrDuplicateID}, // this aborts the transaction, so no tests can follow
30 for _, c := range cases {
31 _, err := cs.Create(c.id, c.typ)
32 if errors.Root(err) != c.want {
33 t.Errorf("Create(%s, %s) error = %s want %s", c.id, c.typ, err, c.want)
38 func TestList(t *testing.T) {
39 ctx := context.Background()
40 testDB := dbm.NewDB("testdb", "leveldb", "temp")
41 defer os.RemoveAll("temp")
42 cs := NewStore(testDB)
44 tokenMap := make(map[string]*Token)
45 tokenMap["ab"] = mustCreateToken(ctx, t, cs, "ab", "test")
46 tokenMap["bc"] = mustCreateToken(ctx, t, cs, "bc", "test")
47 tokenMap["cd"] = mustCreateToken(ctx, t, cs, "cd", "test")
51 t.Errorf("List errored: get list error")
54 if len(got) != len(tokenMap) {
55 t.Error("List errored: get invalid length")
57 for _, v := range got {
58 if v.Token != tokenMap[v.ID].Token {
59 t.Errorf("List error: ID: %s, expected token: %s, DB token: %s", v.ID, *tokenMap[v.ID], v.Token)
65 func TestCheck(t *testing.T) {
66 ctx := context.Background()
67 testDB := dbm.NewDB("testdb", "leveldb", "temp")
68 defer os.RemoveAll("temp")
69 cs := NewStore(testDB)
71 token := mustCreateToken(ctx, t, cs, "x", "client")
72 tokenParts := strings.Split(token.Token, ":")
74 if err := cs.Check(tokenParts[0], tokenParts[1]); err != nil {
78 if err := cs.Check("x", "badsecret"); err != ErrInvalidToken {
79 t.Fatal("invalid token check passed")
83 func TestDelete(t *testing.T) {
84 ctx := context.Background()
85 testDB := dbm.NewDB("testdb", "leveldb", "temp")
86 defer os.RemoveAll("temp")
87 cs := NewStore(testDB)
90 mustCreateToken(ctx, t, cs, id, "client")
97 value := cs.DB.Get([]byte(id))
99 t.Fatal("delete fail")
103 func TestDeleteWithInvalidId(t *testing.T) {
104 testDB := dbm.NewDB("testdb", "leveldb", "temp")
105 defer os.RemoveAll("temp")
106 cs := NewStore(testDB)
108 err := cs.Delete("@")
109 if errors.Root(err) != ErrBadID {
110 t.Errorf("Deletion with invalid id success, while it should not")
114 func mustCreateToken(ctx context.Context, t *testing.T, cs *CredentialStore, id, typ string) *Token {
115 token, err := cs.Create(id, typ)