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