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

Revert "Do not mark forms as failed to send before sending" #5688

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public InstanceGoogleSheetsUploader(DriveApi driveApi, SheetsApi sheetsApi) {

@Override
public String uploadOneSubmission(Instance instance, String spreadsheetUrl) throws FormUploadException {
markSubmissionFailed(instance);

File instanceFile = new File(instance.getInstanceFilePath());
if (!instanceFile.exists()) {
throw new FormUploadException(FAIL + "instance XML file does not exist!");
Expand All @@ -104,7 +106,6 @@ public String uploadOneSubmission(Instance instance, String spreadsheetUrl) thro

Form form = forms.get(0);
if (form.getBASE64RSAPublicKey() != null) {
markSubmissionFailed(instance);
throw new FormUploadException(getLocalizedString(Collect.getInstance(), R.string.google_sheets_encrypted_message));
}

Expand All @@ -122,7 +123,6 @@ public String uploadOneSubmission(Instance instance, String spreadsheetUrl) thro
}
insertRows(instance, instanceElement, null, key, instanceFile, spreadsheet.getSheets().get(0).getProperties().getTitle());
} catch (GoogleJsonResponseException e) {
markSubmissionFailed(instance);
throw new FormUploadException(getErrorMessageFromGoogleJsonResponseException(e));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class MainMenuViewModel(
return if (instance != null) {
val message = if (instance.status == Instance.STATUS_INCOMPLETE) {
R.string.form_saved_as_draft
} else if (instance.status == Instance.STATUS_COMPLETE) {
} else if (instance.status == Instance.STATUS_COMPLETE || instance.status == Instance.STATUS_SUBMISSION_FAILED) {
val form = formsRepositoryProvider.get().getAllByFormIdAndVersion(instance.formId, instance.formVersion).first()
if (form.shouldFormBeSentAutomatically(autoSendSettingsProvider.isAutoSendEnabledInSettings())) {
R.string.form_sending
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public InstanceServerUploader(OpenRosaHttpInterface httpInterface,
*/
@Override
public String uploadOneSubmission(Instance instance, String urlString) throws FormUploadException {
markSubmissionFailed(instance);

Uri submissionUri = Uri.parse(urlString);

long contentLength = 10000000L;
Expand All @@ -84,15 +86,13 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo
submissionUri.toString());
} else {
if (submissionUri.getHost() == null) {
markSubmissionFailed(instance);
throw new FormUploadException(FAIL + "Host name may not be null");
}

URI uri;
try {
uri = URI.create(submissionUri.toString());
} catch (IllegalArgumentException e) {
markSubmissionFailed(instance);
Timber.d(e.getMessage() != null ? e.getMessage() : e.toString());
throw new FormUploadException(getLocalizedString(Collect.getInstance(), R.string.url_error));
}
Expand All @@ -113,13 +113,11 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo
}

} catch (Exception e) {
markSubmissionFailed(instance);
throw new FormUploadException(FAIL
+ (e.getMessage() != null ? e.getMessage() : e.toString()));
}

if (headResult.getStatusCode() == HttpsURLConnection.HTTP_UNAUTHORIZED) {
markSubmissionFailed(instance);
throw new FormUploadAuthRequestedException(getLocalizedString(Collect.getInstance(), R.string.server_auth_credentials, submissionUri.getHost()),
submissionUri);
} else if (headResult.getStatusCode() == HttpsURLConnection.HTTP_NO_CONTENT) {
Expand All @@ -140,20 +138,17 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo
} else {
// Don't follow a redirection attempt to a different host.
// We can't tell if this is a spoof or not.
markSubmissionFailed(instance);
throw new FormUploadException(FAIL
+ "Unexpected redirection attempt to a different host: "
+ newURI.toString());
}
} catch (Exception e) {
markSubmissionFailed(instance);
throw new FormUploadException(FAIL + urlString + " " + e.toString());
}
}
} else {
if (headResult.getStatusCode() >= HttpsURLConnection.HTTP_OK
&& headResult.getStatusCode() < HttpsURLConnection.HTTP_MULT_CHOICE) {
markSubmissionFailed(instance);
throw new FormUploadException("Failed to send to " + uri + ". Is this an OpenRosa " +
"submission endpoint? If you have a web proxy you may need to log in to " +
"your network.\n\nHEAD request result status code: " + headResult.getStatusCode());
Expand All @@ -175,7 +170,6 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo
}

if (!instanceFile.exists() && !submissionFile.exists()) {
markSubmissionFailed(instance);
throw new FormUploadException(FAIL + "instance XML file does not exist!");
}

Expand Down Expand Up @@ -216,12 +210,10 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo
}

}
markSubmissionFailed(instance);
throw exception;
}

} catch (Exception e) {
markSubmissionFailed(instance);
throw new FormUploadException(FAIL + "Generic Exception: "
+ (e.getMessage() != null ? e.getMessage() : e.toString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class MainMenuViewModelTest {
}

@Test
fun `getFormSavedSnackbarDetails should return null when the corresponding instance failed to sent`() {
fun `getFormSavedSnackbarDetails should return proper message and action when the corresponding instance failed to sent`() {
val viewModel = createViewModelWithVersion("")

formsRepository.save(FormUtils.buildForm("1", "1", TempFiles.createTempDir().absolutePath).build())
Expand All @@ -307,8 +307,9 @@ class MainMenuViewModelTest {
whenever(autoSendSettingsProvider.isAutoSendEnabledInSettings()).thenReturn(true)

val uri = InstancesContract.getUri(Project.DEMO_PROJECT_ID, instance.dbId)
val formSavedSnackbarDetails = viewModel.getFormSavedSnackbarDetails(uri)
assertThat(formSavedSnackbarDetails, equalTo(null))
val formSavedSnackbarDetails = viewModel.getFormSavedSnackbarDetails(uri)!!
assertThat(formSavedSnackbarDetails.first, equalTo(R.string.form_sending))
assertThat(formSavedSnackbarDetails.second, equalTo(R.string.view_form))
}

private fun createViewModelWithVersion(version: String): MainMenuViewModel {
Expand Down