Search in sources :

Example 11 with AdminEmailAttributes

use of teammates.common.datatransfer.attributes.AdminEmailAttributes in project teammates by TEAMMATES.

the class AdminEmailComposePageActionTest method testExecuteAndPostProcess.

@Override
@Test
public void testExecuteAndPostProcess() throws Exception {
    final String adminUserId = "admin.user";
    gaeSimulation.loginAsAdmin(adminUserId);
    ______TS("compose new email : typical values given : success");
    AdminEmailComposePageAction action = getAction();
    ShowPageResult pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, false, adminUserId), pageResult.getDestinationWithParams());
    String normalLogSegment = "adminEmailComposePage Page Load";
    AssertHelper.assertContains(normalLogSegment, action.getLogMessage());
    assertEquals("", pageResult.getStatusMessage());
    AdminEmailComposePageData data = (AdminEmailComposePageData) pageResult.data;
    assertNull(data.emailToEdit);
    ______TS("edit existing email : typical values given : success");
    // retrieve email id from logic
    AdminEmailAttributes emailData = dataBundle.adminEmails.get("adminEmail1");
    AdminEmailAttributes email = adminEmailsLogic.getAdminEmailBySubject(emailData.subject);
    action = getAction(Const.ParamsNames.ADMIN_EMAIL_ID, email.emailId);
    pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, false, "admin.user"), pageResult.getDestinationWithParams());
    String expectedLogSegment = normalLogSegment + " : Edit Email [Admin Email 1 <b>bold tags</b>]";
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    assertEquals("", pageResult.getStatusMessage());
    data = (AdminEmailComposePageData) pageResult.data;
    assertNotNull(data.emailToEdit);
    ______TS("edit existing email : email does not exist : failure");
    String emailId = "nonexistingEmailId";
    action = getAction(Const.ParamsNames.ADMIN_EMAIL_ID, emailId);
    pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, true, "admin.user"), pageResult.getDestinationWithParams());
    expectedLogSegment = normalLogSegment + " : " + Const.StatusMessages.EMAIL_NOT_FOUND;
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    assertEquals(Const.StatusMessages.EMAIL_NOT_FOUND, pageResult.getStatusMessage());
    data = (AdminEmailComposePageData) pageResult.data;
    assertNull(data.emailToEdit);
}
Also used : ShowPageResult(teammates.ui.controller.ShowPageResult) AdminEmailComposePageAction(teammates.ui.controller.AdminEmailComposePageAction) AdminEmailComposePageData(teammates.ui.pagedata.AdminEmailComposePageData) AdminEmailAttributes(teammates.common.datatransfer.attributes.AdminEmailAttributes) Test(org.testng.annotations.Test)

Example 12 with AdminEmailAttributes

use of teammates.common.datatransfer.attributes.AdminEmailAttributes in project teammates by TEAMMATES.

the class AdminEmailComposeSaveActionTest method testExecuteAndPostProcess.

@Override
@Test
public void testExecuteAndPostProcess() throws Exception {
    final String adminUserId = "admin.user";
    gaeSimulation.loginAsAdmin(adminUserId);
    ______TS("save new email : typical values given : success");
    String subject = "New Email Subject";
    String content = "<p>Email Content</p>";
    String receiver = "test@example.tmt";
    AdminEmailComposeSaveAction action = getAction(Const.ParamsNames.ADMIN_EMAIL_CONTENT, content, Const.ParamsNames.ADMIN_EMAIL_SUBJECT, subject, Const.ParamsNames.ADMIN_EMAIL_ADDRESS_RECEIVERS, receiver);
    ShowPageResult pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, false, adminUserId), pageResult.getDestinationWithParams());
    String expectedLogSegment = Const.StatusMessages.EMAIL_DRAFT_SAVED + ": <br>Subject: New Email Subject";
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    assertEquals(Const.StatusMessages.EMAIL_DRAFT_SAVED, pageResult.getStatusMessage());
    AdminEmailComposePageData data = (AdminEmailComposePageData) pageResult.data;
    assertNull(data.emailToEdit);
    AdminEmailAttributes savedEmail = adminEmailsLogic.getAdminEmailBySubject(subject);
    assertNotNull("Email should be saved and should exists.", savedEmail);
    assertEquals(SanitizationHelper.sanitizeForRichText(content), savedEmail.getContentValue());
    assertEquals(receiver, String.join(", ", savedEmail.getAddressReceiver().toArray(new String[0])));
    ______TS("save new email : invalid subject : failure");
    content = "<p>Email Content</p>";
    subject = "!Not starting with alphanumeric";
    receiver = "test@example.tmt";
    action = getAction(Const.ParamsNames.ADMIN_EMAIL_CONTENT, content, Const.ParamsNames.ADMIN_EMAIL_SUBJECT, subject, Const.ParamsNames.ADMIN_EMAIL_ADDRESS_RECEIVERS, receiver);
    pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, true, adminUserId), pageResult.getDestinationWithParams());
    expectedLogSegment = Const.ACTION_RESULT_FAILURE;
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    String expectedStatus = "\"!Not starting with alphanumeric\" is not acceptable to TEAMMATES as a/an email subject";
    AssertHelper.assertContains(expectedStatus, pageResult.getStatusMessage());
    data = (AdminEmailComposePageData) pageResult.data;
    assertEquals(subject, data.emailToEdit.subject);
    ______TS("save new email : invalid content : failure");
    content = "";
    subject = "valid subject";
    receiver = "test@example.tmt";
    action = getAction(Const.ParamsNames.ADMIN_EMAIL_CONTENT, content, Const.ParamsNames.ADMIN_EMAIL_SUBJECT, subject, Const.ParamsNames.ADMIN_EMAIL_ADDRESS_RECEIVERS, receiver);
    pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, true, adminUserId), pageResult.getDestinationWithParams());
    expectedLogSegment = Const.ACTION_RESULT_FAILURE;
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    expectedStatus = "email content should not be empty.";
    AssertHelper.assertContains(expectedStatus, pageResult.getStatusMessage());
    data = (AdminEmailComposePageData) pageResult.data;
    assertEquals(subject, data.emailToEdit.subject);
    ______TS("save existing email : typical values given : success");
    AdminEmailAttributes emailData = dataBundle.adminEmails.get("adminEmail1");
    AdminEmailAttributes email = adminEmailsLogic.getAdminEmailBySubject(emailData.getSubject());
    String emailId = email.emailId;
    content = "valid content";
    subject = "valid existing email subject";
    receiver = "test@example.tmt, test2@example.tmt";
    action = getAction(Const.ParamsNames.ADMIN_EMAIL_CONTENT, content, Const.ParamsNames.ADMIN_EMAIL_SUBJECT, subject, Const.ParamsNames.ADMIN_EMAIL_ADDRESS_RECEIVERS, receiver, Const.ParamsNames.ADMIN_EMAIL_ID, emailId);
    pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, false, adminUserId), pageResult.getDestinationWithParams());
    expectedLogSegment = Const.StatusMessages.EMAIL_DRAFT_SAVED + ": <br>" + "Subject: valid existing email subject";
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    assertEquals(Const.StatusMessages.EMAIL_DRAFT_SAVED, pageResult.getStatusMessage());
    data = (AdminEmailComposePageData) pageResult.data;
    assertNull(data.emailToEdit);
    savedEmail = adminEmailsLogic.getAdminEmailBySubject(subject);
    assertNotNull("Email should be saved and should exists.", savedEmail);
    assertEquals(SanitizationHelper.sanitizeForRichText(content), savedEmail.getContentValue());
    assertEquals(receiver, String.join(", ", savedEmail.getAddressReceiver().toArray(new String[0])));
    ______TS("save existing email : values require sanitization : success");
    emailId = email.emailId;
    content = "<p onclick=\"alert('hello');\">contents</p> </div> unclosed tags <script>alert(\"hello\");</script>";
    subject = "valid existing email subject <b>To check sanitization</b>";
    receiver = "test@example.tmt, test2@example.tmt, test3@example.tmt";
    action = getAction(Const.ParamsNames.ADMIN_EMAIL_CONTENT, content, Const.ParamsNames.ADMIN_EMAIL_SUBJECT, subject, Const.ParamsNames.ADMIN_EMAIL_ADDRESS_RECEIVERS, receiver, Const.ParamsNames.ADMIN_EMAIL_ID, emailId);
    pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, false, adminUserId), pageResult.getDestinationWithParams());
    expectedLogSegment = Const.StatusMessages.EMAIL_DRAFT_SAVED + ": <br>" + "Subject: valid existing email subject &lt;b&gt;To check sanitization&lt;&#x2f;b&gt;";
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    assertEquals(Const.StatusMessages.EMAIL_DRAFT_SAVED, pageResult.getStatusMessage());
    data = (AdminEmailComposePageData) pageResult.data;
    assertNull(data.emailToEdit);
    savedEmail = adminEmailsLogic.getAdminEmailBySubject(subject);
    assertNotNull("Email should be saved and should exists.", savedEmail);
    assertEquals(SanitizationHelper.sanitizeForRichText(content), savedEmail.getContentValue());
    assertEquals(receiver, String.join(", ", savedEmail.getAddressReceiver().toArray(new String[0])));
    ______TS("save existing email : invalid subject : failure");
    content = "valid content";
    subject = " ";
    receiver = "test@example.tmt";
    action = getAction(Const.ParamsNames.ADMIN_EMAIL_CONTENT, content, Const.ParamsNames.ADMIN_EMAIL_SUBJECT, subject, Const.ParamsNames.ADMIN_EMAIL_ADDRESS_RECEIVERS, receiver, Const.ParamsNames.ADMIN_EMAIL_ID, emailId);
    pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, true, adminUserId), pageResult.getDestinationWithParams());
    expectedLogSegment = Const.ACTION_RESULT_FAILURE;
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    expectedStatus = "The provided email subject is not acceptable to TEAMMATES as it contains only whitespace" + " or contains extra spaces at the beginning or at the end of the text.";
    AssertHelper.assertContains(expectedStatus, pageResult.getStatusMessage());
    data = (AdminEmailComposePageData) pageResult.data;
    assertEquals(subject, data.emailToEdit.subject);
    assertEquals(email.emailId, data.emailToEdit.emailId);
    ______TS("save existing email : invalid content : failure");
    content = "";
    subject = "valid subject";
    receiver = "test@example.tmt";
    action = getAction(Const.ParamsNames.ADMIN_EMAIL_CONTENT, content, Const.ParamsNames.ADMIN_EMAIL_SUBJECT, subject, Const.ParamsNames.ADMIN_EMAIL_ADDRESS_RECEIVERS, receiver, Const.ParamsNames.ADMIN_EMAIL_ID, emailId);
    pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, true, adminUserId), pageResult.getDestinationWithParams());
    expectedLogSegment = Const.ACTION_RESULT_FAILURE;
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    expectedStatus = "email content should not be empty.";
    AssertHelper.assertContains(expectedStatus, pageResult.getStatusMessage());
    data = (AdminEmailComposePageData) pageResult.data;
    assertEquals(subject, data.emailToEdit.subject);
    assertEquals(email.emailId, data.emailToEdit.emailId);
    ______TS("save non-existing email : typical values given : success");
    emailId = "nonExisitingId";
    content = "valid content";
    subject = "valid non-existing email subject <b>To check sanitization</b>";
    receiver = "test@example.tmt";
    action = getAction(Const.ParamsNames.ADMIN_EMAIL_CONTENT, content, Const.ParamsNames.ADMIN_EMAIL_SUBJECT, subject, Const.ParamsNames.ADMIN_EMAIL_ADDRESS_RECEIVERS, receiver, Const.ParamsNames.ADMIN_EMAIL_ID, emailId);
    pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, false, adminUserId), pageResult.getDestinationWithParams());
    expectedLogSegment = Const.StatusMessages.EMAIL_DRAFT_SAVED + ": <br>" + "Subject: valid non-existing email subject &lt;b&gt;To check sanitization&lt;&#x2f;b&gt;";
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    assertEquals(Const.StatusMessages.EMAIL_DRAFT_SAVED, pageResult.getStatusMessage());
    data = (AdminEmailComposePageData) pageResult.data;
    assertNull(data.emailToEdit);
    savedEmail = adminEmailsLogic.getAdminEmailBySubject(subject);
    assertNotNull("Email should be saved and should exists.", savedEmail);
    assertEquals(SanitizationHelper.sanitizeForRichText(content), savedEmail.getContentValue());
    assertEquals(receiver, String.join(", ", savedEmail.getAddressReceiver().toArray(new String[0])));
    ______TS("save non-existing email : invalid subject : failure");
    emailId = "nonExisitingId";
    content = "valid content";
    subject = "";
    receiver = "test@example.tmt";
    action = getAction(Const.ParamsNames.ADMIN_EMAIL_CONTENT, content, Const.ParamsNames.ADMIN_EMAIL_SUBJECT, subject, Const.ParamsNames.ADMIN_EMAIL_ADDRESS_RECEIVERS, receiver, Const.ParamsNames.ADMIN_EMAIL_ID, emailId);
    pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, true, "admin.user"), pageResult.getDestinationWithParams());
    expectedLogSegment = Const.ACTION_RESULT_FAILURE;
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    expectedStatus = "The field 'email subject' is empty.";
    AssertHelper.assertContains(expectedStatus, pageResult.getStatusMessage());
    data = (AdminEmailComposePageData) pageResult.data;
    assertEquals(subject, data.emailToEdit.subject);
    ______TS("save non-existing email : invalid content : failure");
    emailId = "nonExisitingId";
    content = "";
    subject = "valid subject";
    receiver = "test@example.tmt";
    action = getAction(Const.ParamsNames.ADMIN_EMAIL_CONTENT, content, Const.ParamsNames.ADMIN_EMAIL_SUBJECT, subject, Const.ParamsNames.ADMIN_EMAIL_ADDRESS_RECEIVERS, receiver, Const.ParamsNames.ADMIN_EMAIL_ID, emailId);
    pageResult = getShowPageResult(action);
    assertEquals(getPageResultDestination(Const.ViewURIs.ADMIN_EMAIL, true, "admin.user"), pageResult.getDestinationWithParams());
    expectedLogSegment = Const.ACTION_RESULT_FAILURE;
    AssertHelper.assertContains(expectedLogSegment, action.getLogMessage());
    expectedStatus = "email content should not be empty.";
    AssertHelper.assertContains(expectedStatus, pageResult.getStatusMessage());
    data = (AdminEmailComposePageData) pageResult.data;
    assertEquals(subject, data.emailToEdit.subject);
}
Also used : ShowPageResult(teammates.ui.controller.ShowPageResult) AdminEmailComposeSaveAction(teammates.ui.controller.AdminEmailComposeSaveAction) AdminEmailComposePageData(teammates.ui.pagedata.AdminEmailComposePageData) AdminEmailAttributes(teammates.common.datatransfer.attributes.AdminEmailAttributes) Test(org.testng.annotations.Test)

Example 13 with AdminEmailAttributes

use of teammates.common.datatransfer.attributes.AdminEmailAttributes in project teammates by TEAMMATES.

the class AdminEmailsLogic method moveAdminEmailOutOfTrashBin.

/**
 * Move an admin email out of trash bin.<br>
 * After this the attribute isInTrashBin will be set to false
 */
public void moveAdminEmailOutOfTrashBin(String adminEmailId) throws InvalidParametersException, EntityDoesNotExistException {
    Assumption.assertNotNull(adminEmailId);
    AdminEmailAttributes adminEmailToUpdate = getAdminEmailById(adminEmailId);
    if (adminEmailToUpdate != null) {
        adminEmailToUpdate.isInTrashBin = false;
        adminEmailsDb.updateAdminEmail(adminEmailToUpdate);
    }
}
Also used : AdminEmailAttributes(teammates.common.datatransfer.attributes.AdminEmailAttributes)

Example 14 with AdminEmailAttributes

use of teammates.common.datatransfer.attributes.AdminEmailAttributes in project teammates by TEAMMATES.

the class AdminEmailsLogic method moveAdminEmailToTrashBin.

/**
 * Move an admin email to trash bin.<br>
 * After this the attribute isInTrashBin will be set to true
 */
public void moveAdminEmailToTrashBin(String adminEmailId) throws InvalidParametersException, EntityDoesNotExistException {
    Assumption.assertNotNull(adminEmailId);
    AdminEmailAttributes adminEmailToUpdate = getAdminEmailById(adminEmailId);
    if (adminEmailToUpdate != null) {
        adminEmailToUpdate.isInTrashBin = true;
        adminEmailsDb.updateAdminEmail(adminEmailToUpdate);
    }
}
Also used : AdminEmailAttributes(teammates.common.datatransfer.attributes.AdminEmailAttributes)

Example 15 with AdminEmailAttributes

use of teammates.common.datatransfer.attributes.AdminEmailAttributes in project teammates by TEAMMATES.

the class AdminEmailComposeSaveAction method execute.

@Override
protected ActionResult execute() {
    gateKeeper.verifyAdminPrivileges(account);
    AdminEmailComposePageData data = new AdminEmailComposePageData(account, sessionToken);
    String emailContent = getRequestParamValue(Const.ParamsNames.ADMIN_EMAIL_CONTENT);
    String subject = getRequestParamValue(Const.ParamsNames.ADMIN_EMAIL_SUBJECT);
    String addressReceiverListString = getRequestParamValue(Const.ParamsNames.ADMIN_EMAIL_ADDRESS_RECEIVERS);
    String groupReceiverListFileKey = getRequestParamValue(Const.ParamsNames.ADMIN_EMAIL_GROUP_RECEIVER_LIST_FILE_KEY);
    String emailId = getRequestParamValue(Const.ParamsNames.ADMIN_EMAIL_ID);
    addressReceiver.add(addressReceiverListString);
    if (groupReceiverListFileKey != null && !groupReceiverListFileKey.isEmpty()) {
        groupReceiver.add(groupReceiverListFileKey);
    }
    boolean isNewDraft = emailId == null;
    if (isNewDraft) {
        // this is a new email draft, so create a new admin email entity
        createAndSaveNewDraft(subject, addressReceiver, groupReceiver, emailContent);
    } else {
        // currently editing a previous email draft, so we need to update the previous draft
        // instead of creating a new admin email entity
        // retrieve the previous draft email
        AdminEmailAttributes previousDraft = logic.getAdminEmailById(emailId);
        if (previousDraft == null) {
            // the previous draft is not found (eg. deleted by accident when editing)
            createAndSaveNewDraft(subject, addressReceiver, groupReceiver, emailContent);
        } else {
            // the previous draft exists so simply update it with the latest email info
            updatePreviousEmailDraft(previousDraft.getEmailId(), subject, addressReceiver, groupReceiver, emailContent);
        }
    }
    if (isError) {
        data.emailToEdit = AdminEmailAttributes.builder(subject, addressReceiver, groupReceiver, new Text(emailContent)).withEmailId(emailId).build();
    } else {
        statusToAdmin = Const.StatusMessages.EMAIL_DRAFT_SAVED + ": <br>" + "Subject: " + SanitizationHelper.sanitizeForHtml(subject);
        statusToUser.add(new StatusMessage(Const.StatusMessages.EMAIL_DRAFT_SAVED, StatusMessageColor.SUCCESS));
    }
    return createShowPageResult(Const.ViewURIs.ADMIN_EMAIL, data);
}
Also used : Text(com.google.appengine.api.datastore.Text) AdminEmailComposePageData(teammates.ui.pagedata.AdminEmailComposePageData) AdminEmailAttributes(teammates.common.datatransfer.attributes.AdminEmailAttributes) StatusMessage(teammates.common.util.StatusMessage)

Aggregations

AdminEmailAttributes (teammates.common.datatransfer.attributes.AdminEmailAttributes)17 Test (org.testng.annotations.Test)7 Text (com.google.appengine.api.datastore.Text)5 AdminEmailComposePageData (teammates.ui.pagedata.AdminEmailComposePageData)3 EntityDoesNotExistException (teammates.common.exception.EntityDoesNotExistException)2 InvalidParametersException (teammates.common.exception.InvalidParametersException)2 ShowPageResult (teammates.ui.controller.ShowPageResult)2 BlobKey (com.google.appengine.api.blobstore.BlobKey)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 TeammatesException (teammates.common.exception.TeammatesException)1 EmailWrapper (teammates.common.util.EmailWrapper)1 StatusMessage (teammates.common.util.StatusMessage)1 EmailGenerator (teammates.logic.api.EmailGenerator)1 AdminEmail (teammates.storage.entity.AdminEmail)1 AdminEmailComposePageAction (teammates.ui.controller.AdminEmailComposePageAction)1 AdminEmailComposeSaveAction (teammates.ui.controller.AdminEmailComposeSaveAction)1