9 dbm "github.com/vapor/database/leveldb"
10 "github.com/vapor/errors"
13 func TestCreate(t *testing.T) {
14 testDB := dbm.NewDB("testdb", "leveldb", "temp")
15 defer os.RemoveAll("temp")
16 cs := NewStore(testDB)
23 {"b", "network", nil},
24 {"", "client", ErrBadID},
25 {"bad:id", "client", ErrBadID},
26 {"a", "network", ErrDuplicateID}, // this aborts the transaction, so no tests can follow
29 for _, c := range cases {
30 _, err := cs.Create(c.id, c.typ)
31 if errors.Root(err) != c.want {
32 t.Errorf("Create(%s, %s) error = %s want %s", c.id, c.typ, err, c.want)
37 func TestList(t *testing.T) {
38 ctx := context.Background()
39 testDB := dbm.NewDB("testdb", "leveldb", "temp")
40 defer os.RemoveAll("temp")
41 cs := NewStore(testDB)
43 tokenMap := make(map[string]*Token)
44 tokenMap["ab"] = mustCreateToken(ctx, t, cs, "ab", "test")
45 tokenMap["bc"] = mustCreateToken(ctx, t, cs, "bc", "test")
46 tokenMap["cd"] = mustCreateToken(ctx, t, cs, "cd", "test")
50 t.Errorf("List errored: get list error")
53 if len(got) != len(tokenMap) {
54 t.Error("List errored: get invalid length")
56 for _, v := range got {
57 if v.Token != tokenMap[v.ID].Token {
58 t.Errorf("List error: ID: %s, expected token: %s, DB token: %s", v.ID, *tokenMap[v.ID], v.Token)
64 func TestCheck(t *testing.T) {
65 ctx := context.Background()
66 testDB := dbm.NewDB("testdb", "leveldb", "temp")
67 defer os.RemoveAll("temp")
68 cs := NewStore(testDB)
70 token := mustCreateToken(ctx, t, cs, "x", "client")
71 tokenParts := strings.Split(token.Token, ":")
73 if err := cs.Check(tokenParts[0], tokenParts[1]); err != nil {
77 if err := cs.Check("x", "badsecret"); err != ErrInvalidToken {
78 t.Fatal("invalid token check passed")
82 func TestDelete(t *testing.T) {
83 ctx := context.Background()
84 testDB := dbm.NewDB("testdb", "leveldb", "temp")
85 defer os.RemoveAll("temp")
86 cs := NewStore(testDB)
89 mustCreateToken(ctx, t, cs, id, "client")
96 value := cs.DB.Get([]byte(id))
98 t.Fatal("delete fail")
102 func TestDeleteWithInvalidId(t *testing.T) {
103 testDB := dbm.NewDB("testdb", "leveldb", "temp")
104 defer os.RemoveAll("temp")
105 cs := NewStore(testDB)
107 err := cs.Delete("@")
108 if errors.Root(err) != ErrBadID {
109 t.Errorf("Deletion with invalid id success, while it should not")
113 func mustCreateToken(ctx context.Context, t *testing.T, cs *CredentialStore, id, typ string) *Token {
114 token, err := cs.Create(id, typ)