From 13e60ec292aacda1a4230ba98a3c3dc79cb8d326 Mon Sep 17 00:00:00 2001 From: Michael Desa Date: Wed, 26 Sep 2018 15:56:06 -0400 Subject: [PATCH] test(bolt): ensure boltdb session service conforms to tests --- bolt/bbolt.go | 5 +++++ bolt/session.go | 15 +++++++++++---- bolt/session_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ session.go | 1 + 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 bolt/session_test.go diff --git a/bolt/bbolt.go b/bolt/bbolt.go index 84c8afaed3..ca2d72680a 100644 --- a/bolt/bbolt.go +++ b/bolt/bbolt.go @@ -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 diff --git a/bolt/session.go b/bolt/session.go index 137ec52492..53dd58379c 100644 --- a/bolt/session.go +++ b/bolt/session.go @@ -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 } diff --git a/bolt/session_test.go b/bolt/session_test.go new file mode 100644 index 0000000000..fbc3ee13f4 --- /dev/null +++ b/bolt/session_test.go @@ -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) +} diff --git a/session.go b/session.go index bae0cc2c76..5f20cfe72e 100644 --- a/session.go +++ b/session.go @@ -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")