Users-Group Update API (#49)
* Added structure to swagger * Added updateUserGroups handlers * Updated return definition for user groups. * Logic rewrite * Removed logs * Added some tests to updateUserGroups * lint fix * Updated tests for the new API * Lint * Added comment about why we are setting this groups individually. & more lint fixes * Updated tests page * Added more tests & fixed comments for PR * Lint utils file * Fixed import orders * Changed import order Co-authored-by: Benjamin Perez <benjamin@bexsoft.net>
This commit is contained in:
@@ -32,6 +32,7 @@ import (
|
||||
var minioListUsersMock func() (map[string]madmin.UserInfo, error)
|
||||
var minioAddUserMock func(accessKey, secreyKey string) error
|
||||
var minioRemoveUserMock func(accessKey string) error
|
||||
var minioGetUserInfoMock func(accessKey string) (madmin.UserInfo, error)
|
||||
|
||||
// mock function of listUsers()
|
||||
func (ac adminClientMock) listUsers(ctx context.Context) (map[string]madmin.UserInfo, error) {
|
||||
@@ -48,6 +49,11 @@ func (ac adminClientMock) removeUser(ctx context.Context, accessKey string) erro
|
||||
return minioRemoveUserMock(accessKey)
|
||||
}
|
||||
|
||||
//mock function of getUserInfo()
|
||||
func (ac adminClientMock) getUserInfo(ctx context.Context, accessKey string) (madmin.UserInfo, error) {
|
||||
return minioGetUserInfoMock(accessKey)
|
||||
}
|
||||
|
||||
func TestListUsers(t *testing.T) {
|
||||
assert := asrt.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
@@ -169,3 +175,110 @@ func TestRemoveUser(t *testing.T) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserGroups(t *testing.T) {
|
||||
assert := asrt.New(t)
|
||||
// mock minIO client
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
function := "updateUserGroups()"
|
||||
mockUserGroups := []string{"group1", "group2", "group3"}
|
||||
mockUserName := "testUser"
|
||||
mockResponse := &madmin.UserInfo{
|
||||
MemberOf: []string{"group1", "group2", "gropup3"},
|
||||
PolicyName: "",
|
||||
Status: "enabled",
|
||||
SecretKey: mockUserName,
|
||||
}
|
||||
mockEmptyResponse := &madmin.UserInfo{
|
||||
MemberOf: nil,
|
||||
PolicyName: "",
|
||||
Status: "",
|
||||
SecretKey: "",
|
||||
}
|
||||
|
||||
// Test-1: updateUserGroups() updates the groups for a user
|
||||
// mock function response from updateUserGroups(accessKey, groupsToAssign)
|
||||
|
||||
minioGetUserInfoMock = func(accessKey string) (madmin.UserInfo, error) {
|
||||
return *mockResponse, nil
|
||||
}
|
||||
|
||||
minioUpdateGroupMembersMock = func(remove madmin.GroupAddRemove) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := updateUserGroups(ctx, adminClient, mockUserName, mockUserGroups); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
|
||||
// Test-2: updateUserGroups() make sure errors are handled correctly when error on DeleteUser()
|
||||
// mock function response from removeUser(accessKey)
|
||||
|
||||
minioUpdateGroupMembersMock = func(remove madmin.GroupAddRemove) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
|
||||
if _, err := updateUserGroups(ctx, adminClient, mockUserName, mockUserGroups); assert.Error(err) {
|
||||
assert.Equal("there was an error updating the groups", err.Error())
|
||||
}
|
||||
|
||||
// Test-3: updateUserGroups() make sure we return the correct error when getUserInfo returns error
|
||||
minioGetUserInfoMock = func(accessKey string) (madmin.UserInfo, error) {
|
||||
return *mockEmptyResponse, errors.New("error getting user ")
|
||||
}
|
||||
|
||||
minioUpdateGroupMembersMock = func(remove madmin.GroupAddRemove) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := updateUserGroups(ctx, adminClient, mockUserName, mockUserGroups); assert.Error(err) {
|
||||
assert.Equal("error getting user ", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetUserInfo(t *testing.T) {
|
||||
assert := asrt.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1 : getUserInfo() get user info
|
||||
userName := "userNameTest"
|
||||
mockResponse := &madmin.UserInfo{
|
||||
SecretKey: userName,
|
||||
PolicyName: "",
|
||||
MemberOf: []string{"group1", "group2", "group3"},
|
||||
Status: "enabled",
|
||||
}
|
||||
emptyMockResponse := &madmin.UserInfo{
|
||||
SecretKey: "",
|
||||
PolicyName: "",
|
||||
Status: "",
|
||||
MemberOf: nil,
|
||||
}
|
||||
|
||||
// mock function response from getUserInfo()
|
||||
minioGetUserInfoMock = func(username string) (madmin.UserInfo, error) {
|
||||
return *mockResponse, nil
|
||||
}
|
||||
function := "getUserInfo()"
|
||||
info, err := getUserInfo(ctx, adminClient, userName)
|
||||
if err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
|
||||
assert.Equal(userName, info.SecretKey)
|
||||
assert.Equal("", info.PolicyName)
|
||||
assert.ElementsMatch([]string{"group1", "group2", "group3"}, info.MemberOf)
|
||||
assert.Equal(mockResponse.Status, info.Status)
|
||||
|
||||
// Test-2 : getUserInfo() Return error and see that the error is handled correctly and returned
|
||||
minioGetUserInfoMock = func(username string) (madmin.UserInfo, error) {
|
||||
return *emptyMockResponse, errors.New("error")
|
||||
}
|
||||
_, err = getUserInfo(ctx, adminClient, userName)
|
||||
if assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user