Skip to content

Commit

Permalink
Merge pull request sakaiproject#1970 from ddelblanco/SAK-30521
Browse files Browse the repository at this point in the history
SAK-30521 Batch webservices for enroll and remove users in a site
  • Loading branch information
ern committed Mar 25, 2016
2 parents 14b8c6c + b9a299f commit 878b122
Show file tree
Hide file tree
Showing 3 changed files with 380 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,50 @@ public String addMemberToSiteWithRole(
return "success";
}

/**
* Add a user to a site with a given role
*
* @param sessionid the id of a valid session
* @param siteid the id of the site to add the user to
* @param eids the login usernames (ie jsmith26) separated by commas of the user you want to add to the site
* @param roleid the id of the role to to give the user in the site
* @return success or exception message
*
* TODO: fix for if the role doesn't exist in the site, it is still returning success - SAK-15334
*/
@WebMethod
@Path("/addMemberToSiteWithRoleBatch")
@Produces("text/plain")
@GET
public String addMemberToSiteWithRoleBatch(
@WebParam(name = "sessionid", partName = "sessionid") @QueryParam("sessionid") String sessionid,
@WebParam(name = "siteid", partName = "siteid") @QueryParam("siteid") String siteid,
@WebParam(name = "eids", partName = "eids") @QueryParam("eids") String eids,
@WebParam(name = "roleid", partName = "roleid") @QueryParam("roleid") String roleid) {

Session session = establishSession(sessionid);

if (!securityService.isSuperUser(session.getUserId())) {
LOG.warn("NonSuperUser trying to addMemberToSiteWithRoleBatch: " + session.getUserId());
throw new RuntimeException("NonSuperUser trying to addMemberToSiteWithRoleBatch: " + session.getUserId());
}

try {
Site site = siteService.getSite(siteid);
List<String> eidsList = Arrays.asList(eids.split(","));
for (String eid : eidsList) {
String userid = userDirectoryService.getUserByEid(eid).getId();
site.addMember(userid,roleid,true,false);
}
siteService.save(site);
}
catch (Exception e) {
LOG.error("WS addMemberToSiteWithRoleBatch(): " + e.getClass().getName() + " : " + e.getMessage());
return e.getClass().getName() + " : " + e.getMessage();
}
return "success";
}

/**
* Create a new site
*
Expand Down Expand Up @@ -2580,6 +2624,47 @@ public String removeMemberFromSite(
return "success";
}

/**
* Removes a member from a given site, similar to removeMembeForAuthzGroup but acts on Site directly and uses a
* list of users
*
* @param sessionid the id of a valid session
* @param siteid the id of the site you want to remove the users from
* @param eids comma separated list of users eid
* @return success or string containing error
* @throws AxisFault
*
*/
@Path("/removeMemberFromSiteBatch")
@Produces("text/plain")
@GET
public String removeMemberFromSiteBatch(
@WebParam(name = "sessionid", partName = "sessionid") @QueryParam("sessionid") String sessionid,
@WebParam(name = "siteid", partName = "siteid") @QueryParam("siteid") String siteid,
@WebParam(name = "eids", partName = "eid") @QueryParam("eids") String eids) {

Session session = establishSession(sessionid);

if (!securityService.isSuperUser(session.getUserId())) {
LOG.warn("NonSuperUser trying to removeMemberFromSiteBatch: " + session.getUserId());
throw new RuntimeException("NonSuperUser trying to removeMemberFromSiteBatch: " + session.getUserId());
}

try {
Site site = siteService.getSite(siteid);
List<String> eidsList = Arrays.asList(eids.split(","));
for (String eid : eidsList) {
String userid = userDirectoryService.getUserByEid(eid).getId();
site.removeMember(userid);
}
siteService.save(site);
} catch (Exception e) {
LOG.error("WS removeMemberFromSiteBatch(): " + e.getClass().getName() + " : " + e.getMessage());
return e.getClass().getName() + " : " + e.getMessage();
}
return "success";
}


/**
* Check if a user is in a particular authzgroup
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* Copyright (c) 2005 The Apereo Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/ecl2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sakaiproject.webservices;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.withSettings;


import org.apache.cxf.jaxrs.client.WebClient;
import org.junit.Test;
import org.sakaiproject.tool.api.Session;
import org.sakaiproject.entity.api.ResourcePropertiesEdit;
import org.sakaiproject.user.api.User;
import org.sakaiproject.site.api.Site;
import org.junit.rules.ExpectedException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.RuntimeException;

public class SakaiScriptAddMemberToSiteWithRoleBatchTest extends AbstractCXFTest {
public static final String SESSION_ID = "***SESSION_HAS_BEEN_MOCKERIZED***";
public static final String NULL_SESSION = "***NULL_SESSION***";
private static final String SOAP_OPERATION = "addMemberToSiteWithRoleBatch";

public ExpectedException thrown = ExpectedException.none();

@Override
protected <T extends AbstractWebService> Class<T> getTestClass() {
return (Class<T>) SakaiScript.class;
}

@Override
protected String getOperation() {
return SOAP_OPERATION;
}

private void addClientMocks(WebClient client) {
addCXFClientMocks(client);
}

@Override
protected void addServiceMocks(AbstractWebService service) {

User mockUser1 = mock(User.class);
User mockUser2 = mock(User.class);
Site mockSite = mock(Site.class);

when(service.securityService.isSuperUser("admin")).thenReturn(true);

try {
when(service.siteService.getSite("site1")).thenReturn(mockSite);
when(service.siteService.getSite("nosite")).thenReturn(null);
when(service.userDirectoryService.getUserByEid("user1")).thenReturn(mockUser1);
when(service.userDirectoryService.getUserByEid("user2")).thenReturn(mockUser2);
when(service.userDirectoryService.getUserByEid("nouser")).thenReturn(null);
when(mockUser1.getId()).thenReturn("user1");
when(mockUser2.getId()).thenReturn("user2");
} catch (Exception e) {
}


Session mockSession = mock(Session.class);
when(service.sessionManager.getSession(SESSION_ID)).thenReturn(mockSession);
when(service.sessionManager.getSession(NULL_SESSION)).thenReturn(null);
when(mockSession.getUserId()).thenReturn("admin");

}

@Test
public void testRemoveMemberFromSiteBatch() {
WebClient client = WebClient.create(getFullEndpointAddress());

addClientMocks(client);

// client call
client.accept("text/plain");
client.path("/" + getOperation());
client.query("sessionid", SESSION_ID);
client.query("siteid", "site1");
client.query("eids", "user1,user2");
client.query("roleid", "student");

// client result
String result = client.get(String.class);

// test verifications
assertNotNull(result);
assertEquals("success", result);
}

@Test
public void testRemoveMemberFromSiteBatchNotExistingUser() {
WebClient client = WebClient.create(getFullEndpointAddress());

addClientMocks(client);

// client call
client.accept("text/plain");
client.path("/" + getOperation());
client.query("sessionid", SESSION_ID);
client.query("siteid", "site1");
client.query("eids", "user1,nouser");
client.query("roleid", "student");

// client result
thrown.expect(RuntimeException.class);
client.get(String.class);

}

@Test
public void testRemoveMemberFromSiteBatchNotExistingSite() {
WebClient client = WebClient.create(getFullEndpointAddress());
//MODIFY
addClientMocks(client);

// client call
client.accept("text/plain");
client.path("/" + getOperation());
client.query("sessionid", SESSION_ID);
client.query("siteid", "nosite");
client.query("eids", "user1,nouser");
client.query("roleid", "student");

// client result
thrown.expect(RuntimeException.class);
client.get(String.class);

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* Copyright (c) 2005 The Apereo Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/ecl2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sakaiproject.webservices;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.withSettings;


import org.apache.cxf.jaxrs.client.WebClient;
import org.junit.Test;
import org.sakaiproject.tool.api.Session;
import org.sakaiproject.entity.api.ResourcePropertiesEdit;
import org.sakaiproject.user.api.User;
import org.sakaiproject.site.api.Site;
import org.junit.rules.ExpectedException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.RuntimeException;

public class SakaiScriptRemoveMemberFromSiteBatchTest extends AbstractCXFTest {
public static final String SESSION_ID = "***SESSION_HAS_BEEN_MOCKERIZED***";
public static final String NULL_SESSION = "***NULL_SESSION***";
private static final String SOAP_OPERATION = "removeMemberFromSiteBatch";

public ExpectedException thrown = ExpectedException.none();

@Override
protected <T extends AbstractWebService> Class<T> getTestClass() {
return (Class<T>) SakaiScript.class;
}

@Override
protected String getOperation() {
return SOAP_OPERATION;
}

private void addClientMocks(WebClient client) {
addCXFClientMocks(client);
}

@Override
protected void addServiceMocks(AbstractWebService service) {

User mockUser1 = mock(User.class);
User mockUser2 = mock(User.class);
Site mockSite = mock(Site.class);

when(service.securityService.isSuperUser("admin")).thenReturn(true);

try {
when(service.siteService.getSite("site1")).thenReturn(mockSite);
when(service.siteService.getSite("nosite")).thenReturn(null);
when(service.userDirectoryService.getUserByEid("user1")).thenReturn(mockUser1);
when(service.userDirectoryService.getUserByEid("user2")).thenReturn(mockUser2);
when(service.userDirectoryService.getUserByEid("nouser")).thenReturn(null);
when(mockUser1.getId()).thenReturn("user1");
when(mockUser2.getId()).thenReturn("user2");
} catch (Exception e) {
}


Session mockSession = mock(Session.class);
when(service.sessionManager.getSession(SESSION_ID)).thenReturn(mockSession);
when(service.sessionManager.getSession(NULL_SESSION)).thenReturn(null);
when(mockSession.getUserId()).thenReturn("admin");

}

@Test
public void testRemoveMemberFromSiteBatch() {
WebClient client = WebClient.create(getFullEndpointAddress());

addClientMocks(client);

// client call
client.accept("text/plain");
client.path("/" + getOperation());
client.query("sessionid", SESSION_ID);
client.query("siteid", "site1");
client.query("eids", "user1,user2");

// client result
String result = client.get(String.class);

// test verifications
assertNotNull(result);
assertEquals("success", result);
}

@Test
public void testRemoveMemberFromSiteBatchNotExistingUser() {
WebClient client = WebClient.create(getFullEndpointAddress());

addClientMocks(client);

// client call
client.accept("text/plain");
client.path("/" + getOperation());
client.query("sessionid", SESSION_ID);
client.query("siteid", "site1");
client.query("eids", "user1,nouser");

// client result
thrown.expect(RuntimeException.class);
client.get(String.class);

}

@Test
public void testRemoveMemberFromSiteBatchNotExistingSite() {
WebClient client = WebClient.create(getFullEndpointAddress());
//MODIFY
addClientMocks(client);

// client call
client.accept("text/plain");
client.path("/" + getOperation());
client.query("sessionid", SESSION_ID);
client.query("siteid", "nosite");
client.query("eids", "user1,nouser");

// client result
thrown.expect(RuntimeException.class);
client.get(String.class);

}


}

0 comments on commit 878b122

Please sign in to comment.