Merged 5.2.N (5.2.2) to HEAD (5.2)

135899 aforascu: REPO-2156 / MNT-16748: Failing to extract auditing entries from SearchService using Share, CMIS.
   - Added two JUnit tests that test that the audit records are added when using SearchService query(SearchParameters): one in the AuditMethodInterceptorTest and another one in AuditWebScriptTest that is using the Rest-API get audit method to query the audit
   - Refactored also an existing test in AuditMethodInterceptorTest


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@137433 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Andrei Rebegea
2017-06-15 10:56:19 +00:00
parent c59805a1cb
commit d183eda49f

View File

@@ -25,31 +25,38 @@
*/ */
package org.alfresco.repo.web.scripts.audit; package org.alfresco.repo.web.scripts.audit;
import java.net.URL; import java.net.URL;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import org.alfresco.repo.audit.AuditComponent; import org.alfresco.model.ContentModel;
import org.alfresco.repo.audit.AuditServiceImpl; import org.alfresco.repo.audit.AuditComponent;
import org.alfresco.repo.audit.UserAuditFilter; import org.alfresco.repo.audit.AuditServiceImpl;
import org.alfresco.repo.audit.model.AuditModelRegistryImpl; import org.alfresco.repo.audit.UserAuditFilter;
import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.audit.model.AuditModelRegistryImpl;
import org.alfresco.repo.security.authentication.AuthenticationException; import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.model.Repository;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.repo.web.scripts.BaseWebScriptTest; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.audit.AuditService; import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.service.cmr.audit.AuditService.AuditApplication; import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.security.AuthenticationService; import org.alfresco.service.cmr.audit.AuditService;
import org.alfresco.test_category.OwnJVMTestsCategory; import org.alfresco.service.cmr.audit.AuditService.AuditApplication;
import org.json.JSONArray; import org.alfresco.service.cmr.model.FileFolderService;
import org.json.JSONObject; import org.alfresco.service.cmr.repository.NodeRef;
import org.junit.experimental.categories.Category; import org.alfresco.service.cmr.search.SearchParameters;
import org.springframework.context.ApplicationContext; import org.alfresco.service.cmr.search.SearchService;
import org.springframework.extensions.surf.util.ISO8601DateFormat; import org.alfresco.service.cmr.security.AuthenticationService;
import org.springframework.extensions.webscripts.Status; import org.alfresco.test_category.OwnJVMTestsCategory;
import org.springframework.extensions.webscripts.TestWebScriptServer; import org.alfresco.util.GUID;
import org.springframework.extensions.webscripts.TestWebScriptServer.Response; import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.experimental.categories.Category;
import org.springframework.context.ApplicationContext;
import org.springframework.extensions.surf.util.ISO8601DateFormat;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.TestWebScriptServer;
import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
/** /**
@@ -62,15 +69,24 @@ import org.springframework.util.ResourceUtils;
public class AuditWebScriptTest extends BaseWebScriptTest public class AuditWebScriptTest extends BaseWebScriptTest
{ {
private static final String APP_REPOTEST_NAME = "AlfrescoRepositoryTest"; private static final String APP_REPOTEST_NAME = "AlfrescoRepositoryTest";
private static final String APP_REPOTEST_PATH = "/repositorytest"; private static final String APP_REPOTEST_PATH = "/repositorytest";
private static final String APP_SEARCHTEST_NAME = "SearchAudit";
private static final String APP_SEARCHTEST_PATH = "/searchaudit";
private ApplicationContext ctx; private ApplicationContext ctx;
private AuditService auditService; private AuditService auditService;
private AuthenticationService authenticationService; private SearchService searchService;
private AuthenticationService authenticationService;
private FileFolderService fileFolderService;
private Repository repositoryHelper;
private String admin; private String admin;
private boolean wasGloballyEnabled; private boolean wasGloballyEnabled;
boolean wasRepoEnabled; boolean wasRepoEnabled;
private boolean wasSearchEnabled;
private NodeRef testRoot;
@Override @Override
protected void setUp() throws Exception protected void setUp() throws Exception
{ {
@@ -85,19 +101,25 @@ public class AuditWebScriptTest extends BaseWebScriptTest
AuditServiceImpl auditServiceImpl = (AuditServiceImpl) ctx.getBean("auditService"); AuditServiceImpl auditServiceImpl = (AuditServiceImpl) ctx.getBean("auditService");
auditServiceImpl.setAuditComponent(auditComponent); auditServiceImpl.setAuditComponent(auditComponent);
authenticationService = (AuthenticationService) ctx.getBean("AuthenticationService"); authenticationService = (AuthenticationService) ctx.getBean("AuthenticationService");
auditService = (AuditService) ctx.getBean("AuditService"); auditService = (AuditService) ctx.getBean("AuditService");
searchService = (SearchService) ctx.getBean("SearchService");
repositoryHelper = (Repository)getServer().getApplicationContext().getBean("repositoryHelper");
fileFolderService = (FileFolderService)getServer().getApplicationContext().getBean("FileFolderService");
admin = AuthenticationUtil.getAdminUserName(); admin = AuthenticationUtil.getAdminUserName();
// Register the test model // Register the test models
AuditModelRegistryImpl auditModelRegistry = (AuditModelRegistryImpl) ctx.getBean("auditModel.modelRegistry"); AuditModelRegistryImpl auditModelRegistry = (AuditModelRegistryImpl) ctx.getBean("auditModel.modelRegistry");
URL testModelUrl = ResourceUtils.getURL("classpath:alfresco/testaudit/alfresco-audit-test-repository.xml"); URL testModelUrl = ResourceUtils.getURL("classpath:alfresco/testaudit/alfresco-audit-test-repository.xml");
auditModelRegistry.registerModel(testModelUrl); URL testModelUrl1 = ResourceUtils.getURL("classpath:alfresco/testaudit/alfresco-audit-test-mnt-16748.xml");
auditModelRegistry.registerModel(testModelUrl);
auditModelRegistry.registerModel(testModelUrl1);
auditModelRegistry.loadAuditModels(); auditModelRegistry.loadAuditModels();
AuthenticationUtil.setFullyAuthenticatedUser(admin); AuthenticationUtil.setFullyAuthenticatedUser(admin);
wasGloballyEnabled = auditService.isAuditEnabled(); wasGloballyEnabled = auditService.isAuditEnabled();
wasRepoEnabled = auditService.isAuditEnabled(APP_REPOTEST_NAME, APP_REPOTEST_PATH); wasRepoEnabled = auditService.isAuditEnabled(APP_REPOTEST_NAME, APP_REPOTEST_PATH);
wasSearchEnabled = auditService.isAuditEnabled(APP_SEARCHTEST_NAME, APP_SEARCHTEST_PATH);
// Only enable if required // Only enable if required
if (!wasGloballyEnabled) if (!wasGloballyEnabled)
{ {
@@ -116,6 +138,15 @@ public class AuditWebScriptTest extends BaseWebScriptTest
{ {
fail("Failed to enable repo audit for test"); fail("Failed to enable repo audit for test");
} }
}
if (!wasSearchEnabled)
{
auditService.enableAudit(APP_SEARCHTEST_NAME, APP_SEARCHTEST_PATH);
wasSearchEnabled = auditService.isAuditEnabled(APP_SEARCHTEST_NAME, APP_SEARCHTEST_PATH);
if (!wasSearchEnabled)
{
fail("Failed to enable search audit for test");
}
} }
} }
@@ -149,6 +180,21 @@ public class AuditWebScriptTest extends BaseWebScriptTest
catch (Throwable e) catch (Throwable e)
{ {
throw new RuntimeException("Failed to set repo audit back to enabled/disabled state", e); throw new RuntimeException("Failed to set repo audit back to enabled/disabled state", e);
}
try
{
if (wasSearchEnabled)
{
auditService.enableAudit(APP_SEARCHTEST_NAME, APP_SEARCHTEST_PATH);
}
else
{
auditService.disableAudit(APP_SEARCHTEST_NAME, APP_SEARCHTEST_PATH);
}
}
catch (Throwable e)
{
throw new RuntimeException("Failed to set search audit back to enabled/disabled state", e);
} }
} }
@@ -204,33 +250,67 @@ public class AuditWebScriptTest extends BaseWebScriptTest
testGetIsAuditEnabledGlobally(); testGetIsAuditEnabledGlobally();
} }
public void testGetIsAuditEnabledRepo() throws Exception public void testGetIsAuditEnabledRepo() throws Exception
{ {
boolean wasEnabled = auditService.isAuditEnabled(APP_REPOTEST_NAME, null); boolean wasEnabled = auditService.isAuditEnabled(APP_REPOTEST_NAME, null);
String url = "/api/audit/control/" + APP_REPOTEST_NAME + APP_REPOTEST_PATH;
TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url);
if (wasEnabled)
{
Response response = sendRequest(req, Status.STATUS_OK, admin);
JSONObject json = new JSONObject(response.getContentAsString());
JSONArray apps = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_APPLICATIONS);
assertEquals("Incorrect number of applications reported", 1, apps.length());
JSONObject app = apps.getJSONObject(0);
String appName = app.getString(AbstractAuditWebScript.JSON_KEY_NAME);
String appPath = app.getString(AbstractAuditWebScript.JSON_KEY_PATH);
boolean appEnabled = app.getBoolean(AbstractAuditWebScript.JSON_KEY_ENABLED);
assertEquals("Mismatched application audit enabled", wasEnabled, appEnabled);
assertEquals("Mismatched application audit name", APP_REPOTEST_NAME, appName);
assertEquals("Mismatched application audit path", APP_REPOTEST_PATH, appPath);
}
}
public void testGetAuditSearchService() throws Exception
{
// Delete search audit entries (if any)
String url = "/api/audit/clear/" + APP_SEARCHTEST_NAME;
TestWebScriptServer.PostRequest postReq = new TestWebScriptServer.PostRequest(url, "", MimetypeMap.MIMETYPE_JSON);
Response response = sendRequest(postReq, Status.STATUS_OK, admin);
JSONObject json = new JSONObject(response.getContentAsString());
assertTrue(json.getInt(AbstractAuditWebScript.JSON_KEY_CLEARED) >= 0);
// create a file
this.testRoot = this.repositoryHelper.getCompanyHome();
String filename = "test_doc" + GUID.generate() + ".txt";
NodeRef testFile = this.fileFolderService.create(this.testRoot, filename, ContentModel.TYPE_CONTENT).getNodeRef();
// search the newly created file
SearchParameters sp = new SearchParameters();
sp.setLanguage(SearchService.LANGUAGE_FTS_ALFRESCO);
sp.setQuery("=cm:name:" + filename);
sp.addStore(testFile.getStoreRef());
searchService.query(sp);
// construct the get audit request
url = "/api/audit/query/" + APP_SEARCHTEST_NAME + "/searchaudit/queryX/searchParametersX?verbose=true";
TestWebScriptServer.GetRequest getReq = new TestWebScriptServer.GetRequest(url);
response = sendRequest(getReq, Status.STATUS_OK, admin);
json = new JSONObject(response.getContentAsString());
JSONArray jsonEntries = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_ENTRIES);
assertEquals("Incorrect number of entries reported", 1, jsonEntries.length());
JSONObject values = (JSONObject) ((JSONObject) jsonEntries.get(0)).get(AbstractAuditWebScript.JSON_KEY_ENTRY_VALUES);
assertTrue("Audit entry was not found", values.toString(0).contains("query==cm:name:" + filename));
// clear audit entries for the application
auditService.clearAudit(APP_SEARCHTEST_NAME, null, null);
}
String url = "/api/audit/control/" + APP_REPOTEST_NAME + APP_REPOTEST_PATH;
TestWebScriptServer.GetRequest req = new TestWebScriptServer.GetRequest(url);
if (wasEnabled)
{
Response response = sendRequest(req, Status.STATUS_OK, admin);
JSONObject json = new JSONObject(response.getContentAsString());
JSONArray apps = json.getJSONArray(AbstractAuditWebScript.JSON_KEY_APPLICATIONS);
assertEquals("Incorrect number of applications reported", 1, apps.length());
JSONObject app = apps.getJSONObject(0);
String appName = app.getString(AbstractAuditWebScript.JSON_KEY_NAME);
String appPath = app.getString(AbstractAuditWebScript.JSON_KEY_PATH);
boolean appEnabled = app.getBoolean(AbstractAuditWebScript.JSON_KEY_ENABLED);
assertEquals("Mismatched application audit enabled", wasEnabled, appEnabled);
assertEquals("Mismatched application audit name", APP_REPOTEST_NAME, appName);
assertEquals("Mismatched application audit path", APP_REPOTEST_PATH, appPath);
}
else
{
}
}
public void testSetAuditEnabledRepo() throws Exception public void testSetAuditEnabledRepo() throws Exception
{ {
boolean wasEnabled = auditService.isAuditEnabled(APP_REPOTEST_NAME, APP_REPOTEST_PATH); boolean wasEnabled = auditService.isAuditEnabled(APP_REPOTEST_NAME, APP_REPOTEST_PATH);