influxdb/testing/organization_service.go

625 lines
13 KiB
Go
Raw Normal View History

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
package testing
import (
"bytes"
"context"
"fmt"
"sort"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/platform"
"github.com/influxdata/platform/mock"
)
var organizationCmpOptions = cmp.Options{
cmp.Comparer(func(x, y []byte) bool {
return bytes.Equal(x, y)
}),
cmp.Transformer("Sort", func(in []*platform.Organization) []*platform.Organization {
out := append([]*platform.Organization(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
}),
}
// OrganizationFields will include the IDGenerator, and organizations
type OrganizationFields struct {
IDGenerator platform.IDGenerator
Organizations []*platform.Organization
}
// CreateOrganization testing
func CreateOrganization(
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
t *testing.T,
) {
type args struct {
organization *platform.Organization
}
type wants struct {
err error
organizations []*platform.Organization
}
tests := []struct {
name string
fields OrganizationFields
args args
wants wants
}{
{
name: "create organizations with empty set",
fields: OrganizationFields{
IDGenerator: mock.NewIDGenerator("id1"),
Organizations: []*platform.Organization{},
},
args: args{
organization: &platform.Organization{
Name: "name1",
},
},
wants: wants{
organizations: []*platform.Organization{
{
Name: "name1",
ID: platform.ID("id1"),
},
},
},
},
{
name: "basic create organization",
fields: OrganizationFields{
IDGenerator: &mock.IDGenerator{
IDFn: func() platform.ID {
return platform.ID("2")
},
},
Organizations: []*platform.Organization{
{
ID: platform.ID("1"),
Name: "organization1",
},
},
},
args: args{
organization: &platform.Organization{
Name: "organization2",
},
},
wants: wants{
organizations: []*platform.Organization{
{
ID: platform.ID("1"),
Name: "organization1",
},
{
ID: platform.ID("2"),
Name: "organization2",
},
},
},
},
{
name: "names should not be unique",
fields: OrganizationFields{
IDGenerator: &mock.IDGenerator{
IDFn: func() platform.ID {
return platform.ID("2")
},
},
Organizations: []*platform.Organization{
{
ID: platform.ID("1"),
Name: "organization1",
},
},
},
args: args{
organization: &platform.Organization{
Name: "organization1",
},
},
wants: wants{
organizations: []*platform.Organization{
{
ID: platform.ID("1"),
Name: "organization1",
},
},
err: fmt.Errorf("organization with name organization1 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.CreateOrganization(ctx, tt.args.organization)
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.DeleteOrganization(ctx, tt.args.organization.ID)
organizations, _, err := s.FindOrganizations(ctx, platform.OrganizationFilter{})
if err != nil {
t.Fatalf("failed to retrieve organizations: %v", err)
}
if diff := cmp.Diff(organizations, tt.wants.organizations, organizationCmpOptions...); diff != "" {
t.Errorf("organizations are different -got/+want\ndiff %s", diff)
}
})
}
}
// FindOrganizationByID testing
func FindOrganizationByID(
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
t *testing.T,
) {
type args struct {
id platform.ID
}
type wants struct {
err error
organization *platform.Organization
}
tests := []struct {
name string
fields OrganizationFields
args args
wants wants
}{
{
name: "basic find organization by id",
fields: OrganizationFields{
Organizations: []*platform.Organization{
{
ID: platform.ID("1"),
Name: "organization1",
},
{
ID: platform.ID("2"),
Name: "organization2",
},
},
},
args: args{
id: platform.ID("2"),
},
wants: wants{
organization: &platform.Organization{
ID: platform.ID("2"),
Name: "organization2",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s, done := init(tt.fields, t)
defer done()
ctx := context.TODO()
organization, err := s.FindOrganizationByID(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(organization, tt.wants.organization, organizationCmpOptions...); diff != "" {
t.Errorf("organization is different -got/+want\ndiff %s", diff)
}
})
}
}
// FindOrganizations testing
func FindOrganizations(
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
t *testing.T,
) {
type args struct {
ID string
name string
}
type wants struct {
organizations []*platform.Organization
err error
}
tests := []struct {
name string
fields OrganizationFields
args args
wants wants
}{
{
name: "find all organizations",
fields: OrganizationFields{
Organizations: []*platform.Organization{
{
ID: platform.ID("test1"),
Name: "abc",
},
{
ID: platform.ID("test2"),
Name: "xyz",
},
},
},
args: args{},
wants: wants{
organizations: []*platform.Organization{
{
ID: platform.ID("test1"),
Name: "abc",
},
{
ID: platform.ID("test2"),
Name: "xyz",
},
},
},
},
{
name: "find organization by id",
fields: OrganizationFields{
Organizations: []*platform.Organization{
{
ID: platform.ID("test1"),
Name: "abc",
},
{
ID: platform.ID("test2"),
Name: "xyz",
},
},
},
args: args{
ID: "test2",
},
wants: wants{
organizations: []*platform.Organization{
{
ID: platform.ID("test2"),
Name: "xyz",
},
},
},
},
{
name: "find organization by name",
fields: OrganizationFields{
Organizations: []*platform.Organization{
{
ID: platform.ID("test1"),
Name: "abc",
},
{
ID: platform.ID("test2"),
Name: "xyz",
},
},
},
args: args{
name: "xyz",
},
wants: wants{
organizations: []*platform.Organization{
{
ID: platform.ID("test2"),
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.OrganizationFilter{}
if tt.args.ID != "" {
id := platform.ID(tt.args.ID)
filter.ID = &id
}
if tt.args.name != "" {
filter.Name = &tt.args.name
}
organizations, _, err := s.FindOrganizations(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(organizations, tt.wants.organizations, organizationCmpOptions...); diff != "" {
t.Errorf("organizations are different -got/+want\ndiff %s", diff)
}
})
}
}
// DeleteOrganization testing
func DeleteOrganization(
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
t *testing.T,
) {
type args struct {
ID string
}
type wants struct {
err error
organizations []*platform.Organization
}
tests := []struct {
name string
fields OrganizationFields
args args
wants wants
}{
{
name: "delete organizations using exist id",
fields: OrganizationFields{
Organizations: []*platform.Organization{
{
Name: "orgA",
ID: platform.ID("abc"),
},
{
Name: "orgB",
ID: platform.ID("xyz"),
},
},
},
args: args{
ID: "abc",
},
wants: wants{
organizations: []*platform.Organization{
{
Name: "orgB",
ID: platform.ID("xyz"),
},
},
},
},
{
name: "delete organizations using id that does not exist",
fields: OrganizationFields{
Organizations: []*platform.Organization{
{
Name: "orgA",
ID: platform.ID("abc"),
},
{
Name: "orgB",
ID: platform.ID("xyz"),
},
},
},
args: args{
ID: "123",
},
wants: wants{
err: fmt.Errorf("organization not found"),
organizations: []*platform.Organization{
{
Name: "orgA",
ID: platform.ID("abc"),
},
{
Name: "orgB",
ID: platform.ID("xyz"),
},
},
},
},
}
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.DeleteOrganization(ctx, platform.ID(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.OrganizationFilter{}
organizations, _, err := s.FindOrganizations(ctx, filter)
if err != nil {
t.Fatalf("failed to retrieve organizations: %v", err)
}
if diff := cmp.Diff(organizations, tt.wants.organizations, organizationCmpOptions...); diff != "" {
t.Errorf("organizations are different -got/+want\ndiff %s", diff)
}
})
}
}
// FindOrganization testing
func FindOrganization(
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
t *testing.T,
) {
type args struct {
name string
}
type wants struct {
organization *platform.Organization
err error
}
tests := []struct {
name string
fields OrganizationFields
args args
wants wants
}{
{
name: "find organization by name",
fields: OrganizationFields{
Organizations: []*platform.Organization{
{
ID: platform.ID("a"),
Name: "abc",
},
{
ID: platform.ID("b"),
Name: "xyz",
},
},
},
args: args{
name: "abc",
},
wants: wants{
organization: &platform.Organization{
ID: platform.ID("a"),
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.OrganizationFilter{}
if tt.args.name != "" {
filter.Name = &tt.args.name
}
organization, err := s.FindOrganization(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(organization, tt.wants.organization, organizationCmpOptions...); diff != "" {
t.Errorf("organizations are different -got/+want\ndiff %s", diff)
}
})
}
}
// UpdateOrganization testing
func UpdateOrganization(
init func(OrganizationFields, *testing.T) (platform.OrganizationService, func()),
t *testing.T,
) {
type args struct {
name string
id platform.ID
}
type wants struct {
err error
organization *platform.Organization
}
tests := []struct {
name string
fields OrganizationFields
args args
wants wants
}{
{
name: "update name",
fields: OrganizationFields{
Organizations: []*platform.Organization{
{
ID: platform.ID("1"),
Name: "organization1",
},
{
ID: platform.ID("2"),
Name: "organization2",
},
},
},
args: args{
id: platform.ID("1"),
name: "changed",
},
wants: wants{
organization: &platform.Organization{
ID: platform.ID("1"),
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()
upd := platform.OrganizationUpdate{}
if tt.args.name != "" {
upd.Name = &tt.args.name
}
organization, err := s.UpdateOrganization(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(organization, tt.wants.organization, organizationCmpOptions...); diff != "" {
t.Errorf("organization is different -got/+want\ndiff %s", diff)
}
})
}
}