001/* 002 * (C) Copyright 2012-2018 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Antoine Taillefer <[email protected]> 018 */ 019package org.nuxeo.drive.operations.test; 020 021import static org.nuxeo.drive.operations.test.NuxeoDriveIntegrationTestsHelper.TEST_USER_NAME_PREFIX; 022import static org.nuxeo.drive.operations.test.NuxeoDriveIntegrationTestsHelper.TEST_WORKSPACE_NAME; 023import static org.nuxeo.drive.operations.test.NuxeoDriveIntegrationTestsHelper.TEST_WORKSPACE_TITLE; 024 025import java.util.UUID; 026 027import org.apache.commons.lang3.StringUtils; 028import org.nuxeo.ecm.automation.core.Constants; 029import org.nuxeo.ecm.automation.core.annotations.Context; 030import org.nuxeo.ecm.automation.core.annotations.Operation; 031import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 032import org.nuxeo.ecm.automation.core.annotations.Param; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.CoreSession; 035import org.nuxeo.ecm.core.api.DocumentModel; 036import org.nuxeo.ecm.core.api.DocumentRef; 037import org.nuxeo.ecm.core.api.PathRef; 038import org.nuxeo.ecm.core.api.impl.blob.StringBlob; 039import org.nuxeo.ecm.core.api.security.ACE; 040import org.nuxeo.ecm.core.api.security.ACL; 041import org.nuxeo.ecm.core.api.security.ACP; 042import org.nuxeo.ecm.directory.api.DirectoryService; 043import org.nuxeo.ecm.platform.usermanager.UserManager; 044import org.nuxeo.runtime.api.Framework; 045 046/** 047 * Sets up the Nuxeo Drive integration tests environment for the given user names by: 048 * <ul> 049 * <li>Cleaning it up</li> 050 * <li>Creating test users belonging to the members group</li> 051 * <li>Creating a test workspace</li> 052 * <li>Granting the given permission to the test users on the test workspace</li> 053 * </ul> 054 * Returns the test users' passwords as a JSON comma separated string. 055 * 056 * @author Antoine Taillefer 057 */ 058@Operation(id = NuxeoDriveSetupIntegrationTests.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Setup integration tests") 059public class NuxeoDriveSetupIntegrationTests { 060 061 public static final String ID = "NuxeoDrive.SetupIntegrationTests"; 062 063 @Context 064 protected CoreSession session; 065 066 // Comma separated list of user names 067 @Param(name = "userNames") 068 protected String userNames; 069 070 // Permission granted to the test users on test workspace 071 @Param(name = "permission") 072 protected String permission; 073 074 // Put the test users in the members group to enable Read permission to the 075 // whole repository. 076 @Param(name = "useMembersGroup", required = false) 077 protected boolean useMembersGroup = false; 078 079 @OperationMethod 080 public Blob run() { 081 NuxeoDriveIntegrationTestsHelper.checkOperationAllowed(); 082 NuxeoDriveIntegrationTestsHelper.cleanUp(session); 083 084 String[] userNamesArray = StringUtils.split(userNames, ","); 085 String[] prefixedUserNames = new String[userNamesArray.length]; 086 for (int i = 0; i < userNamesArray.length; i++) { 087 prefixedUserNames[i] = TEST_USER_NAME_PREFIX + userNamesArray[i].trim(); 088 } 089 String testUserCredentials = createTestUsers(prefixedUserNames); 090 createTestWorkspace(prefixedUserNames); 091 092 return new StringBlob(testUserCredentials, "text/plain"); 093 } 094 095 protected String createTestUsers(String[] testUserNames) { 096 097 StringBuilder testUserCredentials = new StringBuilder(); 098 099 UserManager userManager = Framework.getService(UserManager.class); 100 DirectoryService directoryService = Framework.getService(DirectoryService.class); 101 String userSchemaName = userManager.getUserSchemaName(); 102 String userNameField = directoryService.getDirectoryIdField(userManager.getUserDirectoryName()); 103 String passwordField = directoryService.getDirectoryPasswordField(userManager.getUserDirectoryName()); 104 105 for (int i = 0; i < testUserNames.length; i++) { 106 String testUserName = testUserNames[i]; 107 108 // Generate random password 109 String testUserPassword = UUID.randomUUID().toString().substring(0, 6); 110 111 // Create test user 112 DocumentModel testUserModel = userManager.getBareUserModel(); 113 testUserModel.setProperty(userSchemaName, userNameField, testUserName); 114 testUserModel.setProperty(userSchemaName, passwordField, testUserPassword); 115 if (useMembersGroup) { 116 testUserModel.setProperty(userSchemaName, "groups", new String[] { "members" }); 117 } 118 userManager.createUser(testUserModel); 119 120 // Append test user's credentials 121 testUserCredentials.append(testUserName); 122 testUserCredentials.append(":"); 123 testUserCredentials.append(testUserPassword); 124 if (i < testUserNames.length - 1) { 125 testUserCredentials.append(","); 126 } 127 } 128 return testUserCredentials.toString(); 129 } 130 131 protected void createTestWorkspace(String[] testUserNames) { 132 133 // Create test workspace 134 String testWorkspaceParentPath = NuxeoDriveIntegrationTestsHelper.getDefaultDomainPath(session) + "/" 135 + NuxeoDriveIntegrationTestsHelper.TEST_WORKSPACE_PARENT_NAME; 136 DocumentModel testWorkspace = session.createDocumentModel(testWorkspaceParentPath, TEST_WORKSPACE_NAME, 137 "Workspace"); 138 testWorkspace.setPropertyValue("dc:title", TEST_WORKSPACE_TITLE); 139 session.createDocument(testWorkspace); 140 141 // Grant the given permission to the test users on the test workspace 142 String testWorkspacePath = testWorkspaceParentPath + "/" + TEST_WORKSPACE_NAME; 143 DocumentRef testWorkspaceDocRef = new PathRef(testWorkspacePath); 144 ACP acp = session.getACP(testWorkspaceDocRef); 145 ACL localACL = acp.getOrCreateACL(ACL.LOCAL_ACL); 146 for (String testUserName : testUserNames) { 147 localACL.add(new ACE(testUserName, permission, true)); 148 } 149 session.setACP(testWorkspaceDocRef, acp, false); 150 } 151 152}