2018-09-10 20:56:11 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-04-13 20:24:27 +00:00
|
|
|
"io"
|
2018-09-10 20:56:11 +00:00
|
|
|
"net/http"
|
|
|
|
"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"
|
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/authorization"
|
2020-04-03 17:39:20 +00:00
|
|
|
pcontext "github.com/influxdata/influxdb/v2/context"
|
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"
|
|
|
|
"github.com/influxdata/influxdb/v2/kv"
|
|
|
|
"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-04-03 17:39:20 +00:00
|
|
|
platformtesting "github.com/influxdata/influxdb/v2/testing"
|
2019-12-04 23:10:23 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-09-10 20:56:11 +00:00
|
|
|
)
|
|
|
|
|
2019-01-16 12:26:09 +00:00
|
|
|
// NewMockAuthorizationBackend returns a AuthorizationBackend with mock services.
|
2019-12-04 23:10:23 +00:00
|
|
|
func NewMockAuthorizationBackend(t *testing.T) *AuthorizationBackend {
|
2019-01-16 12:26:09 +00:00
|
|
|
return &AuthorizationBackend{
|
2019-12-04 23:10:23 +00:00
|
|
|
log: zaptest.NewLogger(t),
|
2019-01-16 12:26:09 +00:00
|
|
|
|
|
|
|
AuthorizationService: mock.NewAuthorizationService(),
|
|
|
|
OrganizationService: mock.NewOrganizationService(),
|
|
|
|
UserService: mock.NewUserService(),
|
|
|
|
LookupService: mock.NewLookupService(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 20:56:11 +00:00
|
|
|
func TestService_handleGetAuthorizations(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
AuthorizationService platform.AuthorizationService
|
2018-12-28 23:02:19 +00:00
|
|
|
UserService platform.UserService
|
|
|
|
OrganizationService platform.OrganizationService
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
2018-09-10 20:56:11 +00:00
|
|
|
type args struct {
|
|
|
|
queryParams map[string][]string
|
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
2018-09-10 20:56:11 +00:00
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2018-12-28 23:02:19 +00:00
|
|
|
name string
|
2018-09-10 20:56:11 +00:00
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2018-12-28 23:02:19 +00:00
|
|
|
name: "get all authorizations",
|
2018-09-10 20:56:11 +00:00
|
|
|
fields: fields{
|
|
|
|
&mock.AuthorizationService{
|
|
|
|
FindAuthorizationsFn: func(ctx context.Context, filter platform.AuthorizationFilter, opts ...platform.FindOptions) ([]*platform.Authorization, int, error) {
|
|
|
|
return []*platform.Authorization{
|
|
|
|
{
|
2018-12-07 22:22:23 +00:00
|
|
|
ID: platformtesting.MustIDBase16("0d0a657820696e74"),
|
|
|
|
Token: "hello",
|
|
|
|
UserID: platformtesting.MustIDBase16("2070616e656d2076"),
|
2018-12-28 23:02:19 +00:00
|
|
|
OrgID: platformtesting.MustIDBase16("3070616e656d2076"),
|
2018-12-07 22:22:23 +00:00
|
|
|
Description: "t1",
|
2019-01-18 23:27:28 +00:00
|
|
|
Permissions: platform.OperPermissions(),
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
{
|
2018-12-07 22:22:23 +00:00
|
|
|
ID: platformtesting.MustIDBase16("6669646573207375"),
|
|
|
|
Token: "example",
|
|
|
|
UserID: platformtesting.MustIDBase16("6c7574652c206f6e"),
|
2018-12-28 23:02:19 +00:00
|
|
|
OrgID: platformtesting.MustIDBase16("9d70616e656d2076"),
|
2018-12-07 22:22:23 +00:00
|
|
|
Description: "t2",
|
2019-01-18 23:27:28 +00:00
|
|
|
Permissions: platform.OperPermissions(),
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
}, 2, nil
|
|
|
|
},
|
|
|
|
},
|
2018-12-28 23:02:19 +00:00
|
|
|
&mock.UserService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindUserByIDFn: func(ctx context.Context, id platform2.ID) (*platform.User, error) {
|
2018-12-28 23:02:19 +00:00
|
|
|
return &platform.User{
|
|
|
|
ID: id,
|
|
|
|
Name: id.String(),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&mock.OrganizationService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindOrganizationByIDF: func(ctx context.Context, id platform2.ID) (*platform.Organization, error) {
|
2018-12-28 23:02:19 +00:00
|
|
|
return &platform.Organization{
|
|
|
|
ID: id,
|
|
|
|
Name: id.String(),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
args: args{},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
2018-12-28 23:02:19 +00:00
|
|
|
body: fmt.Sprintf(`
|
2018-09-10 20:56:11 +00:00
|
|
|
{
|
|
|
|
"links": {
|
2018-09-26 08:49:19 +00:00
|
|
|
"self": "/api/v2/authorizations"
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
2018-12-17 03:51:21 +00:00
|
|
|
"authorizations": [
|
2018-09-10 20:56:11 +00:00
|
|
|
{
|
|
|
|
"links": {
|
2018-09-25 16:55:34 +00:00
|
|
|
"user": "/api/v2/users/2070616e656d2076",
|
|
|
|
"self": "/api/v2/authorizations/0d0a657820696e74"
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
2018-09-25 16:55:34 +00:00
|
|
|
"id": "0d0a657820696e74",
|
2018-12-28 23:02:19 +00:00
|
|
|
"userID": "2070616e656d2076",
|
|
|
|
"user": "2070616e656d2076",
|
|
|
|
"org": "3070616e656d2076",
|
|
|
|
"orgID": "3070616e656d2076",
|
2018-09-10 20:56:11 +00:00
|
|
|
"status": "",
|
2018-12-07 22:22:23 +00:00
|
|
|
"token": "hello",
|
2018-12-28 23:02:19 +00:00
|
|
|
"description": "t1",
|
2019-11-07 14:46:30 +00:00
|
|
|
"permissions": %s,
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z"
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"links": {
|
2018-09-25 16:55:34 +00:00
|
|
|
"user": "/api/v2/users/6c7574652c206f6e",
|
|
|
|
"self": "/api/v2/authorizations/6669646573207375"
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
2018-09-25 16:55:34 +00:00
|
|
|
"id": "6669646573207375",
|
|
|
|
"userID": "6c7574652c206f6e",
|
2018-12-28 23:02:19 +00:00
|
|
|
"user": "6c7574652c206f6e",
|
|
|
|
"org": "9d70616e656d2076",
|
|
|
|
"orgID": "9d70616e656d2076",
|
2018-09-10 20:56:11 +00:00
|
|
|
"status": "",
|
2018-12-07 22:22:23 +00:00
|
|
|
"token": "example",
|
2018-12-28 23:02:19 +00:00
|
|
|
"description": "t2",
|
2019-11-07 14:46:30 +00:00
|
|
|
"permissions": %s,
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z"
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2019-01-18 23:27:28 +00:00
|
|
|
`,
|
|
|
|
MustMarshal(platform.OperPermissions()),
|
|
|
|
MustMarshal(platform.OperPermissions())),
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
},
|
2019-04-23 21:34:04 +00:00
|
|
|
{
|
|
|
|
name: "skip authorizations with no org",
|
|
|
|
fields: fields{
|
|
|
|
&mock.AuthorizationService{
|
|
|
|
FindAuthorizationsFn: func(ctx context.Context, filter platform.AuthorizationFilter, opts ...platform.FindOptions) ([]*platform.Authorization, int, error) {
|
|
|
|
return []*platform.Authorization{
|
|
|
|
{
|
|
|
|
ID: platformtesting.MustIDBase16("0d0a657820696e74"),
|
|
|
|
Token: "hello",
|
|
|
|
UserID: platformtesting.MustIDBase16("2070616e656d2076"),
|
|
|
|
OrgID: platformtesting.MustIDBase16("3070616e656d2076"),
|
|
|
|
Description: "t1",
|
|
|
|
Permissions: platform.OperPermissions(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: platformtesting.MustIDBase16("6669646573207375"),
|
|
|
|
Token: "example",
|
|
|
|
UserID: platformtesting.MustIDBase16("6c7574652c206f6e"),
|
|
|
|
OrgID: platformtesting.MustIDBase16("9d70616e656d2076"),
|
|
|
|
Description: "t2",
|
|
|
|
Permissions: platform.OperPermissions(),
|
|
|
|
},
|
|
|
|
}, 2, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&mock.UserService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindUserByIDFn: func(ctx context.Context, id platform2.ID) (*platform.User, error) {
|
2019-04-23 21:34:04 +00:00
|
|
|
if id.String() == "2070616e656d2076" {
|
|
|
|
return &platform.User{
|
|
|
|
ID: id,
|
|
|
|
Name: id.String(),
|
|
|
|
}, nil
|
|
|
|
}
|
2021-03-30 18:10:02 +00:00
|
|
|
return nil, &errors.Error{}
|
2019-04-23 21:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&mock.OrganizationService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindOrganizationByIDF: func(ctx context.Context, id platform2.ID) (*platform.Organization, error) {
|
2019-04-23 21:34:04 +00:00
|
|
|
return &platform.Organization{
|
|
|
|
ID: id,
|
|
|
|
Name: id.String(),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: fmt.Sprintf(`
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/authorizations"
|
|
|
|
},
|
|
|
|
"authorizations": [
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"user": "/api/v2/users/2070616e656d2076",
|
|
|
|
"self": "/api/v2/authorizations/0d0a657820696e74"
|
|
|
|
},
|
|
|
|
"id": "0d0a657820696e74",
|
|
|
|
"userID": "2070616e656d2076",
|
|
|
|
"user": "2070616e656d2076",
|
|
|
|
"org": "3070616e656d2076",
|
|
|
|
"orgID": "3070616e656d2076",
|
|
|
|
"status": "",
|
|
|
|
"token": "hello",
|
|
|
|
"description": "t1",
|
2019-11-07 14:46:30 +00:00
|
|
|
"permissions": %s,
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z"
|
2019-04-23 21:34:04 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
MustMarshal(platform.OperPermissions())),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "skip authorizations with no user",
|
|
|
|
fields: fields{
|
|
|
|
&mock.AuthorizationService{
|
|
|
|
FindAuthorizationsFn: func(ctx context.Context, filter platform.AuthorizationFilter, opts ...platform.FindOptions) ([]*platform.Authorization, int, error) {
|
|
|
|
return []*platform.Authorization{
|
|
|
|
{
|
|
|
|
ID: platformtesting.MustIDBase16("0d0a657820696e74"),
|
|
|
|
Token: "hello",
|
|
|
|
UserID: platformtesting.MustIDBase16("2070616e656d2076"),
|
|
|
|
OrgID: platformtesting.MustIDBase16("3070616e656d2076"),
|
|
|
|
Description: "t1",
|
|
|
|
Permissions: platform.OperPermissions(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: platformtesting.MustIDBase16("6669646573207375"),
|
|
|
|
Token: "example",
|
|
|
|
UserID: platformtesting.MustIDBase16("6c7574652c206f6e"),
|
|
|
|
OrgID: platformtesting.MustIDBase16("9d70616e656d2076"),
|
|
|
|
Description: "t2",
|
|
|
|
Permissions: platform.OperPermissions(),
|
|
|
|
},
|
|
|
|
}, 2, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&mock.UserService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindUserByIDFn: func(ctx context.Context, id platform2.ID) (*platform.User, error) {
|
2019-04-23 21:34:04 +00:00
|
|
|
return &platform.User{
|
|
|
|
ID: id,
|
|
|
|
Name: id.String(),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&mock.OrganizationService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindOrganizationByIDF: func(ctx context.Context, id platform2.ID) (*platform.Organization, error) {
|
2019-04-23 21:34:04 +00:00
|
|
|
if id.String() == "3070616e656d2076" {
|
|
|
|
return &platform.Organization{
|
|
|
|
ID: id,
|
|
|
|
Name: id.String(),
|
|
|
|
}, nil
|
|
|
|
}
|
2021-03-30 18:10:02 +00:00
|
|
|
return nil, &errors.Error{}
|
2019-04-23 21:34:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: fmt.Sprintf(`
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/authorizations"
|
|
|
|
},
|
|
|
|
"authorizations": [
|
|
|
|
{
|
|
|
|
"links": {
|
|
|
|
"user": "/api/v2/users/2070616e656d2076",
|
|
|
|
"self": "/api/v2/authorizations/0d0a657820696e74"
|
|
|
|
},
|
|
|
|
"id": "0d0a657820696e74",
|
|
|
|
"userID": "2070616e656d2076",
|
|
|
|
"user": "2070616e656d2076",
|
|
|
|
"org": "3070616e656d2076",
|
|
|
|
"orgID": "3070616e656d2076",
|
|
|
|
"status": "",
|
|
|
|
"token": "hello",
|
|
|
|
"description": "t1",
|
2019-11-07 14:46:30 +00:00
|
|
|
"permissions": %s,
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z"
|
2019-04-23 21:34:04 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
MustMarshal(platform.OperPermissions())),
|
|
|
|
},
|
|
|
|
},
|
2018-09-10 20:56:11 +00:00
|
|
|
{
|
2018-12-28 23:02:19 +00:00
|
|
|
name: "get all authorizations when there are none",
|
2018-09-10 20:56:11 +00:00
|
|
|
fields: fields{
|
2018-12-28 23:02:19 +00:00
|
|
|
AuthorizationService: &mock.AuthorizationService{
|
2018-09-10 20:56:11 +00:00
|
|
|
FindAuthorizationsFn: func(ctx context.Context, filter platform.AuthorizationFilter, opts ...platform.FindOptions) ([]*platform.Authorization, int, error) {
|
|
|
|
return []*platform.Authorization{}, 0, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"links": {
|
2018-09-26 08:49:19 +00:00
|
|
|
"self": "/api/v2/authorizations"
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
2018-12-17 03:51:21 +00:00
|
|
|
"authorizations": []
|
2018-09-10 20:56:11 +00:00
|
|
|
}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
authorizationBackend := NewMockAuthorizationBackend(t)
|
2021-09-13 19:12:35 +00:00
|
|
|
authorizationBackend.HTTPErrorHandler = kithttp.NewErrorHandler(zaptest.NewLogger(t))
|
2019-01-16 12:26:09 +00:00
|
|
|
authorizationBackend.AuthorizationService = tt.fields.AuthorizationService
|
|
|
|
authorizationBackend.UserService = tt.fields.UserService
|
|
|
|
authorizationBackend.OrganizationService = tt.fields.OrganizationService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewAuthorizationHandler(zaptest.NewLogger(t), authorizationBackend)
|
2018-09-10 20:56:11 +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()
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.handleGetAuthorizations(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
2022-04-13 20:24:27 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2018-09-10 20:56:11 +00:00
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Errorf("%q. handleGetAuthorizations() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Errorf("%q. handleGetAuthorizations() = %v, want %v", tt.name, content, tt.wants.contentType)
|
2018-09-10 20:56:11 +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, handleGetAuthorizations(). error unmarshalling json %v", tt.name, err)
|
2019-05-08 19:51:03 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. handleGetAuthorizations() = ***%s***", tt.name, diff)
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handleGetAuthorization(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
AuthorizationService platform.AuthorizationService
|
2018-12-28 23:02:19 +00:00
|
|
|
UserService platform.UserService
|
|
|
|
OrganizationService platform.OrganizationService
|
|
|
|
LookupService platform.LookupService
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2018-12-28 23:02:19 +00:00
|
|
|
name string
|
2018-09-10 20:56:11 +00:00
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2018-12-28 23:02:19 +00:00
|
|
|
name: "get a authorization by id",
|
2018-09-10 20:56:11 +00:00
|
|
|
fields: fields{
|
2018-12-28 23:02:19 +00:00
|
|
|
AuthorizationService: &mock.AuthorizationService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindAuthorizationByIDFn: func(ctx context.Context, id platform2.ID) (*platform.Authorization, error) {
|
2018-10-10 19:39:09 +00:00
|
|
|
if id == platformtesting.MustIDBase16("020f755c3c082000") {
|
2018-09-10 20:56:11 +00:00
|
|
|
return &platform.Authorization{
|
2018-10-10 19:39:09 +00:00
|
|
|
ID: platformtesting.MustIDBase16("020f755c3c082000"),
|
|
|
|
UserID: platformtesting.MustIDBase16("020f755c3c082000"),
|
2018-12-28 23:02:19 +00:00
|
|
|
OrgID: platformtesting.MustIDBase16("020f755c3c083000"),
|
|
|
|
Permissions: []platform.Permission{
|
|
|
|
{
|
2019-01-15 16:09:58 +00:00
|
|
|
Action: platform.ReadAction,
|
|
|
|
Resource: platform.Resource{
|
|
|
|
Type: platform.BucketsResourceType,
|
|
|
|
OrgID: platformtesting.IDPtr(platformtesting.MustIDBase16("020f755c3c083000")),
|
2021-03-30 18:10:02 +00:00
|
|
|
ID: func() *platform2.ID {
|
2019-01-15 16:09:58 +00:00
|
|
|
id := platformtesting.MustIDBase16("020f755c3c084000")
|
|
|
|
return &id
|
|
|
|
}(),
|
|
|
|
},
|
2018-12-28 23:02:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Token: "hello",
|
2018-09-10 20:56:11 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("not found")
|
|
|
|
},
|
|
|
|
},
|
2018-12-28 23:02:19 +00:00
|
|
|
UserService: &mock.UserService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindUserByIDFn: func(ctx context.Context, id platform2.ID) (*platform.User, error) {
|
2018-12-28 23:02:19 +00:00
|
|
|
return &platform.User{
|
|
|
|
ID: id,
|
|
|
|
Name: "u1",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
OrganizationService: &mock.OrganizationService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindOrganizationByIDF: func(ctx context.Context, id platform2.ID) (*platform.Organization, error) {
|
2018-12-28 23:02:19 +00:00
|
|
|
return &platform.Organization{
|
|
|
|
ID: id,
|
|
|
|
Name: "o1",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
LookupService: &mock.LookupService{
|
2021-03-30 18:10:02 +00:00
|
|
|
NameFn: func(ctx context.Context, resource platform.ResourceType, id platform2.ID) (string, error) {
|
2019-01-15 16:09:58 +00:00
|
|
|
switch resource {
|
|
|
|
case platform.BucketsResourceType:
|
|
|
|
return "b1", nil
|
|
|
|
case platform.OrgsResourceType:
|
|
|
|
return "o1", nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("bad resource type %s", resource)
|
2018-12-28 23:02:19 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
2019-11-07 14:46:30 +00:00
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z",
|
2019-01-10 21:21:59 +00:00
|
|
|
"description": "",
|
|
|
|
"id": "020f755c3c082000",
|
2018-09-10 20:56:11 +00:00
|
|
|
"links": {
|
2019-01-10 21:21:59 +00:00
|
|
|
"self": "/api/v2/authorizations/020f755c3c082000",
|
|
|
|
"user": "/api/v2/users/020f755c3c082000"
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
2018-12-28 23:02:19 +00:00
|
|
|
"org": "o1",
|
|
|
|
"orgID": "020f755c3c083000",
|
2019-01-10 21:21:59 +00:00
|
|
|
"permissions": [
|
|
|
|
{
|
|
|
|
"action": "read",
|
2019-01-15 16:09:58 +00:00
|
|
|
"resource": {
|
|
|
|
"type": "buckets",
|
|
|
|
"orgID": "020f755c3c083000",
|
|
|
|
"id": "020f755c3c084000",
|
|
|
|
"name": "b1",
|
|
|
|
"org": "o1"
|
|
|
|
}
|
2019-01-10 21:21:59 +00:00
|
|
|
}
|
|
|
|
],
|
2018-12-07 22:22:23 +00:00
|
|
|
"status": "",
|
2019-01-10 21:21:59 +00:00
|
|
|
"token": "hello",
|
|
|
|
"user": "u1",
|
|
|
|
"userID": "020f755c3c082000"
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-12-28 23:02:19 +00:00
|
|
|
name: "not found",
|
2018-09-10 20:56:11 +00:00
|
|
|
fields: fields{
|
2018-12-28 23:02:19 +00:00
|
|
|
AuthorizationService: &mock.AuthorizationService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindAuthorizationByIDFn: func(ctx context.Context, id platform2.ID) (*platform.Authorization, error) {
|
|
|
|
return nil, &errors.Error{
|
|
|
|
Code: errors.ENotFound,
|
2018-11-07 18:55:52 +00:00
|
|
|
Msg: "authorization not found",
|
|
|
|
}
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-28 23:02:19 +00:00
|
|
|
UserService: &mock.UserService{},
|
|
|
|
OrganizationService: &mock.OrganizationService{},
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNotFound,
|
2018-12-28 23:02:19 +00:00
|
|
|
body: `{"code":"not found","message":"authorization not found"}`,
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
authorizationBackend := NewMockAuthorizationBackend(t)
|
2021-09-13 19:12:35 +00:00
|
|
|
authorizationBackend.HTTPErrorHandler = kithttp.NewErrorHandler(zaptest.NewLogger(t))
|
2019-01-16 12:26:09 +00:00
|
|
|
authorizationBackend.AuthorizationService = tt.fields.AuthorizationService
|
|
|
|
authorizationBackend.UserService = tt.fields.UserService
|
|
|
|
authorizationBackend.OrganizationService = tt.fields.OrganizationService
|
|
|
|
authorizationBackend.LookupService = tt.fields.LookupService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewAuthorizationHandler(zaptest.NewLogger(t), authorizationBackend)
|
2018-09-10 20:56:11 +00:00
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "http://any.url", nil)
|
|
|
|
|
|
|
|
r = r.WithContext(context.WithValue(
|
2018-09-15 00:24:32 +00:00
|
|
|
context.Background(),
|
2018-09-10 20:56:11 +00:00
|
|
|
httprouter.ParamsKey,
|
|
|
|
httprouter.Params{
|
|
|
|
{
|
|
|
|
Key: "id",
|
|
|
|
Value: tt.args.id,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.handleGetAuthorization(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
2022-04-13 20:24:27 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2018-09-10 20:56:11 +00:00
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Logf("headers: %v body: %s", res.Header, body)
|
|
|
|
t.Errorf("%q. handleGetAuthorization() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Errorf("%q. handleGetAuthorization() = %v, want %v", tt.name, content, tt.wants.contentType)
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
2019-01-10 21:21:59 +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, handleGetAuthorization. error unmarshalling json %v", tt.name, err)
|
2019-01-10 21:21:59 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Errorf("%q. handleGetAuthorization() = -got/+want %s**", tt.name, diff)
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handlePostAuthorization(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
AuthorizationService platform.AuthorizationService
|
2018-12-21 16:14:29 +00:00
|
|
|
UserService platform.UserService
|
2018-12-28 23:02:19 +00:00
|
|
|
OrganizationService platform.OrganizationService
|
2019-01-15 16:09:58 +00:00
|
|
|
LookupService platform.LookupService
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
2018-12-28 23:02:19 +00:00
|
|
|
session *platform.Authorization
|
2018-09-10 20:56:11 +00:00
|
|
|
authorization *platform.Authorization
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2018-12-28 23:02:19 +00:00
|
|
|
name string
|
2018-09-10 20:56:11 +00:00
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2018-12-28 23:02:19 +00:00
|
|
|
name: "create a new authorization",
|
2018-09-10 20:56:11 +00:00
|
|
|
fields: fields{
|
2018-12-21 16:14:29 +00:00
|
|
|
AuthorizationService: &mock.AuthorizationService{
|
2018-09-10 20:56:11 +00:00
|
|
|
CreateAuthorizationFn: func(ctx context.Context, c *platform.Authorization) error {
|
2018-10-10 19:39:09 +00:00
|
|
|
c.ID = platformtesting.MustIDBase16("020f755c3c082000")
|
2018-12-28 23:02:19 +00:00
|
|
|
c.Token = "new-test-token"
|
2018-09-10 20:56:11 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
2019-01-15 16:09:58 +00:00
|
|
|
LookupService: &mock.LookupService{
|
2021-03-30 18:10:02 +00:00
|
|
|
NameFn: func(ctx context.Context, resource platform.ResourceType, id platform2.ID) (string, error) {
|
2019-01-15 16:09:58 +00:00
|
|
|
switch resource {
|
|
|
|
case platform.BucketsResourceType:
|
|
|
|
return "b1", nil
|
|
|
|
case platform.OrgsResourceType:
|
|
|
|
return "o1", nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("bad resource type %s", resource)
|
|
|
|
},
|
|
|
|
},
|
2018-12-21 16:14:29 +00:00
|
|
|
UserService: &mock.UserService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindUserByIDFn: func(ctx context.Context, id platform2.ID) (*platform.User, error) {
|
2019-01-14 15:20:20 +00:00
|
|
|
if !id.Valid() {
|
2021-03-30 18:10:02 +00:00
|
|
|
return nil, platform2.ErrInvalidID
|
2019-01-14 15:20:20 +00:00
|
|
|
}
|
2018-12-21 16:14:29 +00:00
|
|
|
return &platform.User{
|
2018-12-28 23:02:19 +00:00
|
|
|
ID: id,
|
|
|
|
Name: "u1",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
OrganizationService: &mock.OrganizationService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindOrganizationByIDF: func(ctx context.Context, id platform2.ID) (*platform.Organization, error) {
|
2019-01-14 15:20:20 +00:00
|
|
|
if !id.Valid() {
|
2021-03-30 18:10:02 +00:00
|
|
|
return nil, platform2.ErrInvalidID
|
2019-01-14 15:20:20 +00:00
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
return &platform.Organization{
|
|
|
|
ID: id,
|
|
|
|
Name: "o1",
|
2018-12-21 16:14:29 +00:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
args: args{
|
2018-12-28 23:02:19 +00:00
|
|
|
session: &platform.Authorization{
|
|
|
|
Token: "session-token",
|
2018-12-07 22:22:23 +00:00
|
|
|
ID: platformtesting.MustIDBase16("020f755c3c082000"),
|
|
|
|
UserID: platformtesting.MustIDBase16("aaaaaaaaaaaaaaaa"),
|
2018-12-28 23:02:19 +00:00
|
|
|
OrgID: platformtesting.MustIDBase16("020f755c3c083000"),
|
|
|
|
Description: "can write to authorization resource",
|
|
|
|
Permissions: []platform.Permission{
|
|
|
|
{
|
2019-01-15 16:09:58 +00:00
|
|
|
Action: platform.WriteAction,
|
|
|
|
Resource: platform.Resource{
|
|
|
|
Type: platform.AuthorizationsResourceType,
|
|
|
|
OrgID: platformtesting.IDPtr(platformtesting.MustIDBase16("020f755c3c083000")),
|
|
|
|
},
|
2018-12-28 23:02:19 +00:00
|
|
|
},
|
2018-12-21 16:14:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
authorization: &platform.Authorization{
|
|
|
|
ID: platformtesting.MustIDBase16("020f755c3c082000"),
|
2018-12-28 23:02:19 +00:00
|
|
|
OrgID: platformtesting.MustIDBase16("020f755c3c083000"),
|
|
|
|
Description: "only read dashboards sucka",
|
|
|
|
Permissions: []platform.Permission{
|
|
|
|
{
|
2019-01-15 16:09:58 +00:00
|
|
|
Action: platform.ReadAction,
|
|
|
|
Resource: platform.Resource{
|
|
|
|
Type: platform.DashboardsResourceType,
|
|
|
|
OrgID: platformtesting.IDPtr(platformtesting.MustIDBase16("020f755c3c083000")),
|
|
|
|
},
|
2018-12-28 23:02:19 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-21 16:14:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wants: wants{
|
2018-12-28 23:02:19 +00:00
|
|
|
statusCode: http.StatusCreated,
|
2018-12-21 16:14:29 +00:00
|
|
|
contentType: "application/json; charset=utf-8",
|
2018-12-28 23:02:19 +00:00
|
|
|
body: `
|
|
|
|
{
|
2019-11-07 14:46:30 +00:00
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z",
|
2019-01-10 21:21:59 +00:00
|
|
|
"description": "only read dashboards sucka",
|
|
|
|
"id": "020f755c3c082000",
|
2018-12-28 23:02:19 +00:00
|
|
|
"links": {
|
2019-01-10 21:21:59 +00:00
|
|
|
"self": "/api/v2/authorizations/020f755c3c082000",
|
|
|
|
"user": "/api/v2/users/aaaaaaaaaaaaaaaa"
|
2018-12-28 23:02:19 +00:00
|
|
|
},
|
|
|
|
"org": "o1",
|
2019-01-10 21:21:59 +00:00
|
|
|
"orgID": "020f755c3c083000",
|
|
|
|
"permissions": [
|
|
|
|
{
|
|
|
|
"action": "read",
|
2019-01-15 16:09:58 +00:00
|
|
|
"resource": {
|
|
|
|
"type": "dashboards",
|
|
|
|
"orgID": "020f755c3c083000",
|
|
|
|
"org": "o1"
|
|
|
|
}
|
2019-01-10 21:21:59 +00:00
|
|
|
}
|
|
|
|
],
|
2018-12-28 23:02:19 +00:00
|
|
|
"status": "active",
|
2019-01-10 21:21:59 +00:00
|
|
|
"token": "new-test-token",
|
|
|
|
"user": "u1",
|
|
|
|
"userID": "aaaaaaaaaaaaaaaa"
|
2018-12-28 23:02:19 +00:00
|
|
|
}
|
2019-01-14 15:20:20 +00:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
authorizationBackend := NewMockAuthorizationBackend(t)
|
2021-09-13 19:12:35 +00:00
|
|
|
authorizationBackend.HTTPErrorHandler = kithttp.NewErrorHandler(zaptest.NewLogger(t))
|
2019-01-16 12:26:09 +00:00
|
|
|
authorizationBackend.AuthorizationService = tt.fields.AuthorizationService
|
|
|
|
authorizationBackend.UserService = tt.fields.UserService
|
|
|
|
authorizationBackend.OrganizationService = tt.fields.OrganizationService
|
|
|
|
authorizationBackend.LookupService = tt.fields.LookupService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewAuthorizationHandler(zaptest.NewLogger(t), authorizationBackend)
|
2018-09-10 20:56:11 +00:00
|
|
|
|
2019-01-14 15:20:20 +00:00
|
|
|
req, err := newPostAuthorizationRequest(tt.args.authorization)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create new authorization request: %v", err)
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(req)
|
2018-09-10 20:56:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal authorization: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "http://any.url", bytes.NewReader(b))
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
ctx := pcontext.SetAuthorizer(context.Background(), tt.args.session)
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
|
2018-09-10 20:56:11 +00:00
|
|
|
h.handlePostAuthorization(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
2022-04-13 20:24:27 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2018-09-10 20:56:11 +00:00
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Logf("headers: %v body: %s", res.Header, body)
|
|
|
|
t.Errorf("%q. handlePostAuthorization() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Errorf("%q. handlePostAuthorization() = %v, want %v", tt.name, content, tt.wants.contentType)
|
2018-09-10 20:56:11 +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, handlePostAuthorization(). error unmarshalling json %v", tt.name, err)
|
2019-05-08 19:51:03 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. handlePostAuthorization() = ***%s***", tt.name, diff)
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_handleDeleteAuthorization(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
AuthorizationService platform.AuthorizationService
|
2018-12-28 23:02:19 +00:00
|
|
|
UserService platform.UserService
|
|
|
|
OrganizationService platform.OrganizationService
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2018-12-28 23:02:19 +00:00
|
|
|
name string
|
2018-09-10 20:56:11 +00:00
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2018-12-28 23:02:19 +00:00
|
|
|
name: "remove a authorization by id",
|
2018-09-10 20:56:11 +00:00
|
|
|
fields: fields{
|
|
|
|
&mock.AuthorizationService{
|
2021-03-30 18:10:02 +00:00
|
|
|
DeleteAuthorizationFn: func(ctx context.Context, id platform2.ID) error {
|
2018-10-10 19:39:09 +00:00
|
|
|
if id == platformtesting.MustIDBase16("020f755c3c082000") {
|
2018-09-10 20:56:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("wrong id")
|
|
|
|
},
|
|
|
|
},
|
2018-12-28 23:02:19 +00:00
|
|
|
&mock.UserService{},
|
|
|
|
&mock.OrganizationService{},
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNoContent,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-12-28 23:02:19 +00:00
|
|
|
name: "authorization not found",
|
2018-09-10 20:56:11 +00:00
|
|
|
fields: fields{
|
|
|
|
&mock.AuthorizationService{
|
2021-03-30 18:10:02 +00:00
|
|
|
DeleteAuthorizationFn: func(ctx context.Context, id platform2.ID) error {
|
|
|
|
return &errors.Error{
|
|
|
|
Code: errors.ENotFound,
|
2018-11-07 18:55:52 +00:00
|
|
|
Msg: "authorization not found",
|
|
|
|
}
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-28 23:02:19 +00:00
|
|
|
&mock.UserService{},
|
|
|
|
&mock.OrganizationService{},
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
id: "020f755c3c082000",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNotFound,
|
2018-12-28 23:02:19 +00:00
|
|
|
body: `{"code":"not found","message":"authorization not found"}`,
|
2018-09-10 20:56:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-04 23:10:23 +00:00
|
|
|
authorizationBackend := NewMockAuthorizationBackend(t)
|
2021-09-13 19:12:35 +00:00
|
|
|
authorizationBackend.HTTPErrorHandler = kithttp.NewErrorHandler(zaptest.NewLogger(t))
|
2019-01-16 12:26:09 +00:00
|
|
|
authorizationBackend.AuthorizationService = tt.fields.AuthorizationService
|
|
|
|
authorizationBackend.UserService = tt.fields.UserService
|
|
|
|
authorizationBackend.OrganizationService = tt.fields.OrganizationService
|
2019-12-04 23:10:23 +00:00
|
|
|
h := NewAuthorizationHandler(zaptest.NewLogger(t), authorizationBackend)
|
2018-09-10 20:56:11 +00:00
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "http://any.url", nil)
|
|
|
|
|
|
|
|
r = r.WithContext(context.WithValue(
|
2018-09-15 00:24:32 +00:00
|
|
|
context.Background(),
|
2018-09-10 20:56:11 +00:00
|
|
|
httprouter.ParamsKey,
|
|
|
|
httprouter.Params{
|
|
|
|
{
|
|
|
|
Key: "id",
|
|
|
|
Value: tt.args.id,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
h.handleDeleteAuthorization(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
2022-04-13 20:24:27 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2018-09-10 20:56:11 +00:00
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Errorf("%q. handleDeleteAuthorization() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Errorf("%q. handleDeleteAuthorization() = %v, want %v", tt.name, content, tt.wants.contentType)
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
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, handleDeleteAuthorization(). error unmarshalling json %v", tt.name, err)
|
2019-05-08 19:51:03 +00:00
|
|
|
} else if !eq {
|
|
|
|
t.Errorf("%q. handleDeleteAuthorization() = ***%s***", tt.name, diff)
|
|
|
|
}
|
2018-09-10 20:56:11 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 15:06:30 +00:00
|
|
|
|
2018-11-07 18:55:52 +00:00
|
|
|
func initAuthorizationService(f platformtesting.AuthorizationFields, t *testing.T) (platform.AuthorizationService, string, func()) {
|
2018-09-14 15:06:30 +00:00
|
|
|
t.Helper()
|
2020-02-13 15:27:57 +00:00
|
|
|
if t.Name() == "TestAuthorizationService_FindAuthorizations/find_authorization_by_token" {
|
2018-09-14 23:03:38 +00:00
|
|
|
/*
|
|
|
|
TODO(goller): need a secure way to communicate get
|
|
|
|
authorization by token string via headers or something
|
|
|
|
*/
|
2020-02-13 15:27:57 +00:00
|
|
|
t.Skip("TestAuthorizationService_FindAuthorizations/find_authorization_by_token skipped because user tokens cannot be queried")
|
2018-09-14 23:03:38 +00:00
|
|
|
}
|
2018-09-14 15:06:30 +00:00
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
if t.Name() == "TestAuthorizationService_CreateAuthorization/providing_a_non_existing_user_is_invalid" {
|
|
|
|
t.Skip("HTTP authorization service does not required a user id on the authentication struct. We get the user from the session token.")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
tenantStore := tenant.NewStore(store)
|
|
|
|
tenantStore.OrgIDGen = f.OrgIDGenerator
|
|
|
|
tenantService := tenant.NewService(tenantStore)
|
|
|
|
|
|
|
|
authStore, err := authorization.NewStore(store)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
authService := authorization.NewService(authStore, tenantService)
|
|
|
|
|
|
|
|
svc := kv.NewService(zaptest.NewLogger(t), store, tenantService)
|
2018-09-14 15:06:30 +00:00
|
|
|
svc.IDGenerator = f.IDGenerator
|
|
|
|
svc.TokenGenerator = f.TokenGenerator
|
2019-11-07 14:46:30 +00:00
|
|
|
svc.TimeGenerator = f.TimeGenerator
|
2019-04-09 18:24:40 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2018-12-28 23:02:19 +00:00
|
|
|
|
2018-09-14 15:06:30 +00:00
|
|
|
for _, u := range f.Users {
|
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 := tenantService.CreateUser(ctx, u); err != nil {
|
2018-09-14 15:06:30 +00:00
|
|
|
t.Fatalf("failed to populate users")
|
|
|
|
}
|
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
|
|
|
for _, o := range f.Orgs {
|
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 := tenantService.CreateOrganization(ctx, o); err != nil {
|
2018-12-28 23:02:19 +00:00
|
|
|
t.Fatalf("failed to populate orgs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var token string
|
|
|
|
|
|
|
|
for _, a := range f.Authorizations {
|
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 := authService.CreateAuthorization(ctx, a); err != nil {
|
2018-09-14 15:06:30 +00:00
|
|
|
t.Fatalf("failed to populate authorizations")
|
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
|
|
|
token = a.Token
|
2018-09-14 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 20:19:51 +00:00
|
|
|
mus := &mock.UserService{
|
2021-03-30 18:10:02 +00:00
|
|
|
FindUserByIDFn: func(ctx context.Context, id platform2.ID) (*platform.User, error) {
|
2019-09-18 20:19:51 +00:00
|
|
|
return &platform.User{}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-04 23:10:23 +00:00
|
|
|
authorizationBackend := NewMockAuthorizationBackend(t)
|
2021-09-13 19:12:35 +00:00
|
|
|
authorizationBackend.HTTPErrorHandler = kithttp.NewErrorHandler(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
|
|
|
authorizationBackend.AuthorizationService = authService
|
2019-09-18 20:19:51 +00:00
|
|
|
authorizationBackend.UserService = mus
|
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
|
|
|
authorizationBackend.OrganizationService = tenantService
|
2019-01-16 12:26:09 +00:00
|
|
|
authorizationBackend.LookupService = &mock.LookupService{
|
2021-03-30 18:10:02 +00:00
|
|
|
NameFn: func(ctx context.Context, resource platform.ResourceType, id platform2.ID) (string, error) {
|
2019-01-15 16:09:58 +00:00
|
|
|
switch resource {
|
|
|
|
case platform.BucketsResourceType:
|
|
|
|
return "b1", nil
|
|
|
|
case platform.OrgsResourceType:
|
|
|
|
return "o1", nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("bad resource type %s", resource)
|
|
|
|
},
|
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
2019-12-04 23:10:23 +00:00
|
|
|
authZ := NewAuthorizationHandler(zaptest.NewLogger(t), authorizationBackend)
|
2021-09-13 19:12:35 +00:00
|
|
|
authN := NewAuthenticationHandler(zaptest.NewLogger(t), kithttp.NewErrorHandler(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
|
|
|
authN.AuthorizationService = authService
|
2018-12-28 23:02:19 +00:00
|
|
|
authN.Handler = authZ
|
2019-09-18 20:19:51 +00:00
|
|
|
authN.UserService = mus
|
2018-12-28 23:02:19 +00:00
|
|
|
|
|
|
|
server := httptest.NewServer(authN)
|
2019-12-17 19:56:04 +00:00
|
|
|
|
|
|
|
httpClient, err := NewHTTPClient(server.URL, token, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-09-14 15:06:30 +00:00
|
|
|
}
|
2019-12-17 19:56:04 +00:00
|
|
|
|
2018-09-15 00:34:00 +00:00
|
|
|
done := server.Close
|
2018-09-14 15:06:30 +00:00
|
|
|
|
2019-12-28 00:58:57 +00:00
|
|
|
return &AuthorizationService{Client: httpClient}, "", done
|
2018-09-14 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizationService_CreateAuthorization(t *testing.T) {
|
|
|
|
platformtesting.CreateAuthorization(initAuthorizationService, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizationService_FindAuthorizationByID(t *testing.T) {
|
|
|
|
platformtesting.FindAuthorizationByID(initAuthorizationService, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizationService_FindAuthorizationByToken(t *testing.T) {
|
2018-09-14 23:03:38 +00:00
|
|
|
/*
|
|
|
|
TODO(goller): need a secure way to communicate get
|
|
|
|
authorization by token string via headers or something
|
|
|
|
*/
|
|
|
|
t.Skip()
|
2018-09-14 15:06:30 +00:00
|
|
|
platformtesting.FindAuthorizationByToken(initAuthorizationService, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizationService_FindAuthorizations(t *testing.T) {
|
2020-02-13 15:27:57 +00:00
|
|
|
platformtesting.FindAuthorizations(initAuthorizationService, t)
|
2018-09-14 15:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthorizationService_DeleteAuthorization(t *testing.T) {
|
2020-02-13 15:27:57 +00:00
|
|
|
platformtesting.DeleteAuthorization(initAuthorizationService, t)
|
2018-09-14 15:06:30 +00:00
|
|
|
}
|
2018-12-28 23:02:19 +00:00
|
|
|
|
2019-03-27 19:02:45 +00:00
|
|
|
func TestAuthorizationService_UpdateAuthorization(t *testing.T) {
|
|
|
|
platformtesting.UpdateAuthorization(initAuthorizationService, t)
|
2019-01-04 17:21:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
func MustMarshal(o interface{}) []byte {
|
|
|
|
b, _ := json.Marshal(o)
|
|
|
|
return b
|
|
|
|
}
|