-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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] Create reject account request endpoint #12985
Merged
jayasting98
merged 16 commits into
TEAMMATES:account-request-form
from
xenosf:arf-reject-request-endpoint
Apr 9, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bd08202
Create account request rejection endpoint
xenosf ce21b37
Add validation
xenosf 09ce7a3
Add check for already rejected request when sending email
xenosf e58bb5b
Add integration test cases
xenosf a8cfdfc
Set request method to post
xenosf 5fb04f7
Fix lint errors
xenosf 5d57d9b
Update tests list
xenosf 66e3acc
Update validation check
xenosf 391dcb6
Add test for validation
xenosf 4a61002
Fix lint errors
xenosf bb81cae
Fix validation comparison
xenosf 6f00d81
Fix error message test
xenosf 6c6078c
Merge branch 'account-request-form' into arf-reject-request-endpoint
xenosf a2ada58
Add email sending
xenosf 0631604
Update test cases
xenosf fe113ca
Refactor reason check code for clarity
xenosf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
208 changes: 208 additions & 0 deletions
208
src/it/java/teammates/it/ui/webapi/RejectAccountRequestActionIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
package teammates.it.ui.webapi; | ||
|
||
import java.util.UUID; | ||
|
||
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.Config; | ||
import teammates.common.util.Const; | ||
import teammates.common.util.EmailType; | ||
import teammates.common.util.EmailWrapper; | ||
import teammates.common.util.HibernateUtil; | ||
import teammates.common.util.SanitizationHelper; | ||
import teammates.storage.sqlentity.AccountRequest; | ||
import teammates.storage.sqlentity.Course; | ||
import teammates.ui.output.AccountRequestData; | ||
import teammates.ui.request.AccountRequestRejectionRequest; | ||
import teammates.ui.request.InvalidHttpRequestBodyException; | ||
import teammates.ui.webapi.EntityNotFoundException; | ||
import teammates.ui.webapi.InvalidHttpParameterException; | ||
import teammates.ui.webapi.InvalidOperationException; | ||
import teammates.ui.webapi.JsonResult; | ||
import teammates.ui.webapi.RejectAccountRequestAction; | ||
|
||
/** | ||
* SUT: {@link RejectAccountRequestAction}. | ||
*/ | ||
public class RejectAccountRequestActionIT extends BaseActionIT<RejectAccountRequestAction> { | ||
|
||
private static final String TYPICAL_TITLE = "We are Unable to Create an Account for you"; | ||
private static final String TYPICAL_BODY = new StringBuilder() | ||
.append("<p>Hi, Example</p>\n") | ||
.append("<p>Thanks for your interest in using TEAMMATES. ") | ||
.append("We are unable to create a TEAMMATES instructor account for you.</p>\n\n") | ||
.append("<p>\n") | ||
.append(" <strong>Reason:</strong> The email address you provided ") | ||
.append("is not an 'official' email address provided by your institution.<br />\n") | ||
.append(" <strong>Remedy:</strong> ") | ||
.append("Please re-submit an account request with your 'official' institution email address.\n") | ||
.append("</p>\n\n") | ||
.append("<p>If you need further clarification or would like to appeal this decision, ") | ||
.append("please feel free to contact us at [email protected].</p>\n") | ||
.append("<p>Regards,<br />TEAMMATES Team.</p>\n") | ||
.toString(); | ||
|
||
@Override | ||
@BeforeMethod | ||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
persistDataBundle(typicalBundle); | ||
HibernateUtil.flushSession(); | ||
} | ||
|
||
@Override | ||
protected String getActionUri() { | ||
return Const.ResourceURIs.ACCOUNT_REQUEST_REJECTION; | ||
} | ||
|
||
@Override | ||
protected String getRequestMethod() { | ||
return POST; | ||
} | ||
|
||
@Override | ||
public void testExecute() throws Exception { | ||
// See individual test methods below | ||
} | ||
|
||
@Test | ||
protected void testExecute_withReasonTitleAndBody_shouldRejectWithEmail() | ||
throws InvalidOperationException, InvalidHttpRequestBodyException { | ||
AccountRequest accountRequest = typicalBundle.accountRequests.get("unregisteredInstructor1"); | ||
accountRequest.setStatus(AccountRequestStatus.PENDING); | ||
UUID id = accountRequest.getId(); | ||
|
||
AccountRequestRejectionRequest requestBody = new AccountRequestRejectionRequest(TYPICAL_TITLE, TYPICAL_BODY); | ||
String[] params = new String[] {Const.ParamsNames.ACCOUNT_REQUEST_ID, id.toString()}; | ||
|
||
RejectAccountRequestAction action = getAction(requestBody, params); | ||
JsonResult result = action.execute(); | ||
|
||
assertEquals(200, result.getStatusCode()); | ||
|
||
AccountRequestData data = (AccountRequestData) result.getOutput(); | ||
assertEquals(accountRequest.getName(), data.getName()); | ||
assertEquals(accountRequest.getEmail(), data.getEmail()); | ||
assertEquals(accountRequest.getInstitute(), data.getInstitute()); | ||
assertEquals(AccountRequestStatus.REJECTED, data.getStatus()); | ||
assertEquals(accountRequest.getComments(), data.getComments()); | ||
|
||
verifyNumberOfEmailsSent(1); | ||
EmailWrapper sentEmail = mockEmailSender.getEmailsSent().get(0); | ||
assertEquals(EmailType.ACCOUNT_REQUEST_REJECTION, sentEmail.getType()); | ||
assertEquals(Config.SUPPORT_EMAIL, sentEmail.getBcc()); | ||
assertEquals(accountRequest.getEmail(), sentEmail.getRecipient()); | ||
assertEquals(SanitizationHelper.sanitizeForRichText(TYPICAL_BODY), sentEmail.getContent()); | ||
assertEquals("TEAMMATES: " + TYPICAL_TITLE, sentEmail.getSubject()); | ||
} | ||
|
||
@Test | ||
protected void testExecute_withoutReasonTitleAndBody_shouldRejectWithoutEmail() | ||
throws InvalidOperationException, InvalidHttpRequestBodyException { | ||
AccountRequest accountRequest = typicalBundle.accountRequests.get("unregisteredInstructor1"); | ||
accountRequest.setStatus(AccountRequestStatus.PENDING); | ||
UUID id = accountRequest.getId(); | ||
|
||
AccountRequestRejectionRequest requestBody = new AccountRequestRejectionRequest(null, null); | ||
String[] params = new String[] {Const.ParamsNames.ACCOUNT_REQUEST_ID, id.toString()}; | ||
|
||
RejectAccountRequestAction action = getAction(requestBody, params); | ||
JsonResult result = action.execute(); | ||
|
||
assertEquals(200, result.getStatusCode()); | ||
|
||
AccountRequestData data = (AccountRequestData) result.getOutput(); | ||
assertEquals(accountRequest.getName(), data.getName()); | ||
assertEquals(accountRequest.getEmail(), data.getEmail()); | ||
assertEquals(accountRequest.getInstitute(), data.getInstitute()); | ||
assertEquals(AccountRequestStatus.REJECTED, data.getStatus()); | ||
assertEquals(accountRequest.getComments(), data.getComments()); | ||
|
||
verifyNoEmailsSent(); | ||
} | ||
|
||
@Test | ||
protected void testExecute_withReasonBodyButNoTitle_shouldThrow() { | ||
AccountRequest accountRequest = typicalBundle.accountRequests.get("unregisteredInstructor1"); | ||
UUID id = accountRequest.getId(); | ||
|
||
AccountRequestRejectionRequest requestBody = new AccountRequestRejectionRequest(null, TYPICAL_BODY); | ||
String[] params = new String[] {Const.ParamsNames.ACCOUNT_REQUEST_ID, id.toString()}; | ||
|
||
InvalidHttpRequestBodyException ihrbe = verifyHttpRequestBodyFailure(requestBody, params); | ||
|
||
assertEquals("Both reason body and title need to be null to reject silently", ihrbe.getMessage()); | ||
verifyNoEmailsSent(); | ||
} | ||
|
||
@Test | ||
protected void testExecute_withReasonTitleButNoBody_shouldThrow() { | ||
AccountRequest accountRequest = typicalBundle.accountRequests.get("unregisteredInstructor1"); | ||
UUID id = accountRequest.getId(); | ||
|
||
AccountRequestRejectionRequest requestBody = new AccountRequestRejectionRequest(TYPICAL_TITLE, null); | ||
String[] params = new String[] {Const.ParamsNames.ACCOUNT_REQUEST_ID, id.toString()}; | ||
|
||
InvalidHttpRequestBodyException ihrbe = verifyHttpRequestBodyFailure(requestBody, params); | ||
|
||
assertEquals("Both reason body and title need to be null to reject silently", ihrbe.getMessage()); | ||
verifyNoEmailsSent(); | ||
} | ||
|
||
@Test | ||
protected void testExecute_alreadyRejected_shouldNotSendEmail() | ||
throws InvalidOperationException, InvalidHttpRequestBodyException { | ||
AccountRequest accountRequest = typicalBundle.accountRequests.get("unregisteredInstructor1"); | ||
accountRequest.setStatus(AccountRequestStatus.REJECTED); | ||
UUID id = accountRequest.getId(); | ||
|
||
AccountRequestRejectionRequest requestBody = new AccountRequestRejectionRequest(TYPICAL_TITLE, TYPICAL_BODY); | ||
String[] params = new String[] {Const.ParamsNames.ACCOUNT_REQUEST_ID, id.toString()}; | ||
|
||
RejectAccountRequestAction action = getAction(requestBody, params); | ||
JsonResult result = action.execute(); | ||
|
||
assertEquals(result.getStatusCode(), 200); | ||
|
||
AccountRequestData data = (AccountRequestData) result.getOutput(); | ||
assertEquals(accountRequest.getName(), data.getName()); | ||
assertEquals(accountRequest.getEmail(), data.getEmail()); | ||
assertEquals(accountRequest.getInstitute(), data.getInstitute()); | ||
assertEquals(accountRequest.getStatus(), data.getStatus()); | ||
assertEquals(accountRequest.getComments(), data.getComments()); | ||
|
||
verifyNoEmailsSent(); | ||
} | ||
|
||
@Test | ||
protected void testExecute_invalidUuid_shouldThrow() { | ||
AccountRequestRejectionRequest requestBody = new AccountRequestRejectionRequest(null, null); | ||
String[] params = new String[] {Const.ParamsNames.ACCOUNT_REQUEST_ID, "invalid"}; | ||
|
||
InvalidHttpParameterException ihpe = verifyHttpParameterFailure(requestBody, params); | ||
assertEquals("Invalid UUID string: invalid", ihpe.getMessage()); | ||
verifyNoEmailsSent(); | ||
} | ||
|
||
@Test | ||
protected void testExecute_accountRequestNotFound_shouldThrow() { | ||
AccountRequestRejectionRequest requestBody = new AccountRequestRejectionRequest(null, null); | ||
String uuid = UUID.randomUUID().toString(); | ||
String[] params = new String[] {Const.ParamsNames.ACCOUNT_REQUEST_ID, uuid}; | ||
|
||
EntityNotFoundException enfe = verifyEntityNotFound(requestBody, params); | ||
assertEquals(String.format("Account request with id = %s not found", uuid), enfe.getMessage()); | ||
verifyNoEmailsSent(); | ||
} | ||
|
||
@Override | ||
@Test | ||
protected void testAccessControl() throws InvalidParametersException, EntityAlreadyExistsException { | ||
Course course = typicalBundle.courses.get("course1"); | ||
verifyOnlyAdminCanAccess(course); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/teammates/ui/request/AccountRequestRejectionRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package teammates.ui.request; | ||
|
||
import java.util.Objects; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import teammates.common.util.SanitizationHelper; | ||
|
||
/** | ||
* The request reasonBody for rejecting an account request. | ||
*/ | ||
public class AccountRequestRejectionRequest extends BasicRequest { | ||
@Nullable | ||
private String reasonTitle; | ||
|
||
@Nullable | ||
private String reasonBody; | ||
|
||
public AccountRequestRejectionRequest(String reasonTitle, String reasonBody) { | ||
this.reasonTitle = SanitizationHelper.sanitizeTitle(reasonTitle); | ||
this.reasonBody = SanitizationHelper.sanitizeForRichText(reasonBody); | ||
} | ||
|
||
@Override | ||
public void validate() throws InvalidHttpRequestBodyException { | ||
if (reasonBody == null || reasonTitle == null) { | ||
assertTrue(Objects.equals(reasonBody, reasonTitle), | ||
"Both reason body and title need to be null to reject silently"); | ||
} | ||
} | ||
|
||
public String getReasonTitle() { | ||
return this.reasonTitle; | ||
} | ||
|
||
public String getReasonBody() { | ||
return this.reasonBody; | ||
} | ||
|
||
/** | ||
* Returns true if both reason body and title are non-null. | ||
*/ | ||
public boolean checkHasReason() { | ||
return this.reasonBody != null && this.reasonTitle != null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/teammates/ui/webapi/RejectAccountRequestAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package teammates.ui.webapi; | ||
|
||
import java.util.UUID; | ||
|
||
import teammates.common.datatransfer.AccountRequestStatus; | ||
import teammates.common.exception.EntityDoesNotExistException; | ||
import teammates.common.exception.InvalidParametersException; | ||
import teammates.common.util.Const; | ||
import teammates.common.util.EmailWrapper; | ||
import teammates.storage.sqlentity.AccountRequest; | ||
import teammates.ui.output.AccountRequestData; | ||
import teammates.ui.request.AccountRequestRejectionRequest; | ||
import teammates.ui.request.InvalidHttpRequestBodyException; | ||
|
||
/** | ||
* Rejects an account request. | ||
*/ | ||
public class RejectAccountRequestAction extends AdminOnlyAction { | ||
|
||
@Override | ||
public JsonResult execute() throws InvalidOperationException, InvalidHttpRequestBodyException { | ||
String id = getNonNullRequestParamValue(Const.ParamsNames.ACCOUNT_REQUEST_ID); | ||
UUID accountRequestId; | ||
|
||
try { | ||
accountRequestId = UUID.fromString(id); | ||
} catch (IllegalArgumentException e) { | ||
throw new InvalidHttpParameterException(e.getMessage(), e); | ||
} | ||
|
||
AccountRequest accountRequest = sqlLogic.getAccountRequest(accountRequestId); | ||
|
||
if (accountRequest == null) { | ||
String errorMessage = String.format(Const.ACCOUNT_REQUEST_NOT_FOUND, accountRequestId.toString()); | ||
throw new EntityNotFoundException(errorMessage); | ||
} | ||
|
||
AccountRequestRejectionRequest accountRequestRejectionRequest = | ||
getAndValidateRequestBody(AccountRequestRejectionRequest.class); | ||
AccountRequestStatus initialStatus = accountRequest.getStatus(); | ||
|
||
try { | ||
accountRequest.setStatus(AccountRequestStatus.REJECTED); | ||
accountRequest = sqlLogic.updateAccountRequest(accountRequest); | ||
if (accountRequestRejectionRequest.checkHasReason() | ||
&& initialStatus != AccountRequestStatus.REJECTED) { | ||
EmailWrapper email = sqlEmailGenerator.generateAccountRequestRejectionEmail(accountRequest, | ||
accountRequestRejectionRequest.getReasonTitle(), accountRequestRejectionRequest.getReasonBody()); | ||
emailSender.sendEmail(email); | ||
} | ||
} catch (InvalidParametersException e) { | ||
throw new InvalidHttpRequestBodyException(e); | ||
} catch (EntityDoesNotExistException e) { | ||
throw new EntityNotFoundException(e); | ||
} | ||
|
||
return new JsonResult(new AccountRequestData(accountRequest)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is already a method for this,
getUuidRequestParamValue
. It would probably be better to use that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xenosf let's submit a separate PR to address this