Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[bugfix] Fix to Id function result document structure and added a test

  • Loading branch information...
commit 9dd27f5b16d8f6e0b2d3abf97fca377d9b403d1d 1 parent d512ac3
@adamretter adamretter authored
View
8 src/org/exist/xquery/functions/securitymanager/IdFunction.java
@@ -46,7 +46,7 @@
"and effective account details are returned, otherwise only the real " +
"account details are returned.",
null,
- new FunctionReturnSequenceType(Type.DOCUMENT, Cardinality.ONE, "Example output when an XQuery is running setUid <id xmlns=\"http://exist-db.org/xquery/securitymanager\"><real><username>guest</username><groups><group>guest</group></groups><effective><username>admin</username><groups><group>dba</group></groups></effective></real></id>.")
+ new FunctionReturnSequenceType(Type.DOCUMENT, Cardinality.ONE, "Example output when an XQuery is running setUid <id xmlns=\"http://exist-db.org/xquery/securitymanager\"><real><username>guest</username><groups><group>guest</group></groups></real><effective><username>admin</username><groups><group>dba</group></groups></effective></id>.")
);
public IdFunction(final XQueryContext context, final FunctionSignature signature) {
@@ -75,14 +75,14 @@ public Sequence eval(final Sequence args[], final Sequence contextSequence) thro
builder.startElement(new QName("real", SecurityManagerModule.NAMESPACE_URI, SecurityManagerModule.PREFIX), null);
subjectToXml(builder, context.getRealUser());
-
+ builder.endElement();
+
if(context.getRealUser().getId() != context.getEffectiveUser().getId()) {
builder.startElement(new QName("effective", SecurityManagerModule.NAMESPACE_URI, SecurityManagerModule.PREFIX), null);
subjectToXml(builder, context.getEffectiveUser());
builder.endElement();
}
-
- builder.endElement();
+
builder.endElement();
builder.endDocument();
View
142 test/src/org/exist/xquery/functions/securitymanager/IdFunctionTest.java
@@ -21,54 +21,128 @@
*/
package org.exist.xquery.functions.securitymanager;
-import org.easymock.classextension.EasyMock;
-import static org.easymock.classextension.EasyMock.expect;
-import static org.easymock.classextension.EasyMock.createMockBuilder;
+import org.custommonkey.xmlunit.SimpleNamespaceContext;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.custommonkey.xmlunit.XpathEngine;
+import org.custommonkey.xmlunit.exceptions.XpathException;
+import org.easymock.EasyMock;
+
+import static org.easymock.EasyMock.createMockBuilder;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+
+import org.exist.memtree.DocumentImpl;
import org.exist.security.Subject;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.Sequence;
+
import static org.junit.Assert.assertEquals;
-import org.junit.Ignore;
+
import org.junit.Test;
+import org.w3c.dom.NodeList;
+
+import java.util.HashMap;
+import java.util.Map;
/**
- *
* @author Adam Retter <adam@exist-db.org>
*/
public class IdFunctionTest {
-
+
/**
* Test of eval method, of class IdFunction.
+ * when real and effective users are different
*/
@Test
- @Ignore //need to update to newer easy mock 3.2?
- public void differingRealAndEffectiveUsers() throws XPathException {
- //final XQueryContext mckContext = EasyMock.createNiceMock(XQueryContext.class);
- final XQueryContext mckContext = createMockBuilder(XQueryContext.class)
- .addMockedMethod("getRealUser")
- .addMockedMethod("getEffectiveUser")
- .createMock();
-
- final Subject mckRealUser = EasyMock.createMock(Subject.class);
-
- expect(mckContext.getRealUser()).andReturn(mckRealUser);
- expect(mckRealUser.getName()).andReturn("real");
- expect(mckRealUser.getGroups()).andReturn(new String[] { "realGroup1", "realGroup2" });
- expect(mckRealUser.getId()).andReturn(1);
-
- final Subject mckEffectiveUser = EasyMock.createMock(Subject.class);
-
- expect(mckContext.getEffectiveUser()).andReturn(mckEffectiveUser);
- expect(mckEffectiveUser.getId()).andReturn(2);
- expect(mckEffectiveUser.getName()).andReturn("effective");
- expect(mckEffectiveUser.getGroups()).andReturn(new String[] { "effectiveGroup1", "effectiveGroup2" });
-
- final IdFunction idFunctions = new IdFunction(mckContext, IdFunction.FNS_ID);
- final Sequence result = idFunctions.eval(new Sequence[] { Sequence.EMPTY_SEQUENCE }, null);
-
- assertEquals(1, result.getItemCount());
-
- //TODO further assertions
+ public void differingRealAndEffectiveUsers() throws XPathException, XpathException {
+ final XQueryContext mckContext = createMockBuilder(XQueryContext.class)
+ .addMockedMethod("getRealUser")
+ .addMockedMethod("getEffectiveUser")
+ .createMock();
+
+ final Subject mckRealUser = EasyMock.createMock(Subject.class);
+ final String realUsername = "real";
+ expect(mckContext.getRealUser()).andReturn(mckRealUser).times(2);
+ expect(mckRealUser.getName()).andReturn(realUsername);
+ expect(mckRealUser.getGroups()).andReturn(new String[]{"realGroup1", "realGroup2"});
+ expect(mckRealUser.getId()).andReturn(1);
+
+ final Subject mckEffectiveUser = EasyMock.createMock(Subject.class);
+ final String effectiveUsername = "effective";
+ expect(mckContext.getEffectiveUser()).andReturn(mckEffectiveUser).times(2);
+ expect(mckEffectiveUser.getId()).andReturn(2);
+ expect(mckEffectiveUser.getName()).andReturn(effectiveUsername);
+ expect(mckEffectiveUser.getGroups()).andReturn(new String[]{"effectiveGroup1", "effectiveGroup2"});
+
+ replay(mckEffectiveUser, mckRealUser, mckContext);
+
+ final IdFunction idFunctions = new IdFunction(mckContext, IdFunction.FNS_ID);
+ final Sequence result = idFunctions.eval(new Sequence[]{Sequence.EMPTY_SEQUENCE}, null);
+
+ assertEquals(1, result.getItemCount());
+
+ final XpathEngine xpathEngine = XMLUnit.newXpathEngine();
+ final Map<String, String> namespaces = new HashMap<String, String>();
+ namespaces.put("sm", "http://exist-db.org/xquery/securitymanager");
+ xpathEngine.setNamespaceContext(new SimpleNamespaceContext(namespaces));
+
+ final DocumentImpl resultDoc = (DocumentImpl)result.itemAt(0);
+
+ final NodeList nlRealUsername = xpathEngine.getMatchingNodes("/sm:id/sm:real/sm:username", resultDoc);
+ assertEquals(1, nlRealUsername.getLength());
+ assertEquals(realUsername, nlRealUsername.item(0).getNodeValue());
+
+ final NodeList nlEffectiveUsername = xpathEngine.getMatchingNodes("/sm:id/sm:effective/sm:username", resultDoc);
+ assertEquals(1, nlEffectiveUsername.getLength());
+ assertEquals(effectiveUsername, nlEffectiveUsername.item(0).getNodeValue());
+
+ verify(mckEffectiveUser, mckRealUser, mckContext);
+ }
+
+ /**
+ * Test of eval method, of class IdFunction.
+ * when real and effective users are the same
+ */
+ @Test
+ public void sameRealAndEffectiveUsers() throws XPathException, XpathException {
+ final XQueryContext mckContext = createMockBuilder(XQueryContext.class)
+ .addMockedMethod("getRealUser")
+ .addMockedMethod("getEffectiveUser")
+ .createMock();
+
+ final Subject mckUser = EasyMock.createMock(Subject.class);
+ final String username = "user1";
+ expect(mckContext.getRealUser()).andReturn(mckUser).times(2);
+ expect(mckUser.getName()).andReturn(username);
+ expect(mckUser.getGroups()).andReturn(new String[]{"group1", "group2"});
+ expect(mckUser.getId()).andReturn(1);
+
+ expect(mckContext.getEffectiveUser()).andReturn(mckUser);
+ expect(mckUser.getId()).andReturn(1);
+
+ replay(mckUser, mckContext);
+
+ final IdFunction idFunctions = new IdFunction(mckContext, IdFunction.FNS_ID);
+ final Sequence result = idFunctions.eval(new Sequence[]{Sequence.EMPTY_SEQUENCE}, null);
+
+ assertEquals(1, result.getItemCount());
+
+ final XpathEngine xpathEngine = XMLUnit.newXpathEngine();
+ final Map<String, String> namespaces = new HashMap<String, String>();
+ namespaces.put("sm", "http://exist-db.org/xquery/securitymanager");
+ xpathEngine.setNamespaceContext(new SimpleNamespaceContext(namespaces));
+
+ final DocumentImpl resultDoc = (DocumentImpl)result.itemAt(0);
+
+ final NodeList nlRealUsername = xpathEngine.getMatchingNodes("/sm:id/sm:real/sm:username", resultDoc);
+ assertEquals(1, nlRealUsername.getLength());
+ assertEquals(username, nlRealUsername.item(0).getNodeValue());
+
+ final NodeList nlEffectiveUsername = xpathEngine.getMatchingNodes("/sm:id/sm:effective/sm:username", resultDoc);
+ assertEquals(0, nlEffectiveUsername.getLength());
+
+ verify(mckUser, mckContext);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.