-
Notifications
You must be signed in to change notification settings - Fork 268
When multiple organization users found, filter results for an exact match on email or login #2285
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
Merged
cinaglia
merged 6 commits into
main
from
grafana-organization-user-should-filter-results
Aug 7, 2025
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
862c464
if multiple organization users found, loop through results and return…
colin-stuart a79d4b5
update tests
colin-stuart 8171106
fetch users via GetOrgUsersForCurrentOrg which returns email addresses
cinaglia 0d1cbef
Merge branch 'main' into grafana-organization-user-should-filter-results
cinaglia 08570fd
feedback: tweak error message when users are not found
cinaglia 18b14f5
Merge branch 'main' into grafana-organization-user-should-filter-results
cinaglia 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,111 @@ func TestAccDatasourceOrganizationUser_basic(t *testing.T) { | |
}, | ||
}) | ||
} | ||
|
||
func TestAccDatasourceOrganizationUser_exactMatch(t *testing.T) { | ||
testutils.CheckOSSTestsEnabled(t) | ||
|
||
var user1, user2 models.UserProfileDTO | ||
checks := []resource.TestCheckFunc{ | ||
userCheckExists.exists("grafana_user.test1", &user1), | ||
userCheckExists.exists("grafana_user.test2", &user2), | ||
// Test that exact login match works when multiple users are returned | ||
resource.TestCheckResourceAttr( | ||
"data.grafana_organization_user.exact_match", "login", "test-exact-match", | ||
), | ||
resource.TestMatchResourceAttr( | ||
"data.grafana_organization_user.exact_match", "user_id", common.IDRegexp, | ||
), | ||
} | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories, | ||
CheckDestroy: userCheckExists.destroyed(&user1, nil), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDatasourceOrganizationUserExactMatch, | ||
Check: resource.ComposeTestCheckFunc(checks...), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
// TestDataSourceOrganizationUserExactMatchLogic tests the exact matching logic without requiring a Grafana instance | ||
func TestDataSourceOrganizationUserExactMatchLogic(t *testing.T) { | ||
// Test case 2: Multiple users returned, exact login match exists | ||
usersMultiple := []*models.UserLookupDTO{ | ||
{ | ||
UserID: 1, | ||
Login: "test-exact-match", | ||
}, | ||
{ | ||
UserID: 2, | ||
Login: "test-exact-match-other", | ||
}, | ||
} | ||
|
||
// Test case 3: Multiple users returned, no exact login match | ||
usersNoExact := []*models.UserLookupDTO{ | ||
{ | ||
UserID: 1, | ||
Login: "test-exact-match-other1", | ||
}, | ||
{ | ||
UserID: 2, | ||
Login: "test-exact-match-other2", | ||
}, | ||
} | ||
|
||
// Test that we can identify exact matches | ||
var exactMatch *models.UserLookupDTO | ||
login := "test-exact-match" | ||
|
||
for _, user := range usersMultiple { | ||
if user.Login == login { | ||
if exactMatch != nil { | ||
t.Fatal("Multiple exact matches found when there should only be one") | ||
} | ||
exactMatch = user | ||
} | ||
} | ||
|
||
if exactMatch == nil { | ||
t.Fatal("Expected to find exact match but didn't") | ||
} | ||
|
||
if exactMatch.UserID != 1 { | ||
t.Fatalf("Expected UserID 1, got %d", exactMatch.UserID) | ||
} | ||
|
||
// Test that we don't find exact matches when they don't exist | ||
exactMatch = nil | ||
for _, user := range usersNoExact { | ||
if user.Login == login { | ||
exactMatch = user | ||
} | ||
} | ||
|
||
if exactMatch != nil { | ||
t.Fatal("Expected no exact match but found one") | ||
} | ||
} | ||
|
||
const testAccDatasourceOrganizationUserExactMatch = ` | ||
resource "grafana_user" "test1" { | ||
email = "[email protected]" | ||
name = "Test Exact Match 1" | ||
login = "test-exact-match" | ||
password = "my-password" | ||
} | ||
|
||
resource "grafana_user" "test2" { | ||
email = "[email protected]" | ||
name = "Test Exact Match 2" | ||
login = "test-exact-match-other" | ||
password = "my-password" | ||
} | ||
|
||
data "grafana_organization_user" "exact_match" { | ||
login = grafana_user.test1.login | ||
} | ||
` |
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.