From de708f3e989bfd5481f0c1b4238eb940a93198f4 Mon Sep 17 00:00:00 2001 From: Kelvin Wang Date: Thu, 3 Jan 2019 15:20:13 -0500 Subject: [PATCH] fix(bolt): check org id exists before create bucket --- bolt/bucket.go | 19 +++++++++++++---- inmem/bucket_service.go | 19 +++++++++++++---- testing/bucket_service.go | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/bolt/bucket.go b/bolt/bucket.go index 12567fc5ea6..0a837c38426 100644 --- a/bolt/bucket.go +++ b/bolt/bucket.go @@ -306,10 +306,21 @@ func (c *Client) CreateBucket(ctx context.Context, b *platform.Bucket) error { var err error op := getOp(platform.OpCreateBucket) return c.db.Update(func(tx *bolt.Tx) error { - if !b.OrganizationID.Valid() { - o, err := c.findOrganizationByName(ctx, tx, b.Organization) - if err != nil { - return err + if b.OrganizationID.Valid() { + _, pe := c.findOrganizationByID(ctx, tx, b.OrganizationID) + if pe != nil { + return &platform.Error{ + Err: pe, + Op: op, + } + } + } else { + o, pe := c.findOrganizationByName(ctx, tx, b.Organization) + if pe != nil { + return &platform.Error{ + Err: pe, + Op: op, + } } b.OrganizationID = o.ID } diff --git a/inmem/bucket_service.go b/inmem/bucket_service.go index 03c3bcf7dac..079137c503c 100644 --- a/inmem/bucket_service.go +++ b/inmem/bucket_service.go @@ -202,10 +202,21 @@ func (s *Service) FindBuckets(ctx context.Context, filter platform.BucketFilter, // CreateBucket creates a new bucket and sets b.ID with the new identifier. func (s *Service) CreateBucket(ctx context.Context, b *platform.Bucket) error { - if !b.OrganizationID.Valid() { - o, err := s.findOrganizationByName(ctx, b.Organization) - if err != nil { - return err + if b.OrganizationID.Valid() { + _, pe := s.FindOrganizationByID(ctx, b.OrganizationID) + if pe != nil { + return &platform.Error{ + Err: pe, + Op: OpPrefix + platform.OpCreateBucket, + } + } + } else { + o, pe := s.findOrganizationByName(ctx, b.Organization) + if pe != nil { + return &platform.Error{ + Err: pe, + Op: OpPrefix + platform.OpCreateBucket, + } } b.OrganizationID = o.ID } diff --git a/testing/bucket_service.go b/testing/bucket_service.go index 9711acc4ee8..2172ebe2830 100644 --- a/testing/bucket_service.go +++ b/testing/bucket_service.go @@ -328,6 +328,50 @@ func CreateBucket( }, }, }, + { + name: "create bucket with orgID not exist", + fields: BucketFields{ + IDGenerator: mock.NewIDGenerator(bucketOneID, t), + Buckets: []*platform.Bucket{}, + Organizations: []*platform.Organization{}, + }, + args: args{ + bucket: &platform.Bucket{ + Name: "name1", + OrganizationID: MustIDBase16(orgOneID), + }, + }, + wants: wants{ + buckets: []*platform.Bucket{}, + err: &platform.Error{ + Code: platform.ENotFound, + Msg: "organization not found", + Op: platform.OpCreateBucket, + }, + }, + }, + { + name: "create bucket with org name not exist", + fields: BucketFields{ + IDGenerator: mock.NewIDGenerator(bucketOneID, t), + Buckets: []*platform.Bucket{}, + Organizations: []*platform.Organization{}, + }, + args: args{ + bucket: &platform.Bucket{ + Name: "name1", + Organization: "org1", + }, + }, + wants: wants{ + buckets: []*platform.Bucket{}, + err: &platform.Error{ + Code: platform.ENotFound, + Msg: "organization not found", + Op: platform.OpCreateBucket, + }, + }, + }, } for _, tt := range tests {