Skip to content

Commit c83fae1

Browse files
committed
- Added a failing test for parse-community#827
- The eventually queue should be cleared at each test execution but Parse.getEventuallyQueue().clear() crashes, that's why line 1585 is commented
1 parent ff3e27b commit c83fae1

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

Parse/src/test/java/com/parse/ParseUserTest.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
*/
99
package com.parse;
1010

11+
import android.Manifest;
1112
import android.os.Parcel;
13+
import android.util.Log;
1214

1315
import org.json.JSONObject;
1416
import org.junit.After;
17+
import org.junit.Assert;
1518
import org.junit.Before;
1619
import org.junit.Rule;
1720
import org.junit.Test;
@@ -20,14 +23,22 @@
2023
import org.mockito.ArgumentCaptor;
2124
import org.mockito.Matchers;
2225
import org.robolectric.RobolectricTestRunner;
26+
import org.robolectric.RuntimeEnvironment;
27+
import org.robolectric.Shadows;
2328
import org.robolectric.annotation.Config;
29+
import org.robolectric.shadows.ShadowApplication;
2430

31+
import java.net.URL;
2532
import java.util.Collections;
33+
import java.util.Date;
2634
import java.util.HashMap;
2735
import java.util.Map;
36+
import java.util.concurrent.CountDownLatch;
2837
import java.util.concurrent.Semaphore;
2938
import java.util.concurrent.TimeUnit;
3039

40+
import bolts.Capture;
41+
import bolts.Continuation;
3142
import bolts.Task;
3243

3344
import static org.junit.Assert.assertEquals;
@@ -1541,4 +1552,102 @@ private static void setLazy(ParseUser user) {
15411552
anonymousAuthData.put("anonymousToken", "anonymousTest");
15421553
user.putAuthData(ParseAnonymousUtils.AUTH_TYPE, anonymousAuthData);
15431554
}
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
15441653
}

0 commit comments

Comments
 (0)