Skip to content

Commit

Permalink
Merge pull request #1459 from dimagi/rc/clear-answer
Browse files Browse the repository at this point in the history
clear media answer
  • Loading branch information
Robert-Costello authored Sep 14, 2023
2 parents 79e38f5 + 2baf396 commit 34cab4c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public FormEntryResponseBean changeLocale(@RequestBody ChangeLocaleRequestBean c
public FormEntryResponseBean answerQuestion(@RequestBody AnswerQuestionRequestBean answerQuestionBean,
@CookieValue(name = Constants.POSTGRES_DJANGO_SESSION_ID, required = false) String authToken)
throws Exception {
return saveAnswer(answerQuestionBean, null);
return saveAnswer(answerQuestionBean, null, false);
}

@RequestMapping(
Expand All @@ -170,11 +170,22 @@ public FormEntryResponseBean answerMediaQuestion(
@CookieValue(name = Constants.POSTGRES_DJANGO_SESSION_ID, required = false) String authToken,
@RequestPart(PART_FILE) MultipartFile file)
throws Exception {
return saveAnswer(answerQuestionBean, file);
return saveAnswer(answerQuestionBean, file, false);
}

@RequestMapping(value = Constants.URL_CLEAR_ANSWER, method = RequestMethod.POST)
@UserLock
@UserRestore
@ConfigureStorageFromSession
public FormEntryResponseBean clearAnswer(
@RequestBody AnswerQuestionRequestBean answerQuestionBean,
@CookieValue(name = Constants.POSTGRES_DJANGO_SESSION_ID, required = false) String authToken)
throws Exception{
return saveAnswer(answerQuestionBean, null, true);
}

private FormEntryResponseBean saveAnswer(AnswerQuestionRequestBean answerQuestionBean,
@Nullable MultipartFile file) throws Exception {
@Nullable MultipartFile file, boolean clear) throws Exception {

SerializableFormSession serializableFormSession = categoryTimingHelper.timed(
Constants.TimingCategories.GET_SESSION,
Expand All @@ -193,6 +204,10 @@ private FormEntryResponseBean saveAnswer(AnswerQuestionRequestBean answerQuestio
String fileId = null;
Path mediaDirPath = formEntrySession.getMediaDirectoryPath(restoreFactory.getDomain(),
restoreFactory.getUsername(), restoreFactory.getAsUsername(), storageFactory.getAppId());
if (clear) {
formEntrySession.cleanCurrentMedia(mediaDirPath, answerQuestionBean.getFormIndex(),
mediaMetaDataService);
}
if (file != null) {
fileId = categoryTimingHelper.timed(
Constants.TimingCategories.PROCESS_MEDIA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ public String saveMediaAnswer(MultipartFile file, String answerIndex, Path media
return fileId;
}

private void cleanCurrentMedia(Path mediaDirectoryPath,
public void cleanCurrentMedia(Path mediaDirectoryPath,
String answerIndex, MediaMetaDataService mediaMetaDataService) {
Object currentAnswer = getCurrentAnswer(answerIndex);
if (currentAnswer != null) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/commcare/formplayer/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Constants {
public static final String URL_INCOMPLETE_SESSION = "incomplete-form";
public static final String URL_DELETE_INCOMPLETE_SESSION = "delete-incomplete-form";
public static final String URL_ANSWER_QUESTION = "answer";
public static final String URL_CLEAR_ANSWER = "clear_answer";
public static final String URL_ANSWER_MEDIA_QUESTION = "answer_media";
public static final String URL_CURRENT = "current";
public static final String URL_SUBMIT_FORM = "submit-all";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.commcare.formplayer.junit.request

import org.commcare.formplayer.beans.AnswerQuestionRequestBean
import org.commcare.formplayer.beans.FormEntryResponseBean
import org.commcare.formplayer.beans.NewFormResponse
import org.commcare.formplayer.beans.NewSessionRequestBean
import org.commcare.formplayer.objects.SerializableFormSession
import org.commcare.formplayer.services.FormSessionService
import org.commcare.formplayer.util.Constants
import org.springframework.test.web.servlet.MockMvc

/**
* Request class for making a mock request that clears the answer for a media form question.
*/
class ClearMediaQuestionRequest (
private val mockMvc: MockMvc,
private val formSessionService: FormSessionService
): MockRequest<AnswerQuestionRequestBean, FormEntryResponseBean>(
mockMvc, Constants.URL_CLEAR_ANSWER, FormEntryResponseBean::class.java
) {

fun request(
questionIndex: String,
sessionId: String
): Response<FormEntryResponseBean> {
val bean = AnswerQuestionRequestBean(questionIndex, null, sessionId)
val session: SerializableFormSession = formSessionService.getSessionById(sessionId)
bean.username = session.username
bean.domain = session.domain
bean.sessionId = sessionId
return requestWithBean(bean)
}
}
27 changes: 27 additions & 0 deletions src/test/java/org/commcare/formplayer/tests/MediaCaptureTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.commcare.formplayer.junit.MediaMetaDataServiceExtension
import org.commcare.formplayer.junit.RestoreFactoryExtension
import org.commcare.formplayer.junit.StorageFactoryExtension
import org.commcare.formplayer.junit.request.AnswerMediaQuestionRequest
import org.commcare.formplayer.junit.request.ClearMediaQuestionRequest
import org.commcare.formplayer.junit.request.NewFormRequest
import org.commcare.formplayer.junit.request.SubmitFormRequest
import org.commcare.formplayer.objects.MediaMetadataRecord
Expand Down Expand Up @@ -235,6 +236,22 @@ class MediaCaptureTest {
}
}

@Test
fun testClearMedia() {
val formResponse = startImageCaptureForm()
val imageResponse = saveImage(formResponse, "media/valid_image.jpg", "valid_image.jpg")

var expectedFilePath = getExpectedMediaPath(formResponse.session_id, imageResponse)
val originalSavedFile = expectedFilePath.toFile()
assertTrue("Could not find saved file on the filesystem", originalSavedFile.exists())

val clearImageResponse = clearImage(formResponse)
assertFalse("Could not remove file from the filesystem", originalSavedFile.exists())

val currentAnswer = clearImageResponse.tree[IMAGE_CAPTURE_INDEX].answer
assertEquals(null, currentAnswer)
}

private fun checkContentType(expectedContentType: String, filePart: HttpEntity<*>) {
val contentType = filePart.headers["Content-Type"]
assertEquals(expectedContentType, contentType!![0])
Expand All @@ -258,4 +275,14 @@ class MediaCaptureTest {
val questionRequest = AnswerMediaQuestionRequest(mockMvc, formSessionService)
return questionRequest.request("" + IMAGE_CAPTURE_INDEX, file, formResponse.sessionId).bean()
}

private fun clearImage(
formResponse: NewFormResponse
): FormEntryResponseBean {
val questions = formResponse.tree
assertEquals("q_image_acquire", questions[IMAGE_CAPTURE_INDEX].question_id)

val questionRequest = ClearMediaQuestionRequest(mockMvc, formSessionService)
return questionRequest.request("" + IMAGE_CAPTURE_INDEX, formResponse.sessionId).bean()
}
}

0 comments on commit 34cab4c

Please sign in to comment.