@Before
  public void init() throws Exception {

    // Get an Automation client session as Administrator
    clientSession = automationClient.getSession("Administrator", "Administrator");
    mapper = new ObjectMapper();
  }
Exemple #2
0
 public static void main(String[] args) throws Exception {
   try {
     HttpAutomationClient client =
         new HttpAutomationClient("http://localhost:8080/nuxeo/site/automation");
     Session session = client.getSession("Administrator", "Administrator");
     DocumentService rs = session.getAdapter(DocumentService.class);
     Documents docs = rs.query("SELECT * from Workspace");
     System.out.println(docs);
     for (Document d : docs) {
       System.out.println(
           d.getTitle()
               + " at "
               + d.getLastModified()
               + " facets :"
               + d.getFacets()
               + " token:"
               + d.getChangeToken());
     }
     client.shutdown();
   } catch (RemoteException e) {
     e.printStackTrace();
     System.out.println(e.getRemoteStackTrace());
   }
 }
  /**
   * Initializes the test hierarchy.
   *
   * <pre>
   * Server side for user1
   * ==============================
   *
   * /user1 (user workspace)
   *   |-- user1Folder1
   *   |     |-- user1File1
   *   |     |-- user1Folder2
   *   |-- user1File2
   *   |-- user1Folder3
   *   |     |-- user1File3
   *   |-- user1Folder4
   *
   * Server side for user2
   * ==============================
   *
   * /user2 (user workspace)
   *   |-- user2Folder1       (registered as a synchronization root with ReadWrite permission for user1)
   *   |     |-- user2File1
   *   |     |-- user2Folder2
   *   |-- user2File2
   *   |-- user2Folder3       (registered as a synchronization root with ReadWrite permission for user1)
   *   |     |-- user2File3
   *
   * </pre>
   */
  @Before
  public void init() throws Exception {

    // Create test users
    createUser("user1", "user1");
    createUser("user2", "user2");

    // Open a core session for each user
    session1 = coreFeature.openCoreSession("user1");
    session2 = coreFeature.openCoreSession("user2");

    // Create user workspace for each user
    userWorkspace1 = userWorkspaceService.getCurrentUserPersonalWorkspace(session1, null);
    userWorkspace2 = userWorkspaceService.getCurrentUserPersonalWorkspace(session2, null);

    userWorkspace1ItemId = USER_SYNC_ROOT_PARENT_ID_PREFIX + userWorkspace1.getId();
    userWorkspace1ItemPath = "/" + TOP_LEVEL_ID + "/" + userWorkspace1ItemId;

    // Populate user workspaces
    // user1
    user1Folder1 =
        createFolder(session1, userWorkspace1.getPathAsString(), "user1Folder1", "Folder");
    user1File1 =
        createFile(
            session1,
            user1Folder1.getPathAsString(),
            "user1File1",
            "File",
            "user1File1.txt",
            CONTENT_PREFIX + "user1File1");
    user1Folder2 = createFolder(session1, user1Folder1.getPathAsString(), "user1Folder2", "Folder");
    user1File2 =
        createFile(
            session1,
            userWorkspace1.getPathAsString(),
            "user1File2",
            "File",
            "user1File2.txt",
            CONTENT_PREFIX + "user1File2");
    user1Folder3 =
        createFolder(session1, userWorkspace1.getPathAsString(), "user1Folder3", "Folder");
    user1File3 =
        createFile(
            session1,
            user1Folder3.getPathAsString(),
            "user1File3",
            "File",
            "user1File3.txt",
            CONTENT_PREFIX + "user1File3");
    user1Folder4 =
        createFolder(session1, userWorkspace1.getPathAsString(), "user1Folder4", "Folder");
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    // user2
    user2Folder1 =
        createFolder(session2, userWorkspace2.getPathAsString(), "user2Folder1", "Folder");
    user2File1 =
        createFile(
            session2,
            user2Folder1.getPathAsString(),
            "user2File1",
            "File",
            "user2File1.txt",
            CONTENT_PREFIX + "user2File1");
    user2Folder2 = createFolder(session2, user2Folder1.getPathAsString(), "user2Folder2", "Folder");
    user2File2 =
        createFile(
            session2,
            userWorkspace2.getPathAsString(),
            "user2File2",
            "File",
            "user2File2.txt",
            CONTENT_PREFIX + "user2File2");
    user2Folder3 =
        createFolder(session2, userWorkspace2.getPathAsString(), "user2Folder3", "Folder");
    user2File3 =
        createFile(
            session2,
            user2Folder3.getPathAsString(),
            "user2File3",
            "File",
            "user2File3.txt",
            CONTENT_PREFIX + "user2File3");
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    setPermission(user2Folder1, "user1", SecurityConstants.READ_WRITE, true);
    setPermission(user2Folder3, "user1", SecurityConstants.READ_WRITE, true);

    // Register shared folders as synchronization roots for user1
    nuxeoDriveManager.registerSynchronizationRoot(
        session1.getPrincipal(), session1.getDocument(user2Folder1.getRef()), session1);
    nuxeoDriveManager.registerSynchronizationRoot(
        session1.getPrincipal(), session1.getDocument(user2Folder3.getRef()), session1);

    // Get an Automation client session for user1
    clientSession1 = automationClient.getSession("user1", "user1");

    mapper = new ObjectMapper();
  }
  @Test
  public void testIntegrationTestsSetupAndTearDown() throws Exception {

    // ---------------------------------------------------------
    // Setup the integration tests environment as Administrator
    // ---------------------------------------------------------
    Blob testUserCredentialsBlob =
        (Blob)
            clientSession
                .newRequest(NuxeoDriveSetupIntegrationTests.ID)
                .set("userNames", "joe,jack")
                .execute();
    assertNotNull(testUserCredentialsBlob);

    // Check test users
    String testUserCredentials = IOUtils.toString(testUserCredentialsBlob.getStream(), "UTF-8");
    assertNotNull(testUserCredentials);
    String[] testUserCrendentialsArray = StringUtils.split(testUserCredentials, ",");
    assertEquals(2, testUserCrendentialsArray.length);
    assertTrue(testUserCrendentialsArray[0].startsWith("nuxeoDriveTestUser_joe:"));
    assertTrue(testUserCrendentialsArray[1].startsWith("nuxeoDriveTestUser_jack:"));

    // useMembersGroup is false by default
    NuxeoPrincipal joePrincipal = userManager.getPrincipal("nuxeoDriveTestUser_joe");
    assertNotNull(joePrincipal);
    assertFalse(joePrincipal.getGroups().contains("members"));
    NuxeoPrincipal jackPrincipal = userManager.getPrincipal("nuxeoDriveTestUser_jack");
    assertNotNull(jackPrincipal);
    assertFalse(jackPrincipal.getGroups().contains("members"));

    // Check test workspace
    DocumentRef testWorkspaceRef = new PathRef(TEST_WORKSPACE_PATH);
    DocumentModel testWorkspace = session.getDocument(testWorkspaceRef);
    assertEquals("Workspace", testWorkspace.getType());
    assertEquals("Nuxeo Drive Test Workspace", testWorkspace.getTitle());
    assertTrue(session.hasPermission(joePrincipal, testWorkspaceRef, SecurityConstants.WRITE));
    assertTrue(session.hasPermission(jackPrincipal, testWorkspaceRef, SecurityConstants.WRITE));

    // Create test users' personal workspaces for cleanup check
    userWorkspaceService.getUserPersonalWorkspace(
        "nuxeoDriveTestUser_joe", session.getRootDocument());
    userWorkspaceService.getUserPersonalWorkspace(
        "nuxeoDriveTestUser_jack", session.getRootDocument());
    assertNotNull(
        session.getDocument(new PathRef(USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-joe")));
    assertNotNull(
        session.getDocument(new PathRef(USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-jack")));

    // ----------------------------------------------------------------------
    // Setup the integration tests environment with other user names without
    // having teared it down previously => should start by cleaning it up
    // ----------------------------------------------------------------------
    testUserCredentialsBlob =
        (Blob)
            clientSession
                .newRequest(NuxeoDriveSetupIntegrationTests.ID)
                .set("userNames", "sarah")
                .set("useMembersGroup", true)
                .execute();
    assertNotNull(testUserCredentialsBlob);

    // Check cleanup
    assertNull(userManager.getPrincipal("nuxeoDriveTestUser_joe"));
    assertNull(userManager.getPrincipal("nuxeoDriveTestUser_jack"));
    // Invalid VCS cache
    session.save();
    try {
      session.getDocument(new PathRef(USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-joe"));
      fail("User workspace should not exist.");
    } catch (ClientException e) {
      assertEquals(
          "Failed to get document " + USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-joe",
          e.getMessage());
    }
    try {
      session.getDocument(new PathRef(USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-jack"));
      fail("User workspace should not exist.");
    } catch (ClientException e) {
      assertEquals(
          "Failed to get document " + USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-jack",
          e.getMessage());
    }

    // Check test users
    testUserCredentials = IOUtils.toString(testUserCredentialsBlob.getStream(), "UTF-8");
    assertNotNull(testUserCredentials);
    testUserCrendentialsArray = StringUtils.split(testUserCredentials, ",");
    assertEquals(1, testUserCrendentialsArray.length);
    assertTrue(testUserCrendentialsArray[0].startsWith("nuxeoDriveTestUser_sarah:"));

    NuxeoPrincipal sarahPrincipal = userManager.getPrincipal("nuxeoDriveTestUser_sarah");
    assertNotNull(sarahPrincipal);
    assertTrue(sarahPrincipal.getGroups().contains("members"));

    // Check test workspace
    testWorkspace = session.getDocument(testWorkspaceRef);
    assertEquals("Nuxeo Drive Test Workspace", testWorkspace.getTitle());
    assertTrue(session.hasPermission(sarahPrincipal, testWorkspaceRef, SecurityConstants.WRITE));

    // Create test users' personal workspaces for cleanup check
    userWorkspaceService.getUserPersonalWorkspace(
        "nuxeoDriveTestUser_sarah", session.getRootDocument());
    assertNotNull(
        session.getDocument(new PathRef(USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-sarah")));

    // ----------------------------------------------------------------------
    // Try to setup the integration tests environment as an unauthorized
    // user => should fail
    // ----------------------------------------------------------------------
    Session unauthorizedSession =
        automationClient.getSession("nuxeoDriveTestUser_sarah", testUserCrendentialsArray[0]);
    try {
      unauthorizedSession
          .newRequest(NuxeoDriveSetupIntegrationTests.ID)
          .set("userNames", "john,bob")
          .execute();
      fail(
          "NuxeoDrive.SetupIntegrationTests operation should not be callable by a non administrator.");
    } catch (Exception e) {
      // Expected
    }

    // ----------------------------------------------------------------------
    // Try to tear down the integration tests environment as an unauthorized
    // user => should fail
    // ----------------------------------------------------------------------
    try {
      unauthorizedSession.newRequest(NuxeoDriveTearDownIntegrationTests.ID).execute();
      fail(
          "NuxeoDrive.TearDownIntegrationTests operation should not be callable by a non administrator.");
    } catch (Exception e) {
      // Expected
    }

    // ----------------------------------------------------------------------
    // Tear down the integration tests environment as Administrator
    // ----------------------------------------------------------------------
    clientSession.newRequest(NuxeoDriveTearDownIntegrationTests.ID).execute();
    assertTrue(userManager.searchUsers("nuxeoDriveTestUser_").isEmpty());
    // Invalid VCS cache
    session.save();
    try {
      session.getDocument(new PathRef(USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-sarah"));
      fail("User workspace should not exist.");
    } catch (ClientException e) {
      assertEquals(
          "Failed to get document " + USER_WORKSPACE_PARENT_PATH + "/nuxeoDriveTestUser-sarah",
          e.getMessage());
    }
    assertFalse(session.exists(testWorkspaceRef));
  }