Skip to content

Commit

Permalink
94 - Removing "_redirect" from app root url (medic#252)
Browse files Browse the repository at this point in the history
Ticket: medic#94

In this commit:
- Removes DISABLE_APP_URL_VALIDATION and therefore the App URL verification (AppUrlVerification class) isn't skipped anymore.
- Stops appending /medic/_design/medic/_rewrite/ to the App URL.
- Adds some unit test coverage
  • Loading branch information
latin-panda authored and melema123 committed Apr 27, 2022
1 parent 27cfda3 commit e442417
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 25 deletions.
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ bundle-all: check-env
uninstall-all:
${GRADLE} uninstallAll

url-tester:
DISABLE_APP_URL_VALIDATION=true ${GRADLE} ${GRADLE_OPTS} install${flavor}Debug

uninstall:
adb uninstall org.medicmobile.webapp.mobile

Expand Down Expand Up @@ -97,7 +94,7 @@ test-ui-gamma:
${GRADLE} connectedMedicmobilegammaDebugAndroidTest -Pabi=${abi} --stacktrace

test-ui-url:
DISABLE_APP_URL_VALIDATION=true ${GRADLE} connectedUnbrandedDebugAndroidTest \
${GRADLE} connectedUnbrandedDebugAndroidTest \
-Pabi=${abi} --stacktrace -Pandroid.testInstrumentationRunnerArguments.class=\
org.medicmobile.webapp.mobile.LastUrlTest

Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ android {
}

applicationVariants.all { variant ->
buildConfigField "boolean", "DISABLE_APP_URL_VALIDATION", "Boolean.parseBoolean(\"${System.env.DISABLE_APP_URL_VALIDATION}\")"
buildConfigField "String", "LOG_TAG", '"MedicMobile"'
buildConfigField "Long", "TTL_LAST_URL", '24l * 60 * 60 * 1000' // 24 hs max time last URL loaded is remembered

Expand Down
21 changes: 9 additions & 12 deletions src/main/java/org/medicmobile/webapp/mobile/AppUrlVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.net.MalformedURLException;
import org.json.JSONException;
import org.json.JSONObject;
import static org.medicmobile.webapp.mobile.BuildConfig.DISABLE_APP_URL_VALIDATION;
import static org.medicmobile.webapp.mobile.MedicLog.trace;
import static org.medicmobile.webapp.mobile.R.string.errAppUrl_apiNotReady;
import static org.medicmobile.webapp.mobile.R.string.errAppUrl_appNotFound;
Expand All @@ -29,30 +28,28 @@ public AppUrlVerifier() {
*/
public AppUrlVerification verify(String appUrl) {
appUrl = clean(appUrl);
if(DISABLE_APP_URL_VALIDATION) {
return AppUrlVerification.ok(appUrl);
}

try {
JSONObject json = jsonClient.get(appUrl + "/setup/poll");

if(!json.getString("handler").equals("medic-api"))
if (!json.getString("handler").equals("medic-api")) {
return AppUrlVerification.failure(appUrl, errAppUrl_appNotFound);
if(!json.getBoolean("ready"))
}

if (!json.getBoolean("ready")) {
return AppUrlVerification.failure(appUrl, errAppUrl_apiNotReady);
}

return AppUrlVerification.ok(appUrl);

} catch(MalformedURLException ex) {
// seems unlikely, as we should have verified this already
return AppUrlVerification.failure(appUrl,
errInvalidUrl);
return AppUrlVerification.failure(appUrl, errInvalidUrl);
} catch(JSONException ex) {
return AppUrlVerification.failure(appUrl,
errAppUrl_appNotFound);
return AppUrlVerification.failure(appUrl, errAppUrl_appNotFound);
} catch(IOException ex) {
trace(ex, "Exception caught trying to verify url: %s", redactUrl(appUrl));
return AppUrlVerification.failure(appUrl,
errAppUrl_serverNotFound);
return AppUrlVerification.failure(appUrl, errAppUrl_serverNotFound);
}
}

Expand Down
10 changes: 2 additions & 8 deletions src/main/java/org/medicmobile/webapp/mobile/SettingsStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.regex.*;

import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG;
import static org.medicmobile.webapp.mobile.BuildConfig.DISABLE_APP_URL_VALIDATION;
import static org.medicmobile.webapp.mobile.BuildConfig.TTL_LAST_URL;
import static org.medicmobile.webapp.mobile.SimpleJsonClient2.redactUrl;
import static org.medicmobile.webapp.mobile.MedicLog.trace;
Expand All @@ -22,17 +21,12 @@ public abstract class SettingsStore {

public abstract String getAppUrl();

public String getRootUrl() {
String appUrl = getAppUrl();
return appUrl + (DISABLE_APP_URL_VALIDATION ? "" : "/medic/_design/medic/_rewrite/");
}

public String getUrlToLoad(Uri url) {
return url != null ? url.toString() : getRootUrl();
return url != null ? url.toString() : getAppUrl();
}

public boolean isRootUrl(String url) {
return getRootUrl().equals(url);
return getAppUrl().equals(url);
}

public abstract boolean hasWebappSettings();
Expand Down
111 changes: 111 additions & 0 deletions src/test/java/org/medicmobile/webapp/mobile/SettingsStoreTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.medicmobile.webapp.mobile;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

import android.content.SharedPreferences;
import android.net.Uri;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockSettings;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(sdk=28)
public class SettingsStoreTest {

private SharedPreferences sharedPreferences;
private SettingsStore settingsStore;
private static final String APP_URL = "https://project-abc.medic.org";

@Before
public void setup() {
sharedPreferences = mock(SharedPreferences.class);

MockSettings mockSettings = withSettings()
.useConstructor(sharedPreferences)
.defaultAnswer(CALLS_REAL_METHODS);

settingsStore = mock(SettingsStore.class, mockSettings);
when(settingsStore.getAppUrl()).thenReturn(APP_URL);
}

@Test
public void getUrlToLoad_withValidUrl_returnsUrlString() {
Uri uri = Uri.parse("https://project-zxy.medic.org");

assertEquals("https://project-zxy.medic.org", settingsStore.getUrlToLoad(uri));
}

@Test
public void getUrlToLoad_withInvalidUrl_returnsAppUrlString() {
assertEquals(APP_URL, settingsStore.getUrlToLoad(null));
}

@Test
public void isRootUrl_withAppUrl_returnsTrue() {
assertTrue(settingsStore.isRootUrl(APP_URL));
}

@Test
public void isRootUrl_withOtherUrl_returnsFalse() {
assertFalse(settingsStore.isRootUrl("https://project.health-ministry.org"));
}

@Test
public void isRootUrl_withNullUrl_returnsFalse() {
assertFalse(settingsStore.isRootUrl(null));
}

@Test
public void get_withPrefSet_returnsValue() {
when(sharedPreferences.getString("a_setting", null)).thenReturn("a_value");

assertEquals("a_value", settingsStore.get("a_setting"));
}

@Test
public void get_withNoPrefSet_returnsNull() {
when(sharedPreferences.getString("a_setting", null)).thenReturn(null);

assertNull(settingsStore.get("a_setting"));
}

@Test
public void hasUserDeniedGeolocation_withPrefSet_returnsValue() {
when(sharedPreferences.getBoolean("denied-geolocation", false)).thenReturn(true);

assertTrue(settingsStore.hasUserDeniedGeolocation());
}

@Test
public void hasUserDeniedGeolocation_withNoPrefSet_returnsFalse() {
when(sharedPreferences.getBoolean("denied-geolocation", false)).thenReturn(false);

assertFalse(settingsStore.hasUserDeniedGeolocation());
}

@Test
public void getLastUrl_withLastUrlTime_returnsLastUrl() {
when(sharedPreferences.getLong("last-url-time-ms", 0)).thenReturn(System.currentTimeMillis());
when(sharedPreferences.getString("last-url", null)).thenReturn("https://project-abc.medic.org/#messages");

assertEquals("https://project-abc.medic.org/#messages", settingsStore.getLastUrl());
}

@Test
public void getLastUrl_withTooOldLastUrlTime_returnsNull() {
when(sharedPreferences.getLong("last-url-time-ms", 0)).thenReturn(60L);

assertNull(settingsStore.getLastUrl());
}
}

0 comments on commit e442417

Please sign in to comment.