test(bolt): ensure boltdb session service conforms to tests
parent
d7e7b9ca9b
commit
13e60ec292
|
@ -128,6 +128,11 @@ func (c *Client) initialize(ctx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Always create Session bucket.
|
||||
if err := c.initializeSessions(ctx, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
|
|
|
@ -59,12 +59,19 @@ func (c *Client) findSession(ctx context.Context, tx *bolt.Tx, key string) (*pla
|
|||
return s, nil
|
||||
}
|
||||
|
||||
func (c *Client) putSession(ctx context.Context, tx *bolt.Tx, key string, s *platform.Session) error {
|
||||
// PutSession puts the session at key.
|
||||
func (c *Client) PutSession(ctx context.Context, s *platform.Session) error {
|
||||
return c.db.Update(func(tx *bolt.Tx) error {
|
||||
return c.putSession(ctx, tx, s)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Client) putSession(ctx context.Context, tx *bolt.Tx, s *platform.Session) error {
|
||||
v, err := json.Marshal(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Bucket(sessionBucket).Put([]byte(key), v); err != nil {
|
||||
if err := tx.Bucket(sessionBucket).Put([]byte(s.Key), v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -80,7 +87,7 @@ func (c *Client) ExpireSession(ctx context.Context, key string) error {
|
|||
|
||||
s.ExpiresAt = time.Now()
|
||||
|
||||
return c.putSession(ctx, tx, key, s)
|
||||
return c.putSession(ctx, tx, s)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -125,7 +132,7 @@ func (c *Client) createSession(ctx context.Context, tx *bolt.Tx, user string) (*
|
|||
// TODO(desa): not totally sure what to do here. Possibly we should have a maximal privilege permission.
|
||||
s.Permissions = []platform.Permission{}
|
||||
|
||||
if err := c.putSession(ctx, tx, s.Key, s); err != nil {
|
||||
if err := c.putSession(ctx, tx, s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package bolt_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/influxdata/platform"
|
||||
platformtesting "github.com/influxdata/platform/testing"
|
||||
)
|
||||
|
||||
func initSessionService(f platformtesting.SessionFields, t *testing.T) (platform.SessionService, func()) {
|
||||
c, closeFn, err := NewTestClient()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create new bolt client: %v", err)
|
||||
}
|
||||
c.IDGenerator = f.IDGenerator
|
||||
c.TokenGenerator = f.TokenGenerator
|
||||
ctx := context.Background()
|
||||
for _, u := range f.Users {
|
||||
if err := c.PutUser(ctx, u); err != nil {
|
||||
t.Fatalf("failed to populate users")
|
||||
}
|
||||
}
|
||||
for _, s := range f.Sessions {
|
||||
if err := c.PutSession(ctx, s); err != nil {
|
||||
t.Fatalf("failed to populate sessions")
|
||||
}
|
||||
}
|
||||
return c, func() {
|
||||
defer closeFn()
|
||||
for _, u := range f.Users {
|
||||
if err := c.DeleteUser(ctx, u.ID); err != nil {
|
||||
t.Logf("failed to remove users: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionService(t *testing.T) {
|
||||
platformtesting.SessionService(initSessionService, t)
|
||||
}
|
|
@ -17,6 +17,7 @@ type Session struct {
|
|||
Permissions []Permission `json:"permissions,omitempty"`
|
||||
}
|
||||
|
||||
// Expired returns an error if the session is expired.
|
||||
func (s *Session) Expired() error {
|
||||
if time.Now().After(s.ExpiresAt) {
|
||||
return fmt.Errorf("session has expired")
|
||||
|
|
Loading…
Reference in New Issue