2018-09-11 23:13:15 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2019-03-28 18:14:46 +00:00
|
|
|
"encoding/json"
|
2019-01-24 00:15:42 +00:00
|
|
|
"fmt"
|
2018-09-11 23:13:15 +00:00
|
|
|
"io/ioutil"
|
2018-12-20 18:16:41 +00:00
|
|
|
"net/http"
|
2018-09-11 23:13:15 +00:00
|
|
|
"net/http/httptest"
|
2020-11-18 01:29:35 +00:00
|
|
|
"strconv"
|
2018-09-11 23:13:15 +00:00
|
|
|
"testing"
|
2019-06-18 22:05:40 +00:00
|
|
|
"time"
|
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
platform2 "github.com/influxdata/influxdb/v2/kit/platform"
|
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform/errors"
|
|
|
|
|
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"
|
|
|
|
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/kv"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/mock"
|
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/tenant"
|
2020-08-12 21:23:09 +00:00
|
|
|
itesting "github.com/influxdata/influxdb/v2/testing"
|
2019-12-04 23:10:23 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-09-11 23:13:15 +00:00
|
|
|
)
|
|
|
|
|
2019-06-18 22:05:40 +00:00
|
|
|
var faketime = time.Date(2006, 5, 4, 1, 2, 3, 0, time.UTC)
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
// NewMockVariableBackend returns a VariableBackend with mock services.
|
2019-12-04 23:10:23 +00:00
|
|
|
func NewMockVariableBackend(t *testing.T) *VariableBackend {
|
2019-02-14 20:32:54 +00:00
|
|
|
return &VariableBackend{
|
2020-02-03 19:07:43 +00:00
|
|
|
HTTPErrorHandler: kithttp.ErrorHandler(0),
|
2019-12-07 18:54:03 +00:00
|
|
|
log: zaptest.NewLogger(t),
|
|
|
|
VariableService: mock.NewVariableService(),
|
|
|
|
LabelService: mock.NewLabelService(),
|
2019-01-16 04:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
func TestVariableService_handleGetVariables(t *testing.T) {
|
2018-09-11 23:13:15 +00:00
|
|
|
type fields struct {
|
2019-02-14 20:32:54 +00:00
|
|
|
VariableService platform.VariableService
|
2019-03-28 18:14:46 +00:00
|
|
|
LabelService platform.LabelService
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
2018-12-20 18:16:41 +00:00
|
|
|
type args struct {
|
|
|
|
queryParams map[string][]string
|
|
|
|
}
|
2018-09-11 23:13:15 +00:00
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
2018-12-20 18:16:41 +00:00
|
|
|
args args
|
2018-09-11 23:13:15 +00:00
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "get all variables",
|
2018-09-11 23:13:15 +00:00
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
|
|
|
FindVariablesF: func(ctx context.Context, filter platform.VariableFilter, opts ...platform.FindOptions) ([]*platform.Variable, error) {
|
|
|
|
return []*platform.Variable{
|
2018-09-11 23:13:15 +00:00
|
|
|
{
|
2020-08-12 21:23:09 +00:00
|
|
|
ID: itesting.MustIDBase16("6162207574726f71"),
|
2021-03-30 18:10:02 +00:00
|
|
|
OrganizationID: platform2.ID(1),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "variable-a",
|
2019-01-15 17:31:02 +00:00
|
|
|
Selected: []string{"b"},
|
2019-02-14 20:32:54 +00:00
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-11 23:13:15 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{"a", "b"},
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
2019-06-18 22:05:40 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: faketime,
|
|
|
|
UpdatedAt: faketime,
|
|
|
|
},
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
{
|
2020-08-12 21:23:09 +00:00
|
|
|
ID: itesting.MustIDBase16("61726920617a696f"),
|
2021-03-30 18:10:02 +00:00
|
|
|
OrganizationID: platform2.ID(1),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "variable-b",
|
2019-01-15 17:31:02 +00:00
|
|
|
Selected: []string{"c"},
|
2019-02-14 20:32:54 +00:00
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-11 23:13:15 +00:00
|
|
|
Type: "map",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableMapValues{"a": "b", "c": "d"},
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
2019-06-18 22:05:40 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: faketime,
|
|
|
|
UpdatedAt: faketime,
|
|
|
|
},
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
2019-03-28 18:14:46 +00:00
|
|
|
&mock.LabelService{
|
|
|
|
FindResourceLabelsFn: func(ctx context.Context, f platform.LabelMappingFilter) ([]*platform.Label, error) {
|
|
|
|
labels := []*platform.Label{
|
|
|
|
{
|
2020-08-12 21:23:09 +00:00
|
|
|
ID: itesting.MustIDBase16("fc3dc670a4be9b9a"),
|
2019-03-28 18:14:46 +00:00
|
|
|
Name: "label",
|
|
|
|
Properties: map[string]string{
|
|
|
|
"color": "fff000",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return labels, nil
|
|
|
|
},
|
|
|
|
},
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
2018-12-20 18:16:41 +00:00
|
|
|
statusCode: http.StatusOK,
|
2018-09-11 23:13:15 +00:00
|
|
|
contentType: "application/json; charset=utf-8",
|
2019-04-02 21:40:42 +00:00
|
|
|
body: `{
|
|
|
|
"links":{
|
2020-11-18 01:29:35 +00:00
|
|
|
"self":"/api/v2/variables?descending=false&limit=` + strconv.Itoa(platform.DefaultPageSize) + `&offset=0"
|
2019-04-02 21:40:42 +00:00
|
|
|
},
|
|
|
|
"variables":[
|
|
|
|
{
|
|
|
|
"arguments":{
|
|
|
|
"type":"constant",
|
|
|
|
"values":[
|
|
|
|
"a",
|
|
|
|
"b"
|
|
|
|
]
|
|
|
|
},
|
2019-06-18 22:05:40 +00:00
|
|
|
"createdAt": "2006-05-04T01:02:03Z",
|
|
|
|
"updatedAt": "2006-05-04T01:02:03Z",
|
2019-04-02 21:40:42 +00:00
|
|
|
"description":"",
|
|
|
|
"id":"6162207574726f71",
|
|
|
|
"labels":[
|
|
|
|
{
|
|
|
|
"id":"fc3dc670a4be9b9a",
|
|
|
|
"name":"label",
|
|
|
|
"properties":{
|
|
|
|
"color":"fff000"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"links":{
|
|
|
|
"labels":"/api/v2/variables/6162207574726f71/labels",
|
|
|
|
"org":"/api/v2/orgs/0000000000000001",
|
|
|
|
"self":"/api/v2/variables/6162207574726f71"
|
|
|
|
},
|
|
|
|
"name":"variable-a",
|
|
|
|
"orgID":"0000000000000001",
|
|
|
|
"selected":[
|
|
|
|
"b"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"arguments":{
|
|
|
|
"type":"map",
|
|
|
|
"values":{
|
|
|
|
"a":"b",
|
|
|
|
"c":"d"
|
|
|
|
}
|
|
|
|
},
|
2019-06-18 22:05:40 +00:00
|
|
|
"createdAt": "2006-05-04T01:02:03Z",
|
|
|
|
"updatedAt": "2006-05-04T01:02:03Z",
|
2019-04-02 21:40:42 +00:00
|
|
|
"description":"",
|
|
|
|
"id":"61726920617a696f",
|
|
|
|
"labels":[
|
|
|
|
{
|
|
|
|
"id":"fc3dc670a4be9b9a",
|
|
|
|
"name":"label",
|
|
|
|
"properties":{
|
|
|
|
"color":"fff000"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"links":{
|
|
|
|
"labels":"/api/v2/variables/61726920617a696f/labels",
|
|
|
|
"org":"/api/v2/orgs/0000000000000001",
|
|
|
|
"self":"/api/v2/variables/61726920617a696f"
|
|
|
|
},
|
|
|
|
"name":"variable-b",
|
|
|
|
"orgID":"0000000000000001",
|
|
|
|
"selected":[
|
|
|
|
"c"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`,
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-20 18:16:41 +00:00
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "get all variables when there are none",
|
2018-12-20 18:16:41 +00:00
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
|
|
|
FindVariablesF: func(ctx context.Context, filter platform.VariableFilter, opts ...platform.FindOptions) ([]*platform.Variable, error) {
|
|
|
|
return []*platform.Variable{}, nil
|
2018-12-20 18:16:41 +00:00
|
|
|
},
|
|
|
|
},
|
2019-03-28 18:14:46 +00:00
|
|
|
&mock.LabelService{
|
|
|
|
FindResourceLabelsFn: func(ctx context.Context, f platform.LabelMappingFilter) ([]*platform.Label, error) {
|
|
|
|
return []*platform.Label{}, nil
|
|
|
|
},
|
|
|
|
},
|
2018-12-20 18:16:41 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
map[string][]string{
|
2019-02-01 06:02:13 +00:00
|
|
|
"limit": {"1"},
|
2018-12-20 18:16:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
2019-02-14 20:32:54 +00:00
|
|
|
body: `{"links":{"self":"/api/v2/variables?descending=false&limit=1&offset=0"},"variables":[]}`,
|
2018-12-20 18:16:41 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-15 17:31:02 +00:00
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "get all variables belonging to an org",
|
2019-01-15 17:31:02 +00:00
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
|
|
|
FindVariablesF: func(ctx context.Context, filter platform.VariableFilter, opts ...platform.FindOptions) ([]*platform.Variable, error) {
|
|
|
|
return []*platform.Variable{
|
2019-01-15 17:31:02 +00:00
|
|
|
{
|
2020-08-12 21:23:09 +00:00
|
|
|
ID: itesting.MustIDBase16("6162207574726f71"),
|
|
|
|
OrganizationID: itesting.MustIDBase16("0000000000000001"),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "variable-a",
|
2019-01-15 17:31:02 +00:00
|
|
|
Selected: []string{"b"},
|
2019-02-14 20:32:54 +00:00
|
|
|
Arguments: &platform.VariableArguments{
|
2019-01-15 17:31:02 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{"a", "b"},
|
2019-01-15 17:31:02 +00:00
|
|
|
},
|
2019-06-18 22:05:40 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: faketime,
|
|
|
|
UpdatedAt: faketime,
|
|
|
|
},
|
2019-01-15 17:31:02 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
2019-03-28 18:14:46 +00:00
|
|
|
&mock.LabelService{
|
|
|
|
FindResourceLabelsFn: func(ctx context.Context, f platform.LabelMappingFilter) ([]*platform.Label, error) {
|
|
|
|
labels := []*platform.Label{
|
|
|
|
{
|
2020-08-12 21:23:09 +00:00
|
|
|
ID: itesting.MustIDBase16("fc3dc670a4be9b9a"),
|
2019-03-28 18:14:46 +00:00
|
|
|
Name: "label",
|
|
|
|
Properties: map[string]string{
|
|
|
|
"color": "fff000",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return labels, nil
|
|
|
|
},
|
|
|
|
},
|
2019-01-15 17:31:02 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
map[string][]string{
|
2019-02-01 06:02:13 +00:00
|
|
|
"orgID": {"0000000000000001"},
|
2019-01-15 17:31:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
2019-04-02 21:40:42 +00:00
|
|
|
body: `{
|
|
|
|
"links": {
|
2020-11-18 01:29:35 +00:00
|
|
|
"self": "/api/v2/variables?descending=false&limit=` + strconv.Itoa(platform.DefaultPageSize) + `&offset=0&orgID=0000000000000001"
|
2019-04-02 21:40:42 +00:00
|
|
|
},
|
|
|
|
"variables": [
|
|
|
|
{
|
|
|
|
"arguments": {
|
|
|
|
"type": "constant",
|
|
|
|
"values": [
|
|
|
|
"a",
|
|
|
|
"b"
|
|
|
|
]
|
|
|
|
},
|
2019-06-18 22:05:40 +00:00
|
|
|
"description": "",
|
2019-04-02 21:40:42 +00:00
|
|
|
"id": "6162207574726f71",
|
|
|
|
"labels": [
|
|
|
|
{
|
|
|
|
"id": "fc3dc670a4be9b9a",
|
|
|
|
"name": "label",
|
|
|
|
"properties": {
|
|
|
|
"color": "fff000"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"links": {
|
|
|
|
"labels": "/api/v2/variables/6162207574726f71/labels",
|
|
|
|
"org": "/api/v2/orgs/0000000000000001",
|
|
|
|
"self": "/api/v2/variables/6162207574726f71"
|
|
|
|
},
|
|
|
|
"name": "variable-a",
|
|
|
|
"orgID": "0000000000000001",
|
|
|
|
"selected": [
|
|
|
|
"b"
|
2019-06-18 22:05:40 +00:00
|
|
|
],
|
|
|
|
"createdAt": "2006-05-04T01:02:03Z",
|
|
|
|
"updatedAt": "2006-05-04T01:02:03Z"
|
2019-04-02 21:40:42 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}`,
|
2019-01-15 17:31:02 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
variableBackend := NewMockVariableBackend(t)
|
2020-02-03 19:07:43 +00:00
|
|
|
variableBackend.HTTPErrorHandler = kithttp.ErrorHandler(0)
|
2019-03-28 18:14:46 +00:00
|
|
|
variableBackend.LabelService = tt.fields.LabelService
|
2019-02-14 20:32:54 +00:00
|
|
|
variableBackend.VariableService = tt.fields.VariableService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewVariableHandler(zaptest.NewLogger(t), variableBackend)
|
2018-12-20 18:16:41 +00:00
|
|
|
|
2018-09-11 23:13:15 +00:00
|
|
|
r := httptest.NewRequest("GET", "http://howdy.tld", nil)
|
2019-03-28 18:14:46 +00:00
|
|
|
|
2018-12-20 18:16:41 +00:00
|
|
|
qp := r.URL.Query()
|
|
|
|
for k, vs := range tt.args.queryParams {
|
|
|
|
for _, v := range vs {
|
|
|
|
qp.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.URL.RawQuery = qp.Encode()
|
|
|
|
|
2018-09-11 23:13:15 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
h.handleGetVariables(w, r)
|
2018-09-11 23:13:15 +00:00
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
contentType := res.Header.Get("Content-Type")
|
2018-12-20 18:16:41 +00:00
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
2018-09-11 23:13:15 +00:00
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
2019-02-14 20:32:54 +00:00
|
|
|
t.Errorf("%q. handleGetVariables() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
if contentType != tt.wants.contentType {
|
2019-02-14 20:32:54 +00:00
|
|
|
t.Errorf("%q. handleGetVariables() = %v, want %v", tt.name, contentType, tt.wants.contentType)
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
2019-05-08 19:51:03 +00:00
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, handleGetDashboards(). error unmarshalling json %v", tt.name, err)
|
2019-05-08 19:51:03 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. handleGetDashboards() = ***%s***", tt.name, diff)
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
func TestVariableService_handleGetVariable(t *testing.T) {
|
2018-09-11 23:13:15 +00:00
|
|
|
type fields struct {
|
2019-02-14 20:32:54 +00:00
|
|
|
VariableService platform.VariableService
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
fields fields
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "get a single variable by id",
|
2018-09-11 23:13:15 +00:00
|
|
|
args: args{
|
2018-09-25 16:55:34 +00:00
|
|
|
id: "75650d0a636f6d70",
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindVariableByIDF: func(ctx context.Context, id platform2.ID) (*platform.Variable, error) {
|
2019-02-14 20:32:54 +00:00
|
|
|
return &platform.Variable{
|
2020-08-12 21:23:09 +00:00
|
|
|
ID: itesting.MustIDBase16("75650d0a636f6d70"),
|
2021-03-30 18:10:02 +00:00
|
|
|
OrganizationID: platform2.ID(1),
|
2019-02-14 20:32:54 +00:00
|
|
|
Name: "variable-a",
|
2019-01-15 17:31:02 +00:00
|
|
|
Selected: []string{"b"},
|
2019-02-14 20:32:54 +00:00
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-11 23:13:15 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{"a", "b"},
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
2019-06-18 22:05:40 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: faketime,
|
|
|
|
UpdatedAt: faketime,
|
|
|
|
},
|
2018-09-11 23:13:15 +00:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
2019-06-18 22:05:40 +00:00
|
|
|
body: `{"id":"75650d0a636f6d70","orgID":"0000000000000001","name":"variable-a","description":"","selected":["b"],"arguments":{"type":"constant","values":["a","b"]},"createdAt":"2006-05-04T01:02:03Z","updatedAt":"2006-05-04T01:02:03Z","labels":[],"links":{"self":"/api/v2/variables/75650d0a636f6d70","labels":"/api/v2/variables/75650d0a636f6d70/labels","org":"/api/v2/orgs/0000000000000001"}}`,
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-04-17 20:30:22 +00:00
|
|
|
name: "get a non-existent variable",
|
2018-09-11 23:13:15 +00:00
|
|
|
args: args{
|
2018-09-25 16:55:34 +00:00
|
|
|
id: "75650d0a636f6d70",
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindVariableByIDF: func(ctx context.Context, id platform2.ID) (*platform.Variable, error) {
|
|
|
|
return nil, &errors.Error{
|
|
|
|
Code: errors.ENotFound,
|
2019-02-14 20:32:54 +00:00
|
|
|
Msg: fmt.Sprintf("variable with ID %v not found", id),
|
2019-01-24 00:15:42 +00:00
|
|
|
}
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: 404,
|
2019-01-24 00:15:42 +00:00
|
|
|
contentType: "application/json; charset=utf-8",
|
2019-02-14 20:32:54 +00:00
|
|
|
body: `{"code":"not found","message":"variable with ID 75650d0a636f6d70 not found"}`,
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-12 17:18:56 +00:00
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "request an invalid variable ID",
|
2018-12-12 17:18:56 +00:00
|
|
|
args: args{
|
|
|
|
id: "baz",
|
|
|
|
},
|
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindVariableByIDF: func(ctx context.Context, id platform2.ID) (*platform.Variable, error) {
|
2018-12-12 17:18:56 +00:00
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: 400,
|
2018-12-15 15:33:54 +00:00
|
|
|
contentType: "application/json; charset=utf-8",
|
2019-01-24 00:15:42 +00:00
|
|
|
body: `{"code":"invalid","message":"id must have a length of 16 bytes"}`,
|
2018-12-12 17:18:56 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
variableBackend := NewMockVariableBackend(t)
|
2020-02-03 19:07:43 +00:00
|
|
|
variableBackend.HTTPErrorHandler = kithttp.ErrorHandler(0)
|
2019-02-14 20:32:54 +00:00
|
|
|
variableBackend.VariableService = tt.fields.VariableService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewVariableHandler(zaptest.NewLogger(t), variableBackend)
|
2018-09-11 23:13:15 +00:00
|
|
|
r := httptest.NewRequest("GET", "http://howdy.tld", nil)
|
|
|
|
r = r.WithContext(context.WithValue(
|
|
|
|
context.TODO(),
|
|
|
|
httprouter.ParamsKey,
|
|
|
|
httprouter.Params{
|
|
|
|
{
|
|
|
|
Key: "id",
|
|
|
|
Value: tt.args.id,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
h.handleGetVariable(w, r)
|
2018-09-11 23:13:15 +00:00
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
contentType := res.Header.Get("Content-Type")
|
|
|
|
bodyBytes, _ := ioutil.ReadAll(res.Body)
|
|
|
|
body := string(bodyBytes[:])
|
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("got = %v, want %v", res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if contentType != tt.wants.contentType {
|
|
|
|
t.Errorf("got = %v, want %v", contentType, tt.wants.contentType)
|
|
|
|
}
|
2019-06-18 22:05:40 +00:00
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, error unmarshalling json %v", tt.name, err)
|
2019-06-18 22:05:40 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. ***%s***", tt.name, diff)
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
func TestVariableService_handlePostVariable(t *testing.T) {
|
2018-09-11 23:13:15 +00:00
|
|
|
type fields struct {
|
2019-02-14 20:32:54 +00:00
|
|
|
VariableService platform.VariableService
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
2019-02-14 20:32:54 +00:00
|
|
|
variable string
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "create a new variable",
|
2018-09-11 23:13:15 +00:00
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
|
|
|
CreateVariableF: func(ctx context.Context, m *platform.Variable) error {
|
2020-08-12 21:23:09 +00:00
|
|
|
m.ID = itesting.MustIDBase16("75650d0a636f6d70")
|
2021-03-30 18:10:02 +00:00
|
|
|
m.OrganizationID = platform2.ID(1)
|
2019-06-18 22:05:40 +00:00
|
|
|
m.UpdatedAt = faketime
|
|
|
|
m.CreatedAt = faketime
|
2018-09-11 23:13:15 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2019-02-14 20:32:54 +00:00
|
|
|
variable: `
|
2018-09-11 23:13:15 +00:00
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
"name": "my-great-variable",
|
2019-02-13 23:30:07 +00:00
|
|
|
"orgID": "0000000000000001",
|
2018-09-11 23:13:15 +00:00
|
|
|
"arguments": {
|
|
|
|
"type": "constant",
|
|
|
|
"values": [
|
|
|
|
"bar",
|
|
|
|
"foo"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"selected": [
|
|
|
|
"'foo'"
|
2019-06-18 22:05:40 +00:00
|
|
|
],
|
|
|
|
"createdAt": "2006-05-04T01:02:03Z",
|
|
|
|
"updatedAt": "2006-05-04T01:02:03Z"
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: 201,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
2019-06-18 22:05:40 +00:00
|
|
|
body: `{"id":"75650d0a636f6d70","orgID":"0000000000000001","name":"my-great-variable","description":"","selected":["'foo'"],"arguments":{"type":"constant","values":["bar","foo"]},"createdAt":"2006-05-04T01:02:03Z","updatedAt":"2006-05-04T01:02:03Z","labels":[],"links":{"self":"/api/v2/variables/75650d0a636f6d70","labels":"/api/v2/variables/75650d0a636f6d70/labels","org":"/api/v2/orgs/0000000000000001"}}
|
2018-09-11 23:13:15 +00:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "create a variable with invalid fields",
|
2018-09-11 23:13:15 +00:00
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
|
|
|
CreateVariableF: func(ctx context.Context, m *platform.Variable) error {
|
2020-08-12 21:23:09 +00:00
|
|
|
m.ID = itesting.MustIDBase16("0")
|
2018-09-11 23:13:15 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2019-02-14 20:32:54 +00:00
|
|
|
variable: `{"data": "nonsense"}`,
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
2019-01-24 00:15:42 +00:00
|
|
|
statusCode: 400,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
2019-02-14 20:32:54 +00:00
|
|
|
body: `{"code":"invalid","message":"missing variable name"}`,
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "create a variable with invalid json",
|
2018-09-11 23:13:15 +00:00
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
|
|
|
CreateVariableF: func(ctx context.Context, m *platform.Variable) error {
|
2020-08-12 21:23:09 +00:00
|
|
|
m.ID = itesting.MustIDBase16("0")
|
2018-09-11 23:13:15 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2019-02-14 20:32:54 +00:00
|
|
|
variable: `howdy`,
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: 400,
|
2019-01-24 00:15:42 +00:00
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `{"code":"invalid","message":"invalid character 'h' looking for beginning of value"}`,
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
variableBackend := NewMockVariableBackend(t)
|
2020-02-03 19:07:43 +00:00
|
|
|
variableBackend.HTTPErrorHandler = kithttp.ErrorHandler(0)
|
2019-02-14 20:32:54 +00:00
|
|
|
variableBackend.VariableService = tt.fields.VariableService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewVariableHandler(zaptest.NewLogger(t), variableBackend)
|
2019-02-14 20:32:54 +00:00
|
|
|
r := httptest.NewRequest("GET", "http://howdy.tld", bytes.NewReader([]byte(tt.args.variable)))
|
2018-09-11 23:13:15 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
h.handlePostVariable(w, r)
|
2018-09-11 23:13:15 +00:00
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
contentType := res.Header.Get("Content-Type")
|
|
|
|
bodyBytes, _ := ioutil.ReadAll(res.Body)
|
|
|
|
body := string(bodyBytes[:])
|
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("got = %v, want %v", res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if contentType != tt.wants.contentType {
|
|
|
|
t.Errorf("got = %v, want %v", contentType, tt.wants.contentType)
|
|
|
|
}
|
2019-06-18 22:05:40 +00:00
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, error unmarshalling json %v", tt.name, err)
|
2019-06-18 22:05:40 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. ***%s***", tt.name, diff)
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 23:11:11 +00:00
|
|
|
func TestVariableService_handlePutVariable(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
VariableService platform.VariableService
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
id string
|
|
|
|
variable string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "PUT a variable",
|
|
|
|
fields: fields{
|
|
|
|
&mock.VariableService{
|
|
|
|
ReplaceVariableF: func(ctx context.Context, m *platform.Variable) error {
|
|
|
|
m.ID = itesting.MustIDBase16("75650d0a636f6d70")
|
2021-03-30 18:10:02 +00:00
|
|
|
m.OrganizationID = platform2.ID(1)
|
2020-08-18 23:11:11 +00:00
|
|
|
m.UpdatedAt = faketime
|
|
|
|
m.CreatedAt = faketime
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "75650d0a636f6d70",
|
|
|
|
variable: `
|
|
|
|
{
|
|
|
|
"name": "my-great-variable",
|
|
|
|
"orgID": "0000000000000001",
|
|
|
|
"arguments": {
|
|
|
|
"type": "constant",
|
|
|
|
"values": [
|
|
|
|
"bar",
|
|
|
|
"foo"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"selected": [
|
|
|
|
"'foo'"
|
|
|
|
],
|
|
|
|
"createdAt": "2006-05-04T01:02:03Z",
|
|
|
|
"updatedAt": "2006-05-04T01:02:03Z"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `{"id":"75650d0a636f6d70","orgID":"0000000000000001","name":"my-great-variable","description":"","selected":["'foo'"],"arguments":{"type":"constant","values":["bar","foo"]},"createdAt":"2006-05-04T01:02:03Z","updatedAt":"2006-05-04T01:02:03Z","labels":[],"links":{"self":"/api/v2/variables/75650d0a636f6d70","labels":"/api/v2/variables/75650d0a636f6d70/labels","org":"/api/v2/orgs/0000000000000001"}}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
variableBackend := NewMockVariableBackend(t)
|
|
|
|
variableBackend.HTTPErrorHandler = kithttp.ErrorHandler(0)
|
|
|
|
variableBackend.VariableService = tt.fields.VariableService
|
|
|
|
h := NewVariableHandler(zaptest.NewLogger(t), variableBackend)
|
|
|
|
r := httptest.NewRequest("GET", "http://howdy.tld", bytes.NewReader([]byte(tt.args.variable)))
|
|
|
|
r = r.WithContext(context.WithValue(
|
|
|
|
context.TODO(),
|
|
|
|
httprouter.ParamsKey,
|
|
|
|
httprouter.Params{
|
|
|
|
{
|
|
|
|
Key: "id",
|
|
|
|
Value: tt.args.id,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.handlePutVariable(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
contentType := res.Header.Get("Content-Type")
|
|
|
|
bodyBytes, _ := ioutil.ReadAll(res.Body)
|
|
|
|
body := string(bodyBytes[:])
|
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("got = %v, want %v", res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if contentType != tt.wants.contentType {
|
|
|
|
t.Errorf("got = %v, want %v", contentType, tt.wants.contentType)
|
|
|
|
}
|
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, error unmarshalling json %v", tt.name, err)
|
2020-08-18 23:11:11 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. ***%s***", tt.name, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
func TestVariableService_handlePatchVariable(t *testing.T) {
|
2018-09-11 23:13:15 +00:00
|
|
|
type fields struct {
|
2019-02-14 20:32:54 +00:00
|
|
|
VariableService platform.VariableService
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
id string
|
|
|
|
update string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "update a variable name",
|
2018-09-11 23:13:15 +00:00
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
2021-03-30 18:10:02 +00:00
|
|
|
UpdateVariableF: func(ctx context.Context, id platform2.ID, u *platform.VariableUpdate) (*platform.Variable, error) {
|
2019-02-14 20:32:54 +00:00
|
|
|
return &platform.Variable{
|
2020-08-12 21:23:09 +00:00
|
|
|
ID: itesting.MustIDBase16("75650d0a636f6d70"),
|
2021-03-30 18:10:02 +00:00
|
|
|
OrganizationID: platform2.ID(2),
|
2019-01-15 17:31:02 +00:00
|
|
|
Name: "new-name",
|
2019-02-14 20:32:54 +00:00
|
|
|
Arguments: &platform.VariableArguments{
|
2018-09-11 23:13:15 +00:00
|
|
|
Type: "constant",
|
2019-02-14 20:32:54 +00:00
|
|
|
Values: platform.VariableConstantValues{},
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
Selected: []string{},
|
2019-06-18 22:05:40 +00:00
|
|
|
CRUDLog: platform.CRUDLog{
|
|
|
|
CreatedAt: faketime,
|
|
|
|
UpdatedAt: faketime,
|
|
|
|
},
|
2018-09-11 23:13:15 +00:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2018-09-25 16:55:34 +00:00
|
|
|
id: "75650d0a636f6d70",
|
2018-09-11 23:13:15 +00:00
|
|
|
update: `{"name": "new-name"}`,
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: 200,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
2019-06-18 22:05:40 +00:00
|
|
|
body: `{"id":"75650d0a636f6d70","orgID":"0000000000000002","name":"new-name","description":"","selected":[],"arguments":{"type":"constant","values":[]},"createdAt":"2006-05-04T01:02:03Z","updatedAt": "2006-05-04T01:02:03Z","labels":[],"links":{"self":"/api/v2/variables/75650d0a636f6d70","labels":"/api/v2/variables/75650d0a636f6d70/labels","org":"/api/v2/orgs/0000000000000002"}}`,
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "with an empty json body",
|
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{},
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2018-09-25 16:55:34 +00:00
|
|
|
id: "75650d0a636f6d70",
|
2018-09-11 23:13:15 +00:00
|
|
|
update: `{}`,
|
|
|
|
},
|
|
|
|
wants: wants{
|
2019-01-24 00:15:42 +00:00
|
|
|
statusCode: 400,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `{"code":"invalid","message":"no fields supplied in update"}`,
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
variableBackend := NewMockVariableBackend(t)
|
2020-02-03 19:07:43 +00:00
|
|
|
variableBackend.HTTPErrorHandler = kithttp.ErrorHandler(0)
|
2019-02-14 20:32:54 +00:00
|
|
|
variableBackend.VariableService = tt.fields.VariableService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewVariableHandler(zaptest.NewLogger(t), variableBackend)
|
2018-09-11 23:13:15 +00:00
|
|
|
r := httptest.NewRequest("GET", "http://howdy.tld", bytes.NewReader([]byte(tt.args.update)))
|
|
|
|
r = r.WithContext(context.WithValue(
|
|
|
|
context.TODO(),
|
|
|
|
httprouter.ParamsKey,
|
|
|
|
httprouter.Params{
|
|
|
|
{
|
|
|
|
Key: "id",
|
|
|
|
Value: tt.args.id,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
h.handlePatchVariable(w, r)
|
2018-09-11 23:13:15 +00:00
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
contentType := res.Header.Get("Content-Type")
|
|
|
|
bodyBytes, _ := ioutil.ReadAll(res.Body)
|
|
|
|
body := string(bodyBytes[:])
|
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("got = %v, want %v", res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if contentType != tt.wants.contentType {
|
|
|
|
t.Errorf("got = %v, want %v", contentType, tt.wants.contentType)
|
|
|
|
}
|
2019-06-18 22:05:40 +00:00
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, error unmarshalling json %v", tt.name, err)
|
2019-06-18 22:05:40 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. ***%s***", tt.name, diff)
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
func TestVariableService_handleDeleteVariable(t *testing.T) {
|
2018-09-11 23:13:15 +00:00
|
|
|
type fields struct {
|
2019-02-14 20:32:54 +00:00
|
|
|
VariableService platform.VariableService
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
type wants struct {
|
2018-11-01 18:06:35 +00:00
|
|
|
statusCode int
|
2018-09-11 23:13:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2019-02-14 20:32:54 +00:00
|
|
|
name: "delete a variable",
|
2018-09-11 23:13:15 +00:00
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
2021-03-30 18:10:02 +00:00
|
|
|
DeleteVariableF: func(ctx context.Context, id platform2.ID) error {
|
2018-09-11 23:13:15 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2018-09-25 16:55:34 +00:00
|
|
|
id: "75650d0a636f6d70",
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: 204,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-04-17 20:30:22 +00:00
|
|
|
name: "delete a non-existent variable",
|
2018-09-11 23:13:15 +00:00
|
|
|
fields: fields{
|
2019-02-14 20:32:54 +00:00
|
|
|
&mock.VariableService{
|
2021-03-30 18:10:02 +00:00
|
|
|
DeleteVariableF: func(ctx context.Context, id platform2.ID) error {
|
|
|
|
return &errors.Error{
|
|
|
|
Code: errors.ENotFound,
|
2019-02-14 20:32:54 +00:00
|
|
|
Msg: fmt.Sprintf("variable with ID %v not found", id),
|
2019-01-24 00:15:42 +00:00
|
|
|
}
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2018-09-25 16:55:34 +00:00
|
|
|
id: "75650d0a636f6d70",
|
2018-09-11 23:13:15 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: 404,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
variableBackend := NewMockVariableBackend(t)
|
2020-02-03 19:07:43 +00:00
|
|
|
variableBackend.HTTPErrorHandler = kithttp.ErrorHandler(0)
|
2019-02-14 20:32:54 +00:00
|
|
|
variableBackend.VariableService = tt.fields.VariableService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewVariableHandler(zaptest.NewLogger(t), variableBackend)
|
2018-09-11 23:13:15 +00:00
|
|
|
r := httptest.NewRequest("GET", "http://howdy.tld", nil)
|
|
|
|
r = r.WithContext(context.WithValue(
|
|
|
|
context.TODO(),
|
|
|
|
httprouter.ParamsKey,
|
|
|
|
httprouter.Params{
|
|
|
|
{
|
|
|
|
Key: "id",
|
|
|
|
Value: tt.args.id,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
h.handleDeleteVariable(w, r)
|
2018-09-11 23:13:15 +00:00
|
|
|
|
|
|
|
statusCode := w.Result().StatusCode
|
|
|
|
|
|
|
|
if statusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("got = %v, want %v", statusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 15:30:52 +00:00
|
|
|
|
2019-03-28 18:14:46 +00:00
|
|
|
func TestService_handlePostVariableLabel(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
LabelService platform.LabelService
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
labelMapping *platform.LabelMapping
|
2021-03-30 18:10:02 +00:00
|
|
|
variableID platform2.ID
|
2019-03-28 18:14:46 +00:00
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "add label to variable",
|
|
|
|
fields: fields{
|
|
|
|
LabelService: &mock.LabelService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindLabelByIDFn: func(ctx context.Context, id platform2.ID) (*platform.Label, error) {
|
2019-03-28 18:14:46 +00:00
|
|
|
return &platform.Label{
|
|
|
|
ID: 1,
|
|
|
|
Name: "label",
|
|
|
|
Properties: map[string]string{
|
|
|
|
"color": "fff000",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
CreateLabelMappingFn: func(ctx context.Context, m *platform.LabelMapping) error { return nil },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
labelMapping: &platform.LabelMapping{
|
|
|
|
ResourceID: 100,
|
|
|
|
LabelID: 1,
|
|
|
|
},
|
|
|
|
variableID: 100,
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusCreated,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"label": {
|
|
|
|
"id": "0000000000000001",
|
|
|
|
"name": "label",
|
|
|
|
"properties": {
|
|
|
|
"color": "fff000"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/labels/0000000000000001"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
variableBackend := NewMockVariableBackend(t)
|
2020-02-03 19:07:43 +00:00
|
|
|
variableBackend.HTTPErrorHandler = kithttp.ErrorHandler(0)
|
2019-03-28 18:14:46 +00:00
|
|
|
variableBackend.LabelService = tt.fields.LabelService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewVariableHandler(zaptest.NewLogger(t), variableBackend)
|
2019-03-28 18:14:46 +00:00
|
|
|
|
|
|
|
b, err := json.Marshal(tt.args.labelMapping)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal label mapping: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-09-03 21:40:29 +00:00
|
|
|
url := fmt.Sprintf("http://localhost:8086/api/v2/variables/%s/labels", tt.args.variableID)
|
2019-03-28 18:14:46 +00:00
|
|
|
r := httptest.NewRequest("POST", url, bytes.NewReader(b))
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("got %v, want %v", res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("got %v, want %v", content, tt.wants.contentType)
|
|
|
|
}
|
2019-05-08 19:51:03 +00:00
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, error unmarshalling json %v", tt.name, err)
|
2019-05-08 19:51:03 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. ***%s***", tt.name, diff)
|
2019-03-28 18:14:46 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-12-07 18:54:03 +00:00
|
|
|
|
2020-08-12 21:23:09 +00:00
|
|
|
func initVariableService(f itesting.VariableFields, t *testing.T) (platform.VariableService, string, func()) {
|
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
|
|
|
store := NewTestInmemStore(t)
|
|
|
|
tenantService := tenant.NewService(tenant.NewStore(store))
|
|
|
|
|
|
|
|
svc := kv.NewService(zaptest.NewLogger(t), store, tenantService)
|
2019-12-07 18:54:03 +00:00
|
|
|
svc.IDGenerator = f.IDGenerator
|
|
|
|
svc.TimeGenerator = f.TimeGenerator
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
for _, v := range f.Variables {
|
|
|
|
if err := svc.ReplaceVariable(ctx, v); err != nil {
|
|
|
|
t.Fatalf("failed to replace variable: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fakeBackend := NewMockVariableBackend(t)
|
|
|
|
fakeBackend.VariableService = svc
|
|
|
|
|
|
|
|
handler := NewVariableHandler(zaptest.NewLogger(t), fakeBackend)
|
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
client := VariableService{
|
|
|
|
Client: mustNewHTTPClient(t, server.URL, ""),
|
|
|
|
}
|
|
|
|
done := server.Close
|
|
|
|
|
2019-12-28 00:58:57 +00:00
|
|
|
return &client, "", done
|
2019-12-07 18:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVariableService(t *testing.T) {
|
2020-08-12 21:23:09 +00:00
|
|
|
itesting.VariableService(initVariableService, t, itesting.WithHTTPValidation())
|
2019-12-07 18:54:03 +00:00
|
|
|
}
|