/* Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ */ package org.mozilla.android.sync.test.integration; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.json.simple.JSONArray; import org.json.simple.parser.ParseException; import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; import org.mozilla.android.sync.test.helpers.MockGlobalSessionCallback; import org.mozilla.android.sync.test.helpers.MockPrefsGlobalSession; import org.mozilla.android.sync.test.integration.TestBasicFetch.LiveDelegate; import org.mozilla.gecko.sync.CryptoRecord; import org.mozilla.gecko.sync.ExtendedJSONObject; import org.mozilla.gecko.sync.GlobalSession; import org.mozilla.gecko.sync.NonObjectJSONException; import org.mozilla.gecko.sync.SyncConfiguration; import org.mozilla.gecko.sync.SyncConfigurationException; import org.mozilla.gecko.sync.config.ClientRecordTerminator; import org.mozilla.gecko.sync.crypto.CryptoException; import org.mozilla.gecko.sync.crypto.KeyBundle; import org.mozilla.gecko.sync.repositories.domain.FormHistoryRecord; @Category(IntegrationTestCategory.class) public class TestClientRecordTerminator { // TODO: switch this to use a local server, with appropriate setup. static final String TEST_CLUSTER_URL = "https://scl2-sync1283.services.mozilla.com/"; static final String TEST_ACCOUNT = "nalexander+test0425@mozilla.com"; static final String TEST_USERNAME = "6gnkjphdltbntwnrgvu46ey6mu7ncjdl"; static final String TEST_PASSWORD = "test0425"; static final String TEST_USER_PASS = TEST_USERNAME + ":" + TEST_PASSWORD; static final String TEST_SYNC_KEY = "fuyx96ea8rkfazvjdfuqumupye"; // Weave.Identity.syncKey static final String TEST_CLIENT_GUID_PREFIX = "testGuid"; private KeyBundle syncKeyBundle; private MockGlobalSessionCallback callback; // We go to the trouble of using a session and its internal SyncConfiguration // because ClientRecordTerminator creates its URI by hand. By testing against // URIs generated by SyncConfiguration, we hope to keep the two in step. private GlobalSession session; @Before public void setUp() throws IllegalStateException, NonObjectJSONException, IOException, ParseException, CryptoException, SyncConfigurationException, IllegalArgumentException, URISyntaxException { syncKeyBundle = new KeyBundle(TEST_USERNAME, TEST_SYNC_KEY); callback = new MockGlobalSessionCallback(); session = new MockPrefsGlobalSession(SyncConfiguration.DEFAULT_USER_API, TEST_CLUSTER_URL, TEST_USERNAME, TEST_PASSWORD, null, syncKeyBundle, callback, null, null, null); session.config.clusterURL = new URI(TEST_CLUSTER_URL); } @Test public void testDeleteClientRecord() throws Exception { final String COLLECTION = "clients"; final String COLLECTION_URL = session.config.collectionURI(COLLECTION).toString(); final String[] RECS = new String[] { "keep1", "kill1", "keep2", "kill2" }; // Records to be PUT to server. final String[] KEEP = new String[] { "keep1", "keep2" }; // Records to leave on server. final String[] KILL = new String[] { "kill1", "kill2" }; // Records to delete from server. // Put records -- doesn't matter what type of record. This overwrites anything already on the server. for (String guid : RECS) { final FormHistoryRecord record = new FormHistoryRecord(guid, COLLECTION); record.fieldName = "testFieldName"; record.fieldValue = "testFieldValue"; CryptoRecord rec = record.getEnvelope(); rec.setKeyBundle(syncKeyBundle); rec.encrypt(); final String RECORD_URL = session.config.wboURI(COLLECTION, guid).toString(); LiveDelegate ld = TestBasicFetch.realLivePut(TEST_USERNAME, TEST_PASSWORD, RECORD_URL, rec); assertNotNull(ld.body()); } // Make sure record appears in collection guids. JSONArray a = (JSONArray) ExtendedJSONObject.parse(TestBasicFetch.realLiveFetch(TEST_USERNAME, TEST_PASSWORD, COLLECTION_URL).body()); for (String guid : RECS) { assertTrue(a.contains(guid)); } for (String guid : KILL) { ClientRecordTerminator.deleteClientRecord(TEST_USERNAME, TEST_PASSWORD, TEST_CLUSTER_URL, guid); } // Make sure record does not appear in collection guids. a = (JSONArray) ExtendedJSONObject.parse(TestBasicFetch.realLiveFetch(TEST_USERNAME, TEST_PASSWORD, COLLECTION_URL).body()); for (String guid : KEEP) { assertTrue(a.contains(guid)); } for (String guid : KILL) { assertFalse(a.contains(guid)); } } }