influxdb/testing/user_resource_mapping_servi...

555 lines
14 KiB
Go
Raw Permalink Normal View History

package testing
import (
"bytes"
"context"
"fmt"
"sort"
"testing"
"github.com/google/go-cmp/cmp"
platform "github.com/influxdata/influxdb/v2"
platform2 "github.com/influxdata/influxdb/v2/kit/platform"
)
var mappingCmpOptions = cmp.Options{
cmp.Comparer(func(x, y []byte) bool {
return bytes.Equal(x, y)
}),
cmp.Transformer("Sort", func(in []*platform.UserResourceMapping) []*platform.UserResourceMapping {
out := append([]*platform.UserResourceMapping(nil), in...) // Copy input to avoid mutating it
sort.Slice(out, func(i, j int) bool {
return out[i].ResourceID.String() > out[j].ResourceID.String()
})
return out
}),
}
// UserResourceFields includes prepopulated data for mapping tests
type UserResourceFields struct {
Organizations []*platform.Organization
Users []*platform.User
Buckets []*platform.Bucket
UserResourceMappings []*platform.UserResourceMapping
}
type userResourceMappingServiceF func(
init func(UserResourceFields, *testing.T) (platform.UserResourceMappingService, func()),
t *testing.T,
)
// UserResourceMappingService tests all the service functions.
func UserResourceMappingService(
init func(UserResourceFields, *testing.T) (platform.UserResourceMappingService, func()),
t *testing.T,
) {
tests := []struct {
name string
fn userResourceMappingServiceF
}{
{
name: "CreateUserResourceMapping",
fn: CreateUserResourceMapping,
},
{
name: "FindUserResourceMappings",
fn: FindUserResourceMappings,
},
{
name: "DeleteUserResourceMapping",
fn: DeleteUserResourceMapping,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt := tt
t.Parallel()
tt.fn(init, t)
})
}
}
// baseUserResourceFields creates base fields to create URMs.
// Users for URMs must exist in order not to fail on creation.
func baseUserResourceFields() UserResourceFields {
return UserResourceFields{
Users: []*platform.User{
{
Name: "user1",
ID: MustIDBase16(userOneID),
},
{
Name: "user2",
ID: MustIDBase16(userTwoID),
},
},
}
}
func CreateUserResourceMapping(
init func(UserResourceFields, *testing.T) (platform.UserResourceMappingService, func()),
t *testing.T,
) {
type args struct {
mapping *platform.UserResourceMapping
}
type wants struct {
err error
mappings []*platform.UserResourceMapping
}
tests := []struct {
name string
fields UserResourceFields
args args
wants wants
}{
{
name: "basic create user resource mapping",
fields: func() UserResourceFields {
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
}
return f
}(),
args: args{
mapping: &platform.UserResourceMapping{
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
},
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
},
},
},
{
name: "duplicate mappings are not allowed",
fields: func() UserResourceFields {
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
}
return f
}(),
args: args{
mapping: &platform.UserResourceMapping{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
},
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
},
refactor: http error serialization matches the new error schema (#15196) The http error schema has been changed to simplify the outward facing API. The `op` and `error` attributes have been dropped because they confused people. The `error` attribute will likely be readded in some form in the future, but only as additional context and will not be required or even suggested for the UI to use. Errors are now output differently both when they are serialized to JSON and when they are output as strings. The `op` is no longer used if it is present. It will only appear as an optional attribute if at all. The `message` attribute for an error is always output and it will be the prefix for any nested error. When this is serialized to JSON, the message is automatically flattened so a nested error such as: influxdb.Error{ Msg: errors.New("something bad happened"), Err: io.EOF, } This would be written to the message as: something bad happened: EOF This matches a developers expectations much more easily as most programmers assume that wrapping an error will act as a prefix for the inner error. This is flattened when written out to HTTP in order to make this logic immaterial to a frontend developer. The code is still present and plays an important role in categorizing the error type. On the other hand, the code will not be output as part of the message as it commonly plays a redundant and confusing role when humans read it. The human readable message usually gives more context and a message like with the code acting as a prefix is generally not desired. But, the code plays a very important role in helping to identify categories of errors and so it is very important as part of the return response.
2019-09-19 15:06:47 +00:00
//lint:ignore ST1005 Error is capitalized in the tested code.
err: fmt.Errorf("Unexpected error when assigning user to a resource: mapping for user %s already exists", userOneID),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.Background()
err := s.CreateUserResourceMapping(ctx, tt.args.mapping)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
}
}
defer s.DeleteUserResourceMapping(ctx, tt.args.mapping.ResourceID, tt.args.mapping.UserID)
mappings, _, err := s.FindUserResourceMappings(ctx, platform.UserResourceMappingFilter{})
if err != nil {
t.Fatalf("failed to retrieve mappings: %v", err)
}
if diff := cmp.Diff(mappings, tt.wants.mappings, mappingCmpOptions...); diff != "" {
t.Errorf("mappings are different -got/+want\ndiff %s", diff)
}
})
}
}
func DeleteUserResourceMapping(
init func(UserResourceFields, *testing.T) (platform.UserResourceMappingService, func()),
t *testing.T,
) {
type args struct {
resourceID platform2.ID
userID platform2.ID
}
type wants struct {
err error
mappings []*platform.UserResourceMapping
}
tests := []struct {
name string
fields UserResourceFields
args args
wants wants
}{
{
name: "basic delete user resource mapping",
fields: func() UserResourceFields {
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
}
return f
}(),
args: args{
resourceID: idOne,
userID: MustIDBase16(userOneID),
},
wants: wants{
mappings: []*platform.UserResourceMapping{},
},
},
{
2019-04-17 20:30:22 +00:00
name: "deleting a non-existent user",
fields: UserResourceFields{
UserResourceMappings: []*platform.UserResourceMapping{},
},
args: args{
resourceID: idOne,
userID: MustIDBase16(userOneID),
},
wants: wants{
mappings: []*platform.UserResourceMapping{},
refactor: http error serialization matches the new error schema (#15196) The http error schema has been changed to simplify the outward facing API. The `op` and `error` attributes have been dropped because they confused people. The `error` attribute will likely be readded in some form in the future, but only as additional context and will not be required or even suggested for the UI to use. Errors are now output differently both when they are serialized to JSON and when they are output as strings. The `op` is no longer used if it is present. It will only appear as an optional attribute if at all. The `message` attribute for an error is always output and it will be the prefix for any nested error. When this is serialized to JSON, the message is automatically flattened so a nested error such as: influxdb.Error{ Msg: errors.New("something bad happened"), Err: io.EOF, } This would be written to the message as: something bad happened: EOF This matches a developers expectations much more easily as most programmers assume that wrapping an error will act as a prefix for the inner error. This is flattened when written out to HTTP in order to make this logic immaterial to a frontend developer. The code is still present and plays an important role in categorizing the error type. On the other hand, the code will not be output as part of the message as it commonly plays a redundant and confusing role when humans read it. The human readable message usually gives more context and a message like with the code acting as a prefix is generally not desired. But, the code plays a very important role in helping to identify categories of errors and so it is very important as part of the return response.
2019-09-19 15:06:47 +00:00
err: fmt.Errorf("user to resource mapping not found"),
},
},
{
name: "delete user resource mapping for org",
fields: UserResourceFields{
Organizations: []*platform.Organization{
{
ID: idOne,
Name: "organization1",
},
},
Users: []*platform.User{
{
ID: MustIDBase16(userOneID),
Name: "user1",
},
},
Buckets: []*platform.Bucket{
{
ID: idOne,
Name: "bucket1",
OrgID: idOne,
},
},
UserResourceMappings: []*platform.UserResourceMapping{
{
ResourceID: idOne,
ResourceType: platform.OrgsResourceType,
MappingType: platform.UserMappingType,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
},
},
},
args: args{
resourceID: idOne,
userID: MustIDBase16(userOneID),
},
wants: wants{
mappings: []*platform.UserResourceMapping{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.Background()
err := s.DeleteUserResourceMapping(ctx, tt.args.resourceID, tt.args.userID)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
}
}
mappings, _, err := s.FindUserResourceMappings(ctx, platform.UserResourceMappingFilter{})
if err != nil {
t.Fatalf("failed to retrieve mappings: %v", err)
}
if diff := cmp.Diff(mappings, tt.wants.mappings, mappingCmpOptions...); diff != "" {
t.Errorf("mappings are different -got/+want\ndiff %s", diff)
}
})
}
}
func FindUserResourceMappings(
init func(UserResourceFields, *testing.T) (platform.UserResourceMappingService, func()),
t *testing.T,
) {
type args struct {
filter platform.UserResourceMappingFilter
}
type wants struct {
err error
mappings []*platform.UserResourceMapping
}
tests := []struct {
name string
fields UserResourceFields
args args
wants wants
}{
{
name: "basic find mappings",
fields: func() UserResourceFields {
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
}
return f
}(),
args: args{
filter: platform.UserResourceMappingFilter{},
},
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
},
},
},
{
name: "find mappings filtered by user",
fields: func() UserResourceFields {
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
}
return f
}(),
args: args{
filter: platform.UserResourceMappingFilter{
UserID: MustIDBase16(userOneID),
},
},
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
},
},
},
{
name: "find mappings filtered by resource",
fields: func() UserResourceFields {
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
}
return f
}(),
args: args{
filter: platform.UserResourceMappingFilter{
ResourceID: idOne,
},
},
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
},
},
},
{
2018-10-04 21:08:31 +00:00
name: "find mappings filtered by user type",
fields: func() UserResourceFields {
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
{
ResourceID: idOne,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
},
{
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Owner,
ResourceType: platform.BucketsResourceType,
},
}
return f
}(),
args: args{
filter: platform.UserResourceMappingFilter{
UserType: platform.Owner,
},
},
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Owner,
ResourceType: platform.BucketsResourceType,
},
},
},
},
2018-10-04 21:08:31 +00:00
{
name: "find mappings filtered by resource type",
fields: func() UserResourceFields {
f := baseUserResourceFields()
f.UserResourceMappings = []*platform.UserResourceMapping{
2018-10-04 21:08:31 +00:00
{
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
2018-10-04 21:08:31 +00:00
},
}
return f
}(),
2018-10-04 21:08:31 +00:00
args: args{
filter: platform.UserResourceMappingFilter{
ResourceType: platform.BucketsResourceType,
2018-10-04 21:08:31 +00:00
},
},
wants: wants{
mappings: []*platform.UserResourceMapping{
{
ResourceID: idTwo,
UserID: MustIDBase16(userTwoID),
UserType: platform.Member,
ResourceType: platform.BucketsResourceType,
2018-10-04 21:08:31 +00:00
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.Background()
mappings, _, err := s.FindUserResourceMappings(ctx, tt.args.filter)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected error '%v' got '%v'", tt.wants.err, err)
}
if err != nil && tt.wants.err != nil {
if err.Error() != tt.wants.err.Error() {
t.Fatalf("expected error messages to match '%v' got '%v'", tt.wants.err, err.Error())
}
}
if diff := cmp.Diff(mappings, tt.wants.mappings, mappingCmpOptions...); diff != "" {
t.Errorf("mappings are different -got/+want\ndiff %s", diff)
}
})
}
}