generated from IBM/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 18: improve mocking of users and UserManager
- Loading branch information
wolf.bubenik
committed
Aug 15, 2024
1 parent
9e605a0
commit 489c196
Showing
5 changed files
with
358 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,9 +31,11 @@ | |
import info.magnolia.cms.security.User; | ||
import info.magnolia.cms.security.UserManager; | ||
import info.magnolia.context.WebContext; | ||
import org.mockito.Mockito; | ||
|
||
import javax.jcr.RepositoryException; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.UUID; | ||
|
||
import static de.ibmix.magkit.test.cms.context.ContextMockUtils.mockWebContext; | ||
|
@@ -46,7 +48,7 @@ | |
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* An util class to create Mockito mocks of magnolia security classes. | ||
* A util class to create Mockito mocks of magnolia security classes. | ||
* | ||
* @author [email protected] | ||
* @since 2013-04-30 | ||
|
@@ -80,13 +82,17 @@ public static AccessManager mockAccessManager(String repositoryId, AccessManager | |
return am; | ||
} | ||
|
||
public static UserManager mockUserManager(String realm) { | ||
public static UserManager mockUserManager(String realm, UserManagerStubbingOperation... stubbings) { | ||
assertThat(realm, notNullValue()); | ||
SecuritySupport security = mockSecuritySupport(); | ||
UserManager userManager = security.getUserManager(realm); | ||
if (userManager == null) { | ||
userManager = mock(UserManager.class); | ||
when(userManager.getAllUsers()).thenReturn(new HashSet<>()); | ||
when(security.getUserManager(realm)).thenReturn(userManager); | ||
} | ||
UserManager finalManager = userManager; | ||
Arrays.stream(stubbings).forEach(stubbing -> stubbing.of(finalManager)); | ||
return userManager; | ||
} | ||
|
||
|
@@ -115,37 +121,20 @@ public static RoleManager mockRoleManager(RoleManagerStubbingOperation... stubbi | |
return finalManager; | ||
} | ||
|
||
public static void register(String realm, User user) { | ||
UserManager userManager = mockUserManager(realm); | ||
when(userManager.getUser(user.getName())).thenReturn(user); | ||
when(userManager.getUserById(user.getIdentifier())).thenReturn(user); | ||
} | ||
|
||
public static void register(Group group) throws AccessDeniedException { | ||
GroupManager manager = mockGroupManager(); | ||
when(manager.getGroup(group.getName())).thenReturn(group); | ||
} | ||
|
||
public static void register(Role role) { | ||
mockRoleManager(RoleManagerStubbingOperation.stubRole(role)); | ||
} | ||
|
||
public static User mockUser(final String name, UserStubbingOperation... stubbings) { | ||
return mockUser(WEBSITE, name, UUID.randomUUID().toString(), stubbings); | ||
} | ||
|
||
public static User mockUser(final String realm, final String name, final String uuid, UserStubbingOperation... stubbings) { | ||
assertThat(stubbings, notNullValue()); | ||
User user = mockUserManager(realm).getUser(name); | ||
UserManager userManager = mockUserManager(realm); | ||
User user = userManager.getUser(name); | ||
if (user == null) { | ||
user = mock(User.class); | ||
UserStubbingOperation.stubName(name).of(user); | ||
UserStubbingOperation.stubIdentifier(uuid).of(user); | ||
register(realm, user); | ||
UserManagerStubbingOperation.stubUser(name, uuid).of(userManager); | ||
} | ||
User finalUser = user; | ||
User finalUser = userManager.getUser(name); | ||
Arrays.stream(stubbings).forEach(stubbing -> stubbing.of(finalUser)); | ||
return user; | ||
return finalUser; | ||
} | ||
|
||
public static Group mockGroup(final String name, GroupStubbingOperation... stubbings) throws AccessDeniedException { | ||
|
@@ -154,12 +143,13 @@ public static Group mockGroup(final String name, GroupStubbingOperation... stubb | |
|
||
public static Group mockGroup(final String name, final String uuid, GroupStubbingOperation... stubbings) throws AccessDeniedException { | ||
assertThat(stubbings, notNullValue()); | ||
Group group = mockGroupManager().getGroup(name); | ||
GroupManager manager = mockGroupManager(); | ||
Group group = manager.getGroup(name); | ||
if (group == null) { | ||
group = mock(Group.class); | ||
GroupStubbingOperation.stubName(name).of(group); | ||
GroupStubbingOperation.stubId(uuid).of(group); | ||
register(group); | ||
GroupManagerStubbingOperation.stubGroup(group).of(manager); | ||
} | ||
Group finalGroup = group; | ||
Arrays.stream(stubbings).forEach(stubbing -> stubbing.of(finalGroup)); | ||
|
@@ -171,12 +161,13 @@ public static Role mockRole(final String name) { | |
} | ||
|
||
public static Role mockRole(final String name, final String uuid) { | ||
Role role = mockRoleManager().getRole(name); | ||
RoleManager roleManager = mockRoleManager(); | ||
Role role = roleManager.getRole(name); | ||
if (role == null) { | ||
role = mock(Role.class); | ||
RoleStubbingOperation.stubName(name).of(role); | ||
RoleStubbingOperation.stubId(uuid).of(role); | ||
register(role); | ||
RoleManagerStubbingOperation.stubRole(role).of(roleManager); | ||
} | ||
return role; | ||
} | ||
|
162 changes: 162 additions & 0 deletions
162
...est-cms/src/main/java/de/ibmix/magkit/test/cms/security/UserManagerStubbingOperation.java
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 |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package de.ibmix.magkit.test.cms.security; | ||
|
||
/*- | ||
* #%L | ||
* magkit-test-cms Magnolia Module | ||
* %% | ||
* Copyright (C) 2023 IBM iX | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
|
||
import de.ibmix.magkit.test.StubbingOperation; | ||
import info.magnolia.cms.security.User; | ||
import info.magnolia.cms.security.UserManager; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.UUID; | ||
|
||
import static org.apache.commons.lang3.StringUtils.isNotEmpty; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.IsNull.notNullValue; | ||
import static org.mockito.Mockito.when; | ||
|
||
public abstract class UserManagerStubbingOperation implements StubbingOperation<UserManager> { | ||
|
||
public static UserManagerStubbingOperation stubUser(final String name, final String uuid, UserStubbingOperation... stubbings) { | ||
return new UserManagerStubbingOperation() { | ||
@Override | ||
public void of(UserManager mock) { | ||
assertThat(mock, notNullValue()); | ||
assertThat(name, notNullValue()); | ||
User user = mock.getUser(name); | ||
if (user == null) { | ||
user = Mockito.mock(User.class); | ||
UserStubbingOperation.stubName(name).of(user); | ||
UserStubbingOperation.stubIdentifier(uuid).of(user); | ||
stubUser(user).of(mock); | ||
} | ||
User finalUser = user; | ||
Arrays.stream(stubbings).forEach(stubbing -> stubbing.of(finalUser)); | ||
} | ||
}; | ||
} | ||
|
||
public static UserManagerStubbingOperation stubUser(final User user) { | ||
return new UserManagerStubbingOperation() { | ||
@Override | ||
public void of(UserManager mock) { | ||
assertThat(mock, notNullValue()); | ||
assertThat(user, notNullValue()); | ||
Collection<User> allUsers = mock.getAllUsers(); | ||
String userName = user.getName(); | ||
if (isNotEmpty(userName)) { | ||
when(mock.getUser(userName)).thenReturn(user); | ||
} | ||
String uuid = user.getIdentifier(); | ||
if (isNotEmpty(uuid)) { | ||
User existing = mock.getUserById(uuid); | ||
if (existing != null) { | ||
allUsers.remove(existing); | ||
} | ||
when(mock.getUserById(uuid)).thenReturn(user); | ||
} | ||
allUsers.add(user); | ||
} | ||
}; | ||
} | ||
|
||
public static UserManagerStubbingOperation stubSystemUser(UserStubbingOperation... stubbings) { | ||
return new UserManagerStubbingOperation() { | ||
@Override | ||
public void of(UserManager mock) { | ||
assertThat(mock, notNullValue()); | ||
User systemUser = mock.getSystemUser(); | ||
if (systemUser == null) { | ||
String identifier = UUID.randomUUID().toString(); | ||
stubUser(UserManager.SYSTEM_USER, identifier, UserStubbingOperation.stubPassword(UserManager.SYSTEM_USER)).of(mock); | ||
systemUser = mock.getUserById(identifier); | ||
when(mock.getSystemUser()).thenReturn(systemUser); | ||
} | ||
User finalUser = systemUser; | ||
Arrays.stream(stubbings).forEach(stubbing -> stubbing.of(finalUser)); | ||
} | ||
}; | ||
} | ||
|
||
public static UserManagerStubbingOperation stubAnonymousUser(UserStubbingOperation... stubbings) { | ||
return new UserManagerStubbingOperation() { | ||
@Override | ||
public void of(UserManager mock) { | ||
assertThat(mock, notNullValue()); | ||
User anonymous = mock.getAnonymousUser(); | ||
if (anonymous == null) { | ||
String identifier = UUID.randomUUID().toString(); | ||
stubUser(UserManager.ANONYMOUS_USER, identifier).of(mock); | ||
anonymous = mock.getUserById(identifier); | ||
when(mock.getAnonymousUser()).thenReturn(anonymous); | ||
} | ||
User finalUser = anonymous; | ||
Arrays.stream(stubbings).forEach(stubbing -> stubbing.of(finalUser)); | ||
} | ||
}; | ||
} | ||
|
||
public static UserManagerStubbingOperation stubAllUsers(final Collection<User> allUsers) { | ||
return new UserManagerStubbingOperation() { | ||
@Override | ||
public void of(UserManager mock) { | ||
assertThat(mock, notNullValue()); | ||
// remove stubbings for existing users (ignore anonymous & system user): | ||
for (User old : mock.getAllUsers()) { | ||
when(mock.getUserById(old.getIdentifier())).thenReturn(null); | ||
when(mock.getUser(old.getName())).thenReturn(null); | ||
} | ||
// assert that we always use a Set internally (no dublettes - well almost :-) ) | ||
Collection<User> newUsers = new HashSet<>(); | ||
if (allUsers != null) { | ||
newUsers.addAll(allUsers); | ||
} | ||
for (User newUser : newUsers) { | ||
when(mock.getUserById(newUser.getIdentifier())).thenReturn(newUser); | ||
when(mock.getUser(newUser.getName())).thenReturn(newUser); | ||
} | ||
when(mock.getAllUsers()).thenReturn(newUsers); | ||
} | ||
}; | ||
} | ||
|
||
public static UserManagerStubbingOperation stubLockTimePeriod(int lockTimePeriod) { | ||
return new UserManagerStubbingOperation() { | ||
@Override | ||
public void of(UserManager mock) { | ||
assertThat(mock, notNullValue()); | ||
when(mock.getLockTimePeriod()).thenReturn(lockTimePeriod); | ||
} | ||
}; | ||
} | ||
|
||
public static UserManagerStubbingOperation stubMaxFailedLoginAttempts(int maxFailedLoginAttempts) { | ||
return new UserManagerStubbingOperation() { | ||
@Override | ||
public void of(UserManager mock) { | ||
assertThat(mock, notNullValue()); | ||
when(mock.getMaxFailedLoginAttempts()).thenReturn(maxFailedLoginAttempts); | ||
} | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.