Commit 1f6fe52e authored by rahadi's avatar rahadi

Added Pemutakhiran Module

Migrate to id.ac.stis.capi
parent fb614149
{
"project_info": {
"project_number": "1043539700437",
"firebase_url": "https://capi-stis-95e29.firebaseio.com",
"project_id": "capi-stis-95e29",
"storage_bucket": "capi-stis-95e29.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:1043539700437:android:5e1d7c3c296329e8",
"android_client_info": {
"package_name": "org.odk.collect.android"
}
},
"oauth_client": [
{
"client_id": "1043539700437-q0vjmegkorlp8enhq26eahj2p0f5dil8.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyC8ocyCpQ_khywwxxF_SHd7aBqW1ME5pbY"
}
],
"services": {
"analytics_service": {
"status": 1
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"ads_service": {
"status": 2
}
}
}
],
"configuration_version": "1"
}
\ No newline at end of file
package id.ac.stis.capi.tasks; package id.ac.stis.capi.tasks;
import org.junit.Test; import org.junit.Test;
import id.ac.stis.capi.MockedServerTest;
import id.ac.stis.capi.logic.FormDetails; import id.ac.stis.capi.logic.FormDetails;
import org.ac.stis.capi.test.MockedServerTest;
import java.util.Map; import java.util.Map;
import okhttp3.mockwebserver.RecordedRequest; import okhttp3.mockwebserver.RecordedRequest;
import static id.ac.stis.capi.TestUtils.assertMatches;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.ac.stis.capi.test.TestUtils.assertMatches;
public class DownloadFormListTaskTest extends MockedServerTest { public class DownloadFormListTaskTest extends MockedServerTest {
...@@ -26,7 +27,7 @@ public class DownloadFormListTaskTest extends MockedServerTest { ...@@ -26,7 +27,7 @@ public class DownloadFormListTaskTest extends MockedServerTest {
RecordedRequest r = nextRequest(); RecordedRequest r = nextRequest();
assertEquals("GET", r.getMethod()); assertEquals("GET", r.getMethod());
assertEquals("/formList", r.getPath()); assertEquals("/formList", r.getPath());
assertMatches("Dalvik/.* org.odk.collect.android/.*", r.getHeader("User-Agent")); assertMatches("Dalvik/.* id.ac.stis.capi/.*", r.getHeader("User-Agent"));
assertEquals("1.0", r.getHeader("X-OpenRosa-Version")); assertEquals("1.0", r.getHeader("X-OpenRosa-Version"));
assertEquals("gzip", r.getHeader("Accept-Encoding")); assertEquals("gzip", r.getHeader("Accept-Encoding"));
......
...@@ -7,20 +7,21 @@ import java.io.File; ...@@ -7,20 +7,21 @@ import java.io.File;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import id.ac.stis.capi.MockedServerTest;
import id.ac.stis.capi.dao.InstancesDao; import id.ac.stis.capi.dao.InstancesDao;
import id.ac.stis.capi.dto.Instance; import id.ac.stis.capi.dto.Instance;
import id.ac.stis.capi.provider.InstanceProviderAPI; import id.ac.stis.capi.provider.InstanceProviderAPI;
import id.ac.stis.capi.tasks.InstanceUploaderTask.Outcome; import id.ac.stis.capi.tasks.InstanceUploaderTask.Outcome;
import org.ac.stis.capi.test.MockedServerTest;
import okhttp3.mockwebserver.RecordedRequest; import okhttp3.mockwebserver.RecordedRequest;
import static id.ac.stis.capi.TestUtils.assertMatches;
import static id.ac.stis.capi.TestUtils.cleanUpTempFiles;
import static id.ac.stis.capi.TestUtils.createTempFile;
import static id.ac.stis.capi.TestUtils.resetInstancesContentProvider;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.ac.stis.capi.test.TestUtils.assertMatches;
import static org.ac.stis.capi.test.TestUtils.cleanUpTempFiles;
import static org.ac.stis.capi.test.TestUtils.createTempFile;
import static org.ac.stis.capi.test.TestUtils.resetInstancesContentProvider;
public class InstanceUploaderTest extends MockedServerTest { public class InstanceUploaderTest extends MockedServerTest {
private InstancesDao dao; private InstancesDao dao;
...@@ -56,7 +57,7 @@ public class InstanceUploaderTest extends MockedServerTest { ...@@ -56,7 +57,7 @@ public class InstanceUploaderTest extends MockedServerTest {
RecordedRequest r = nextRequest(); RecordedRequest r = nextRequest();
assertEquals("HEAD", r.getMethod()); assertEquals("HEAD", r.getMethod());
assertMatches("/submission\\?deviceID=\\w+%3A\\w+", r.getPath()); assertMatches("/submission\\?deviceID=\\w+%3A\\w+", r.getPath());
assertMatches("Dalvik/.* org.odk.collect.android/.*", r.getHeader("User-Agent")); assertMatches("Dalvik/.* id.ac.stis.capi/.*", r.getHeader("User-Agent"));
assertEquals("1.0", r.getHeader("X-OpenRosa-Version")); assertEquals("1.0", r.getHeader("X-OpenRosa-Version"));
assertEquals("gzip,deflate", r.getHeader("Accept-Encoding")); assertEquals("gzip,deflate", r.getHeader("Accept-Encoding"));
} }
...@@ -66,7 +67,7 @@ public class InstanceUploaderTest extends MockedServerTest { ...@@ -66,7 +67,7 @@ public class InstanceUploaderTest extends MockedServerTest {
RecordedRequest r = nextRequest(); RecordedRequest r = nextRequest();
assertEquals("POST", r.getMethod()); assertEquals("POST", r.getMethod());
assertEquals("/submission", r.getPath()); assertEquals("/submission", r.getPath());
assertMatches("Dalvik/.* org.odk.collect.android/.*", r.getHeader("User-Agent")); assertMatches("Dalvik/.* id.ac.stis.capi/.*", r.getHeader("User-Agent"));
assertEquals("1.0", r.getHeader("X-OpenRosa-Version")); assertEquals("1.0", r.getHeader("X-OpenRosa-Version"));
assertEquals("gzip,deflate", r.getHeader("Accept-Encoding")); assertEquals("gzip,deflate", r.getHeader("Accept-Encoding"));
assertMatches("multipart/form-data; boundary=.*", r.getHeader("Content-Type")); assertMatches("multipart/form-data; boundary=.*", r.getHeader("Content-Type"));
......
...@@ -2,8 +2,9 @@ package id.ac.stis.capi.utilities; ...@@ -2,8 +2,9 @@ package id.ac.stis.capi.utilities;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import id.ac.stis.capi.MockedServerTest;
import id.ac.stis.capi.application.Collect; import id.ac.stis.capi.application.Collect;
import org.ac.stis.capi.test.MockedServerTest;
import org.opendatakit.httpclientandroidlib.client.HttpClient; import org.opendatakit.httpclientandroidlib.client.HttpClient;
import org.opendatakit.httpclientandroidlib.client.methods.HttpGet; import org.opendatakit.httpclientandroidlib.client.methods.HttpGet;
import org.opendatakit.httpclientandroidlib.protocol.HttpContext; import org.opendatakit.httpclientandroidlib.protocol.HttpContext;
...@@ -13,10 +14,10 @@ import java.net.URI; ...@@ -13,10 +14,10 @@ import java.net.URI;
import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest; import okhttp3.mockwebserver.RecordedRequest;
import static id.ac.stis.capi.TestUtils.assertMatches;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.ac.stis.capi.test.TestUtils.assertMatches;
public class WebUtilsTest extends MockedServerTest { public class WebUtilsTest extends MockedServerTest {
@Before @Before
...@@ -33,7 +34,7 @@ public class WebUtilsTest extends MockedServerTest { ...@@ -33,7 +34,7 @@ public class WebUtilsTest extends MockedServerTest {
// then // then
RecordedRequest r = nextRequest(); RecordedRequest r = nextRequest();
assertEquals("GET /some-path HTTP/1.1", r.getRequestLine()); assertEquals("GET /some-path HTTP/1.1", r.getRequestLine());
assertTrue(r.getHeader("User-Agent").matches("Dalvik/.* org.odk.collect.android/.*")); assertTrue(r.getHeader("User-Agent").matches("Dalvik/.* id.ac.stis.capi/.*"));
} }
@Test @Test
...@@ -42,7 +43,7 @@ public class WebUtilsTest extends MockedServerTest { ...@@ -42,7 +43,7 @@ public class WebUtilsTest extends MockedServerTest {
WebUtils.getXmlDocument(url("/list-forms"), httpContext(), httpClient()); WebUtils.getXmlDocument(url("/list-forms"), httpContext(), httpClient());
// then // then
assertMatches("Dalvik/.* org.odk.collect.android/.*", assertMatches("Dalvik/.* id.ac.stis.capi/.*",
nextRequest().getHeader("User-Agent")); nextRequest().getHeader("User-Agent"));
} }
......
/*
* Copyright 2017 Nafundi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ac.stis.capi;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.support.test.runner.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import id.ac.stis.capi.application.Collect;
import id.ac.stis.capi.preferences.AdminKeys;
import id.ac.stis.capi.preferences.PreferenceKeys;
import id.ac.stis.capi.provider.FormsProviderAPI;
import id.ac.stis.capi.provider.InstanceProviderAPI;
import id.ac.stis.capi.utilities.ResetUtility;
import org.osmdroid.config.Configuration;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
@RunWith(AndroidJUnit4.class)
public class ResetAppStateTestCase {
@Before
public void setUp() throws IOException {
resetAppState(Arrays.asList(
ResetUtility.ResetAction.RESET_PREFERENCES, ResetUtility.ResetAction.RESET_INSTANCES,
ResetUtility.ResetAction.RESET_FORMS, ResetUtility.ResetAction.RESET_LAYERS,
ResetUtility.ResetAction.RESET_CACHE, ResetUtility.ResetAction.RESET_OSM_DROID
));
}
@After
public void tearDown() throws IOException {
resetAppState(Arrays.asList(
ResetUtility.ResetAction.RESET_PREFERENCES, ResetUtility.ResetAction.RESET_INSTANCES,
ResetUtility.ResetAction.RESET_FORMS, ResetUtility.ResetAction.RESET_LAYERS,
ResetUtility.ResetAction.RESET_CACHE, ResetUtility.ResetAction.RESET_OSM_DROID
));
}
@Test
public void resetSettingsTest() throws IOException {
setupTestSettings();
resetAppState(Collections.singletonList(ResetUtility.ResetAction.RESET_PREFERENCES));
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(Collect.getInstance());
assertNull(settings.getString(PreferenceKeys.KEY_USERNAME, null));
assertNull(settings.getString(PreferenceKeys.KEY_PASSWORD, null));
assertEquals(true, settings.getBoolean(AdminKeys.KEY_VIEW_SENT, true));
assertEquals(0, getFormsCount());
assertEquals(0, getInstancesCount());
}
@Test
public void resetFormsTest() throws IOException {
saveTestFormFiles();
setupTestFormsDatabase();
createTestItemsetsDatabaseFile();
resetAppState(Collections.singletonList(ResetUtility.ResetAction.RESET_FORMS));
assertFolderEmpty(Collect.FORMS_PATH);
assertFalse(new File(Collect.METADATA_PATH + "/itemsets.db").exists());
}
@Test
public void resetInstancesTest() throws IOException {
saveTestInstanceFiles();
setupTestInstancesDatabase();
resetAppState(Collections.singletonList(ResetUtility.ResetAction.RESET_INSTANCES));
assertFolderEmpty(Collect.INSTANCES_PATH);
}
@Test
public void resetLayersTest() throws IOException {
saveTestLayerFiles();
resetAppState(Collections.singletonList(ResetUtility.ResetAction.RESET_LAYERS));
assertFolderEmpty(Collect.OFFLINE_LAYERS);
}
@Test
public void resetCacheTest() throws IOException {
saveTestCacheFiles();
resetAppState(Collections.singletonList(ResetUtility.ResetAction.RESET_CACHE));
assertFolderEmpty(Collect.CACHE_PATH);
}
@Test
public void resetOSMDroidTest() throws IOException {
saveTestOSMDroidFiles();
resetAppState(Collections.singletonList(ResetUtility.ResetAction.RESET_OSM_DROID));
assertFolderEmpty(Configuration.getInstance().getOsmdroidTileCache().getPath());
}
private void resetAppState(List<Integer> resetActions) {
List<Integer> failedResetActions = new ResetUtility().reset(Collect.getInstance(), resetActions);
assertEquals(0, failedResetActions.size());
}
private void setupTestSettings() throws IOException {
String username = "usernameTest";
String password = "passwordTest";
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(Collect.getInstance());
settings
.edit()
.putString(PreferenceKeys.KEY_USERNAME, username)
.putString(PreferenceKeys.KEY_PASSWORD, password)
.apply();
assertEquals(username, settings.getString(PreferenceKeys.KEY_USERNAME, null));
assertEquals(password, settings.getString(PreferenceKeys.KEY_PASSWORD, null));
settings
.edit()
.putBoolean(AdminKeys.KEY_VIEW_SENT, false)
.apply();
assertEquals(false, settings.getBoolean(AdminKeys.KEY_VIEW_SENT, false));
assertTrue(new File(Collect.SETTINGS).exists() || new File(Collect.SETTINGS).mkdir());
assertTrue(new File(Collect.SETTINGS + "/collect.settings").createNewFile());
assertTrue(new File(Collect.ODK_ROOT + "/collect.settings").createNewFile());
}
private void setupTestFormsDatabase() {
ContentValues values = new ContentValues();
values.put(FormsProviderAPI.FormsColumns.JRCACHE_FILE_PATH, Collect.ODK_ROOT + "/.cache/3a76a386464925b6f3e53422673dfe3c.formdef");
values.put(FormsProviderAPI.FormsColumns.JR_FORM_ID, "jrFormId");
values.put(FormsProviderAPI.FormsColumns.FORM_MEDIA_PATH, Collect.FORMS_PATH + "/testFile1-media");
values.put(FormsProviderAPI.FormsColumns.DATE, "1487077903756");
values.put(FormsProviderAPI.FormsColumns.DISPLAY_NAME, "displayName");
values.put(FormsProviderAPI.FormsColumns.FORM_FILE_PATH, Collect.FORMS_PATH + "/testFile1.xml");
values.put(FormsProviderAPI.FormsColumns.DISPLAY_SUBTEXT, "Added on Tue, Feb 14, 2017 at 14:21");
Collect.getInstance().getContentResolver()
.insert(FormsProviderAPI.FormsColumns.CONTENT_URI, values);
assertEquals(1, getFormsCount());
}
private void setupTestInstancesDatabase() {
ContentValues values = new ContentValues();
values.put(InstanceProviderAPI.InstanceColumns.INSTANCE_FILE_PATH, Collect.INSTANCES_PATH + "/testDir1/testFile1");
values.put(InstanceProviderAPI.InstanceColumns.SUBMISSION_URI, "submissionUri");
values.put(InstanceProviderAPI.InstanceColumns.DISPLAY_NAME, "displayName");
values.put(InstanceProviderAPI.InstanceColumns.DISPLAY_NAME, "formName");
values.put(InstanceProviderAPI.InstanceColumns.JR_FORM_ID, "jrformid");
values.put(InstanceProviderAPI.InstanceColumns.JR_VERSION, "jrversion");
Collect.getInstance().getContentResolver()
.insert(InstanceProviderAPI.InstanceColumns.CONTENT_URI, values);
assertEquals(1, getInstancesCount());
}
private void createTestItemsetsDatabaseFile() throws IOException {
assertTrue(new File(Collect.METADATA_PATH + "/itemsets.db").createNewFile());
}
private void saveTestFormFiles() throws IOException {
assertTrue(new File(Collect.FORMS_PATH + "/testFile1.xml").createNewFile());
assertTrue(new File(Collect.FORMS_PATH + "/testFile2.xml").createNewFile());
assertTrue(new File(Collect.FORMS_PATH + "/testFile3.xml").createNewFile());
assertTrue(new File(Collect.FORMS_PATH + "/testDir1/testFile1-media").mkdirs());
assertTrue(new File(Collect.FORMS_PATH + "/testDir2/testFile2-media").mkdirs());
assertTrue(new File(Collect.FORMS_PATH + "/testDir3/testFile3-media/testFile.csv").mkdirs());
}
private void saveTestInstanceFiles() {
assertTrue(new File(Collect.INSTANCES_PATH + "/testDir1/testFile1.xml").mkdirs());
assertTrue(new File(Collect.INSTANCES_PATH + "/testDir2/testFile2.xml").mkdirs());
assertTrue(new File(Collect.INSTANCES_PATH + "/testDir3").mkdirs());
}
private void saveTestLayerFiles() throws IOException {
assertTrue(new File(Collect.OFFLINE_LAYERS + "/testFile1").createNewFile());
assertTrue(new File(Collect.OFFLINE_LAYERS + "/testFile2").createNewFile());
assertTrue(new File(Collect.OFFLINE_LAYERS + "/testFile3").createNewFile());
assertTrue(new File(Collect.OFFLINE_LAYERS + "/testFile4").createNewFile());
}
private void saveTestCacheFiles() throws IOException {
assertTrue(new File(Collect.CACHE_PATH + "/testFile1").createNewFile());
assertTrue(new File(Collect.CACHE_PATH + "/testFile2").createNewFile());
assertTrue(new File(Collect.CACHE_PATH + "/testFile3").createNewFile());
}
private void saveTestOSMDroidFiles() throws IOException {
assertTrue(new File(Configuration.getInstance().getOsmdroidTileCache().getPath() + "/testFile1").mkdirs());
assertTrue(new File(Configuration.getInstance().getOsmdroidTileCache().getPath() + "/testFile2").mkdirs());
assertTrue(new File(Configuration.getInstance().getOsmdroidTileCache().getPath() + "/testFile3").mkdirs());
}
private int getFormsCount() {
int forms = 0;
Cursor cursor = Collect.getInstance().getContentResolver().query(
FormsProviderAPI.FormsColumns.CONTENT_URI, null, null, null,
FormsProviderAPI.FormsColumns.DISPLAY_NAME + " ASC");
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
forms++;
cursor.moveToNext();
}
}
} finally {
cursor.close();
}
}
return forms;
}
private int getInstancesCount() {
int instances = 0;
Cursor cursor = Collect.getInstance().getContentResolver().query(
InstanceProviderAPI.InstanceColumns.CONTENT_URI, null, null, null,
InstanceProviderAPI.InstanceColumns.DISPLAY_NAME + " ASC");
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
instances++;
cursor.moveToNext();
}
}
} finally {
cursor.close();
}
}
return instances;
}
private void assertFolderEmpty(String folder) {
assertTrue(new File(folder).isDirectory());
assertEquals(new File(folder).list().length, 0);
}
}
package org.ac.stis.capi;
import android.support.test.filters.Suppress;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opendatakit.httpclientandroidlib.HttpResponse;
import org.opendatakit.httpclientandroidlib.HttpStatus;
import org.opendatakit.httpclientandroidlib.client.HttpClient;
import org.opendatakit.httpclientandroidlib.client.methods.HttpGet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import javax.net.ssl.HttpsURLConnection;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import static id.ac.stis.capi.utilities.WebUtils.CONNECTION_TIMEOUT;
import static id.ac.stis.capi.utilities.WebUtils.createHttpClient;
import static id.ac.stis.capi.utilities.WebUtils.createOpenRosaHttpGet;
/**
* An on-device test for TLS server name indication support.
*
* @see <a href="https://github.com/opendatakit/collect/issues/199">COLLECT-199</a>
*/
@Suppress
@RunWith(AndroidJUnit4.class)
public class SNITest {
public static final URI SNI_URI = URI.create("https://sni.velox.ch/");
public static final String SUCCESS_SENTINEL = "sent the following TLS server name indication extension";
@Test
public void apacheHttpClientSupportsSNI() throws IOException {
HttpClient client = createHttpClient(CONNECTION_TIMEOUT);
HttpGet req = createOpenRosaHttpGet(SNI_URI);
HttpResponse rsp = client.execute(req);
assertHttpSuccess(rsp.getStatusLine().getStatusCode());
assertPageContent(rsp.getEntity().getContent());
}
@Test
public void urlConnectionSupportsSNI() throws IOException {
HttpsURLConnection conn = (HttpsURLConnection) SNI_URI.toURL().openConnection();
assertHttpSuccess(conn.getResponseCode());
assertPageContent(conn.getInputStream());
}
/*
* Confirms the request returned HTTP success (200) or fails test.
*/
private void assertHttpSuccess(int statusCode) {
assertEquals(HttpStatus.SC_OK, statusCode);
}
/*
* Confirms that the response content contains confirmation that SNI is working.
*/
private void assertPageContent(InputStream contentStream) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(contentStream));
String line;
while ((line = in.readLine()) != null) {
if (line.contains(SUCCESS_SENTINEL)) {
return;
}
}
fail(String.format("did not find sentinel '%s' in response", SUCCESS_SENTINEL));
}
}
package org.ac.stis.capi.externalintents;
import junit.framework.Assert;
import java.io.File;
import timber.log.Timber;
import static id.ac.stis.capi.application.Collect.CACHE_PATH;