Skip to content

Commit

Permalink
updates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Deary Hudson committed Sep 27, 2019
1 parent a002c42 commit f5e5d7d
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 109 deletions.
4 changes: 0 additions & 4 deletions http/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,6 @@ 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: 0 additions & 105 deletions http/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ 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 @@ -57,104 +53,3 @@ 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)
}
}
})
}
}

0 comments on commit f5e5d7d

Please sign in to comment.