2018-09-26 19:55:11 +00:00
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2020-05-11 21:04:11 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
2020-04-03 17:39:20 +00:00
|
|
|
platform "github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/mock"
|
2018-09-26 19:55:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-11-01 18:06:35 +00:00
|
|
|
sessionOneID = "020f755c3c082000"
|
|
|
|
sessionTwoID = "020f755c3c082001"
|
2018-09-26 19:55:11 +00:00
|
|
|
)
|
|
|
|
|
2020-04-15 15:45:29 +00:00
|
|
|
var sessionCmpOptions = sessionCompareOptions("CreatedAt", "ExpiresAt", "Permissions")
|
|
|
|
|
|
|
|
func sessionCompareOptions(ignore ...string) cmp.Options {
|
|
|
|
return cmp.Options{
|
|
|
|
cmp.Comparer(func(x, y []byte) bool {
|
|
|
|
return bytes.Equal(x, y)
|
|
|
|
}),
|
|
|
|
cmp.Transformer("Sort", func(in []*platform.Session) []*platform.Session {
|
|
|
|
out := append([]*platform.Session(nil), in...) // Copy input to avoid mutating it
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].ID.String() > out[j].ID.String()
|
|
|
|
})
|
|
|
|
return out
|
|
|
|
}),
|
|
|
|
cmpopts.IgnoreFields(platform.Session{}, ignore...),
|
|
|
|
cmpopts.EquateEmpty(),
|
|
|
|
}
|
2018-09-26 19:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SessionFields will include the IDGenerator, TokenGenerator, Sessions, and Users
|
|
|
|
type SessionFields struct {
|
|
|
|
IDGenerator platform.IDGenerator
|
|
|
|
TokenGenerator platform.TokenGenerator
|
|
|
|
Sessions []*platform.Session
|
|
|
|
Users []*platform.User
|
|
|
|
}
|
|
|
|
|
2018-12-19 15:24:57 +00:00
|
|
|
type sessionServiceFunc func(
|
|
|
|
init func(SessionFields, *testing.T) (platform.SessionService, string, func()),
|
|
|
|
t *testing.T,
|
|
|
|
)
|
|
|
|
|
2018-09-26 19:55:11 +00:00
|
|
|
// SessionService tests all the service functions.
|
|
|
|
func SessionService(
|
2018-12-19 15:24:57 +00:00
|
|
|
init func(SessionFields, *testing.T) (platform.SessionService, string, func()), t *testing.T,
|
2018-09-26 19:55:11 +00:00
|
|
|
) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2018-12-19 15:24:57 +00:00
|
|
|
fn sessionServiceFunc
|
2018-09-26 19:55:11 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "CreateSession",
|
|
|
|
fn: CreateSession,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindSession",
|
|
|
|
fn: FindSession,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ExpireSession",
|
|
|
|
fn: ExpireSession,
|
|
|
|
},
|
2019-01-11 22:07:50 +00:00
|
|
|
{
|
|
|
|
name: "RenewSession",
|
|
|
|
fn: RenewSession,
|
|
|
|
},
|
2018-09-26 19:55:11 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tt.fn(init, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSession testing
|
|
|
|
func CreateSession(
|
2018-12-19 15:24:57 +00:00
|
|
|
init func(SessionFields, *testing.T) (platform.SessionService, string, func()),
|
2018-09-26 19:55:11 +00:00
|
|
|
t *testing.T,
|
|
|
|
) {
|
|
|
|
type args struct {
|
|
|
|
user string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
err error
|
|
|
|
session *platform.Session
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields SessionFields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "create sessions with empty set",
|
|
|
|
fields: SessionFields{
|
|
|
|
IDGenerator: mock.NewIDGenerator(sessionTwoID, t),
|
|
|
|
TokenGenerator: mock.NewTokenGenerator("abc123xyz", nil),
|
|
|
|
Users: []*platform.User{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(sessionOneID),
|
2018-09-26 19:55:11 +00:00
|
|
|
Name: "user1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
user: "user1",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
session: &platform.Session{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(sessionTwoID),
|
|
|
|
UserID: MustIDBase16(sessionOneID),
|
2018-09-26 19:55:11 +00:00
|
|
|
Key: "abc123xyz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2018-12-19 15:24:57 +00:00
|
|
|
s, opPrefix, done := init(tt.fields, t)
|
2018-09-26 19:55:11 +00:00
|
|
|
defer done()
|
2018-12-28 23:02:19 +00:00
|
|
|
ctx := context.Background()
|
2018-09-26 19:55:11 +00:00
|
|
|
session, err := s.CreateSession(ctx, tt.args.user)
|
2018-12-19 15:24:57 +00:00
|
|
|
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
|
2018-09-26 19:55:11 +00:00
|
|
|
|
|
|
|
if diff := cmp.Diff(session, tt.wants.session, sessionCmpOptions...); diff != "" {
|
|
|
|
t.Errorf("sessions are different -got/+want\ndiff %s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindSession testing
|
|
|
|
func FindSession(
|
2018-12-19 15:24:57 +00:00
|
|
|
init func(SessionFields, *testing.T) (platform.SessionService, string, func()),
|
2018-09-26 19:55:11 +00:00
|
|
|
t *testing.T,
|
|
|
|
) {
|
|
|
|
type args struct {
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
err error
|
|
|
|
session *platform.Session
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields SessionFields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic find session",
|
|
|
|
fields: SessionFields{
|
|
|
|
IDGenerator: mock.NewIDGenerator(sessionTwoID, t),
|
|
|
|
TokenGenerator: mock.NewTokenGenerator("abc123xyz", nil),
|
|
|
|
Sessions: []*platform.Session{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
2018-09-26 19:55:11 +00:00
|
|
|
Key: "abc123xyz",
|
|
|
|
ExpiresAt: time.Date(2030, 9, 26, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
key: "abc123xyz",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
session: &platform.Session{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
2018-09-26 19:55:11 +00:00
|
|
|
Key: "abc123xyz",
|
|
|
|
ExpiresAt: time.Date(2030, 9, 26, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-12-19 15:24:57 +00:00
|
|
|
{
|
2019-04-17 20:30:22 +00:00
|
|
|
name: "look for not existing session",
|
2018-12-19 15:24:57 +00:00
|
|
|
args: args{
|
|
|
|
key: "abc123xyz",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: &platform.Error{
|
|
|
|
Code: platform.ENotFound,
|
|
|
|
Op: platform.OpFindSession,
|
|
|
|
Msg: platform.ErrSessionNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-09-26 19:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2018-12-19 15:24:57 +00:00
|
|
|
s, opPrefix, done := init(tt.fields, t)
|
2018-09-26 19:55:11 +00:00
|
|
|
defer done()
|
2018-12-28 23:02:19 +00:00
|
|
|
ctx := context.Background()
|
2018-09-26 19:55:11 +00:00
|
|
|
|
|
|
|
session, err := s.FindSession(ctx, tt.args.key)
|
2018-12-19 15:24:57 +00:00
|
|
|
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
|
2018-09-26 19:55:11 +00:00
|
|
|
|
|
|
|
if diff := cmp.Diff(session, tt.wants.session, sessionCmpOptions...); diff != "" {
|
|
|
|
t.Errorf("session is different -got/+want\ndiff %s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExpireSession testing
|
|
|
|
func ExpireSession(
|
2018-12-19 15:24:57 +00:00
|
|
|
init func(SessionFields, *testing.T) (platform.SessionService, string, func()),
|
2018-09-26 19:55:11 +00:00
|
|
|
t *testing.T,
|
|
|
|
) {
|
|
|
|
type args struct {
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
err error
|
|
|
|
session *platform.Session
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields SessionFields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic find session",
|
|
|
|
fields: SessionFields{
|
|
|
|
IDGenerator: mock.NewIDGenerator(sessionTwoID, t),
|
|
|
|
TokenGenerator: mock.NewTokenGenerator("abc123xyz", nil),
|
|
|
|
Sessions: []*platform.Session{
|
|
|
|
{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
2018-09-26 19:55:11 +00:00
|
|
|
Key: "abc123xyz",
|
|
|
|
ExpiresAt: time.Date(2030, 9, 26, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
key: "abc123xyz",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
session: &platform.Session{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
2018-09-26 19:55:11 +00:00
|
|
|
Key: "abc123xyz",
|
|
|
|
ExpiresAt: time.Date(2030, 9, 26, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2018-12-19 15:24:57 +00:00
|
|
|
s, opPrefix, done := init(tt.fields, t)
|
2018-09-26 19:55:11 +00:00
|
|
|
defer done()
|
2018-12-28 23:02:19 +00:00
|
|
|
ctx := context.Background()
|
2018-09-26 19:55:11 +00:00
|
|
|
|
|
|
|
err := s.ExpireSession(ctx, tt.args.key)
|
2018-12-19 15:24:57 +00:00
|
|
|
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
|
2018-09-26 19:55:11 +00:00
|
|
|
|
|
|
|
session, err := s.FindSession(ctx, tt.args.key)
|
2020-05-11 21:04:11 +00:00
|
|
|
if err.Error() != influxdb.ErrSessionExpired && err.Error() != influxdb.ErrSessionNotFound {
|
2018-09-26 19:55:11 +00:00
|
|
|
t.Errorf("expected session to be expired got %v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-11 21:04:11 +00:00
|
|
|
if session != nil {
|
|
|
|
t.Errorf("expected a nil session but got: %v", session)
|
2018-09-26 19:55:11 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-01-11 22:07:50 +00:00
|
|
|
|
|
|
|
// RenewSession testing
|
|
|
|
func RenewSession(
|
|
|
|
init func(SessionFields, *testing.T) (platform.SessionService, string, func()),
|
|
|
|
t *testing.T,
|
|
|
|
) {
|
|
|
|
type args struct {
|
|
|
|
session *platform.Session
|
|
|
|
key string
|
|
|
|
expireAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type wants struct {
|
|
|
|
err error
|
|
|
|
session *platform.Session
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields SessionFields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic renew session",
|
|
|
|
fields: SessionFields{
|
|
|
|
IDGenerator: mock.NewIDGenerator(sessionTwoID, t),
|
|
|
|
TokenGenerator: mock.NewTokenGenerator("abc123xyz", nil),
|
|
|
|
Sessions: []*platform.Session{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
|
|
|
Key: "abc123xyz",
|
|
|
|
ExpiresAt: time.Date(2030, 9, 26, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
session: &platform.Session{
|
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
|
|
|
Key: "abc123xyz",
|
|
|
|
ExpiresAt: time.Date(2030, 9, 26, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
key: "abc123xyz",
|
|
|
|
expireAt: time.Date(2031, 9, 26, 0, 0, 10, 0, time.UTC),
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
session: &platform.Session{
|
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
|
|
|
Key: "abc123xyz",
|
|
|
|
ExpiresAt: time.Date(2031, 9, 26, 0, 0, 10, 0, time.UTC),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-04-15 15:45:29 +00:00
|
|
|
{
|
|
|
|
name: "renew session with an earlier time than existing expiration",
|
|
|
|
fields: SessionFields{
|
|
|
|
IDGenerator: mock.NewIDGenerator(sessionTwoID, t),
|
|
|
|
TokenGenerator: mock.NewTokenGenerator("abc123xyz", nil),
|
|
|
|
Sessions: []*platform.Session{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
|
|
|
Key: "abc123xyz",
|
|
|
|
ExpiresAt: time.Date(2031, 9, 26, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
session: &platform.Session{
|
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
|
|
|
Key: "abc123xyz",
|
|
|
|
ExpiresAt: time.Date(2031, 9, 26, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
key: "abc123xyz",
|
|
|
|
expireAt: time.Date(2030, 9, 26, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
session: &platform.Session{
|
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
|
|
|
Key: "abc123xyz",
|
|
|
|
ExpiresAt: time.Date(2031, 9, 26, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-01-11 22:07:50 +00:00
|
|
|
{
|
|
|
|
name: "renew nil session",
|
|
|
|
fields: SessionFields{
|
|
|
|
IDGenerator: mock.NewIDGenerator(sessionTwoID, t),
|
|
|
|
TokenGenerator: mock.NewTokenGenerator("abc123xyz", nil),
|
|
|
|
Sessions: []*platform.Session{
|
|
|
|
{
|
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
|
|
|
Key: "abc123xyz",
|
|
|
|
ExpiresAt: time.Date(2030, 9, 26, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
key: "abc123xyz",
|
|
|
|
expireAt: time.Date(2031, 9, 26, 0, 0, 10, 0, time.UTC),
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: &platform.Error{
|
|
|
|
Code: platform.EInternal,
|
|
|
|
Msg: "session is nil",
|
|
|
|
Op: platform.OpRenewSession,
|
|
|
|
},
|
|
|
|
session: &platform.Session{
|
|
|
|
ID: MustIDBase16(sessionOneID),
|
|
|
|
UserID: MustIDBase16(sessionTwoID),
|
|
|
|
Key: "abc123xyz",
|
2020-04-15 15:45:29 +00:00
|
|
|
ExpiresAt: time.Date(2030, 9, 26, 0, 0, 0, 0, time.UTC),
|
2019-01-11 22:07:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s, opPrefix, done := init(tt.fields, t)
|
|
|
|
defer done()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err := s.RenewSession(ctx, tt.args.session, tt.args.expireAt)
|
|
|
|
diffPlatformErrors(tt.name, err, tt.wants.err, opPrefix, t)
|
|
|
|
|
|
|
|
session, err := s.FindSession(ctx, tt.args.key)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("err in find session %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:45:29 +00:00
|
|
|
cmpOptions := sessionCompareOptions("CreatedAt", "Permissions")
|
|
|
|
if diff := cmp.Diff(session, tt.wants.session, cmpOptions...); diff != "" {
|
2019-01-11 22:07:50 +00:00
|
|
|
t.Errorf("session is different -got/+want\ndiff %s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|