Skip to content

Commit

Permalink
fix: improve error message when onboarding requests are missing data (i…
Browse files Browse the repository at this point in the history
  • Loading branch information
danxmoran authored Jul 14, 2021
1 parent b1aa943 commit ba8f91f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This release adds an embedded SQLite database for storing metadata required by t
1. [21800](https://github.com/influxdata/influxdb/pull/21800): Return an error instead of panicking when InfluxQL statement rewrites fail.
1. [21840](https://github.com/influxdata/influxdb/pull/21840): Run migrations on restored bolt & SQLite metadata databases as part of the restore process.
1. [21844](https://github.com/influxdata/influxdb/pull/21844): Upgrade to latest version of `influxdata/cron` so that tasks can be created with interval of `every: 1w`.
1. [21849](https://github.com/influxdata/influxdb/pull/21849): Specify which fields are missing when rejecting an incomplete onboarding request.

## v2.0.7 [2021-06-04]

Expand Down
5 changes: 0 additions & 5 deletions tenant/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ var (
Msg: "onboarding has already been completed",
}

ErrOnboardInvalid = &errors.Error{
Code: errors.EEmptyValue,
Msg: "onboard failed, missing value",
}

ErrNotFound = &errors.Error{
Code: errors.ENotFound,
Msg: "not found",
Expand Down
26 changes: 24 additions & 2 deletions tenant/service_onboarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package tenant
import (
"context"
"fmt"
"strings"

"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kit/platform/errors"

"github.com/influxdata/influxdb/v2"
icontext "github.com/influxdata/influxdb/v2/context"
Expand Down Expand Up @@ -85,8 +87,28 @@ func (s *OnboardService) OnboardInitialUser(ctx context.Context, req *influxdb.O

// onboardUser allows us to onboard new users.
func (s *OnboardService) onboardUser(ctx context.Context, req *influxdb.OnboardingRequest, permFn func(orgID, userID platform.ID) []influxdb.Permission) (*influxdb.OnboardingResults, error) {
if req == nil || req.User == "" || req.Org == "" || req.Bucket == "" {
return nil, ErrOnboardInvalid
if req == nil {
return nil, &errors.Error{
Code: errors.EEmptyValue,
Msg: "onboarding failed: no request body provided",
}
}

var missingFields []string
if req.User == "" {
missingFields = append(missingFields, "username")
}
if req.Org == "" {
missingFields = append(missingFields, "org")
}
if req.Bucket == "" {
missingFields = append(missingFields, "bucket")
}
if len(missingFields) > 0 {
return nil, &errors.Error{
Code: errors.EUnprocessableEntity,
Msg: fmt.Sprintf("onboarding failed: missing required fields [%s]", strings.Join(missingFields, ",")),
}
}

result := &influxdb.OnboardingResults{}
Expand Down
25 changes: 3 additions & 22 deletions testing/onboarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func OnboardInitialUser(
},
},
wants: wants{
errCode: errors.EEmptyValue,
errCode: errors.EUnprocessableEntity,
},
},
{
Expand All @@ -104,7 +104,7 @@ func OnboardInitialUser(
},
},
wants: wants{
errCode: errors.EEmptyValue,
errCode: errors.EUnprocessableEntity,
},
},
{
Expand All @@ -123,26 +123,7 @@ func OnboardInitialUser(
},
},
wants: wants{
errCode: errors.EEmptyValue,
},
},
{
name: "missing password should fail",
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: errors.EEmptyValue,
errCode: errors.EUnprocessableEntity,
},
},
{
Expand Down

0 comments on commit ba8f91f

Please sign in to comment.