From ca933ce831d900bc4408668bb9ed333cd3af2be3 Mon Sep 17 00:00:00 2001 From: Jelena Cule Date: Tue, 16 Aug 2016 15:35:11 +0000 Subject: [PATCH] REPO-1023: Service Pack: MNT-16454 Can't use WebDAV to delete files which have the versionable aspect - check that node is is actually locked before unlocking to avoid access denied - added unit test git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@129529 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../org/alfresco/RemoteApi01TestSuite.java | 3 +- .../repo/webdav/DeleteMethodTest.java | 226 ++++++++++++++++++ 2 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 source/test-java/org/alfresco/repo/webdav/DeleteMethodTest.java diff --git a/source/test-java/org/alfresco/RemoteApi01TestSuite.java b/source/test-java/org/alfresco/RemoteApi01TestSuite.java index 416e74b40d..d9b95d15e4 100644 --- a/source/test-java/org/alfresco/RemoteApi01TestSuite.java +++ b/source/test-java/org/alfresco/RemoteApi01TestSuite.java @@ -71,7 +71,8 @@ public class RemoteApi01TestSuite extends TestSuite static void tests3(TestSuite suite) // { suite.addTestSuite(org.alfresco.repo.webdav.GetMethodRegressionTest.class); - suite.addTest(new JUnit4TestAdapter(org.alfresco.repo.webdav.MoveMethodTest.class)); + suite.addTest(new JUnit4TestAdapter(org.alfresco.repo.webdav.MoveMethodTest.class)); + suite.addTest(new JUnit4TestAdapter(org.alfresco.repo.webdav.DeleteMethodTest.class)); suite.addTest(new JUnit4TestAdapter(org.alfresco.repo.webdav.UnlockMethodTest.class)); suite.addTest(new JUnit4TestAdapter(org.alfresco.repo.webdav.LockMethodTest.class)); suite.addTest(new JUnit4TestAdapter(org.alfresco.repo.webdav.WebDAVHelperIntegrationTest.class)); diff --git a/source/test-java/org/alfresco/repo/webdav/DeleteMethodTest.java b/source/test-java/org/alfresco/repo/webdav/DeleteMethodTest.java new file mode 100644 index 0000000000..23574f2fd8 --- /dev/null +++ b/source/test-java/org/alfresco/repo/webdav/DeleteMethodTest.java @@ -0,0 +1,226 @@ +/* + * #%L + * Alfresco Remote API + * %% + * Copyright (C) 2005 - 2016 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.repo.webdav; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.InputStream; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletResponse; +import javax.transaction.Status; +import javax.transaction.UserTransaction; + +import org.alfresco.model.ContentModel; +import org.alfresco.repo.security.authentication.AuthenticationUtil; +import org.alfresco.repo.transaction.RetryingTransactionHelper; +import org.alfresco.service.cmr.repository.ContentService; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.repository.StoreRef; +import org.alfresco.service.cmr.search.ResultSet; +import org.alfresco.service.cmr.search.SearchService; +import org.alfresco.service.namespace.QName; +import org.alfresco.service.transaction.TransactionService; +import org.alfresco.util.ApplicationContextHelper; +import org.alfresco.util.GUID; +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +/** + * + * Class for webdav delete method unit tests + * + * @author jcule + * + */ +public class DeleteMethodTest +{ + //protected static Log logger = LogFactory.getLog("org.alfresco.webdav.protocol"); + + private static ApplicationContext ctx; + + private MockHttpServletRequest request; + private MockHttpServletResponse response; + private DeleteMethod method; + private UserTransaction txn = null; + + private SearchService searchService; + private TransactionService transactionService; + private NodeService nodeService; + private ContentService contentService; + private WebDAVHelper webDAVHelper; + + private NodeRef companyHomeNodeRef; + private NodeRef versionableDoc; + private String versionableDocName; + private StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore"); + + + @BeforeClass + public static void setUpBeforeClass() throws Exception + { + ctx = ApplicationContextHelper.getApplicationContext(new String[] + { + "classpath:alfresco/application-context.xml", + "classpath:alfresco/web-scripts-application-context.xml", + "classpath:alfresco/remote-api-context.xml" + }); + } + + @Before + public void setUp() throws Exception + { + transactionService = ctx.getBean("transactionService", TransactionService.class); + searchService = ctx.getBean("SearchService", SearchService.class); + nodeService = ctx.getBean("NodeService", NodeService.class); + contentService = ctx.getBean("contentService", ContentService.class); + webDAVHelper = ctx.getBean("webDAVHelper", WebDAVHelper.class); + } + + @After + public void tearDown() throws Exception + { + method = null; + request = null; + response = null; + + if (txn.getStatus() == Status.STATUS_MARKED_ROLLBACK) + { + txn.rollback(); + } + else + { + txn.commit(); + } + + AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser(); + + nodeService.deleteNode(versionableDoc); + + // As per MNT-10037 try to create a node and delete it in the next txn + txn = transactionService.getUserTransaction(); + txn.begin(); + + Map properties = new HashMap(); + String nodeName = "leak-session-doc-" + GUID.generate(); + properties.put(ContentModel.PROP_NAME, nodeName); + + NodeRef nodeRef = nodeService.createNode(companyHomeNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName(ContentModel.USER_MODEL_URI, nodeName), + ContentModel.TYPE_CONTENT, properties).getChildRef(); + contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true).putContent("WebDAVTestContent"); + + txn.commit(); + + txn = transactionService.getUserTransaction(); + txn.begin(); + + nodeService.deleteNode(nodeRef); + + txn.commit(); + + AuthenticationUtil.clearCurrentSecurityContext(); + } + + /** + * Checks that file with the versionable aspect applied can be deleted + * + * @throws Exception + */ + @Test + public void testDeleteFileWithVersionableAspect() throws Exception + { + //create file + createFileWithVersionableAscpect(); + + //delete file + method = new DeleteMethod(); + request = new MockHttpServletRequest(WebDAV.METHOD_DELETE, "/alfresco/webdav/" + versionableDocName); + response = new MockHttpServletResponse(); + request.setServerPort(8080); + request.setServletPath("/webdav"); + method.setDetails(request, response, webDAVHelper, companyHomeNodeRef); + method.execute(); + + //check the response + assertEquals(HttpServletResponse.SC_OK, response.getStatus()); + + } + + /** + * Create file with versionable aspect + * + * @throws Exception + */ + private void createFileWithVersionableAscpect() throws Exception + { + AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName()); + + companyHomeNodeRef = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback() + { + @Override + public NodeRef execute() throws Throwable + { + // find "Company Home" + ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home\""); + NodeRef result = resultSet.getNodeRef(0); + resultSet.close(); + + return result; + } + }); + txn = transactionService.getUserTransaction(); + txn.begin(); + + // Create a test file with versionable aspect and content + Map properties = new HashMap(); + versionableDocName = "doc-" + GUID.generate(); + properties.put(ContentModel.PROP_NAME, versionableDocName); + + versionableDoc = nodeService.createNode(companyHomeNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName(ContentModel.USER_MODEL_URI, versionableDocName), + ContentModel.TYPE_CONTENT, properties).getChildRef(); + contentService.getWriter(versionableDoc, ContentModel.PROP_CONTENT, true).putContent("WebDAVTestContent"); + nodeService.addAspect(versionableDoc, ContentModel.ASPECT_VERSIONABLE, null); + + txn.commit(); + + txn = transactionService.getUserTransaction(); + txn.begin(); + } + +}