/** * Creates an anonymous user in the background. * * @return A Task that will be resolved when logging in is completed. */ public static Task<ParseUser> logInInBackground() { return ParseUser.logInWithInBackground(AUTH_TYPE, getAuthData()); }
@Test public void testLoginWithAsyncWithLinkedLazyUser() throws Exception { // Register a mock currentUserController to make getCurrentUser work ParseUser currentUser = new ParseUser(); currentUser.putAuthData(ParseAnonymousUtils.AUTH_TYPE, new HashMap<String, String>()); setLazy(currentUser); ParseUser partialMockCurrentUser = spy(currentUser); when(partialMockCurrentUser.getSessionToken()).thenReturn("oldSessionToken"); doReturn(Task.<ParseUser>forResult(null)) .when(partialMockCurrentUser) .resolveLazinessAsync(Matchers.<Task<Void>>any()); ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(false)).thenReturn(Task.forResult(partialMockCurrentUser)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); String authType = "facebook"; Map<String, String> authData = new HashMap<>(); authData.put("token", "123"); ParseUser userAfterLogin = ParseTaskUtils.wait(ParseUser.logInWithInBackground(authType, authData)); // Make sure we stripAnonymity assertNull(userAfterLogin.getAuthData().get(ParseAnonymousUtils.AUTH_TYPE)); // Make sure we update authData assertEquals(authData, userAfterLogin.getAuthData().get("facebook")); // Make sure we resolveLaziness verify(partialMockCurrentUser, times(1)).resolveLazinessAsync(Matchers.<Task<Void>>any()); }
@Test public void testLoginWithAsyncWithoutExistingLazyUser() throws ParseException { ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(false)).thenReturn(Task.<ParseUser>forResult(null)); when(currentUserController.setAsync(any(ParseUser.class))) .thenReturn(Task.<Void>forResult(null)); ParseUser.State userState = mock(ParseUser.State.class); when(userState.className()).thenReturn("_User"); when(userState.objectId()).thenReturn("1234"); when(userState.isComplete()).thenReturn(true); ParseUserController userController = mock(ParseUserController.class); when(userController.logInAsync(anyString(), anyMapOf(String.class, String.class))) .thenReturn(Task.forResult(userState)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); ParseCorePlugins.getInstance().registerUserController(userController); String authType = "facebook"; Map<String, String> authData = new HashMap<>(); authData.put("token", "123"); ParseUser user = ParseTaskUtils.wait(ParseUser.logInWithInBackground(authType, authData)); verify(currentUserController).getAsync(false); verify(userController).logInAsync(authType, authData); verify(currentUserController).setAsync(user); assertSame(userState, user.getState()); verifyNoMoreInteractions(currentUserController); verifyNoMoreInteractions(userController); }
@Test public void testLoginWithAsyncWithLinkedLazyUseAndResolveLazinessFailure() throws Exception { // Register a mock currentUserController to make getCurrentUser work ParseUser currentUser = new ParseUser(); Map<String, String> oldAnonymousAuthData = new HashMap<>(); oldAnonymousAuthData.put("oldKey", "oldToken"); currentUser.putAuthData(ParseAnonymousUtils.AUTH_TYPE, oldAnonymousAuthData); ParseUser partialMockCurrentUser = spy(currentUser); when(partialMockCurrentUser.getSessionToken()).thenReturn("oldSessionToken"); doReturn(Task.<ParseUser>forError(new Exception())) .when(partialMockCurrentUser) .resolveLazinessAsync(Matchers.<Task<Void>>any()); ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(false)).thenReturn(Task.forResult(partialMockCurrentUser)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); String authType = "facebook"; Map<String, String> authData = new HashMap<>(); authData.put("token", "123"); Task<ParseUser> loginTask = ParseUser.logInWithInBackground(authType, authData); loginTask.waitForCompletion(); // Make sure we try to resolveLaziness verify(partialMockCurrentUser, times(1)).resolveLazinessAsync(Matchers.<Task<Void>>any()); // Make sure we do not save new authData assertNull(partialMockCurrentUser.getAuthData().get("facebook")); // Make sure we restore anonymity after resolve laziness failure assertEquals(oldAnonymousAuthData, partialMockCurrentUser.getAuthData() .get(ParseAnonymousUtils.AUTH_TYPE)); // Make sure task fails assertTrue(loginTask.isFaulted()); }
@Override public Task<ParseUser> logInWithInBackground(String authType, Map<String, String> authData) { return ParseUser.logInWithInBackground(authType, authData); } }
@Test public void testLoginWithAsyncWithLinkedNotLazyUser() throws Exception { // Register a mock currentUserController to make getCurrentUser work ParseUser.State state = new ParseUser.State.Builder() .objectId("objectId") // Make it not lazy .putAuthData(ParseAnonymousUtils.AUTH_TYPE, new HashMap<String, String>()) .build(); ParseUser currentUser = ParseUser.from(state); ParseUser partialMockCurrentUser = spy(currentUser); // ParseUser.mutex doReturn(Task.<Void>forResult(null)) .when(partialMockCurrentUser) .linkWithInBackground(anyString(), Matchers.<Map<String, String>>any()); ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync()).thenReturn(Task.forResult(partialMockCurrentUser)); when(currentUserController.getAsync(anyBoolean())) .thenReturn(Task.forResult(partialMockCurrentUser)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); String authType = "facebook"; Map<String, String> authData = new HashMap<>(); authData.put("token", "123"); ParseUser userAfterLogin = ParseTaskUtils.wait(ParseUser.logInWithInBackground(authType, authData)); // Make sure we link authData verify(partialMockCurrentUser, times(1)).linkWithInBackground(authType, authData); assertSame(partialMockCurrentUser, userAfterLogin); }
@Test public void testLoginWithAsyncWithNoCurrentUser() throws Exception { // Register a mock userController to make logIn work ParseUserController userController = mock(ParseUserController.class); ParseUser.State newUserState = new ParseUser.State.Builder() .put("newKey", "newValue") .sessionToken("newSessionToken") .build(); when(userController.logInAsync(anyString(), Matchers.<Map<String, String>>any())) .thenReturn(Task.forResult(newUserState)); ParseCorePlugins.getInstance().registerUserController(userController); // Register a mock currentUserController to make getCurrentUser work ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(false)).thenReturn(Task.<ParseUser>forResult(null)); when(currentUserController.setAsync(any(ParseUser.class))) .thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); String authType = "facebook"; Map<String, String> authData = new HashMap<>(); authData.put("token", "123"); ParseUser userAfterLogin = ParseTaskUtils.wait(ParseUser.logInWithInBackground(authType, authData)); // Make sure we login authData verify(userController, times(1)).logInAsync("facebook", authData); // Make sure we save the new created user as currentUser verify(currentUserController, times(1)).setAsync(any(ParseUser.class)); // Make sure the new created user has correct data assertEquals("newValue", userAfterLogin.get("newKey")); assertEquals("newSessionToken", userAfterLogin.getSessionToken()); }
Map<String, String> authData = new HashMap<>(); authData.put("token", "123"); ParseUser userAfterLogin = ParseTaskUtils.wait(ParseUser.logInWithInBackground(authType, authData));
@Override public Task<ParseUser> then(Task<Map<String, String>> task) throws Exception { return ParseUser.logInWithInBackground(provider.getAuthType(), task.getResult()); } });
/** * Logs in a ParseUser using Facebook for authentication. If a user for the given Facebook * credentials does not already exist, a new user will be created. This method allows you to * handle getting access tokens for the user yourself, rather than delegating to the Facebook SDK. * * @param facebookId * The facebook ID of the user being linked. * @param accessToken * The access token for the user. * @param expirationDate * The expiration date of the access token. * @return A Task that will be resolved when logging in is completed. */ public static Task<ParseUser> logInInBackground(String facebookId, String accessToken, Date expirationDate) { checkInitialization(); return ParseUser.logInWithInBackground( provider.getAuthType(), provider.getAuthData(facebookId, accessToken, expirationDate)); }
@NonNull @CheckReturnValue public static Single<ParseUser> logInWith(@NonNull final String authType, @NonNull final Map<String, String> authData) { return RxTask.single(() -> ParseUser.logInWithInBackground(authType, authData)); }