Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#11878] Add GetAllPendingAccountRequests API #12927

Merged
109 changes: 109 additions & 0 deletions src/it/java/teammates/it/ui/webapi/GetAccountRequestsActionIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package teammates.it.ui.webapi;

import java.util.List;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import teammates.common.datatransfer.AccountRequestStatus;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.Const;
import teammates.common.util.HibernateUtil;
import teammates.storage.sqlentity.AccountRequest;
import teammates.storage.sqlentity.Course;
import teammates.ui.output.AccountRequestData;
import teammates.ui.output.AccountRequestsData;
import teammates.ui.webapi.GetAccountRequestsAction;
import teammates.ui.webapi.JsonResult;

/**
* SUT: {@link GetAccountRequestsAction}.
*/
public class GetAccountRequestsActionIT extends BaseActionIT<GetAccountRequestsAction> {
private final String[] validParams = { Const.ParamsNames.ACCOUNT_REQUEST_STATUS, "pending" };

@Override
@BeforeMethod
protected void setUp() throws Exception {
super.setUp();
persistDataBundle(typicalBundle);
HibernateUtil.flushSession();
}

@Override
protected String getActionUri() {
return Const.ResourceURIs.ACCOUNT_REQUESTS;
}

@Override
protected String getRequestMethod() {
return GET;
}

@Override
@Test
public void testExecute() {
______TS("accountrequeststatus param is null");

verifyHttpParameterFailure();
verifyHttpParameterFailure(Const.ParamsNames.ACCOUNT_REQUEST_STATUS, "pendin");

______TS("No pending account requests initially");

GetAccountRequestsAction action = getAction(this.validParams);
JsonResult result = getJsonResult(action);
AccountRequestsData data = (AccountRequestsData) result.getOutput();
List<AccountRequestData> arData = data.getAccountRequests();

assertEquals(0, arData.size());

______TS("1 pending account request, case insensitive match for status request param");

AccountRequest accountRequest1 = typicalBundle.accountRequests.get("instructor1");
accountRequest1.setStatus(AccountRequestStatus.PENDING);

String[] params = { Const.ParamsNames.ACCOUNT_REQUEST_STATUS, "PendinG" };
action = getAction(params);
result = getJsonResult(action);
data = (AccountRequestsData) result.getOutput();
arData = data.getAccountRequests();

assertEquals(1, arData.size());

______TS("Get 2 pending account requests, ignore 1 approved account request");
AccountRequest approvedAccountRequest1 = typicalBundle.accountRequests.get("instructor2");
approvedAccountRequest1.setStatus(AccountRequestStatus.APPROVED);

accountRequest1 = typicalBundle.accountRequests.get("instructor1");
AccountRequest accountRequest2 = typicalBundle.accountRequests.get("instructor1OfCourse2");
accountRequest1.setStatus(AccountRequestStatus.PENDING);
accountRequest2.setStatus(AccountRequestStatus.PENDING);

action = getAction(this.validParams);
result = getJsonResult(action);
data = (AccountRequestsData) result.getOutput();
arData = data.getAccountRequests();

assertEquals(2, arData.size());

// account request 1
assertEquals(arData.get(0).getEmail(), accountRequest1.getEmail());
assertEquals(arData.get(0).getInstitute(), accountRequest1.getInstitute());
assertEquals(arData.get(0).getName(), accountRequest1.getName());
assertEquals(arData.get(0).getRegistrationKey(), accountRequest1.getRegistrationKey());

// account request 2
assertEquals(arData.get(1).getEmail(), accountRequest2.getEmail());
assertEquals(arData.get(1).getInstitute(), accountRequest2.getInstitute());
assertEquals(arData.get(1).getName(), accountRequest2.getName());
assertEquals(arData.get(1).getRegistrationKey(), accountRequest2.getRegistrationKey());
}

@Override
@Test
public void testAccessControl() throws InvalidParametersException, EntityAlreadyExistsException {
Course course = typicalBundle.courses.get("course1");
verifyOnlyAdminCanAccess(course);
}
}
2 changes: 2 additions & 0 deletions src/main/java/teammates/common/util/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public static class ParamsNames {
public static final String INSTRUCTOR_INSTITUTION = "instructorinstitution";
public static final String IS_CREATING_ACCOUNT = "iscreatingaccount";
public static final String IS_INSTRUCTOR = "isinstructor";
public static final String ACCOUNT_REQUEST_STATUS = "status";

public static final String FEEDBACK_SESSION_NAME = "fsname";
public static final String FEEDBACK_SESSION_STARTTIME = "starttime";
Expand Down Expand Up @@ -332,6 +333,7 @@ public static class ResourceURIs {
public static final String ACCOUNT = URI_PREFIX + "/account";
public static final String ACCOUNT_RESET = URI_PREFIX + "/account/reset";
public static final String ACCOUNT_REQUEST = URI_PREFIX + "/account/request";
public static final String ACCOUNT_REQUESTS = URI_PREFIX + "/account/requests";
public static final String ACCOUNT_REQUEST_RESET = ACCOUNT_REQUEST + "/reset";
public static final String ACCOUNTS = URI_PREFIX + "/accounts";
public static final String RESPONSE_COMMENT = URI_PREFIX + "/responsecomment";
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/teammates/sqllogic/api/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ public void deleteAccountRequest(String email, String institute) {
accountRequestLogic.deleteAccountRequest(email, institute);
}

/**
* Gets all pending account requests.
*/
public List<AccountRequest> getPendingAccountRequests() {
return accountRequestLogic.getPendingAccountRequests();
}

/**
* Gets an account.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ public AccountRequest getAccountRequestByRegistrationKey(String regkey) {
return accountRequestDb.getAccountRequestByRegistrationKey(regkey);
}

/**
* Gets all pending account requests.
*/
public List<AccountRequest> getPendingAccountRequests() {
return accountRequestDb.getPendingAccountRequests();
}

/**
* Creates/resets the account request with the given email and institute such that it is not registered.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/teammates/storage/sqlapi/AccountRequestsDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.UUID;

import teammates.common.datatransfer.AccountRequestStatus;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.exception.SearchServiceException;
Expand Down Expand Up @@ -68,6 +69,19 @@ public AccountRequest getAccountRequest(String email, String institute) {
return query.getResultStream().findFirst().orElse(null);
}

/**
* Get all Account Requests with {@code status} of 'pending'.
*/
public List<AccountRequest> getPendingAccountRequests() {
CriteriaBuilder cb = HibernateUtil.getCriteriaBuilder();
CriteriaQuery<AccountRequest> cr = cb.createQuery(AccountRequest.class);
Root<AccountRequest> root = cr.from(AccountRequest.class);
cr.select(root).where(cb.equal(root.get("status"), AccountRequestStatus.PENDING));

TypedQuery<AccountRequest> query = HibernateUtil.createQuery(cr);
return query.getResultList();
}

/**
* Get AccountRequest by {@code registrationKey} from database.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum ResourceEndpoints {
ACCOUNT(ResourceURIs.ACCOUNT),
ACCOUNT_RESET(ResourceURIs.ACCOUNT_RESET),
ACCOUNT_REQUEST(ResourceURIs.ACCOUNT_REQUEST),
ACCOUNT_REQUESTS(ResourceURIs.ACCOUNT_REQUESTS),
ACCOUNT_REQUEST_RESET(ResourceURIs.ACCOUNT_REQUEST_RESET),
ACCOUNTS(ResourceURIs.ACCOUNTS),
RESPONSE_COMMENT(ResourceURIs.RESPONSE_COMMENT),
Expand Down
1 change: 1 addition & 0 deletions src/main/java/teammates/ui/webapi/ActionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class ActionFactory {
map(ResourceURIs.ACCOUNT_REQUEST, GET, GetAccountRequestAction.class);
map(ResourceURIs.ACCOUNT_REQUEST, POST, CreateAccountRequestAction.class);
map(ResourceURIs.ACCOUNT_REQUEST, DELETE, DeleteAccountRequestAction.class);
map(ResourceURIs.ACCOUNT_REQUESTS, GET, GetAccountRequestsAction.class);
map(ResourceURIs.ACCOUNT_REQUEST_RESET, PUT, ResetAccountRequestAction.class);
map(ResourceURIs.ACCOUNTS, GET, GetAccountsAction.class);
map(ResourceURIs.COURSE, GET, GetCourseAction.class);
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/teammates/ui/webapi/GetAccountRequestsAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package teammates.ui.webapi;

import java.util.List;
import java.util.stream.Collectors;

import teammates.common.datatransfer.AccountRequestStatus;
import teammates.common.util.Const;
import teammates.storage.sqlentity.AccountRequest;
import teammates.ui.output.AccountRequestData;
import teammates.ui.output.AccountRequestsData;

/**
* Action: Gets pending account requests.
*/
public class GetAccountRequestsAction extends AdminOnlyAction {
@Override
public JsonResult execute() {
String accountRequestStatus = getNonNullRequestParamValue(Const.ParamsNames.ACCOUNT_REQUEST_STATUS);
String pending = AccountRequestStatus.PENDING.name(); // 'PENDING'
if (!pending.equalsIgnoreCase(accountRequestStatus)) {
throw new InvalidHttpParameterException("Only 'pending' is allowed for account request status.");
}

List<AccountRequest> accountRequests = sqlLogic.getPendingAccountRequests();
List<AccountRequestData> accountRequestDatas = accountRequests
.stream()
.map(ar -> new AccountRequestData(ar))
.collect(Collectors.toList());

AccountRequestsData output = new AccountRequestsData();
output.setAccountRequests(accountRequestDatas);
return new JsonResult(output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ protected void testExecute() {
CreateAccountRequestAction.class,
GetAccountRequestAction.class,
DeleteAccountRequestAction.class,
GetAccountRequestsAction.class,
GetAccountAction.class,
GetAccountsAction.class,
FeedbackSessionPublishedRemindersAction.class,
Expand Down
Loading