2018-09-12 19:24:17 +00:00
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2018-10-18 19:09:25 +00:00
|
|
|
"time"
|
2018-09-12 19:24:17 +00:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-01-08 00:37:16 +00:00
|
|
|
platform "github.com/influxdata/influxdb"
|
|
|
|
"github.com/influxdata/influxdb/mock"
|
2018-09-12 19:24:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// OnboardingFields will include the IDGenerator, TokenGenerator
|
|
|
|
// and IsOnboarding
|
|
|
|
type OnboardingFields struct {
|
|
|
|
IDGenerator platform.IDGenerator
|
|
|
|
TokenGenerator platform.TokenGenerator
|
2019-05-17 15:12:09 +00:00
|
|
|
TimeGenerator platform.TimeGenerator
|
2018-09-12 19:24:17 +00:00
|
|
|
IsOnboarding bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate testing
|
|
|
|
func Generate(
|
2018-10-03 14:51:14 +00:00
|
|
|
init func(OnboardingFields, *testing.T) (platform.OnboardingService, func()),
|
2018-09-12 19:24:17 +00:00
|
|
|
t *testing.T,
|
|
|
|
) {
|
|
|
|
type args struct {
|
|
|
|
request *platform.OnboardingRequest
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
errCode string
|
|
|
|
results *platform.OnboardingResults
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields OnboardingFields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "denied",
|
|
|
|
fields: OnboardingFields{
|
|
|
|
IDGenerator: &loopIDGenerator{
|
|
|
|
s: []string{oneID, twoID, threeID, fourID},
|
|
|
|
},
|
|
|
|
TokenGenerator: mock.NewTokenGenerator(oneToken, nil),
|
|
|
|
IsOnboarding: false,
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
errCode: platform.EConflict,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing password",
|
|
|
|
fields: OnboardingFields{
|
|
|
|
IDGenerator: &loopIDGenerator{
|
|
|
|
s: []string{oneID, twoID, threeID, fourID},
|
|
|
|
},
|
|
|
|
TokenGenerator: mock.NewTokenGenerator(oneToken, nil),
|
|
|
|
IsOnboarding: true,
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
request: &platform.OnboardingRequest{
|
|
|
|
User: "admin",
|
|
|
|
Org: "org1",
|
|
|
|
Bucket: "bucket1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
errCode: platform.EEmptyValue,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing username",
|
|
|
|
fields: OnboardingFields{
|
|
|
|
IDGenerator: &loopIDGenerator{
|
|
|
|
s: []string{oneID, twoID, threeID, fourID},
|
|
|
|
},
|
|
|
|
TokenGenerator: mock.NewTokenGenerator(oneToken, nil),
|
|
|
|
IsOnboarding: true,
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
request: &platform.OnboardingRequest{
|
|
|
|
Org: "org1",
|
|
|
|
Bucket: "bucket1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
errCode: platform.EEmptyValue,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing org",
|
|
|
|
fields: OnboardingFields{
|
|
|
|
IDGenerator: &loopIDGenerator{
|
|
|
|
s: []string{oneID, twoID, threeID, fourID},
|
|
|
|
},
|
|
|
|
TokenGenerator: mock.NewTokenGenerator(oneToken, nil),
|
|
|
|
IsOnboarding: true,
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
request: &platform.OnboardingRequest{
|
|
|
|
User: "admin",
|
|
|
|
Bucket: "bucket1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
errCode: platform.EEmptyValue,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing bucket",
|
|
|
|
fields: OnboardingFields{
|
|
|
|
IDGenerator: &loopIDGenerator{
|
|
|
|
s: []string{oneID, twoID, threeID, fourID},
|
|
|
|
},
|
|
|
|
TokenGenerator: mock.NewTokenGenerator(oneToken, nil),
|
|
|
|
IsOnboarding: true,
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
request: &platform.OnboardingRequest{
|
|
|
|
User: "admin",
|
|
|
|
Org: "org1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
errCode: platform.EEmptyValue,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-12-28 23:02:19 +00:00
|
|
|
name: "valid onboarding json should create a user, org, bucket, and authorization",
|
2018-09-12 19:24:17 +00:00
|
|
|
fields: OnboardingFields{
|
|
|
|
IDGenerator: &loopIDGenerator{
|
|
|
|
s: []string{oneID, twoID, threeID, fourID},
|
|
|
|
},
|
2019-05-17 15:12:09 +00:00
|
|
|
TimeGenerator: mock.TimeGenerator{FakeValue: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)},
|
2018-09-12 19:24:17 +00:00
|
|
|
TokenGenerator: mock.NewTokenGenerator(oneToken, nil),
|
|
|
|
IsOnboarding: true,
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
request: &platform.OnboardingRequest{
|
2018-10-18 19:09:25 +00:00
|
|
|
User: "admin",
|
|
|
|
Org: "org1",
|
|
|
|
Bucket: "bucket1",
|
2019-02-19 23:47:19 +00:00
|
|
|
Password: "password1",
|
2018-10-18 19:09:25 +00:00
|
|
|
RetentionPeriod: 24 * 7, // 1 week
|
2018-09-12 19:24:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
2019-02-19 23:47:19 +00:00
|
|
|
password: "password1",
|
2018-09-12 19:24:17 +00:00
|
|
|
results: &platform.OnboardingResults{
|
|
|
|
User: &platform.User{
|
2019-09-27 18:43:19 +00:00
|
|
|
ID: MustIDBase16(oneID),
|
|
|
|
Name: "admin",
|
|
|
|
Status: platform.Active,
|
2018-09-12 19:24:17 +00:00
|
|
|
},
|
|
|
|
Org: &platform.Organization{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: MustIDBase16(twoID),
|
2018-09-12 19:24:17 +00:00
|
|
|
Name: "org1",
|
2019-05-17 20:25:19 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC),
|
|
|
|
UpdatedAt: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC),
|
|
|
|
},
|
2018-09-12 19:24:17 +00:00
|
|
|
},
|
|
|
|
Bucket: &platform.Bucket{
|
2018-10-18 19:09:25 +00:00
|
|
|
ID: MustIDBase16(threeID),
|
|
|
|
Name: "bucket1",
|
2019-04-10 21:16:35 +00:00
|
|
|
OrgID: MustIDBase16(twoID),
|
2018-10-18 19:09:25 +00:00
|
|
|
RetentionPeriod: time.Hour * 24 * 7,
|
2019-05-17 15:12:09 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC),
|
|
|
|
UpdatedAt: time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC),
|
|
|
|
},
|
2018-09-12 19:24:17 +00:00
|
|
|
},
|
|
|
|
Auth: &platform.Authorization{
|
2018-12-07 22:22:23 +00:00
|
|
|
ID: MustIDBase16(fourID),
|
|
|
|
Token: oneToken,
|
|
|
|
Status: platform.Active,
|
|
|
|
UserID: MustIDBase16(oneID),
|
2018-12-20 21:46:21 +00:00
|
|
|
Description: "admin's Token",
|
2018-12-28 23:02:19 +00:00
|
|
|
OrgID: MustIDBase16(twoID),
|
2019-01-18 23:27:28 +00:00
|
|
|
Permissions: platform.OperPermissions(),
|
2018-09-12 19:24:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s, done := init(tt.fields, t)
|
|
|
|
defer done()
|
|
|
|
ctx := context.Background()
|
|
|
|
results, err := s.Generate(ctx, tt.args.request)
|
|
|
|
if (err != nil) != (tt.wants.errCode != "") {
|
2019-02-19 23:47:19 +00:00
|
|
|
t.Logf("Error: %v", err)
|
2018-09-12 19:24:17 +00:00
|
|
|
t.Fatalf("expected error code '%s' got '%v'", tt.wants.errCode, err)
|
|
|
|
}
|
|
|
|
if err != nil && tt.wants.errCode != "" {
|
|
|
|
if code := platform.ErrorCode(err); code != tt.wants.errCode {
|
2019-02-19 23:47:19 +00:00
|
|
|
t.Logf("Error: %v", err)
|
2018-09-12 19:24:17 +00:00
|
|
|
t.Fatalf("expected error code to match '%s' got '%v'", tt.wants.errCode, code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(results, tt.wants.results); diff != "" {
|
|
|
|
t.Errorf("onboarding results are different -got/+want\ndiff %s", diff)
|
|
|
|
}
|
|
|
|
if results != nil {
|
|
|
|
if err = s.ComparePassword(ctx, results.User.Name, tt.wants.password); err != nil {
|
|
|
|
t.Errorf("onboarding set password is wrong")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
oneID = "020f755c3c082000"
|
|
|
|
twoID = "020f755c3c082001"
|
|
|
|
threeID = "020f755c3c082002"
|
|
|
|
fourID = "020f755c3c082003"
|
2019-07-24 02:46:42 +00:00
|
|
|
fiveID = "020f755c3c082004"
|
|
|
|
sixID = "020f755c3c082005"
|
2018-09-12 19:24:17 +00:00
|
|
|
oneToken = "020f755c3c082008"
|
|
|
|
)
|
|
|
|
|
|
|
|
type loopIDGenerator struct {
|
|
|
|
s []string
|
|
|
|
p int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *loopIDGenerator) ID() platform.ID {
|
|
|
|
if g.p == len(g.s) {
|
|
|
|
g.p = 0
|
|
|
|
}
|
2018-10-10 19:39:09 +00:00
|
|
|
id := MustIDBase16(g.s[g.p])
|
2018-09-12 19:24:17 +00:00
|
|
|
g.p++
|
|
|
|
return id
|
|
|
|
}
|