package de.hsadmin.remote; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import java.util.HashMap; import java.util.Map; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.junit.After; import org.junit.Before; import org.junit.Test; public class UnixUserTest { private static final String MODULE = "user"; private XmlRpcClient client; private RemoteCASHelper cas; @Before public void setUp() throws Exception { client = RemoteTestHelper.getClient(); cas = new RemoteCASHelper(); } @After public void tearDown() throws Exception { client = null; cas = null; } @Test public void testSearchAsPacAdmin() { String user = "peh00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; try { Object execute = client.execute(MODULE + ".search", params); Object[] result = (Object[]) execute; assertEquals(22, result.length); for (Object o : result) { if (o instanceof Map) { Map row = (Map) o; assertEquals("peh00", row.get("pac")); } else { fail("map expected"); } } } catch (XmlRpcException e) { fail(e.getMessage()); } } @Test public void testAddAsPacAdmin() { int count = getObjectCount(); try { String user = "peh00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); setParams.put("name", "peh00-test2"); setParams.put("password", "test123"); setParams.put("quota", "128"); setParams.put("quotalimit", "192"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams }; Object execute = client.execute(MODULE + ".add", params); if (execute instanceof Map) { Map entry = (Map) execute; assertEquals("peh00-test2", entry.get("name")); assertEquals("peh00", entry.get("pac")); assertEquals(null, entry.get("password")); } else { fail("map expected"); } } catch (XmlRpcException e) { fail(e.getMessage()); } assertEquals(count + 1, getObjectCount()); } @Test public void testAddAsPacAdminFail() { int count = getObjectCount(); try { String user = "peh00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); setParams.put("name", "peh01-testfail"); setParams.put("password", "test123"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams }; Object execute = client.execute(MODULE + ".add", params); assertNull(execute); fail("should fail"); } catch (XmlRpcException e) { } assertEquals(count, getObjectCount()); } @Test public void testAddWithFalseName() { int count = getObjectCount(); try { String user = "peh00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map setParams = new HashMap(); setParams.put("name", "peh00-langer-name"); setParams.put("password", "test123"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams }; Object execute = client.execute(MODULE + ".add", params); assertNull(execute); fail("exception expected"); } catch (XmlRpcException e) { } assertEquals(count, getObjectCount()); } @Test public void testUpdateAsPacAdmin() { int count = getObjectCount(); try { String user = "peh00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Map setParams = new HashMap(); whereParams.put("name", "peh00-test2"); setParams.put("password", "test1234"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), setParams, whereParams }; Object execute = client.execute(MODULE + ".update", params); if (execute instanceof Object[]) { Object[] a = (Object[]) execute; for (Object o : a) { if (o instanceof Map) { Map entry = (Map) o; assertNull(entry.get("password")); assertEquals("peh00-test2", entry.get("name")); } else { fail("map expected"); } } } else { fail("Object[] axpected"); } } catch (XmlRpcException e) { fail(e.getMessage()); } assertEquals(count, getObjectCount()); } @Test public void testDeleteWithLongName() { int count = getObjectCount(); try { String user = "peh00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); whereParams.put("name", "peh00-langer-name"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; Object execute = client.execute(MODULE + ".delete", params); assertNull(execute); } catch (XmlRpcException e) { fail(e.getMessage()); } assertEquals(count, getObjectCount()); } @Test public void testDeleteAsPacAdminFail() { int count = getObjectCount(); try { String user = "peh01"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); whereParams.put("name", "peh00-test2"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; Object execute = client.execute(MODULE + ".delete", params); assertNull(execute); } catch (XmlRpcException e) { fail(e.getMessage()); } assertEquals(count, getObjectCount()); } @Test public void testDeleteAsPacAdmin() { int count = getObjectCount(); try { String user = "peh00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); whereParams.put("name", "peh00-test2"); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; Object execute = client.execute(MODULE + ".delete", params); assertNull(execute); } catch (XmlRpcException e) { fail(e.getMessage()); } assertEquals(count - 1, getObjectCount()); } private int getObjectCount() { int count = -1; try { String user = "peh00"; String grantingTicketURL = cas.getGrantingTicketURL(user); Map whereParams = new HashMap(); Object[] params = new Object[] { user, cas.getServiceTicket(grantingTicketURL, RemoteTestHelper.getBackendURL()), whereParams }; Object execute = client.execute(MODULE + ".search", params); Object[] result = (Object[]) execute; count = result.length; } catch (XmlRpcException e) { fail(e.getMessage()); } return count; } }