-
Notifications
You must be signed in to change notification settings - Fork 39
feat: add GetUserAttributeKeys #1994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
745a7e6
f6dab2f
c7f6326
f34edc3
f6f9b1f
2b57793
c2e8525
d3e0ad3
e459365
f2b668c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||||||||||||
| // Copyright 2025 The Bucketeer Authors. | ||||||||||||||||
| // | ||||||||||||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||
| // you may not use this file except in compliance with the License. | ||||||||||||||||
| // You may obtain a copy of the License at | ||||||||||||||||
| // | ||||||||||||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||
| // | ||||||||||||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||||||||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||
| // See the License for the specific language governing permissions and | ||||||||||||||||
| // limitations under the License. | ||||||||||||||||
|
|
||||||||||||||||
| package api | ||||||||||||||||
|
|
||||||||||||||||
| import ( | ||||||||||||||||
| "context" | ||||||||||||||||
|
|
||||||||||||||||
| "go.uber.org/zap" | ||||||||||||||||
| "google.golang.org/genproto/googleapis/rpc/errdetails" | ||||||||||||||||
|
|
||||||||||||||||
| "github.com/bucketeer-io/bucketeer/pkg/locale" | ||||||||||||||||
| "github.com/bucketeer-io/bucketeer/pkg/log" | ||||||||||||||||
| accountproto "github.com/bucketeer-io/bucketeer/proto/account" | ||||||||||||||||
| featureproto "github.com/bucketeer-io/bucketeer/proto/feature" | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| func (s *FeatureService) GetUserAttributeKeys( | ||||||||||||||||
| ctx context.Context, | ||||||||||||||||
| req *featureproto.GetUserAttributeKeysRequest, | ||||||||||||||||
| ) (*featureproto.GetUserAttributeKeysResponse, error) { | ||||||||||||||||
| localizer := locale.NewLocalizer(ctx) | ||||||||||||||||
| _, err := s.checkEnvironmentRole( | ||||||||||||||||
| ctx, accountproto.AccountV2_Role_Environment_VIEWER, | ||||||||||||||||
| req.EnvironmentId, localizer) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| s.logger.Error("Failed to get user attribute keys", zap.Error(err)) | ||||||||||||||||
| return nil, err | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
Comment on lines
+34
to
+41
|
||||||||||||||||
| _, err := s.checkEnvironmentRole( | |
| ctx, accountproto.AccountV2_Role_Environment_VIEWER, | |
| req.EnvironmentId, localizer) | |
| if err != nil { | |
| s.logger.Error("Failed to get user attribute keys", zap.Error(err)) | |
| return nil, err | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| // Copyright 2025 The Bucketeer Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "go.uber.org/mock/gomock" | ||
| "google.golang.org/grpc/metadata" | ||
|
|
||
| cachev3mock "github.com/bucketeer-io/bucketeer/pkg/cache/v3/mock" | ||
|
|
||
| "github.com/bucketeer-io/bucketeer/pkg/locale" | ||
| accountproto "github.com/bucketeer-io/bucketeer/proto/account" | ||
| featureproto "github.com/bucketeer-io/bucketeer/proto/feature" | ||
| ) | ||
|
|
||
| func TestGetUserAttributeKeys(t *testing.T) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An e2e test for this API would be nice! |
||
| t.Parallel() | ||
| mockController := gomock.NewController(t) | ||
| defer mockController.Finish() | ||
|
|
||
| patterns := []struct { | ||
| desc string | ||
| service *FeatureService | ||
| context context.Context | ||
| setup func(*FeatureService) | ||
| input *featureproto.GetUserAttributeKeysRequest | ||
| expected *featureproto.GetUserAttributeKeysResponse | ||
| getExpectedErr func(localizer locale.Localizer) error | ||
| }{ | ||
| { | ||
| desc: "error: permission denied", | ||
| service: createFeatureServiceWithGetAccountByEnvironmentMock(mockController, accountproto.AccountV2_Role_Organization_UNASSIGNED, accountproto.AccountV2_Role_Environment_UNASSIGNED), | ||
| context: createContextWithTokenRoleUnassigned(), | ||
| setup: func(s *FeatureService) {}, | ||
| input: &featureproto.GetUserAttributeKeysRequest{ | ||
| EnvironmentId: "ns0", | ||
| }, | ||
| getExpectedErr: func(localizer locale.Localizer) error { | ||
| return createError(t, statusPermissionDenied, localizer.MustLocalize(locale.PermissionDenied), localizer) | ||
| }, | ||
| }, | ||
| { | ||
| desc: "error: cache error", | ||
| service: createFeatureServiceNew(mockController), | ||
| context: createContextWithToken(), | ||
| setup: func(s *FeatureService) { | ||
| s.userAttributesCache.(*cachev3mock.MockUserAttributesCache).EXPECT(). | ||
| GetUserAttributeKeyAll("ns0"). | ||
| Return(nil, errors.New("cache error")) | ||
| }, | ||
| input: &featureproto.GetUserAttributeKeysRequest{ | ||
| EnvironmentId: "ns0", | ||
| }, | ||
| getExpectedErr: func(localizer locale.Localizer) error { | ||
| return createError(t, statusInternal, localizer.MustLocalize(locale.InternalServerError), localizer) | ||
| }, | ||
| }, | ||
| { | ||
| desc: "success: empty user attribute keys", | ||
| service: createFeatureServiceNew(mockController), | ||
| context: createContextWithToken(), | ||
| setup: func(s *FeatureService) { | ||
| s.userAttributesCache.(*cachev3mock.MockUserAttributesCache).EXPECT(). | ||
| GetUserAttributeKeyAll("ns0"). | ||
| Return([]string{ | ||
| "key1", | ||
| "key2", | ||
| }, nil) | ||
| }, | ||
| input: &featureproto.GetUserAttributeKeysRequest{ | ||
| EnvironmentId: "ns0", | ||
| }, | ||
| expected: &featureproto.GetUserAttributeKeysResponse{ | ||
| UserAttributeKeys: []string{ | ||
| "key1", | ||
| "key2", | ||
| }, | ||
| }, | ||
| getExpectedErr: func(localizer locale.Localizer) error { | ||
| return nil | ||
| }, | ||
| }, | ||
| { | ||
| desc: "success: with user attribute keys", | ||
| service: createFeatureServiceNew(mockController), | ||
| context: createContextWithToken(), | ||
| setup: func(s *FeatureService) { | ||
| expectedKeys := []string{"appVersion", "platform", "country", "deviceType"} | ||
| s.userAttributesCache.(*cachev3mock.MockUserAttributesCache).EXPECT(). | ||
| GetUserAttributeKeyAll("ns0"). | ||
| Return(expectedKeys, nil) | ||
| }, | ||
| input: &featureproto.GetUserAttributeKeysRequest{ | ||
| EnvironmentId: "ns0", | ||
| }, | ||
| expected: &featureproto.GetUserAttributeKeysResponse{ | ||
| UserAttributeKeys: []string{"appVersion", "platform", "country", "deviceType"}, | ||
| }, | ||
| getExpectedErr: func(localizer locale.Localizer) error { | ||
| return nil | ||
| }, | ||
| }, | ||
| { | ||
| desc: "success: with Viewer Account", | ||
| service: createFeatureServiceWithGetAccountByEnvironmentMock(mockController, accountproto.AccountV2_Role_Organization_MEMBER, accountproto.AccountV2_Role_Environment_VIEWER), | ||
| context: createContextWithTokenRoleUnassigned(), | ||
| setup: func(s *FeatureService) { | ||
| expectedKeys := []string{"appVersion", "platform"} | ||
| s.userAttributesCache.(*cachev3mock.MockUserAttributesCache).EXPECT(). | ||
| GetUserAttributeKeyAll("ns0"). | ||
| Return(expectedKeys, nil) | ||
| }, | ||
| input: &featureproto.GetUserAttributeKeysRequest{ | ||
| EnvironmentId: "ns0", | ||
| }, | ||
| expected: &featureproto.GetUserAttributeKeysResponse{ | ||
| UserAttributeKeys: []string{"appVersion", "platform"}, | ||
| }, | ||
| getExpectedErr: func(localizer locale.Localizer) error { | ||
| return nil | ||
| }, | ||
| }, | ||
| } | ||
| for _, p := range patterns { | ||
| t.Run(p.desc, func(t *testing.T) { | ||
| fs := p.service | ||
| if p.setup != nil { | ||
| p.setup(fs) | ||
| } | ||
| ctx := p.context | ||
| ctx = metadata.NewIncomingContext(ctx, metadata.MD{ | ||
| "accept-language": []string{"ja"}, | ||
| }) | ||
| localizer := locale.NewLocalizer(ctx) | ||
|
|
||
| resp, err := fs.GetUserAttributeKeys(ctx, p.input) | ||
| assert.Equal(t, p.getExpectedErr(localizer), err) | ||
| if err == nil { | ||
| assert.Equal(t, p.expected, resp) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.