2019-01-18 19:03:36 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-04-13 20:24:27 +00:00
|
|
|
"io"
|
2019-12-04 23:10:23 +00:00
|
|
|
"net/http"
|
2019-01-18 19:03:36 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2019-12-04 23:10:23 +00:00
|
|
|
"github.com/influxdata/httprouter"
|
2020-04-03 17:39:20 +00:00
|
|
|
platform "github.com/influxdata/influxdb/v2"
|
2021-09-13 19:12:35 +00:00
|
|
|
platform2 "github.com/influxdata/influxdb/v2/kit/platform"
|
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform/errors"
|
2020-04-03 17:39:20 +00:00
|
|
|
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/label"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/mock"
|
|
|
|
platformtesting "github.com/influxdata/influxdb/v2/testing"
|
2019-12-04 23:10:23 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-01-18 19:03:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestService_handleGetLabels(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
LabelService platform.LabelService
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "get all labels",
|
|
|
|
fields: fields{
|
|
|
|
&mock.LabelService{
|
|
|
|
FindLabelsFn: func(ctx context.Context, filter platform.LabelFilter) ([]*platform.Label, error) {
|
|
|
|
return []*platform.Label{
|
|
|
|
{
|
|
|
|
ID: platformtesting.MustIDBase16("0b501e7e557ab1ed"),
|
|
|
|
Name: "hello",
|
|
|
|
Properties: map[string]string{
|
|
|
|
"color": "fff000",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: platformtesting.MustIDBase16("c0175f0077a77005"),
|
|
|
|
Name: "example",
|
|
|
|
Properties: map[string]string{
|
|
|
|
"color": "fff000",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/labels"
|
|
|
|
},
|
|
|
|
"labels": [
|
|
|
|
{
|
|
|
|
"id": "0b501e7e557ab1ed",
|
|
|
|
"name": "hello",
|
|
|
|
"properties": {
|
|
|
|
"color": "fff000"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "c0175f0077a77005",
|
|
|
|
"name": "example",
|
|
|
|
"properties": {
|
|
|
|
"color": "fff000"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "get all labels when there are none",
|
|
|
|
fields: fields{
|
|
|
|
&mock.LabelService{
|
|
|
|
FindLabelsFn: func(ctx context.Context, filter platform.LabelFilter) ([]*platform.Label, error) {
|
|
|
|
return []*platform.Label{}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/labels"
|
|
|
|
},
|
|
|
|
"labels": []
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-09-13 19:12:35 +00:00
|
|
|
h := NewLabelHandler(zaptest.NewLogger(t), tt.fields.LabelService, kithttp.NewErrorHandler(zaptest.NewLogger(t)))
|
2019-01-18 19:03:36 +00:00
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "http://any.url", nil)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.handleGetLabels(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
2022-04-13 20:24:27 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2019-01-18 19:03:36 +00:00
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("%q. handleGetLabels() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("%q. handleGetLabels() = %v, want %v", tt.name, content, tt.wants.contentType)
|
|
|
|
}
|
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil || tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. handleGetLabels() = ***%v***", tt.name, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handleGetLabel(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
LabelService platform.LabelService
|
|
|
|
}
|
|
|
|
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 label by id",
|
|
|
|
fields: fields{
|
|
|
|
&mock.LabelService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindLabelByIDFn: func(ctx context.Context, id platform2.ID) (*platform.Label, error) {
|
2019-01-18 19:03:36 +00:00
|
|
|
if id == platformtesting.MustIDBase16("020f755c3c082000") {
|
|
|
|
return &platform.Label{
|
|
|
|
ID: platformtesting.MustIDBase16("020f755c3c082000"),
|
|
|
|
Name: "mylabel",
|
|
|
|
Properties: map[string]string{
|
|
|
|
"color": "fff000",
|
|
|
|
},
|
|
|
|
}, 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/labels/020f755c3c082000"
|
|
|
|
},
|
|
|
|
"label": {
|
|
|
|
"id": "020f755c3c082000",
|
|
|
|
"name": "mylabel",
|
|
|
|
"properties": {
|
|
|
|
"color": "fff000"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not found",
|
|
|
|
fields: fields{
|
|
|
|
&mock.LabelService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindLabelByIDFn: func(ctx context.Context, id platform2.ID) (*platform.Label, error) {
|
|
|
|
return nil, &errors.Error{
|
|
|
|
Code: errors.ENotFound,
|
2019-04-11 15:17:05 +00:00
|
|
|
Msg: platform.ErrLabelNotFound,
|
2019-01-18 19:03:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-09-13 19:12:35 +00:00
|
|
|
h := NewLabelHandler(zaptest.NewLogger(t), tt.fields.LabelService, kithttp.NewErrorHandler(zaptest.NewLogger(t)))
|
2019-01-18 19:03:36 +00:00
|
|
|
|
|
|
|
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.handleGetLabel(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
2022-04-13 20:24:27 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2019-01-18 19:03:36 +00:00
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("%q. handleGetLabel() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("%q. handleGetLabel() = %v, want %v", tt.name, content, tt.wants.contentType)
|
|
|
|
}
|
2019-05-08 19:51:03 +00:00
|
|
|
if tt.wants.body != "" {
|
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, handleGetLabel(). error unmarshalling json %v", tt.name, err)
|
2019-05-08 19:51:03 +00:00
|
|
|
} else if !eq {
|
|
|
|
t.Errorf("%q. handleGetLabel() = ***%s***", tt.name, diff)
|
|
|
|
}
|
2019-01-18 19:03:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handlePostLabel(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
LabelService platform.LabelService
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
label *platform.Label
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "create a new label",
|
|
|
|
fields: fields{
|
|
|
|
&mock.LabelService{
|
|
|
|
CreateLabelFn: func(ctx context.Context, l *platform.Label) error {
|
|
|
|
l.ID = platformtesting.MustIDBase16("020f755c3c082000")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
label: &platform.Label{
|
2019-04-11 23:58:40 +00:00
|
|
|
Name: "mylabel",
|
|
|
|
OrgID: platformtesting.MustIDBase16("020f755c3c082008"),
|
2019-01-18 19:03:36 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusCreated,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/labels/020f755c3c082000"
|
|
|
|
},
|
|
|
|
"label": {
|
|
|
|
"id": "020f755c3c082000",
|
2019-03-19 14:00:43 +00:00
|
|
|
"name": "mylabel",
|
|
|
|
"orgID": "020f755c3c082008"
|
2019-01-18 19:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-09-13 19:12:35 +00:00
|
|
|
h := NewLabelHandler(zaptest.NewLogger(t), tt.fields.LabelService, kithttp.NewErrorHandler(zaptest.NewLogger(t)))
|
2019-01-18 19:03:36 +00:00
|
|
|
|
|
|
|
l, err := json.Marshal(tt.args.label)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to marshal label: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "http://any.url", bytes.NewReader(l))
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.handlePostLabel(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
2022-04-13 20:24:27 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2019-01-18 19:03:36 +00:00
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("%q. handlePostLabel() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("%q. handlePostLabel() = %v, want %v", tt.name, content, tt.wants.contentType)
|
|
|
|
}
|
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil || tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. handlePostLabel() = ***%v***", tt.name, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handleDeleteLabel(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
LabelService platform.LabelService
|
|
|
|
}
|
|
|
|
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: "remove a label by id",
|
|
|
|
fields: fields{
|
|
|
|
&mock.LabelService{
|
2021-03-30 18:10:02 +00:00
|
|
|
DeleteLabelFn: func(ctx context.Context, id platform2.ID) error {
|
2019-01-18 19:03:36 +00:00
|
|
|
if id == platformtesting.MustIDBase16("020f755c3c082000") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("wrong id")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNoContent,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "label not found",
|
|
|
|
fields: fields{
|
|
|
|
&mock.LabelService{
|
2021-03-30 18:10:02 +00:00
|
|
|
DeleteLabelFn: func(ctx context.Context, id platform2.ID) error {
|
|
|
|
return &errors.Error{
|
|
|
|
Code: errors.ENotFound,
|
2019-04-11 15:17:05 +00:00
|
|
|
Msg: platform.ErrLabelNotFound,
|
2019-01-18 19:03:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-09-13 19:12:35 +00:00
|
|
|
h := NewLabelHandler(zaptest.NewLogger(t), tt.fields.LabelService, kithttp.NewErrorHandler(zaptest.NewLogger(t)))
|
2019-01-18 19:03:36 +00:00
|
|
|
|
|
|
|
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.handleDeleteLabel(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
2022-04-13 20:24:27 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2019-01-18 19:03:36 +00:00
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("%q. handlePostLabel() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("%q. handlePostLabel() = %v, want %v", tt.name, content, tt.wants.contentType)
|
|
|
|
}
|
2019-05-08 19:51:03 +00:00
|
|
|
if tt.wants.body != "" {
|
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, handlePostLabel(). error unmarshalling json %v", tt.name, err)
|
2019-05-08 19:51:03 +00:00
|
|
|
} else if !eq {
|
|
|
|
t.Errorf("%q. handlePostLabel() = ***%s***", tt.name, diff)
|
|
|
|
}
|
2019-01-18 19:03:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handlePatchLabel(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
LabelService platform.LabelService
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
id string
|
|
|
|
properties map[string]string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "update label properties",
|
|
|
|
fields: fields{
|
|
|
|
&mock.LabelService{
|
2021-03-30 18:10:02 +00:00
|
|
|
UpdateLabelFn: func(ctx context.Context, id platform2.ID, upd platform.LabelUpdate) (*platform.Label, error) {
|
2019-01-18 19:03:36 +00:00
|
|
|
if id == platformtesting.MustIDBase16("020f755c3c082000") {
|
|
|
|
l := &platform.Label{
|
|
|
|
ID: platformtesting.MustIDBase16("020f755c3c082000"),
|
|
|
|
Name: "mylabel",
|
|
|
|
Properties: map[string]string{
|
|
|
|
"color": "fff000",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range upd.Properties {
|
|
|
|
if v == "" {
|
|
|
|
delete(l.Properties, k)
|
|
|
|
} else {
|
|
|
|
l.Properties[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("not found")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
properties: map[string]string{
|
|
|
|
"color": "aaabbb",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/labels/020f755c3c082000"
|
|
|
|
},
|
|
|
|
"label": {
|
|
|
|
"id": "020f755c3c082000",
|
|
|
|
"name": "mylabel",
|
|
|
|
"properties": {
|
|
|
|
"color": "aaabbb"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "label not found",
|
|
|
|
fields: fields{
|
|
|
|
&mock.LabelService{
|
2021-03-30 18:10:02 +00:00
|
|
|
UpdateLabelFn: func(ctx context.Context, id platform2.ID, upd platform.LabelUpdate) (*platform.Label, error) {
|
|
|
|
return nil, &errors.Error{
|
|
|
|
Code: errors.ENotFound,
|
2019-04-11 15:17:05 +00:00
|
|
|
Msg: platform.ErrLabelNotFound,
|
2019-01-18 19:03:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
properties: map[string]string{
|
|
|
|
"color": "aaabbb",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-09-13 19:12:35 +00:00
|
|
|
h := NewLabelHandler(zaptest.NewLogger(t), tt.fields.LabelService, kithttp.NewErrorHandler(zaptest.NewLogger(t)))
|
2019-01-18 19:03:36 +00:00
|
|
|
|
|
|
|
upd := platform.LabelUpdate{}
|
|
|
|
if len(tt.args.properties) > 0 {
|
|
|
|
upd.Properties = tt.args.properties
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := json.Marshal(upd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to marshal label update: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "http://any.url", bytes.NewReader(l))
|
|
|
|
|
|
|
|
r = r.WithContext(context.WithValue(
|
|
|
|
context.Background(),
|
|
|
|
httprouter.ParamsKey,
|
|
|
|
httprouter.Params{
|
|
|
|
{
|
|
|
|
Key: "id",
|
|
|
|
Value: tt.args.id,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.handlePatchLabel(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
2022-04-13 20:24:27 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2019-01-18 19:03:36 +00:00
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("%q. handlePatchLabel() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("%q. handlePatchLabel() = %v, want %v", tt.name, content, tt.wants.contentType)
|
|
|
|
}
|
2019-05-08 19:51:03 +00:00
|
|
|
if tt.wants.body != "" {
|
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, handlePatchLabel(). error unmarshalling json %v", tt.name, err)
|
2019-05-08 19:51:03 +00:00
|
|
|
} else if !eq {
|
|
|
|
t.Errorf("%q. handlePatchLabel() = ***%s***", tt.name, diff)
|
|
|
|
}
|
2019-01-18 19:03:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-12-07 18:54:03 +00:00
|
|
|
|
|
|
|
func initLabelService(f platformtesting.LabelFields, t *testing.T) (platform.LabelService, string, func()) {
|
2021-08-31 20:43:45 +00:00
|
|
|
store := platformtesting.NewTestInmemStore(t)
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
labelStore, err := label.NewStore(store)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.IDGenerator != nil {
|
|
|
|
labelStore.IDGenerator = f.IDGenerator
|
|
|
|
}
|
|
|
|
|
|
|
|
labelService := label.NewService(labelStore)
|
2019-12-07 18:54:03 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, l := range f.Labels {
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
mock.SetIDForFunc(&labelStore.IDGenerator, l.ID, func() {
|
|
|
|
if err := labelService.CreateLabel(ctx, l); err != nil {
|
|
|
|
t.Fatalf("failed to populate labels: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2019-12-07 18:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range f.Mappings {
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
if err := labelService.CreateLabelMapping(ctx, m); err != nil {
|
2019-12-07 18:54:03 +00:00
|
|
|
t.Fatalf("failed to populate label mappings: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 19:12:35 +00:00
|
|
|
handler := NewLabelHandler(zaptest.NewLogger(t), labelService, kithttp.NewErrorHandler(zaptest.NewLogger(t)))
|
2019-12-07 18:54:03 +00:00
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
client := LabelService{
|
2019-12-28 00:58:57 +00:00
|
|
|
Client: mustNewHTTPClient(t, server.URL, ""),
|
2019-12-07 18:54:03 +00:00
|
|
|
}
|
|
|
|
done := server.Close
|
|
|
|
|
2019-12-28 00:58:57 +00:00
|
|
|
return &client, "", done
|
2019-12-07 18:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLabelService(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
testFn func(
|
|
|
|
init func(platformtesting.LabelFields, *testing.T) (platform.LabelService, string, func()),
|
|
|
|
t *testing.T,
|
|
|
|
)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "create label",
|
|
|
|
testFn: platformtesting.CreateLabel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete label",
|
|
|
|
testFn: platformtesting.DeleteLabel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "update label",
|
|
|
|
testFn: platformtesting.UpdateLabel,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "find labels",
|
|
|
|
testFn: platformtesting.FindLabels,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "find label by ID",
|
|
|
|
testFn: platformtesting.FindLabelByID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tt.testFn(initLabelService, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|