-
Notifications
You must be signed in to change notification settings - Fork 2
Targeting context accessor #40
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
Open
linglingye001
wants to merge
2
commits into
release/v1.2.0
Choose a base branch
from
linglingye/targeting-context-accessor
base: release/v1.2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package featuremanagement | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/go-viper/mapstructure/v2" | ||
| ) | ||
|
|
||
| // mockTargetingContextAccessor implements TargetingContextAccessor for testing | ||
| type mockTargetingContextAccessor struct { | ||
| targetingContext TargetingContext | ||
| err error | ||
| } | ||
|
|
||
| func (m *mockTargetingContextAccessor) GetTargetingContext() (TargetingContext, error) { | ||
| return m.targetingContext, m.err | ||
| } | ||
|
|
||
| func TestTargetingContextAccessor_IsEnabled(t *testing.T) { | ||
| featureFlagData := map[string]any{ | ||
| "ID": "TargetedFeature", | ||
| "Enabled": true, | ||
| "Conditions": map[string]any{ | ||
| "ClientFilters": []any{ | ||
| map[string]any{ | ||
| "Name": "Microsoft.Targeting", | ||
| "Parameters": map[string]any{ | ||
| "Audience": map[string]any{ | ||
| "Users": []any{"Alice"}, | ||
| "Groups": []any{}, | ||
| "DefaultRolloutPercentage": 0, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| var featureFlag FeatureFlag | ||
| err := mapstructure.Decode(featureFlagData, &featureFlag) | ||
| if err != nil { | ||
| t.Fatalf("Failed to decode feature flag: %v", err) | ||
| } | ||
|
|
||
| provider := &mockFeatureFlagProvider{ | ||
| featureFlags: []FeatureFlag{featureFlag}, | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| accessor *mockTargetingContextAccessor | ||
| expectedResult bool | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "Accessor provides targeted user - should be enabled", | ||
| accessor: &mockTargetingContextAccessor{ | ||
| targetingContext: TargetingContext{UserID: "Alice"}, | ||
| }, | ||
| expectedResult: true, | ||
| }, | ||
| { | ||
| name: "Accessor provides non-targeted user - should be disabled", | ||
| accessor: &mockTargetingContextAccessor{ | ||
| targetingContext: TargetingContext{UserID: "Bob"}, | ||
| }, | ||
| expectedResult: false, | ||
| }, | ||
| { | ||
| name: "Accessor returns error - targeting filter fails gracefully", | ||
| accessor: &mockTargetingContextAccessor{ | ||
| err: fmt.Errorf("no user context available"), | ||
| }, | ||
| expectedResult: false, | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| manager, err := NewFeatureManager(provider, &Options{ | ||
| TargetingContextAccessor: tc.accessor, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create feature manager: %v", err) | ||
| } | ||
|
|
||
| // Call IsEnabled without appContext — accessor should provide targeting context | ||
| result, err := manager.IsEnabled("TargetedFeature") | ||
| if tc.expectError { | ||
| if err == nil { | ||
| t.Error("Expected error but got none") | ||
| } | ||
| return | ||
| } | ||
| if err != nil { | ||
| t.Errorf("Unexpected error: %v", err) | ||
| return | ||
| } | ||
| if result != tc.expectedResult { | ||
| t.Errorf("Expected %v, got %v", tc.expectedResult, result) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTargetingContextAccessor_ExplicitContextOverridesAccessor(t *testing.T) { | ||
| featureFlagData := map[string]any{ | ||
| "ID": "TargetedFeature", | ||
| "Enabled": true, | ||
| "Conditions": map[string]any{ | ||
| "ClientFilters": []any{ | ||
| map[string]any{ | ||
| "Name": "Microsoft.Targeting", | ||
| "Parameters": map[string]any{ | ||
| "Audience": map[string]any{ | ||
| "Users": []any{"Alice"}, | ||
| "Groups": []any{}, | ||
| "DefaultRolloutPercentage": 0, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| var featureFlag FeatureFlag | ||
| err := mapstructure.Decode(featureFlagData, &featureFlag) | ||
| if err != nil { | ||
| t.Fatalf("Failed to decode feature flag: %v", err) | ||
| } | ||
|
|
||
| provider := &mockFeatureFlagProvider{ | ||
| featureFlags: []FeatureFlag{featureFlag}, | ||
| } | ||
|
|
||
| // Accessor returns "Bob" (not targeted), but explicit context says "Alice" (targeted) | ||
| accessor := &mockTargetingContextAccessor{ | ||
| targetingContext: TargetingContext{UserID: "Bob"}, | ||
| } | ||
|
|
||
| manager, err := NewFeatureManager(provider, &Options{ | ||
| TargetingContextAccessor: accessor, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create feature manager: %v", err) | ||
| } | ||
|
|
||
| // Explicit context should override the accessor | ||
| result, err := manager.IsEnabledWithAppContext("TargetedFeature", TargetingContext{UserID: "Alice"}) | ||
| if err != nil { | ||
| t.Errorf("Unexpected error: %v", err) | ||
| return | ||
| } | ||
| if !result { | ||
| t.Error("Expected feature to be enabled for Alice (explicit context), but it was disabled") | ||
| } | ||
| } | ||
|
|
||
| func TestTargetingContextAccessor_GetVariant(t *testing.T) { | ||
| featureFlagData := map[string]any{ | ||
| "ID": "VariantFeature", | ||
| "Enabled": true, | ||
| "Allocation": map[string]any{ | ||
| "DefaultWhenEnabled": "Small", | ||
| "User": []any{ | ||
| map[string]any{ | ||
| "Variant": "Big", | ||
| "Users": []any{"Alice"}, | ||
| }, | ||
| }, | ||
| }, | ||
| "Variants": []any{ | ||
| map[string]any{"Name": "Big", "ConfigurationValue": "500px"}, | ||
| map[string]any{"Name": "Small", "ConfigurationValue": "300px"}, | ||
| }, | ||
| } | ||
|
|
||
| var featureFlag FeatureFlag | ||
| err := mapstructure.Decode(featureFlagData, &featureFlag) | ||
| if err != nil { | ||
| t.Fatalf("Failed to decode feature flag: %v", err) | ||
| } | ||
|
|
||
| provider := &mockFeatureFlagProvider{ | ||
| featureFlags: []FeatureFlag{featureFlag}, | ||
| } | ||
|
|
||
| accessor := &mockTargetingContextAccessor{ | ||
| targetingContext: TargetingContext{UserID: "Alice"}, | ||
| } | ||
|
|
||
| manager, err := NewFeatureManager(provider, &Options{ | ||
| TargetingContextAccessor: accessor, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create feature manager: %v", err) | ||
| } | ||
|
|
||
| // Call GetVariant with nil appContext — accessor should provide targeting context | ||
| variant, err := manager.GetVariant("VariantFeature", nil) | ||
| if err != nil { | ||
| t.Fatalf("Unexpected error: %v", err) | ||
| } | ||
|
|
||
| if variant == nil { | ||
| t.Fatal("Expected a variant but got nil") | ||
| } | ||
|
|
||
| if variant.Name != "Big" { | ||
| t.Errorf("Expected variant 'Big' for Alice, got '%s'", variant.Name) | ||
| } | ||
| } | ||
|
|
||
| func TestTargetingContextAccessor_NilAccessor(t *testing.T) { | ||
| featureFlagData := map[string]any{ | ||
| "ID": "SimpleFeature", | ||
| "Enabled": true, | ||
| } | ||
|
|
||
| var featureFlag FeatureFlag | ||
| err := mapstructure.Decode(featureFlagData, &featureFlag) | ||
| if err != nil { | ||
| t.Fatalf("Failed to decode feature flag: %v", err) | ||
| } | ||
|
|
||
| provider := &mockFeatureFlagProvider{ | ||
| featureFlags: []FeatureFlag{featureFlag}, | ||
| } | ||
|
|
||
| // No accessor — should work fine for features without targeting | ||
| manager, err := NewFeatureManager(provider, nil) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create feature manager: %v", err) | ||
| } | ||
|
|
||
| result, err := manager.IsEnabled("SimpleFeature") | ||
| if err != nil { | ||
| t.Errorf("Unexpected error: %v", err) | ||
| return | ||
| } | ||
| if !result { | ||
| t.Error("Expected feature to be enabled") | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.