|
8 | 8 | */
|
9 | 9 | package com.parse;
|
10 | 10 |
|
| 11 | +import android.Manifest; |
11 | 12 | import android.os.Parcel;
|
| 13 | +import android.util.Log; |
12 | 14 |
|
13 | 15 | import org.json.JSONObject;
|
14 | 16 | import org.junit.After;
|
| 17 | +import org.junit.Assert; |
15 | 18 | import org.junit.Before;
|
16 | 19 | import org.junit.Rule;
|
17 | 20 | import org.junit.Test;
|
|
20 | 23 | import org.mockito.ArgumentCaptor;
|
21 | 24 | import org.mockito.Matchers;
|
22 | 25 | import org.robolectric.RobolectricTestRunner;
|
| 26 | +import org.robolectric.RuntimeEnvironment; |
| 27 | +import org.robolectric.Shadows; |
23 | 28 | import org.robolectric.annotation.Config;
|
| 29 | +import org.robolectric.shadows.ShadowApplication; |
24 | 30 |
|
| 31 | +import java.net.URL; |
25 | 32 | import java.util.Collections;
|
| 33 | +import java.util.Date; |
26 | 34 | import java.util.HashMap;
|
27 | 35 | import java.util.Map;
|
| 36 | +import java.util.concurrent.CountDownLatch; |
28 | 37 | import java.util.concurrent.Semaphore;
|
29 | 38 | import java.util.concurrent.TimeUnit;
|
30 | 39 |
|
| 40 | +import bolts.Capture; |
| 41 | +import bolts.Continuation; |
31 | 42 | import bolts.Task;
|
32 | 43 |
|
33 | 44 | import static org.junit.Assert.assertEquals;
|
@@ -1541,4 +1552,102 @@ private static void setLazy(ParseUser user) {
|
1541 | 1552 | anonymousAuthData.put("anonymousToken", "anonymousTest");
|
1542 | 1553 | user.putAuthData(ParseAnonymousUtils.AUTH_TYPE, anonymousAuthData);
|
1543 | 1554 | }
|
| 1555 | + |
| 1556 | + //region testSaveEventuallyWhenSessionIsInvalid |
| 1557 | + |
| 1558 | + @Test |
| 1559 | + public void testSaveEventuallyWhenSessionIsInvalid() throws Exception { |
| 1560 | + |
| 1561 | + ParseRESTCommand.server = new URL("https://api.parse.com/1"); |
| 1562 | + Parse.enableLocalDatastore(RuntimeEnvironment.application); |
| 1563 | + |
| 1564 | + Parse.Configuration configuration = new Parse.Configuration.Builder(RuntimeEnvironment.application) |
| 1565 | + .build(); |
| 1566 | + ParsePlugins plugins = mock(ParsePlugins.class); |
| 1567 | + when(plugins.configuration()).thenReturn(configuration); |
| 1568 | + when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application); |
| 1569 | + ParsePlugins.set(plugins); |
| 1570 | + |
| 1571 | + ShadowApplication application = Shadows.shadowOf(RuntimeEnvironment.application); |
| 1572 | + application.grantPermissions(Manifest.permission.ACCESS_NETWORK_STATE); |
| 1573 | + |
| 1574 | + ParseUser.State userState = new ParseUser.State.Builder() |
| 1575 | + .objectId("test") |
| 1576 | + .sessionToken("r:sessionToken") |
| 1577 | + .build(); |
| 1578 | + ParseUser user = ParseObject.from(userState); |
| 1579 | + |
| 1580 | + ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); |
| 1581 | + when(currentUserController.getAsync(anyBoolean())).thenReturn(Task.forResult(user)); |
| 1582 | + when(currentUserController.getAsync()).thenReturn(Task.forResult(user)); |
| 1583 | + ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); |
| 1584 | + |
| 1585 | + //Parse.getEventuallyQueue().clear(); |
| 1586 | + |
| 1587 | + user.put("field", "data"); |
| 1588 | + |
| 1589 | + JSONObject mockResponse = new JSONObject(); |
| 1590 | + mockResponse.put("updatedAt", ParseDateFormat.getInstance().format(new Date())); |
| 1591 | + ParseHttpClient restClient = |
| 1592 | + ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 200, "OK"); |
| 1593 | + when(plugins.restClient()).thenReturn(restClient); |
| 1594 | + |
| 1595 | + final CountDownLatch saveCountDown1 = new CountDownLatch(1); |
| 1596 | + final Capture<Exception> exceptionCapture = new Capture<>(); |
| 1597 | + user.saveInBackground().continueWith(new Continuation<Void, Void>() { |
| 1598 | + @Override |
| 1599 | + public Void then(Task<Void> task) throws Exception { |
| 1600 | + exceptionCapture.set(task.getError()); |
| 1601 | + saveCountDown1.countDown(); |
| 1602 | + return null; |
| 1603 | + } |
| 1604 | + }); |
| 1605 | + assertTrue(saveCountDown1.await(5, TimeUnit.SECONDS)); |
| 1606 | + assertNull(exceptionCapture.get()); |
| 1607 | + |
| 1608 | + user.put("field", "other data"); |
| 1609 | + |
| 1610 | + mockResponse = new JSONObject(); |
| 1611 | + mockResponse.put("error", "invalid session token"); |
| 1612 | + mockResponse.put("code", 209); |
| 1613 | + restClient = |
| 1614 | + ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 400, "Bad Request"); |
| 1615 | + when(plugins.restClient()).thenReturn(restClient); |
| 1616 | + |
| 1617 | + final CountDownLatch saveEventuallyCountDown = new CountDownLatch(1); |
| 1618 | + user.saveEventually().continueWith(new Continuation<Void, Void>() { |
| 1619 | + @Override |
| 1620 | + public Void then(Task<Void> task) throws Exception { |
| 1621 | + exceptionCapture.set(task.getError()); |
| 1622 | + saveEventuallyCountDown.countDown(); |
| 1623 | + return null; |
| 1624 | + } |
| 1625 | + }); |
| 1626 | + assertTrue(saveEventuallyCountDown.await(5, TimeUnit.SECONDS)); |
| 1627 | + assertTrue(exceptionCapture.get() instanceof ParseException); |
| 1628 | + assertEquals(ParseException.INVALID_SESSION_TOKEN, ((ParseException)exceptionCapture.get()).getCode()); |
| 1629 | + assertEquals("invalid session token", exceptionCapture.get().getMessage()); |
| 1630 | + |
| 1631 | + user.put("field", "another data"); |
| 1632 | + |
| 1633 | + mockResponse = new JSONObject(); |
| 1634 | + mockResponse.put("updatedAt", ParseDateFormat.getInstance().format(new Date())); |
| 1635 | + restClient = |
| 1636 | + ParseTestUtils.mockParseHttpClientWithResponse(mockResponse, 200, "OK"); |
| 1637 | + when(plugins.restClient()).thenReturn(restClient); |
| 1638 | + |
| 1639 | + final CountDownLatch saveCountDown2 = new CountDownLatch(1); |
| 1640 | + user.saveInBackground().continueWith(new Continuation<Void, Void>() { |
| 1641 | + @Override |
| 1642 | + public Void then(Task<Void> task) throws Exception { |
| 1643 | + exceptionCapture.set(task.getError()); |
| 1644 | + saveCountDown2.countDown(); |
| 1645 | + return null; |
| 1646 | + } |
| 1647 | + }); |
| 1648 | + assertTrue(saveCountDown2.await(5, TimeUnit.SECONDS)); |
| 1649 | + assertNull(exceptionCapture.get()); |
| 1650 | + } |
| 1651 | + |
| 1652 | + //endregion |
1544 | 1653 | }
|
0 commit comments