Skip to content

Commit

Permalink
fix(http): add default status for user response
Browse files Browse the repository at this point in the history
  • Loading branch information
Deary Hudson authored and Deary Hudson committed Sep 27, 2019
1 parent 9f5390e commit a002c42
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 74 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v2.0.0-alpha.19 [unreleased]
### Bug Fixes
1. [15295](https://github.com/influxdata/influxdb/pull/15295): Add default status for user response

## v2.0.0-alpha.18 [2019-09-26]

### Features
Expand Down
2 changes: 2 additions & 0 deletions bolt/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

bolt "github.com/coreos/bbolt"
influxdb "github.com/influxdata/influxdb"
platform "github.com/influxdata/influxdb"
platformcontext "github.com/influxdata/influxdb/context"
)
Expand Down Expand Up @@ -226,6 +227,7 @@ func (c *Client) CreateUser(ctx context.Context, u *platform.User) error {
}

u.ID = c.IDGenerator.ID()
u.Status = influxdb.Active

if err := c.appendUserEventToLog(ctx, tx, u.ID, userCreatedEvent); err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions http/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ type userResponse struct {
}

func newUserResponse(u *influxdb.User) *userResponse {
if u.Status == "" {
u.Status = influxdb.Active
}

return &userResponse{
Links: map[string]string{
"self": fmt.Sprintf("/api/v2/users/%s", u.ID),
Expand Down
105 changes: 105 additions & 0 deletions http/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package http

import (
"context"
"fmt"
"io/ioutil"
http "net/http"
"net/http/httptest"
"testing"

platform "github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/inmem"
"github.com/influxdata/influxdb/mock"
platformtesting "github.com/influxdata/influxdb/testing"
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -53,3 +57,104 @@ func TestUserService(t *testing.T) {
t.Parallel()
platformtesting.UserService(initUserService, t)
}

func TestService_handleGetUser(t *testing.T) {
type fields struct {
UserService platform.UserService
}
type args struct {
id string
}
type wants struct {
statusCode int
contentType string
body string
}

tests := []struct {
name string
fields fields
args args
wants wants
}{
{
name: "get a user with no status defaults to `active`",
fields: fields{
&mock.UserService{
FindUserByIDFn: func(ctx context.Context, id platform.ID) (*platform.User, error) {
if id == platformtesting.MustIDBase16("020f755c3c082000") {
return &platform.User{
ID: platformtesting.MustIDBase16("020f755c3c082000"),
Name: "myname",
}, nil
}

return nil, fmt.Errorf("not found")
},
},
},
args: args{
id: "020f755c3c082000",
},
wants: wants{
statusCode: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: `
{
"links": {
"self": "/api/v2/users/020f755c3c082000",
"logs": "/api/v2/users/020f755c3c082000/logs"
},
"id": "020f755c3c082000",
"name": "myname",
"status": "active"
}
`,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
userBackend := NewMockUserBackend()
userBackend.HTTPErrorHandler = ErrorHandler(0)
userBackend.UserService = tt.fields.UserService
h := NewUserHandler(userBackend)

r := httptest.NewRequest("GET", "http://any.url", nil)

r = r.WithContext(context.WithValue(
context.Background(),
httprouter.ParamsKey,
httprouter.Params{
{
Key: "id",
Value: tt.args.id,
},
}))

w := httptest.NewRecorder()

h.handleGetUser(w, r)

res := w.Result()
content := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body)
t.Logf(res.Header.Get("X-Influx-Error"))

if res.StatusCode != tt.wants.statusCode {
t.Errorf("%q. handleGetUser() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
}
if tt.wants.contentType != "" && content != tt.wants.contentType {
t.Errorf("%q. handleGetUser() = %v, want %v", tt.name, content, tt.wants.contentType)
}
if tt.wants.body != "" {
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
t.Errorf("%q, handleGetUser(). error unmarshaling json %v", tt.name, err)
} else if !eq {
t.Errorf("%q. handleGetUser() = ***%s***", tt.name, diff)
}
}
})
}
}
2 changes: 2 additions & 0 deletions inmem/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/influxdata/influxdb"
platform "github.com/influxdata/influxdb"
)

Expand Down Expand Up @@ -157,6 +158,7 @@ func (s *Service) CreateUser(ctx context.Context, u *platform.User) error {
}
}
u.ID = s.IDGenerator.ID()
u.Status = influxdb.Active
s.PutUser(ctx, u)
return nil
}
Expand Down
1 change: 1 addition & 0 deletions kv/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func (s *Service) createUser(ctx context.Context, tx Tx, u *influxdb.User) error
}

u.ID = s.IDGenerator.ID()
u.Status = influxdb.Active
if err := s.appendUserEventToLog(ctx, tx, u.ID, userCreatedEvent); err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions testing/onboarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ func Generate(
password: "password1",
results: &platform.OnboardingResults{
User: &platform.User{
ID: MustIDBase16(oneID),
Name: "admin",
ID: MustIDBase16(oneID),
Name: "admin",
Status: platform.Active,
},
Org: &platform.Organization{
ID: MustIDBase16(twoID),
Expand Down
Loading

0 comments on commit a002c42

Please sign in to comment.