Skip to content
Snippets Groups Projects
Unverified Commit a6b36f4d authored by Timm Fitschen's avatar Timm Fitschen
Browse files

TST: fix tests

parent 206b6f89
No related branches found
No related tags found
No related merge requests found
...@@ -122,6 +122,7 @@ public class UserSources extends HashMap<String, UserSource> { ...@@ -122,6 +122,7 @@ public class UserSources extends HashMap<String, UserSource> {
/** /**
* Return the roles of a given user. * Return the roles of a given user.
*
* @param realm * @param realm
* @param username * @param username
* @return * @return
......
...@@ -170,35 +170,36 @@ public abstract class AbstractCaosDBServerResource extends ServerResource { ...@@ -170,35 +170,36 @@ public abstract class AbstractCaosDBServerResource extends ServerResource {
/** /**
* Add the user info to the Response Element. * Add the user info to the Response Element.
*
* @param retRoot * @param retRoot
* @param user * @param user
*/ */
private void addUserInfo(Element retRoot, Subject user) { private void addUserInfo(Element retRoot, Subject user) {
if (user != null && user.isAuthenticated()){ if (user != null && user.isAuthenticated()) {
Element userInfo = new Element("UserInfo"); Element userInfo = new Element("UserInfo");
if(!user.getPrincipal().equals(AuthenticationUtils.ANONYMOUS_USER.getPrincipal())) { if (!user.getPrincipal().equals(AuthenticationUtils.ANONYMOUS_USER.getPrincipal())) {
// TODO: deprecated // TODO: deprecated
addNameAndRealm(retRoot, user); addNameAndRealm(retRoot, user);
// this is the new, correct way // this is the new, correct way
addNameAndRealm(userInfo, user); addNameAndRealm(userInfo, user);
} }
addRoles(userInfo, user); addRoles(userInfo, user);
} }
} }
/** /**
* Add all roles of the current user to the user info, like this: * Add all roles of the current user to the user info, like this:
* `<UserInfo><Roles><Role>role1</Role><Role>role2</Role>...</Roles></UserInfo>` * `<UserInfo><Roles><Role>role1</Role><Role>role2</Role>...</Roles></UserInfo>`
* *
* @param userInfo * @param userInfo
* @param user * @param user
*/ */
private void addRoles(Element userInfo, Subject user) { private void addRoles(Element userInfo, Subject user) {
Element roles = new Element("Roles"); Element roles = new Element("Roles");
for(String role : UserSources.resolve(user.getPrincipals())){ for (String role : UserSources.resolve(user.getPrincipals())) {
Element r = new Element("Role"); Element r = new Element("Role");
r.addContent(role); r.addContent(role);
roles.addContent(r); roles.addContent(r);
...@@ -208,6 +209,7 @@ public abstract class AbstractCaosDBServerResource extends ServerResource { ...@@ -208,6 +209,7 @@ public abstract class AbstractCaosDBServerResource extends ServerResource {
/** /**
* Add the username and the realm of the current user to the user info (as attributes). * Add the username and the realm of the current user to the user info (as attributes).
*
* @param userInfo * @param userInfo
*/ */
private void addNameAndRealm(Element userInfo, Subject user) { private void addNameAndRealm(Element userInfo, Subject user) {
......
package caosdb.server.resource; package caosdb.server.resource;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import caosdb.server.CaosDBException;
import caosdb.server.accessControl.AnonymousAuthenticationToken;
import caosdb.server.accessControl.AnonymousRealm;
import caosdb.server.database.backend.implementation.MySQL.ConnectionException;
import java.io.IOException; import java.io.IOException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.sql.SQLException; import java.sql.SQLException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.mgt.DefaultSecurityManager; import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.session.mgt.DelegatingSession;
import org.apache.shiro.subject.Subject; import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.DelegatingSubject; import org.apache.shiro.subject.support.DelegatingSubject;
import org.jdom2.Element; import org.jdom2.Element;
import org.junit.Test; import org.junit.Test;
import org.restlet.data.Reference; import org.restlet.data.Reference;
import org.restlet.representation.Representation; import org.restlet.representation.Representation;
import caosdb.server.CaosDBException;
import caosdb.server.accessControl.AnonymousAuthenticationToken;
import caosdb.server.accessControl.AnonymousRealm;
import caosdb.server.accessControl.CaosDBAuthorizingRealm;
import caosdb.server.database.backend.implementation.MySQL.ConnectionException;
public class TestAbstractCaosDBServerResource { public class TestAbstractCaosDBServerResource {
@Test @Test
public void testReponseRootElement(){ public void testReponseRootElement() {
final Subject user = new DelegatingSubject(new DefaultSecurityManager(new AnonymousRealm())); final Subject user = new DelegatingSubject(new DefaultSecurityManager(new AnonymousRealm()));
user.login(AnonymousAuthenticationToken.getInstance()); user.login(AnonymousAuthenticationToken.getInstance());
AbstractCaosDBServerResource s = new AbstractCaosDBServerResource() { AbstractCaosDBServerResource s =
new AbstractCaosDBServerResource() {
@Override
protected Representation httpGetInChildClass() throws ConnectionException, IOException, @Override
SQLException, CaosDBException, NoSuchAlgorithmException, Exception { protected Representation httpGetInChildClass()
// TODO Auto-generated method stub throws ConnectionException, IOException, SQLException, CaosDBException,
return null; NoSuchAlgorithmException, Exception {
} // TODO Auto-generated method stub
return null;
@Override }
public String getSRID() {
return "TEST-SRID"; @Override
} public String getSRID() {
return "TEST-SRID";
}
@Override
public String getCRID() {
return "TEST-CRID";
}
@Override
public Long getTimestamp() {
return 0L;
}
@Override
public Reference getRootRef() {
return new Reference("https://example.com/root/");
}
@Override @Override
public String getCRID() { public Subject getUser() {
return "TEST-CRID"; // TODO Auto-generated method stub
} return user;
}
@Override };
public Long getTimestamp() {
return 0L;
}
@Override
public Reference getRootRef() {
return new Reference("https://example.com/root/");
}
@Override
public Subject getUser() {
// TODO Auto-generated method stub
return user;
}
};
Element response = s.generateRootElement(); Element response = s.generateRootElement();
assertNotNull(response); assertNotNull(response);
assertEquals("TEST-SRID", response.getAttribute("srid").getValue()); assertEquals("TEST-SRID", response.getAttribute("srid").getValue());
assertEquals("TEST-CRID", response.getAttribute("crid").getValue()); assertEquals("TEST-CRID", response.getAttribute("crid").getValue());
assertEquals("0", response.getAttribute("timestamp").getValue()); assertEquals("0", response.getAttribute("timestamp").getValue());
assertEquals("https://example.com/root/", response.getAttributeValue("baseuri")); assertEquals("https://example.com/root/", response.getAttributeValue("baseuri"));
Element userInfo = response.getChild("UserInfo"); }
assertNotNull(userInfo);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment