2019-08-08 21:59:03 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2019-12-13 23:32:21 +00:00
|
|
|
"path"
|
2019-08-08 21:59:03 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
"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
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
pcontext "github.com/influxdata/influxdb/v2/context"
|
|
|
|
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
|
|
|
|
"github.com/influxdata/influxdb/v2/kv"
|
|
|
|
"github.com/influxdata/influxdb/v2/mock"
|
|
|
|
"github.com/influxdata/influxdb/v2/notification/endpoint"
|
2020-10-28 15:22:14 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/notification/endpoint/service"
|
|
|
|
endpointTesting "github.com/influxdata/influxdb/v2/notification/endpoint/service/testing"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/pkg/testttp"
|
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/secret"
|
2020-10-28 15:22:14 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/tenant"
|
2020-04-03 17:39:20 +00:00
|
|
|
influxTesting "github.com/influxdata/influxdb/v2/testing"
|
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/stretchr/testify/require"
|
2019-12-04 23:10:23 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-08-08 21:59:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewMockNotificationEndpointBackend returns a NotificationEndpointBackend with mock services.
|
2019-12-04 23:10:23 +00:00
|
|
|
func NewMockNotificationEndpointBackend(t *testing.T) *NotificationEndpointBackend {
|
2019-08-08 21:59:03 +00:00
|
|
|
return &NotificationEndpointBackend{
|
2019-12-10 19:27:48 +00:00
|
|
|
log: zaptest.NewLogger(t),
|
2020-02-03 19:07:43 +00:00
|
|
|
HTTPErrorHandler: kithttp.ErrorHandler(0),
|
2019-08-08 21:59:03 +00:00
|
|
|
NotificationEndpointService: &mock.NotificationEndpointService{},
|
|
|
|
UserResourceMappingService: mock.NewUserResourceMappingService(),
|
|
|
|
LabelService: mock.NewLabelService(),
|
|
|
|
UserService: mock.NewUserService(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handleGetNotificationEndpoints(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
NotificationEndpointService influxdb.NotificationEndpointService
|
|
|
|
LabelService influxdb.LabelService
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
queryParams map[string][]string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "get all notification endpoints",
|
|
|
|
fields: fields{
|
|
|
|
&mock.NotificationEndpointService{
|
|
|
|
FindNotificationEndpointsF: func(ctx context.Context, filter influxdb.NotificationEndpointFilter, opts ...influxdb.FindOptions) ([]influxdb.NotificationEndpoint, int, error) {
|
|
|
|
return []influxdb.NotificationEndpoint{
|
|
|
|
&endpoint.Slack{
|
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr("0b501e7e557ab1ed"),
|
2019-08-08 21:59:03 +00:00
|
|
|
Name: "hello",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr("50f7ba1150f7ba11"),
|
2019-08-08 21:59:03 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
},
|
2019-09-06 05:28:35 +00:00
|
|
|
URL: "http://example.com",
|
2019-08-08 21:59:03 +00:00
|
|
|
},
|
2019-08-27 17:29:49 +00:00
|
|
|
&endpoint.HTTP{
|
2019-08-08 21:59:03 +00:00
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr("c0175f0077a77005"),
|
2019-08-08 21:59:03 +00:00
|
|
|
Name: "example",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr("7e55e118dbabb1ed"),
|
2019-08-08 21:59:03 +00:00
|
|
|
Status: influxdb.Inactive,
|
|
|
|
},
|
|
|
|
URL: "example.com",
|
2019-08-27 17:29:49 +00:00
|
|
|
Username: influxdb.SecretField{Key: "http-user-key"},
|
|
|
|
Password: influxdb.SecretField{Key: "http-password-key"},
|
2019-08-08 21:59:03 +00:00
|
|
|
AuthMethod: "basic",
|
|
|
|
Method: "POST",
|
|
|
|
ContentTemplate: "template",
|
2019-09-06 16:32:57 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"x-header-1": "header 1",
|
|
|
|
"x-header-2": "header 2",
|
|
|
|
},
|
2019-08-08 21:59:03 +00:00
|
|
|
},
|
|
|
|
}, 2, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&mock.LabelService{
|
|
|
|
FindResourceLabelsFn: func(ctx context.Context, f influxdb.LabelMappingFilter) ([]*influxdb.Label, error) {
|
|
|
|
labels := []*influxdb.Label{
|
|
|
|
{
|
|
|
|
ID: influxTesting.MustIDBase16("fc3dc670a4be9b9a"),
|
|
|
|
Name: "label",
|
|
|
|
Properties: map[string]string{
|
|
|
|
"color": "fff000",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return labels, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
map[string][]string{
|
|
|
|
"limit": {"1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/notificationEndpoints?descending=false&limit=1&offset=0",
|
|
|
|
"next": "/api/v2/notificationEndpoints?descending=false&limit=1&offset=1"
|
|
|
|
},
|
|
|
|
"notificationEndpoints": [
|
|
|
|
{
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"id": "0b501e7e557ab1ed",
|
|
|
|
"labels": [
|
|
|
|
{
|
|
|
|
"id": "fc3dc670a4be9b9a",
|
|
|
|
"name": "label",
|
|
|
|
"properties": {
|
|
|
|
"color": "fff000"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"links": {
|
|
|
|
"labels": "/api/v2/notificationEndpoints/0b501e7e557ab1ed/labels",
|
|
|
|
"members": "/api/v2/notificationEndpoints/0b501e7e557ab1ed/members",
|
|
|
|
"owners": "/api/v2/notificationEndpoints/0b501e7e557ab1ed/owners",
|
|
|
|
"self": "/api/v2/notificationEndpoints/0b501e7e557ab1ed"
|
|
|
|
},
|
|
|
|
"name": "hello",
|
|
|
|
"orgID": "50f7ba1150f7ba11",
|
|
|
|
"status": "active",
|
2019-09-06 05:28:35 +00:00
|
|
|
"type": "slack",
|
|
|
|
"token": "",
|
2019-08-08 21:59:03 +00:00
|
|
|
"updatedAt": "0001-01-01T00:00:00Z",
|
|
|
|
"url": "http://example.com"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"url": "example.com",
|
|
|
|
"id": "c0175f0077a77005",
|
|
|
|
"labels": [
|
|
|
|
{
|
|
|
|
"id": "fc3dc670a4be9b9a",
|
|
|
|
"name": "label",
|
|
|
|
"properties": {
|
|
|
|
"color": "fff000"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"links": {
|
|
|
|
"labels": "/api/v2/notificationEndpoints/c0175f0077a77005/labels",
|
|
|
|
"members": "/api/v2/notificationEndpoints/c0175f0077a77005/members",
|
|
|
|
"owners": "/api/v2/notificationEndpoints/c0175f0077a77005/owners",
|
|
|
|
"self": "/api/v2/notificationEndpoints/c0175f0077a77005"
|
|
|
|
},
|
|
|
|
"name": "example",
|
|
|
|
"orgID": "7e55e118dbabb1ed",
|
2019-08-27 17:29:49 +00:00
|
|
|
"authMethod": "basic",
|
2019-08-08 21:59:03 +00:00
|
|
|
"contentTemplate": "template",
|
2019-08-27 17:29:49 +00:00
|
|
|
"password": "secret: http-password-key",
|
2019-08-08 21:59:03 +00:00
|
|
|
"token":"",
|
|
|
|
"method": "POST",
|
|
|
|
"status": "inactive",
|
2019-09-06 16:32:57 +00:00
|
|
|
"type": "http",
|
|
|
|
"headers": {
|
|
|
|
"x-header-1": "header 1",
|
|
|
|
"x-header-2": "header 2"
|
|
|
|
},
|
2019-08-08 21:59:03 +00:00
|
|
|
"updatedAt": "0001-01-01T00:00:00Z",
|
2019-08-27 17:29:49 +00:00
|
|
|
"username": "secret: http-user-key"
|
2019-08-08 21:59:03 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "get all notification endpoints when there are none",
|
|
|
|
fields: fields{
|
|
|
|
&mock.NotificationEndpointService{
|
|
|
|
FindNotificationEndpointsF: func(ctx context.Context, filter influxdb.NotificationEndpointFilter, opts ...influxdb.FindOptions) ([]influxdb.NotificationEndpoint, int, error) {
|
|
|
|
return []influxdb.NotificationEndpoint{}, 0, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&mock.LabelService{},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
map[string][]string{
|
|
|
|
"limit": {"1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/notificationEndpoints?descending=false&limit=1&offset=0"
|
|
|
|
},
|
|
|
|
"notificationEndpoints": []
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
notificationEndpointBackend := NewMockNotificationEndpointBackend(t)
|
2019-08-08 21:59:03 +00:00
|
|
|
notificationEndpointBackend.NotificationEndpointService = tt.fields.NotificationEndpointService
|
|
|
|
notificationEndpointBackend.LabelService = tt.fields.LabelService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewNotificationEndpointHandler(zaptest.NewLogger(t), notificationEndpointBackend)
|
2019-08-08 21:59:03 +00:00
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "http://any.url", nil)
|
|
|
|
|
|
|
|
qp := r.URL.Query()
|
|
|
|
for k, vs := range tt.args.queryParams {
|
|
|
|
for _, v := range vs {
|
|
|
|
qp.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.URL.RawQuery = qp.Encode()
|
2019-10-24 17:28:34 +00:00
|
|
|
r = r.WithContext(pcontext.SetAuthorizer(r.Context(), &influxdb.Session{UserID: user1ID}))
|
2019-08-08 21:59:03 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.handleGetNotificationEndpoints(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("%q. handleGetNotificationEndpoints() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("%q. handleGetNotificationEndpoints() = %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. handleGetNotificationEndpoints() = ***%v***", tt.name, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handleGetNotificationEndpoint(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
NotificationEndpointService influxdb.NotificationEndpointService
|
|
|
|
}
|
|
|
|
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 notification endpoint by id",
|
|
|
|
fields: fields{
|
|
|
|
&mock.NotificationEndpointService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindNotificationEndpointByIDF: func(ctx context.Context, id platform.ID) (influxdb.NotificationEndpoint, error) {
|
2019-08-08 21:59:03 +00:00
|
|
|
if id == influxTesting.MustIDBase16("020f755c3c082000") {
|
2019-08-27 17:29:49 +00:00
|
|
|
return &endpoint.HTTP{
|
2019-08-08 21:59:03 +00:00
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr("020f755c3c082000"),
|
|
|
|
OrgID: influxTesting.MustIDBase16Ptr("020f755c3c082000"),
|
2019-08-08 21:59:03 +00:00
|
|
|
Name: "hello",
|
|
|
|
Status: influxdb.Active,
|
|
|
|
},
|
|
|
|
URL: "example.com",
|
2019-08-27 17:29:49 +00:00
|
|
|
Username: influxdb.SecretField{Key: "http-user-key"},
|
|
|
|
Password: influxdb.SecretField{Key: "http-password-key"},
|
2019-08-08 21:59:03 +00:00
|
|
|
AuthMethod: "basic",
|
|
|
|
Method: "POST",
|
|
|
|
ContentTemplate: "template",
|
|
|
|
}, 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/notificationEndpoints/020f755c3c082000",
|
|
|
|
"labels": "/api/v2/notificationEndpoints/020f755c3c082000/labels",
|
|
|
|
"members": "/api/v2/notificationEndpoints/020f755c3c082000/members",
|
|
|
|
"owners": "/api/v2/notificationEndpoints/020f755c3c082000/owners"
|
|
|
|
},
|
|
|
|
"labels": [],
|
2019-08-27 17:29:49 +00:00
|
|
|
"authMethod": "basic",
|
2019-08-08 21:59:03 +00:00
|
|
|
"method": "POST",
|
|
|
|
"contentTemplate": "template",
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z",
|
|
|
|
"id": "020f755c3c082000",
|
|
|
|
"url": "example.com",
|
2019-08-27 17:29:49 +00:00
|
|
|
"username": "secret: http-user-key",
|
|
|
|
"password": "secret: http-password-key",
|
2019-08-08 21:59:03 +00:00
|
|
|
"token":"",
|
|
|
|
"status": "active",
|
2019-08-27 17:29:49 +00:00
|
|
|
"type": "http",
|
2019-08-08 21:59:03 +00:00
|
|
|
"orgID": "020f755c3c082000",
|
|
|
|
"name": "hello"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not found",
|
|
|
|
fields: fields{
|
|
|
|
&mock.NotificationEndpointService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindNotificationEndpointByIDF: func(ctx context.Context, id platform.ID) (influxdb.NotificationEndpoint, error) {
|
|
|
|
return nil, &errors.Error{
|
|
|
|
Code: errors.ENotFound,
|
2019-08-08 21:59:03 +00:00
|
|
|
Msg: "notification endpoint not found",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
notificationEndpointBackend := NewMockNotificationEndpointBackend(t)
|
2020-02-03 19:07:43 +00:00
|
|
|
notificationEndpointBackend.HTTPErrorHandler = kithttp.ErrorHandler(0)
|
2019-08-08 21:59:03 +00:00
|
|
|
notificationEndpointBackend.NotificationEndpointService = tt.fields.NotificationEndpointService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewNotificationEndpointHandler(zaptest.NewLogger(t), notificationEndpointBackend)
|
2019-08-08 21:59:03 +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.handleGetNotificationEndpoint(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. handleGetNotificationEndpoint() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("%q. handleGetNotificationEndpoint() = %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 {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, handleGetNotificationEndpoint(). error unmarshalling json %v", tt.name, err)
|
2019-08-08 21:59:03 +00:00
|
|
|
} else if !eq {
|
|
|
|
t.Errorf("%q. handleGetNotificationEndpoint() = ***%s***", tt.name, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handlePostNotificationEndpoint(t *testing.T) {
|
|
|
|
type fields struct {
|
2019-08-28 20:02:17 +00:00
|
|
|
Secrets map[string]string
|
|
|
|
SecretService influxdb.SecretService
|
2019-08-08 21:59:03 +00:00
|
|
|
NotificationEndpointService influxdb.NotificationEndpointService
|
|
|
|
OrganizationService influxdb.OrganizationService
|
|
|
|
}
|
|
|
|
type args struct {
|
2019-08-28 20:02:17 +00:00
|
|
|
endpoint interface{}
|
2019-08-08 21:59:03 +00:00
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "create a new notification endpoint",
|
|
|
|
fields: fields{
|
2019-08-28 20:02:17 +00:00
|
|
|
Secrets: map[string]string{},
|
2019-08-08 21:59:03 +00:00
|
|
|
NotificationEndpointService: &mock.NotificationEndpointService{
|
2021-03-30 18:10:02 +00:00
|
|
|
CreateNotificationEndpointF: func(ctx context.Context, edp influxdb.NotificationEndpoint, userID platform.ID) error {
|
2019-08-08 21:59:03 +00:00
|
|
|
edp.SetID(influxTesting.MustIDBase16("020f755c3c082000"))
|
2019-08-28 20:02:17 +00:00
|
|
|
edp.BackfillSecretKeys()
|
2019-08-08 21:59:03 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
OrganizationService: &mock.OrganizationService{
|
|
|
|
FindOrganizationF: func(ctx context.Context, f influxdb.OrganizationFilter) (*influxdb.Organization, error) {
|
|
|
|
return &influxdb.Organization{ID: influxTesting.MustIDBase16("6f626f7274697320")}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2019-08-28 20:02:17 +00:00
|
|
|
endpoint: map[string]interface{}{
|
|
|
|
"name": "hello",
|
|
|
|
"type": "http",
|
|
|
|
"orgID": "6f626f7274697320",
|
|
|
|
"description": "desc1",
|
|
|
|
"status": "active",
|
|
|
|
"url": "example.com",
|
|
|
|
"username": "user1",
|
|
|
|
"password": "password1",
|
|
|
|
"authMethod": "basic",
|
|
|
|
"method": "POST",
|
|
|
|
"contentTemplate": "template",
|
2019-08-08 21:59:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusCreated,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/notificationEndpoints/020f755c3c082000",
|
|
|
|
"labels": "/api/v2/notificationEndpoints/020f755c3c082000/labels",
|
|
|
|
"members": "/api/v2/notificationEndpoints/020f755c3c082000/members",
|
|
|
|
"owners": "/api/v2/notificationEndpoints/020f755c3c082000/owners"
|
|
|
|
},
|
|
|
|
"url": "example.com",
|
|
|
|
"status": "active",
|
2019-08-28 20:02:17 +00:00
|
|
|
"username": "secret: 020f755c3c082000-username",
|
|
|
|
"password": "secret: 020f755c3c082000-password",
|
2019-08-08 21:59:03 +00:00
|
|
|
"token":"",
|
2019-08-27 17:29:49 +00:00
|
|
|
"authMethod": "basic",
|
2019-08-08 21:59:03 +00:00
|
|
|
"contentTemplate": "template",
|
2019-08-27 17:29:49 +00:00
|
|
|
"type": "http",
|
2019-08-08 21:59:03 +00:00
|
|
|
"method": "POST",
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z",
|
|
|
|
"id": "020f755c3c082000",
|
|
|
|
"orgID": "6f626f7274697320",
|
|
|
|
"name": "hello",
|
|
|
|
"description": "desc1",
|
|
|
|
"labels": []
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
notificationEndpointBackend := NewMockNotificationEndpointBackend(t)
|
2019-08-08 21:59:03 +00:00
|
|
|
notificationEndpointBackend.NotificationEndpointService = tt.fields.NotificationEndpointService
|
|
|
|
|
2019-12-13 23:32:21 +00:00
|
|
|
testttp.
|
|
|
|
PostJSON(t, prefixNotificationEndpoints, tt.args.endpoint).
|
|
|
|
WrapCtx(authCtxFn(user1ID)).
|
|
|
|
Do(NewNotificationEndpointHandler(zaptest.NewLogger(t), notificationEndpointBackend)).
|
|
|
|
ExpectHeader("Content-Type", tt.wants.contentType).
|
|
|
|
ExpectStatus(tt.wants.statusCode).
|
|
|
|
ExpectBody(func(body *bytes.Buffer) {
|
|
|
|
if tt.wants.body != "" {
|
|
|
|
if eq, diff, err := jsonEqual(body.String(), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, handlePostNotificationEndpoint(). error unmarshalling json %v", tt.name, err)
|
2019-12-13 23:32:21 +00:00
|
|
|
} else if !eq {
|
|
|
|
t.Errorf("%q. handlePostNotificationEndpoint() = ***%s***", tt.name, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-08-08 21:59:03 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handleDeleteNotificationEndpoint(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
NotificationEndpointService influxdb.NotificationEndpointService
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
type wants struct {
|
2019-12-13 23:32:21 +00:00
|
|
|
statusCode int
|
|
|
|
body string
|
2019-08-08 21:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "remove a notification endpoint by id",
|
|
|
|
fields: fields{
|
2019-08-28 20:02:17 +00:00
|
|
|
NotificationEndpointService: &mock.NotificationEndpointService{
|
2021-03-30 18:10:02 +00:00
|
|
|
DeleteNotificationEndpointF: func(ctx context.Context, id platform.ID) ([]influxdb.SecretField, platform.ID, error) {
|
2019-08-08 21:59:03 +00:00
|
|
|
if id == influxTesting.MustIDBase16("020f755c3c082000") {
|
2019-08-28 20:02:17 +00:00
|
|
|
return []influxdb.SecretField{
|
|
|
|
{Key: "k1"},
|
|
|
|
}, influxTesting.MustIDBase16("020f755c3c082001"), nil
|
2019-08-08 21:59:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-28 20:02:17 +00:00
|
|
|
return nil, 0, fmt.Errorf("wrong id")
|
2019-08-08 21:59:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNoContent,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "notification endpoint not found",
|
|
|
|
fields: fields{
|
2019-08-28 20:02:17 +00:00
|
|
|
NotificationEndpointService: &mock.NotificationEndpointService{
|
2021-03-30 18:10:02 +00:00
|
|
|
DeleteNotificationEndpointF: func(ctx context.Context, id platform.ID) ([]influxdb.SecretField, platform.ID, error) {
|
|
|
|
return nil, 0, &errors.Error{
|
|
|
|
Code: errors.ENotFound,
|
2019-08-08 21:59:03 +00:00
|
|
|
Msg: "notification endpoint not found",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
notificationEndpointBackend := NewMockNotificationEndpointBackend(t)
|
2020-02-03 19:07:43 +00:00
|
|
|
notificationEndpointBackend.HTTPErrorHandler = kithttp.ErrorHandler(0)
|
2019-08-08 21:59:03 +00:00
|
|
|
notificationEndpointBackend.NotificationEndpointService = tt.fields.NotificationEndpointService
|
2019-08-28 20:02:17 +00:00
|
|
|
|
2019-12-13 23:32:21 +00:00
|
|
|
testttp.
|
|
|
|
Delete(t, path.Join(prefixNotificationEndpoints, tt.args.id)).
|
|
|
|
Do(NewNotificationEndpointHandler(zaptest.NewLogger(t), notificationEndpointBackend)).
|
|
|
|
ExpectStatus(tt.wants.statusCode).
|
|
|
|
ExpectBody(func(buf *bytes.Buffer) {
|
|
|
|
if tt.wants.body != "" {
|
|
|
|
if eq, diff, err := jsonEqual(buf.String(), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, handleDeleteNotificationEndpoint(). error unmarshalling json %v", tt.name, err)
|
2019-12-13 23:32:21 +00:00
|
|
|
} else if !eq {
|
|
|
|
t.Errorf("%q. handleDeleteNotificationEndpoint() = ***%s***", tt.name, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-08-08 21:59:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handlePatchNotificationEndpoint(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
NotificationEndpointService influxdb.NotificationEndpointService
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
id string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "update a notification endpoint name",
|
|
|
|
fields: fields{
|
|
|
|
&mock.NotificationEndpointService{
|
2021-03-30 18:10:02 +00:00
|
|
|
PatchNotificationEndpointF: func(ctx context.Context, id platform.ID, upd influxdb.NotificationEndpointUpdate) (influxdb.NotificationEndpoint, error) {
|
2019-08-08 21:59:03 +00:00
|
|
|
if id == influxTesting.MustIDBase16("020f755c3c082000") {
|
|
|
|
d := &endpoint.Slack{
|
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr("020f755c3c082000"),
|
2019-08-08 21:59:03 +00:00
|
|
|
Name: "hello",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr("020f755c3c082000"),
|
2019-08-08 21:59:03 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
},
|
2019-09-06 05:28:35 +00:00
|
|
|
URL: "http://example.com",
|
2019-08-08 21:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if upd.Name != nil {
|
|
|
|
d.Name = *upd.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("not found")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
name: "example",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/notificationEndpoints/020f755c3c082000",
|
|
|
|
"labels": "/api/v2/notificationEndpoints/020f755c3c082000/labels",
|
|
|
|
"members": "/api/v2/notificationEndpoints/020f755c3c082000/members",
|
|
|
|
"owners": "/api/v2/notificationEndpoints/020f755c3c082000/owners"
|
|
|
|
},
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z",
|
|
|
|
"id": "020f755c3c082000",
|
|
|
|
"orgID": "020f755c3c082000",
|
|
|
|
"url": "http://example.com",
|
|
|
|
"name": "example",
|
|
|
|
"status": "active",
|
2019-09-06 05:28:35 +00:00
|
|
|
"type": "slack",
|
|
|
|
"token": "",
|
2019-08-08 21:59:03 +00:00
|
|
|
"labels": []
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "notification endpoint not found",
|
|
|
|
fields: fields{
|
|
|
|
&mock.NotificationEndpointService{
|
2021-03-30 18:10:02 +00:00
|
|
|
PatchNotificationEndpointF: func(ctx context.Context, id platform.ID, upd influxdb.NotificationEndpointUpdate) (influxdb.NotificationEndpoint, error) {
|
|
|
|
return nil, &errors.Error{
|
|
|
|
Code: errors.ENotFound,
|
2019-08-08 21:59:03 +00:00
|
|
|
Msg: "notification endpoint not found",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
name: "hello",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
notificationEndpointBackend := NewMockNotificationEndpointBackend(t)
|
2020-02-03 19:07:43 +00:00
|
|
|
notificationEndpointBackend.HTTPErrorHandler = kithttp.ErrorHandler(0)
|
2019-08-08 21:59:03 +00:00
|
|
|
notificationEndpointBackend.NotificationEndpointService = tt.fields.NotificationEndpointService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewNotificationEndpointHandler(zaptest.NewLogger(t), notificationEndpointBackend)
|
2019-08-08 21:59:03 +00:00
|
|
|
|
|
|
|
upd := influxdb.NotificationEndpointUpdate{}
|
|
|
|
if tt.args.name != "" {
|
|
|
|
upd.Name = &tt.args.name
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(upd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal notification endpoint update: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "http://any.url", bytes.NewReader(b))
|
|
|
|
r = r.WithContext(pcontext.SetAuthorizer(r.Context(), &influxdb.Session{UserID: user1ID}))
|
|
|
|
|
|
|
|
r = r.WithContext(context.WithValue(
|
|
|
|
context.Background(),
|
|
|
|
httprouter.ParamsKey,
|
|
|
|
httprouter.Params{
|
|
|
|
{
|
|
|
|
Key: "id",
|
|
|
|
Value: tt.args.id,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.handlePatchNotificationEndpoint(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("%q. handlePatchNotificationEndpoint() = %v, want %v %v", tt.name, res.StatusCode, tt.wants.statusCode, w.Header())
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("%q. handlePatchNotificationEndpoint() = %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 {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, handlePatchNotificationEndpoint(). error unmarshalling json %v", tt.name, err)
|
2019-08-08 21:59:03 +00:00
|
|
|
} else if !eq {
|
|
|
|
t.Errorf("%q. handlePatchNotificationEndpoint() = ***%s***", tt.name, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handleUpdateNotificationEndpoint(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
NotificationEndpointService influxdb.NotificationEndpointService
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
id string
|
2019-08-28 20:02:17 +00:00
|
|
|
edp map[string]interface{}
|
2019-08-08 21:59:03 +00:00
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "update a notification endpoint name",
|
|
|
|
fields: fields{
|
2019-08-28 20:02:17 +00:00
|
|
|
NotificationEndpointService: &mock.NotificationEndpointService{
|
2021-03-30 18:10:02 +00:00
|
|
|
UpdateNotificationEndpointF: func(ctx context.Context, id platform.ID, edp influxdb.NotificationEndpoint, userID platform.ID) (influxdb.NotificationEndpoint, error) {
|
2019-08-08 21:59:03 +00:00
|
|
|
if id == influxTesting.MustIDBase16("020f755c3c082000") {
|
2019-08-28 20:02:17 +00:00
|
|
|
edp.SetID(id)
|
|
|
|
edp.BackfillSecretKeys()
|
|
|
|
return edp, nil
|
2019-08-08 21:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("not found")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
2019-08-28 20:02:17 +00:00
|
|
|
edp: map[string]interface{}{
|
|
|
|
"name": "example",
|
|
|
|
"status": "active",
|
|
|
|
"orgID": "020f755c3c082001",
|
|
|
|
"type": "slack",
|
2019-09-06 05:28:35 +00:00
|
|
|
"token": "",
|
2019-08-28 20:02:17 +00:00
|
|
|
"url": "example.com",
|
2019-08-08 21:59:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/notificationEndpoints/020f755c3c082000",
|
|
|
|
"labels": "/api/v2/notificationEndpoints/020f755c3c082000/labels",
|
|
|
|
"members": "/api/v2/notificationEndpoints/020f755c3c082000/members",
|
|
|
|
"owners": "/api/v2/notificationEndpoints/020f755c3c082000/owners"
|
|
|
|
},
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z",
|
|
|
|
"id": "020f755c3c082000",
|
2019-08-28 20:02:17 +00:00
|
|
|
"orgID": "020f755c3c082001",
|
2019-08-08 21:59:03 +00:00
|
|
|
"name": "example",
|
|
|
|
"url": "example.com",
|
|
|
|
"type": "slack",
|
|
|
|
"status": "active",
|
2019-09-06 05:28:35 +00:00
|
|
|
"token": "",
|
2019-08-08 21:59:03 +00:00
|
|
|
"labels": []
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "notification endpoint not found",
|
|
|
|
fields: fields{
|
2019-08-28 20:02:17 +00:00
|
|
|
NotificationEndpointService: &mock.NotificationEndpointService{
|
2021-03-30 18:10:02 +00:00
|
|
|
UpdateNotificationEndpointF: func(ctx context.Context, id platform.ID, edp influxdb.NotificationEndpoint, userID platform.ID) (influxdb.NotificationEndpoint, error) {
|
|
|
|
return nil, &errors.Error{
|
|
|
|
Code: errors.ENotFound,
|
2019-08-08 21:59:03 +00:00
|
|
|
Msg: "notification endpoint not found",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
2019-08-28 20:02:17 +00:00
|
|
|
edp: map[string]interface{}{
|
|
|
|
"type": "slack",
|
|
|
|
"name": "example",
|
2019-08-08 21:59:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
notificationEndpointBackend := NewMockNotificationEndpointBackend(t)
|
2020-02-03 19:07:43 +00:00
|
|
|
notificationEndpointBackend.HTTPErrorHandler = kithttp.ErrorHandler(0)
|
2019-08-08 21:59:03 +00:00
|
|
|
notificationEndpointBackend.NotificationEndpointService = tt.fields.NotificationEndpointService
|
|
|
|
|
2019-12-13 23:32:21 +00:00
|
|
|
resp := testttp.
|
|
|
|
PutJSON(t, path.Join(prefixNotificationEndpoints, tt.args.id), tt.args.edp).
|
|
|
|
WrapCtx(authCtxFn(user1ID)).
|
|
|
|
Do(NewNotificationEndpointHandler(zaptest.NewLogger(t), notificationEndpointBackend)).
|
|
|
|
ExpectStatus(tt.wants.statusCode).
|
|
|
|
ExpectBody(func(body *bytes.Buffer) {
|
|
|
|
if tt.wants.body != "" {
|
|
|
|
if eq, diff, err := jsonEqual(body.String(), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, handlePutNotificationEndpoint(). error unmarshalling json %v", tt.name, err)
|
2019-12-13 23:32:21 +00:00
|
|
|
} else if !eq {
|
|
|
|
t.Errorf("%q. handlePutNotificationEndpoint() = ***%s***", tt.name, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if tt.wants.contentType != "" {
|
|
|
|
resp.ExpectHeader("Content-Type", tt.wants.contentType)
|
2019-08-28 20:02:17 +00:00
|
|
|
}
|
2019-08-08 21:59:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handlePostNotificationEndpointMember(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
UserService influxdb.UserService
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
notificationEndpointID string
|
|
|
|
user *influxdb.User
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "add a notification endpoint member",
|
|
|
|
fields: fields{
|
|
|
|
UserService: &mock.UserService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindUserByIDFn: func(ctx context.Context, id platform.ID) (*influxdb.User, error) {
|
2019-08-08 21:59:03 +00:00
|
|
|
return &influxdb.User{
|
2019-09-24 22:18:42 +00:00
|
|
|
ID: id,
|
|
|
|
Name: "name",
|
|
|
|
Status: influxdb.Active,
|
2019-08-08 21:59:03 +00:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
notificationEndpointID: "020f755c3c082000",
|
|
|
|
user: &influxdb.User{
|
|
|
|
ID: influxTesting.MustIDBase16("6f626f7274697320"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusCreated,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"logs": "/api/v2/users/6f626f7274697320/logs",
|
|
|
|
"self": "/api/v2/users/6f626f7274697320"
|
|
|
|
},
|
|
|
|
"role": "member",
|
|
|
|
"id": "6f626f7274697320",
|
2019-09-24 22:18:42 +00:00
|
|
|
"name": "name",
|
|
|
|
"status": "active"
|
2019-08-08 21:59:03 +00:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
notificationEndpointBackend := NewMockNotificationEndpointBackend(t)
|
2019-08-08 21:59:03 +00:00
|
|
|
notificationEndpointBackend.UserService = tt.fields.UserService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewNotificationEndpointHandler(zaptest.NewLogger(t), notificationEndpointBackend)
|
2019-08-08 21:59:03 +00:00
|
|
|
|
|
|
|
b, err := json.Marshal(tt.args.user)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to marshal user: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("/api/v2/notificationEndpoints/%s/members", tt.args.notificationEndpointID)
|
|
|
|
r := httptest.NewRequest("POST", path, bytes.NewReader(b))
|
|
|
|
r = r.WithContext(pcontext.SetAuthorizer(r.Context(), &influxdb.Session{UserID: user1ID}))
|
|
|
|
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("%q. handlePostNotificationEndpointMember() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("%q. handlePostNotificationEndpointMember() = %v, want %v", tt.name, content, 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, handlePostNotificationEndpointMember(). error unmarshalling json %v", tt.name, err)
|
2019-08-08 21:59:03 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. handlePostNotificationEndpointMember() = ***%s***", tt.name, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handlePostNotificationEndpointOwner(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
UserService influxdb.UserService
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
notificationEndpointID string
|
|
|
|
user *influxdb.User
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "add a notification endpoint owner",
|
|
|
|
fields: fields{
|
|
|
|
UserService: &mock.UserService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindUserByIDFn: func(ctx context.Context, id platform.ID) (*influxdb.User, error) {
|
2019-08-08 21:59:03 +00:00
|
|
|
return &influxdb.User{
|
2019-09-24 22:18:42 +00:00
|
|
|
ID: id,
|
|
|
|
Name: "name",
|
|
|
|
Status: influxdb.Active,
|
2019-08-08 21:59:03 +00:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
notificationEndpointID: "020f755c3c082000",
|
|
|
|
user: &influxdb.User{
|
|
|
|
ID: influxTesting.MustIDBase16("6f626f7274697320"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusCreated,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"logs": "/api/v2/users/6f626f7274697320/logs",
|
|
|
|
"self": "/api/v2/users/6f626f7274697320"
|
|
|
|
},
|
|
|
|
"role": "owner",
|
|
|
|
"id": "6f626f7274697320",
|
2019-09-24 22:18:42 +00:00
|
|
|
"name": "name",
|
|
|
|
"status": "active"
|
2019-08-08 21:59:03 +00:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range cases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
notificationEndpointBackend := NewMockNotificationEndpointBackend(t)
|
2019-08-08 21:59:03 +00:00
|
|
|
notificationEndpointBackend.UserService = tt.fields.UserService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewNotificationEndpointHandler(zaptest.NewLogger(t), notificationEndpointBackend)
|
2019-08-08 21:59:03 +00:00
|
|
|
|
|
|
|
b, err := json.Marshal(tt.args.user)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to marshal user: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("/api/v2/notificationEndpoints/%s/owners", tt.args.notificationEndpointID)
|
|
|
|
r := httptest.NewRequest("POST", path, 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("%q. handlePostNotificationEndpointOwner() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("%q. handlePostNotificationEndpointOwner() = %v, want %v", tt.name, content, 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, handlePostNotificationEndpointOwner(). error unmarshalling json %v", tt.name, err)
|
2019-08-08 21:59:03 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. handlePostNotificationEndpointOwner() = ***%s***", tt.name, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 19:27:48 +00:00
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
func initNotificationEndpointService(f endpointTesting.NotificationEndpointFields, t *testing.T) (influxdb.NotificationEndpointService, influxdb.SecretService, func()) {
|
2020-07-01 11:08:20 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
store := NewTestInmemStore(t)
|
|
|
|
logger := zaptest.NewLogger(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
|
|
|
|
|
|
|
tenantStore := tenant.NewStore(store)
|
|
|
|
tenantService := tenant.NewService(tenantStore)
|
|
|
|
|
|
|
|
kvSvc := kv.NewService(logger, store, tenantService)
|
2020-10-28 15:22:14 +00:00
|
|
|
kvSvc.IDGenerator = f.IDGenerator
|
|
|
|
kvSvc.TimeGenerator = f.TimeGenerator
|
|
|
|
|
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
|
|
|
secretStore, err := secret.NewStore(store)
|
|
|
|
require.NoError(t, err)
|
|
|
|
secretSvc := secret.NewService(secretStore)
|
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
endpointStore := service.NewStore(store)
|
|
|
|
endpointStore.IDGenerator = f.IDGenerator
|
|
|
|
endpointStore.TimeGenerator = f.TimeGenerator
|
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
|
|
|
endpointService := service.New(endpointStore, secretSvc)
|
2020-10-28 15:22:14 +00:00
|
|
|
|
|
|
|
for _, o := range f.Orgs {
|
|
|
|
withOrgID(tenantStore, o.ID, func() {
|
|
|
|
if err := tenantService.CreateOrganization(ctx, o); err != nil {
|
|
|
|
t.Fatalf("failed to populate org: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2019-12-10 19:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range f.NotificationEndpoints {
|
2020-10-28 15:22:14 +00:00
|
|
|
if err := endpointStore.PutNotificationEndpoint(ctx, v); err != nil {
|
2019-12-10 19:27:48 +00:00
|
|
|
t.Fatalf("failed to update endpoint: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fakeBackend := NewMockNotificationEndpointBackend(t)
|
2020-10-28 15:22:14 +00:00
|
|
|
fakeBackend.NotificationEndpointService = endpointService
|
|
|
|
fakeBackend.UserResourceMappingService = tenantService
|
|
|
|
fakeBackend.UserService = tenantService
|
2019-12-10 19:27:48 +00:00
|
|
|
|
|
|
|
handler := NewNotificationEndpointHandler(zaptest.NewLogger(t), fakeBackend)
|
|
|
|
auth := func(next http.Handler) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r = r.WithContext(pcontext.SetAuthorizer(r.Context(), &influxdb.Session{UserID: user1ID}))
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
server := httptest.NewServer(auth(handler))
|
|
|
|
done := server.Close
|
|
|
|
|
|
|
|
client := mustNewHTTPClient(t, server.URL, "")
|
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
|
|
|
|
|
|
|
return NewNotificationEndpointService(client), secretSvc, done
|
2019-12-10 19:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotificationEndpointService(t *testing.T) {
|
|
|
|
t.Skip("wonky")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2020-10-28 15:22:14 +00:00
|
|
|
testFn func(init func(endpointTesting.NotificationEndpointFields, *testing.T) (influxdb.NotificationEndpointService, influxdb.SecretService, func()), t *testing.T)
|
2019-12-10 19:27:48 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "CreateNotificationEndpoint",
|
2020-10-28 15:22:14 +00:00
|
|
|
testFn: endpointTesting.CreateNotificationEndpoint,
|
2019-12-10 19:27:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tt.testFn(initNotificationEndpointService, t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 23:32:21 +00:00
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
func authCtxFn(userID platform.ID) func(context.Context) context.Context {
|
2019-12-13 23:32:21 +00:00
|
|
|
return func(ctx context.Context) context.Context {
|
|
|
|
return pcontext.SetAuthorizer(ctx, &influxdb.Session{UserID: userID})
|
|
|
|
}
|
|
|
|
}
|
2020-10-28 15:22:14 +00:00
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
func withOrgID(store *tenant.Store, orgID platform.ID, fn func()) {
|
2020-10-28 15:22:14 +00:00
|
|
|
backup := store.OrgIDGen
|
|
|
|
defer func() { store.OrgIDGen = backup }()
|
|
|
|
|
|
|
|
store.OrgIDGen = mock.NewStaticIDGenerator(orgID)
|
|
|
|
|
|
|
|
fn()
|
|
|
|
}
|