Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
#22 Now have an easy way of testing permissions
  • Loading branch information
rjrudin committed Jun 11, 2015
commit fbb1e5331828ca45a6c6f3e1fcda1eab582740cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
public class LoadModulesCommand extends AbstractCommand {

private ModulesLoader modulesLoader;
private String modulePermissions;

@Override
public Integer getExecuteSortOrder() {
Expand Down Expand Up @@ -41,4 +42,8 @@ public void undo(CommandContext context) {
public void setModulesLoader(ModulesLoader modulesLoader) {
this.modulesLoader = modulesLoader;
}

public void setModulePermissions(String modulePermissions) {
this.modulePermissions = modulePermissions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.marklogic.appdeployer.AbstractAppDeployerTest;
import com.marklogic.appdeployer.command.restapis.CreateRestApiServersCommand;
import com.marklogic.rest.mgmt.PermissionsFragment;
import com.marklogic.xccutil.template.XccTemplate;

public class LoadModulesTest extends AbstractAppDeployerTest {
Expand All @@ -18,15 +19,27 @@ public void loadModulesFromMultiplePaths() {

appDeployer.deploy(appConfig);

assertModuleExists("sample-lib is loaded from /ext in the default path", "/ext/sample-lib.xqy");
assertModuleExists("some-lib.xqy is loaded from the path added at the start of the test", "/ext/some-lib.xqy");
assertModuleExistsWithDefaultPermissions("sample-lib is loaded from /ext in the default path",
"/ext/sample-lib.xqy");
assertModuleExistsWithDefaultPermissions("some-lib.xqy is loaded from the path added at the start of the test",
"/ext/some-lib.xqy");
}

private void assertModuleExists(String message, String path) {
@Test
public void loadModulesWithCustomPermissions() {

}

private void assertModuleExistsWithDefaultPermissions(String message, String uri) {
if (xccTemplate == null) {
xccTemplate = newModulesXccTemplate();
}

assertEquals(message, "true", xccTemplate.executeAdhocQuery(format("fn:doc-available('%s')", path)));
assertEquals(message, "true", xccTemplate.executeAdhocQuery(format("fn:doc-available('%s')", uri)));

PermissionsFragment perms = getDocumentPermissions(uri, xccTemplate);
perms.assertPermissionExists("rest-admin", "read");
perms.assertPermissionExists("rest-admin", "update");
perms.assertPermissionExists("rest-extension-user", "execute");
}
}
16 changes: 14 additions & 2 deletions src/test/java/com/marklogic/rest/mgmt/AbstractMgmtTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.marklogic.rest.mgmt;

import org.junit.Assert;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -9,15 +8,17 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

import com.marklogic.junit.XmlHelper;
import com.marklogic.junit.spring.LoggingTestExecutionListener;
import com.marklogic.xccutil.template.XccTemplate;

/**
* Base class for tests that just talk to the Mgmt API and don't depend on an AppDeployer instance.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestConfig.class })
@TestExecutionListeners({ LoggingTestExecutionListener.class, DependencyInjectionTestExecutionListener.class })
public abstract class AbstractMgmtTest extends Assert {
public abstract class AbstractMgmtTest extends XmlHelper {

@Autowired
private ManageConfig manageConfig;
Expand All @@ -34,4 +35,15 @@ protected String format(String s, Object... args) {
return String.format(s, args);
}

/**
* TODO Would be nice to move this and PermissionsFragment up to ml-junit.
*/
protected PermissionsFragment getDocumentPermissions(String uri, XccTemplate t) {
String xquery = format("for $perm in xdmp:document-get-permissions('%s') ", uri);
xquery += "return element {fn:node-name($perm)} {";
xquery += " $perm/*,";
xquery += " xdmp:eval('import module namespace sec=\"http://marklogic.com/xdmp/security\" at \"/MarkLogic/security.xqy\"; sec:get-role-names(' || $perm/sec:role-id/fn:string() || ')', (), ";
xquery += " <options xmlns='xdmp:eval'><database>{xdmp:security-database()}</database></options>) }";
return new PermissionsFragment(parse("<permissions>" + t.executeAdhocQuery(xquery) + "</permissions>"));
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/marklogic/rest/mgmt/PermissionsFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.marklogic.rest.mgmt;

import com.marklogic.junit.Fragment;

public class PermissionsFragment extends Fragment {

public PermissionsFragment(Fragment other) {
super(other);
}

public void assertPermissionExists(String roleName, String capability) {
assertElementExists(format("/node()/sec:permission[sec:role-name = '%s' and sec:capability = '%s']", roleName,
capability));
}
}