|
| 1 | +package com.iterable.iterableapi; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | + |
| 5 | +import org.json.JSONObject; |
| 6 | +import org.junit.After; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.robolectric.shadows.ShadowPausedAsyncTask; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | + |
| 14 | +import okhttp3.mockwebserver.MockResponse; |
| 15 | +import okhttp3.mockwebserver.MockWebServer; |
| 16 | +import okhttp3.mockwebserver.RecordedRequest; |
| 17 | + |
| 18 | +import static android.os.Looper.getMainLooper; |
| 19 | +import static junit.framework.Assert.assertEquals; |
| 20 | +import static junit.framework.Assert.assertNotNull; |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | +import static org.robolectric.Shadows.shadowOf; |
| 25 | + |
| 26 | +public class IterableApiIntegrationTest extends BaseTest { |
| 27 | + private static final String TEST_TOKEN = "testToken"; |
| 28 | + |
| 29 | + private MockWebServer server; |
| 30 | + private IterablePushRegistrationTask.Util.UtilImpl originalPushRegistrationUtil; |
| 31 | + private IterablePushRegistrationTask.Util.UtilImpl pushRegistrationUtilMock; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUp() { |
| 35 | + server = new MockWebServer(); |
| 36 | + IterableApi.overrideURLEndpointPath(server.url("").toString()); |
| 37 | + IterableInAppManager inAppManagerMock = mock(IterableInAppManager.class); |
| 38 | + IterableApi.sharedInstance = new IterableApi(inAppManagerMock); |
| 39 | + |
| 40 | + originalPushRegistrationUtil = IterablePushRegistrationTask.Util.instance; |
| 41 | + pushRegistrationUtilMock = mock(IterablePushRegistrationTask.Util.UtilImpl.class); |
| 42 | + IterablePushRegistrationTask.Util.instance = pushRegistrationUtilMock; |
| 43 | + ShadowPausedAsyncTask.reset(); // Enable real threading in AsyncTask so we keep the execution sequence similar to the real one. |
| 44 | + } |
| 45 | + |
| 46 | + @After |
| 47 | + public void tearDown() throws IOException { |
| 48 | + IterablePushRegistrationTask.Util.instance = originalPushRegistrationUtil; |
| 49 | + |
| 50 | + server.shutdown(); |
| 51 | + server = null; |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testDisablePushOnLogout() throws Exception { |
| 56 | + server.enqueue(new MockResponse().setResponseCode(200).setBody("{}")); |
| 57 | + when(pushRegistrationUtilMock.getFirebaseResouceId(any(Context.class))).thenReturn(1); |
| 58 | + when(pushRegistrationUtilMock.getFirebaseToken()).thenReturn(TEST_TOKEN); |
| 59 | + IterableApi.initialize(getContext(), "apiKey", new IterableConfig.Builder().setAutoPushRegistration(true).build()); |
| 60 | + IterableApi. getInstance(). setEmail( "[email protected]"); |
| 61 | + shadowOf(getMainLooper()).idle(); |
| 62 | + RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS); |
| 63 | + assertNotNull(request); |
| 64 | + JSONObject requestJson = new JSONObject(request.getBody().readUtf8()); |
| 65 | + assertEquals("/" + IterableConstants.ENDPOINT_REGISTER_DEVICE_TOKEN, request.getPath()); |
| 66 | + assertEquals( "[email protected]", requestJson. getString( IterableConstants. KEY_EMAIL)); |
| 67 | + JSONObject deviceJson = requestJson.getJSONObject(IterableConstants.KEY_DEVICE); |
| 68 | + assertEquals(TEST_TOKEN, deviceJson.getString(IterableConstants.KEY_TOKEN)); |
| 69 | + |
| 70 | + server.enqueue(new MockResponse().setResponseCode(200).setBody("{}")); |
| 71 | + IterableApi.getInstance().setEmail(null); |
| 72 | + request = server.takeRequest(1, TimeUnit.SECONDS); |
| 73 | + assertNotNull(request); |
| 74 | + requestJson = new JSONObject(request.getBody().readUtf8()); |
| 75 | + assertEquals("/" + IterableConstants.ENDPOINT_DISABLE_DEVICE, request.getPath()); |
| 76 | + assertEquals( "[email protected]", requestJson. getString( IterableConstants. KEY_EMAIL)); |
| 77 | + assertEquals(TEST_TOKEN, requestJson.getString(IterableConstants.KEY_TOKEN)); |
| 78 | + } |
| 79 | +} |
0 commit comments