influxdb/testing/user_service.go

671 lines
13 KiB
Go
Raw Normal View History

package testing
import (
"bytes"
"context"
"fmt"
"sort"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/platform"
"github.com/influxdata/platform/mock"
)
const (
userOneID = "020f755c3c082000"
userTwoID = "020f755c3c082001"
userThreeID = "020f755c3c082002"
)
var userCmpOptions = cmp.Options{
cmp.Comparer(func(x, y []byte) bool {
return bytes.Equal(x, y)
}),
cmp.Transformer("Sort", func(in []*platform.User) []*platform.User {
out := append([]*platform.User(nil), in...) // Copy input to avoid mutating it
sort.Slice(out, func(i, j int) bool {
return out[i].ID.String() > out[j].ID.String()
})
return out
}),
}
// UserFields will include the IDGenerator, and users
type UserFields struct {
IDGenerator platform.IDGenerator
Users []*platform.User
}
// UserService tests all the service functions.
func UserService(
init func(UserFields, *testing.T) (platform.UserService, func()), t *testing.T,
) {
tests := []struct {
name string
fn func(init func(UserFields, *testing.T) (platform.UserService, func()),
t *testing.T)
}{
{
name: "CreateUser",
fn: CreateUser,
},
{
name: "FindUserByID",
fn: FindUserByID,
},
{
name: "FindUsers",
fn: FindUsers,
},
{
name: "DeleteUser",
fn: DeleteUser,
},
{
name: "FindUser",
fn: FindUser,
},
{
name: "UpdateUser",
fn: UpdateUser,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.fn(init, t)
})
}
}
// CreateUser testing
func CreateUser(
init func(UserFields, *testing.T) (platform.UserService, func()),
t *testing.T,
) {
type args struct {
user *platform.User
}
type wants struct {
err error
users []*platform.User
}
tests := []struct {
name string
fields UserFields
args args
wants wants
}{
{
name: "create users with empty set",
fields: UserFields{
IDGenerator: mock.NewIDGenerator(userOneID, t),
Users: []*platform.User{},
},
args: args{
user: &platform.User{
Name: "name1",
},
},
wants: wants{
users: []*platform.User{
{
Name: "name1",
ID: MustIDFromString(userOneID),
},
},
},
},
{
name: "basic create user",
fields: UserFields{
IDGenerator: mock.NewIDGenerator(userTwoID, t),
Users: []*platform.User{
{
ID: MustIDFromString(userOneID),
Name: "user1",
},
},
},
args: args{
user: &platform.User{
Name: "user2",
},
},
wants: wants{
users: []*platform.User{
{
ID: MustIDFromString(userOneID),
Name: "user1",
},
{
ID: MustIDFromString(userTwoID),
Name: "user2",
},
},
},
},
{
name: "names should be unique",
fields: UserFields{
IDGenerator: &mock.IDGenerator{
IDFn: func() platform.ID {
return MustIDFromString(userOneID)
},
},
Users: []*platform.User{
{
ID: MustIDFromString(userOneID),
Name: "user1",
},
},
},
args: args{
user: &platform.User{
Name: "user1",
},
},
wants: wants{
users: []*platform.User{
{
ID: MustIDFromString(userOneID),
Name: "user1",
},
},
err: fmt.Errorf("user with name user1 already exists"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.TODO()
err := s.CreateUser(ctx, tt.args.user)
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())
}
}
// Delete only created users - ie., having a not nil ID
if tt.args.user.ID.Valid() {
defer s.DeleteUser(ctx, tt.args.user.ID)
}
users, _, err := s.FindUsers(ctx, platform.UserFilter{})
if err != nil {
t.Fatalf("failed to retrieve users: %v", err)
}
if diff := cmp.Diff(users, tt.wants.users, userCmpOptions...); diff != "" {
t.Errorf("users are different -got/+want\ndiff %s", diff)
}
})
}
}
// FindUserByID testing
func FindUserByID(
init func(UserFields, *testing.T) (platform.UserService, func()),
t *testing.T,
) {
type args struct {
id platform.ID
}
type wants struct {
err error
user *platform.User
}
tests := []struct {
name string
fields UserFields
args args
wants wants
}{
{
name: "basic find user by id",
fields: UserFields{
Users: []*platform.User{
{
ID: MustIDFromString(userOneID),
Name: "user1",
},
{
ID: MustIDFromString(userTwoID),
Name: "user2",
},
},
},
args: args{
id: MustIDFromString(userTwoID),
},
wants: wants{
user: &platform.User{
ID: MustIDFromString(userTwoID),
Name: "user2",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.TODO()
user, err := s.FindUserByID(ctx, tt.args.id)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected errors to be equal '%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 '%v' got '%v'", tt.wants.err, err)
}
}
if diff := cmp.Diff(user, tt.wants.user, userCmpOptions...); diff != "" {
t.Errorf("user is different -got/+want\ndiff %s", diff)
}
})
}
}
// FindUsers testing
func FindUsers(
init func(UserFields, *testing.T) (platform.UserService, func()),
t *testing.T,
) {
type args struct {
ID platform.ID
name string
}
type wants struct {
users []*platform.User
err error
}
tests := []struct {
name string
fields UserFields
args args
wants wants
}{
{
name: "find all users",
fields: UserFields{
Users: []*platform.User{
{
ID: MustIDFromString(userOneID),
Name: "abc",
},
{
ID: MustIDFromString(userTwoID),
Name: "xyz",
},
},
},
args: args{},
wants: wants{
users: []*platform.User{
{
ID: MustIDFromString(userOneID),
Name: "abc",
},
{
ID: MustIDFromString(userTwoID),
Name: "xyz",
},
},
},
},
{
name: "find user by id",
fields: UserFields{
Users: []*platform.User{
{
ID: MustIDFromString(userOneID),
Name: "abc",
},
{
ID: MustIDFromString(userTwoID),
Name: "xyz",
},
},
},
args: args{
ID: MustIDFromString(userTwoID),
},
wants: wants{
users: []*platform.User{
{
ID: MustIDFromString(userTwoID),
Name: "xyz",
},
},
},
},
{
name: "find user by name",
fields: UserFields{
Users: []*platform.User{
{
ID: MustIDFromString(userOneID),
Name: "abc",
},
{
ID: MustIDFromString(userTwoID),
Name: "xyz",
},
},
},
args: args{
name: "xyz",
},
wants: wants{
users: []*platform.User{
{
ID: MustIDFromString(userTwoID),
Name: "xyz",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.TODO()
filter := platform.UserFilter{}
if tt.args.ID.Valid() {
filter.ID = &tt.args.ID
}
if tt.args.name != "" {
filter.Name = &tt.args.name
}
users, _, err := s.FindUsers(ctx, filter)
if (err != nil) != (tt.wants.err != nil) {
t.Fatalf("expected errors to be equal '%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 '%v' got '%v'", tt.wants.err, err)
}
}
if diff := cmp.Diff(users, tt.wants.users, userCmpOptions...); diff != "" {
t.Errorf("users are different -got/+want\ndiff %s", diff)
}
})
}
}
// DeleteUser testing
func DeleteUser(
init func(UserFields, *testing.T) (platform.UserService, func()),
t *testing.T,
) {
type args struct {
ID platform.ID
}
type wants struct {
err error
users []*platform.User
}
tests := []struct {
name string
fields UserFields
args args
wants wants
}{
{
name: "delete users using exist id",
fields: UserFields{
Users: []*platform.User{
{
Name: "orgA",
ID: MustIDFromString(userOneID),
},
{
Name: "orgB",
ID: MustIDFromString(userTwoID),
},
},
},
args: args{
ID: MustIDFromString(userOneID),
},
wants: wants{
users: []*platform.User{
{
Name: "orgB",
ID: MustIDFromString(userTwoID),
},
},
},
},
{
feat(platform): add boltdb implementation of services feat(platform): add id to authorization feat(platform): add user arg to CreateAuthorization method on auth svc migrate(platform): move idp command to platform directory This comit did not move the ifql command as it depends on the query service which has yet to be migrated. feat(platform): add optional user name to authorization struct feat(platform): add organization name to bucket struct Additionally allow filtering buckets by organization name. feat(prom): ensure that prom auth svc implement base interface feat(prometheus): add user to create authorization method feat(prom): drop user string from create authorization feat(zap): ensure that zap auth svc implements base service interface feat(zap): add user to create authorization method feat(zap): drop user string from create authorization feat(http): add ids to authorization service feat(http): ensure that http authoriztaion service implements auth svc interface feat(http): use authorization ids in authorization handler squash(http): add check for http status accepted in authorization service feat(http): clean up authorization service and handlers feat(http): drop user string from create authorization fix(http): normalize the http authorization service feat(http): normalize bucket service and handler methods Additonally, we added support for DELETE bucket feat(http): add delete user handler Additionally, there was a bit of general cleanup feat(http): add delete route for organization handler and service Did a bit of additional cleanup of the http code. test(testing): add service conformance tests test(testing): add organization service conformance tests test(testing): add conformance test for orgs service Additionally, there was a bit of cleanup in the users service tests test(testing): add conformance test for authorizations service test(testing): update auth tests to validate that user exists test(testing): update authorization conformance tests with user name test(testing): update bucket conformance tests to include organizations feat(bolt): add bolt implementation services feat(bolt): add bolt implementation of organization service feat(bolt): add bolt implementation of users service feat(bolt): add bolt implementation of authorization service feat(bolt): add user to create authorization method feat(bolt): drop user string from create authorization fix(bolt): set user name on authorization after put feat(bolt): update bucket servie to include organizations feat(bolt): add dependent destroy of resources feat(cmd/idpd): use bolt services in platform server feat(cmd/idpd): use bolt organization service in platform server feat(cmd/idpd): use bolt users service in plaform server feat(cmd/idpd): use bolt client as authorization service feat(cmd/idp): show user name in output of auth sub command feat(cmd/idp): clean up bucket subcommand of idp command fix(cmd/idp): normalize idp command output for users fix(cmd/idp): normalize auth subcommand output feat(cmd/idp): add support for delete organiztion command migrate(idp): move ifql subcommand of idp to platform
2018-05-16 18:59:35 +00:00
name: "delete users using id that does not exist",
fields: UserFields{
Users: []*platform.User{
{
Name: "orgA",
ID: MustIDFromString(userOneID),
},
{
Name: "orgB",
ID: MustIDFromString(userTwoID),
},
},
},
args: args{
ID: MustIDFromString(userThreeID),
},
wants: wants{
feat(platform): add boltdb implementation of services feat(platform): add id to authorization feat(platform): add user arg to CreateAuthorization method on auth svc migrate(platform): move idp command to platform directory This comit did not move the ifql command as it depends on the query service which has yet to be migrated. feat(platform): add optional user name to authorization struct feat(platform): add organization name to bucket struct Additionally allow filtering buckets by organization name. feat(prom): ensure that prom auth svc implement base interface feat(prometheus): add user to create authorization method feat(prom): drop user string from create authorization feat(zap): ensure that zap auth svc implements base service interface feat(zap): add user to create authorization method feat(zap): drop user string from create authorization feat(http): add ids to authorization service feat(http): ensure that http authoriztaion service implements auth svc interface feat(http): use authorization ids in authorization handler squash(http): add check for http status accepted in authorization service feat(http): clean up authorization service and handlers feat(http): drop user string from create authorization fix(http): normalize the http authorization service feat(http): normalize bucket service and handler methods Additonally, we added support for DELETE bucket feat(http): add delete user handler Additionally, there was a bit of general cleanup feat(http): add delete route for organization handler and service Did a bit of additional cleanup of the http code. test(testing): add service conformance tests test(testing): add organization service conformance tests test(testing): add conformance test for orgs service Additionally, there was a bit of cleanup in the users service tests test(testing): add conformance test for authorizations service test(testing): update auth tests to validate that user exists test(testing): update authorization conformance tests with user name test(testing): update bucket conformance tests to include organizations feat(bolt): add bolt implementation services feat(bolt): add bolt implementation of organization service feat(bolt): add bolt implementation of users service feat(bolt): add bolt implementation of authorization service feat(bolt): add user to create authorization method feat(bolt): drop user string from create authorization fix(bolt): set user name on authorization after put feat(bolt): update bucket servie to include organizations feat(bolt): add dependent destroy of resources feat(cmd/idpd): use bolt services in platform server feat(cmd/idpd): use bolt organization service in platform server feat(cmd/idpd): use bolt users service in plaform server feat(cmd/idpd): use bolt client as authorization service feat(cmd/idp): show user name in output of auth sub command feat(cmd/idp): clean up bucket subcommand of idp command fix(cmd/idp): normalize idp command output for users fix(cmd/idp): normalize auth subcommand output feat(cmd/idp): add support for delete organiztion command migrate(idp): move ifql subcommand of idp to platform
2018-05-16 18:59:35 +00:00
err: fmt.Errorf("user not found"),
users: []*platform.User{
{
Name: "orgA",
ID: MustIDFromString(userOneID),
},
{
Name: "orgB",
ID: MustIDFromString(userTwoID),
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.TODO()
err := s.DeleteUser(ctx, tt.args.ID)
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())
}
}
filter := platform.UserFilter{}
users, _, err := s.FindUsers(ctx, filter)
if err != nil {
t.Fatalf("failed to retrieve users: %v", err)
}
if diff := cmp.Diff(users, tt.wants.users, userCmpOptions...); diff != "" {
t.Errorf("users are different -got/+want\ndiff %s", diff)
}
})
}
}
// FindUser testing
func FindUser(
init func(UserFields, *testing.T) (platform.UserService, func()),
t *testing.T,
) {
type args struct {
name string
}
type wants struct {
user *platform.User
err error
}
tests := []struct {
name string
fields UserFields
args args
wants wants
}{
{
name: "find user by name",
fields: UserFields{
Users: []*platform.User{
{
ID: MustIDFromString(userOneID),
Name: "abc",
},
{
ID: MustIDFromString(userTwoID),
Name: "xyz",
},
},
},
args: args{
name: "abc",
},
wants: wants{
user: &platform.User{
ID: MustIDFromString(userOneID),
Name: "abc",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.TODO()
filter := platform.UserFilter{}
if tt.args.name != "" {
filter.Name = &tt.args.name
}
user, err := s.FindUser(ctx, 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(user, tt.wants.user, userCmpOptions...); diff != "" {
t.Errorf("users are different -got/+want\ndiff %s", diff)
}
})
}
}
// UpdateUser testing
func UpdateUser(
init func(UserFields, *testing.T) (platform.UserService, func()),
t *testing.T,
) {
type args struct {
name string
id platform.ID
}
type wants struct {
err error
user *platform.User
}
tests := []struct {
name string
fields UserFields
args args
wants wants
}{
{
feat(platform): add boltdb implementation of services feat(platform): add id to authorization feat(platform): add user arg to CreateAuthorization method on auth svc migrate(platform): move idp command to platform directory This comit did not move the ifql command as it depends on the query service which has yet to be migrated. feat(platform): add optional user name to authorization struct feat(platform): add organization name to bucket struct Additionally allow filtering buckets by organization name. feat(prom): ensure that prom auth svc implement base interface feat(prometheus): add user to create authorization method feat(prom): drop user string from create authorization feat(zap): ensure that zap auth svc implements base service interface feat(zap): add user to create authorization method feat(zap): drop user string from create authorization feat(http): add ids to authorization service feat(http): ensure that http authoriztaion service implements auth svc interface feat(http): use authorization ids in authorization handler squash(http): add check for http status accepted in authorization service feat(http): clean up authorization service and handlers feat(http): drop user string from create authorization fix(http): normalize the http authorization service feat(http): normalize bucket service and handler methods Additonally, we added support for DELETE bucket feat(http): add delete user handler Additionally, there was a bit of general cleanup feat(http): add delete route for organization handler and service Did a bit of additional cleanup of the http code. test(testing): add service conformance tests test(testing): add organization service conformance tests test(testing): add conformance test for orgs service Additionally, there was a bit of cleanup in the users service tests test(testing): add conformance test for authorizations service test(testing): update auth tests to validate that user exists test(testing): update authorization conformance tests with user name test(testing): update bucket conformance tests to include organizations feat(bolt): add bolt implementation services feat(bolt): add bolt implementation of organization service feat(bolt): add bolt implementation of users service feat(bolt): add bolt implementation of authorization service feat(bolt): add user to create authorization method feat(bolt): drop user string from create authorization fix(bolt): set user name on authorization after put feat(bolt): update bucket servie to include organizations feat(bolt): add dependent destroy of resources feat(cmd/idpd): use bolt services in platform server feat(cmd/idpd): use bolt organization service in platform server feat(cmd/idpd): use bolt users service in plaform server feat(cmd/idpd): use bolt client as authorization service feat(cmd/idp): show user name in output of auth sub command feat(cmd/idp): clean up bucket subcommand of idp command fix(cmd/idp): normalize idp command output for users fix(cmd/idp): normalize auth subcommand output feat(cmd/idp): add support for delete organiztion command migrate(idp): move ifql subcommand of idp to platform
2018-05-16 18:59:35 +00:00
name: "update name",
fields: UserFields{
Users: []*platform.User{
{
ID: MustIDFromString(userOneID),
Name: "user1",
},
{
ID: MustIDFromString(userTwoID),
Name: "user2",
},
},
},
args: args{
id: MustIDFromString(userOneID),
name: "changed",
},
wants: wants{
user: &platform.User{
ID: MustIDFromString(userOneID),
Name: "changed",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.TODO()
feat(platform): add boltdb implementation of services feat(platform): add id to authorization feat(platform): add user arg to CreateAuthorization method on auth svc migrate(platform): move idp command to platform directory This comit did not move the ifql command as it depends on the query service which has yet to be migrated. feat(platform): add optional user name to authorization struct feat(platform): add organization name to bucket struct Additionally allow filtering buckets by organization name. feat(prom): ensure that prom auth svc implement base interface feat(prometheus): add user to create authorization method feat(prom): drop user string from create authorization feat(zap): ensure that zap auth svc implements base service interface feat(zap): add user to create authorization method feat(zap): drop user string from create authorization feat(http): add ids to authorization service feat(http): ensure that http authoriztaion service implements auth svc interface feat(http): use authorization ids in authorization handler squash(http): add check for http status accepted in authorization service feat(http): clean up authorization service and handlers feat(http): drop user string from create authorization fix(http): normalize the http authorization service feat(http): normalize bucket service and handler methods Additonally, we added support for DELETE bucket feat(http): add delete user handler Additionally, there was a bit of general cleanup feat(http): add delete route for organization handler and service Did a bit of additional cleanup of the http code. test(testing): add service conformance tests test(testing): add organization service conformance tests test(testing): add conformance test for orgs service Additionally, there was a bit of cleanup in the users service tests test(testing): add conformance test for authorizations service test(testing): update auth tests to validate that user exists test(testing): update authorization conformance tests with user name test(testing): update bucket conformance tests to include organizations feat(bolt): add bolt implementation services feat(bolt): add bolt implementation of organization service feat(bolt): add bolt implementation of users service feat(bolt): add bolt implementation of authorization service feat(bolt): add user to create authorization method feat(bolt): drop user string from create authorization fix(bolt): set user name on authorization after put feat(bolt): update bucket servie to include organizations feat(bolt): add dependent destroy of resources feat(cmd/idpd): use bolt services in platform server feat(cmd/idpd): use bolt organization service in platform server feat(cmd/idpd): use bolt users service in plaform server feat(cmd/idpd): use bolt client as authorization service feat(cmd/idp): show user name in output of auth sub command feat(cmd/idp): clean up bucket subcommand of idp command fix(cmd/idp): normalize idp command output for users fix(cmd/idp): normalize auth subcommand output feat(cmd/idp): add support for delete organiztion command migrate(idp): move ifql subcommand of idp to platform
2018-05-16 18:59:35 +00:00
upd := platform.UserUpdate{}
if tt.args.name != "" {
upd.Name = &tt.args.name
}
user, err := s.UpdateUser(ctx, tt.args.id, upd)
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(user, tt.wants.user, userCmpOptions...); diff != "" {
t.Errorf("user is different -got/+want\ndiff %s", diff)
}
})
}
}