RM-2027 Add option to specify user names in MockAuthenticationUtilHelper.

+review RM-9

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@100775 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-03-31 08:15:35 +00:00
parent 51ff6a1851
commit c93dba7c42

View File

@@ -53,6 +53,23 @@ public class MockAuthenticationUtilHelper
return mockAuthenticationUtil; return mockAuthenticationUtil;
} }
/**
* Create a Mockito mock <code>AuthenticationUtil</code> that executes all methods assuming the user has permission.
*
* @param adminUserName The name of the default admin user.
* @param fullyAuthenticatedUser The name of the user that last authenticated.
* @param systemUserName The name of the system user.
* @return The new mock.
*/
public static AuthenticationUtil create(String adminUserName, String fullyAuthenticatedUser, String systemUserName)
{
AuthenticationUtil mockAuthenticationUtil = mock(AuthenticationUtil.class);
setup(mockAuthenticationUtil, adminUserName, fullyAuthenticatedUser, systemUserName);
return mockAuthenticationUtil;
}
/** /**
* Set up a Mockito mock <code>AuthenticationUtil</code> so that it executes all methods assuming the user has * Set up a Mockito mock <code>AuthenticationUtil</code> so that it executes all methods assuming the user has
* permissions. If the mock is asked for details about the user then it assumes the user is "admin". * permissions. If the mock is asked for details about the user then it assumes the user is "admin".
@@ -63,6 +80,24 @@ public class MockAuthenticationUtilHelper
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected static void setup(AuthenticationUtil mockAuthenticationUtil) protected static void setup(AuthenticationUtil mockAuthenticationUtil)
{
setup(mockAuthenticationUtil, "admin", "admin", "admin");
}
/**
* Set up a Mockito mock <code>AuthenticationUtil</code> so that it executes all methods assuming the user has
* permissions.
* <p>
* TODO: Change this method to private and this class to be a factory.
*
* @param mockAuthenticationUtil The mock to initialise.
* @param adminUserName The name of the default admin user.
* @param fullyAuthenticatedUser The name of the user that last authenticated.
* @param systemUserName The name of the system user.
*/
@SuppressWarnings("unchecked")
protected static void setup(AuthenticationUtil mockAuthenticationUtil, String adminUserName,
String fullyAuthenticatedUser, String systemUserName)
{ {
reset(mockAuthenticationUtil); reset(mockAuthenticationUtil);
@@ -93,7 +128,8 @@ public class MockAuthenticationUtilHelper
}).when(mockAuthenticationUtil).<Object> runAs(any(RunAsWork.class), anyString()); }).when(mockAuthenticationUtil).<Object> runAs(any(RunAsWork.class), anyString());
// assume admin // assume admin
doReturn("admin").when(mockAuthenticationUtil).getAdminUserName(); doReturn(adminUserName).when(mockAuthenticationUtil).getAdminUserName();
doReturn("admin").when(mockAuthenticationUtil).getFullyAuthenticatedUser(); doReturn(fullyAuthenticatedUser).when(mockAuthenticationUtil).getFullyAuthenticatedUser();
doReturn(systemUserName).when(mockAuthenticationUtil).getSystemUserName();
} }
} }