Skip to content

Commit

Permalink
Merge pull request getodk#5794 from grzesiek2010/COLLECT-5790
Browse files Browse the repository at this point in the history
Revert 'Do not mark forms as failed to send before sending' in v2023.3
  • Loading branch information
seadowg committed Oct 30, 2023
1 parent ae9c6a5 commit 3626c86
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,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 @@ -103,7 +105,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(), org.odk.collect.strings.R.string.google_sheets_encrypted_message));
}

Expand All @@ -121,7 +122,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.isDraft()) {
org.odk.collect.strings.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())) {
org.odk.collect.strings.R.string.form_sending
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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 @@ -83,15 +85,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(), org.odk.collect.strings.R.string.url_error));
}
Expand All @@ -112,13 +112,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(), org.odk.collect.strings.R.string.server_auth_credentials, submissionUri.getHost()),
submissionUri);
} else if (headResult.getStatusCode() == HttpsURLConnection.HTTP_NO_CONTENT) {
Expand All @@ -139,20 +137,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 @@ -174,7 +169,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 @@ -215,12 +209,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(org.odk.collect.strings.R.string.form_sending))
assertThat(formSavedSnackbarDetails.second, equalTo(org.odk.collect.strings.R.string.view_form))
}

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

0 comments on commit 3626c86

Please sign in to comment.