forked from databricks/terraform-provider-databricks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
databricks#3468: added 'user_name_contains' attribute to data_users.g…
…o to allow filtering by userName, and added additional test cases on data_users_test.go
- Loading branch information
Showing
2 changed files
with
147 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package scim | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/databricks/databricks-sdk-go/experimental/mocks" | ||
|
@@ -9,7 +10,7 @@ import ( | |
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestDataSourceDataUsers(t *testing.T) { | ||
func TestDataSourceDataUsers_DisplayNameContains(t *testing.T) { | ||
qa.ResourceFixture{ | ||
MockAccountClientFunc: func(m *mocks.MockAccountClient) { | ||
e := m.GetMockAccountUsersAPI() | ||
|
@@ -49,12 +50,139 @@ func TestDataSourceDataUsers(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestCatalogsData_Error(t *testing.T) { | ||
func TestDataSourceDataUsers_UserNameContains(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: qa.HTTPFailures, | ||
MockAccountClientFunc: func(m *mocks.MockAccountClient) { | ||
e := m.GetMockAccountUsersAPI() | ||
e.On("ListAll", | ||
mock.Anything, | ||
mock.MatchedBy(func(req iam.ListAccountUsersRequest) bool { | ||
return req.Filter == `userName co "testuser"` && | ||
req.Attributes == "id,userName,displayName" | ||
})).Return([]iam.User{ | ||
{ | ||
Id: "123", | ||
UserName: "[email protected]", | ||
DisplayName: "testuser", | ||
}, | ||
{ | ||
Id: "456", | ||
UserName: "[email protected]", | ||
DisplayName: "testuser2", | ||
}, | ||
}, nil) | ||
}, | ||
Resource: DataSourceDataUsers(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
HCL: ` | ||
user_name_contains = "testuser" | ||
`, | ||
}.ApplyAndExpectData(t, map[string]any{ | ||
"users.#": 2, | ||
"users.0.id": "123", | ||
"users.0.user_name": "[email protected]", | ||
"users.0.display_name": "testuser", | ||
"users.1.id": "456", | ||
"users.1.user_name": "[email protected]", | ||
"users.1.display_name": "testuser2", | ||
}) | ||
} | ||
|
||
func TestDataSourceDataUsers_BothFiltersSpecified(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Resource: DataSourceDataUsers(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
HCL: ` | ||
display_name_contains = "user" | ||
user_name_contains = "example.com" | ||
`, | ||
}.ExpectError(t, "exactly one of display_name_contains or user_name_contains should be specified, not both") | ||
} | ||
|
||
func TestDataSourceDataUsersNoUsers(t *testing.T) { | ||
qa.ResourceFixture{ | ||
MockAccountClientFunc: func(m *mocks.MockAccountClient) { | ||
e := m.GetMockAccountUsersAPI() | ||
e.On("ListAll", | ||
mock.Anything, | ||
mock.MatchedBy(func(req iam.ListAccountUsersRequest) bool { | ||
return req.Filter == `displayName co "testuser"` && | ||
req.Attributes == "id,userName,displayName" | ||
})).Return([]iam.User{}, nil) | ||
}, | ||
Resource: DataSourceDataUsers(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
}.ExpectError(t, "i'm a teapot") | ||
HCL: ` | ||
display_name_contains = "testuser" | ||
`, | ||
}.ExpectError(t, "cannot find users with display name containing testuser") | ||
} | ||
|
||
func TestDataSourceDataUsersNoFilter(t *testing.T) { | ||
qa.ResourceFixture{ | ||
MockAccountClientFunc: func(m *mocks.MockAccountClient) { | ||
e := m.GetMockAccountUsersAPI() | ||
e.On("ListAll", | ||
mock.Anything, | ||
mock.MatchedBy(func(req iam.ListAccountUsersRequest) bool { | ||
return req.Attributes == "id,userName,displayName" | ||
})).Return([]iam.User{ | ||
{ | ||
Id: "123", | ||
UserName: "[email protected]", | ||
DisplayName: "testuser", | ||
}, | ||
{ | ||
Id: "456", | ||
UserName: "[email protected]", | ||
DisplayName: "testuser2", | ||
}, | ||
{ | ||
Id: "789", | ||
UserName: "[email protected]", | ||
DisplayName: "testuser3", | ||
}, | ||
}, nil) | ||
}, | ||
Resource: DataSourceDataUsers(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
}.ApplyAndExpectData(t, map[string]any{ | ||
"users.#": 3, | ||
"users.0.id": "123", | ||
"users.0.user_name": "[email protected]", | ||
"users.0.display_name": "testuser", | ||
"users.1.id": "456", | ||
"users.1.user_name": "[email protected]", | ||
"users.1.display_name": "testuser2", | ||
"users.2.id": "789", | ||
"users.2.user_name": "[email protected]", | ||
"users.2.display_name": "testuser3", | ||
}) | ||
} | ||
|
||
func TestDataSourceDataUsers_APIError(t *testing.T) { | ||
qa.ResourceFixture{ | ||
MockAccountClientFunc: func(m *mocks.MockAccountClient) { | ||
usersAPI := m.GetMockAccountUsersAPI() | ||
usersAPI.On("ListAll", | ||
mock.Anything, | ||
mock.Anything, | ||
).Return(nil, fmt.Errorf("api error")) | ||
}, | ||
Resource: DataSourceDataUsers(), | ||
Read: true, | ||
NonWritable: true, | ||
ID: "_", | ||
HCL: ` | ||
display_name_contains = "testuser" | ||
`, | ||
}.ExpectError(t, "api error") | ||
} |