Skip to content

Commit f1fcda4

Browse files
committed
Rename to ParseAuthenticationManager, add unit tests
1 parent 40abc27 commit f1fcda4

File tree

6 files changed

+185
-63
lines changed

6 files changed

+185
-63
lines changed

Parse/src/main/java/com/parse/ParseAuthenticationController.java

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
package com.parse;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
import bolts.Continuation;
15+
import bolts.Task;
16+
17+
/*** package */ class ParseAuthenticationManager {
18+
19+
private final Object lock = new Object();
20+
private final Map<String, ParseAuthenticationProvider> authenticationProviders = new HashMap<>();
21+
private final ParseCurrentUserController controller;
22+
23+
public ParseAuthenticationManager(ParseCurrentUserController controller) {
24+
this.controller = controller;
25+
}
26+
27+
public void register(ParseAuthenticationProvider provider) {
28+
final String authType = provider.getAuthType();
29+
synchronized (lock) {
30+
if (authenticationProviders.containsKey(authType)) {
31+
throw new IllegalStateException("Another " + authType + " provider was already registered: "
32+
+ authenticationProviders.get(authType));
33+
}
34+
authenticationProviders.put(provider.getAuthType(), provider);
35+
}
36+
37+
if (provider instanceof AnonymousAuthenticationProvider) {
38+
// There's nothing to synchronize
39+
return;
40+
}
41+
42+
// Synchronize the current user with the auth provider.
43+
controller.getAsync(false).onSuccess(new Continuation<ParseUser, Void>() {
44+
@Override
45+
public Void then(Task<ParseUser> task) throws Exception {
46+
ParseUser user = task.getResult();
47+
if (user != null) {
48+
user.synchronizeAuthData(authType);
49+
}
50+
return null;
51+
}
52+
});
53+
}
54+
55+
/* package for tests */ void unregister(ParseAuthenticationProvider provider) {
56+
57+
}
58+
59+
public boolean restoreAuthentication(String authType, Map<String, String> authData) {
60+
ParseAuthenticationProvider provider;
61+
synchronized (lock) {
62+
provider = authenticationProviders.get(authType);
63+
}
64+
return provider == null || provider.restoreAuthentication(authData);
65+
}
66+
67+
public Task<Void> deauthenticateAsync(String authType) {
68+
ParseAuthenticationProvider provider;
69+
synchronized (lock) {
70+
provider = authenticationProviders.get(authType);
71+
}
72+
if (provider != null) {
73+
return provider.deauthenticateAsync();
74+
}
75+
return Task.forResult(null);
76+
}
77+
}

Parse/src/main/java/com/parse/ParseAuthenticationProvider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import java.util.Map;
1212

13-
import bolts.Continuation;
1413
import bolts.Task;
1514

1615
/**

Parse/src/main/java/com/parse/ParseCorePlugins.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static ParseCorePlugins getInstance() {
3535
private AtomicReference<ParseCurrentInstallationController> currentInstallationController =
3636
new AtomicReference<>();
3737

38-
private AtomicReference<ParseAuthenticationController> authenticationController =
38+
private AtomicReference<ParseAuthenticationManager> authenticationController =
3939
new AtomicReference<>();
4040

4141
private AtomicReference<ParseQueryController> queryController = new AtomicReference<>();
@@ -290,9 +290,10 @@ public void registerCurrentInstallationController(ParseCurrentInstallationContro
290290
}
291291
}
292292

293-
public ParseAuthenticationController getAuthenticationController() {
293+
public ParseAuthenticationManager getAuthenticationManager() {
294294
if (authenticationController.get() == null) {
295-
ParseAuthenticationController controller = new ParseAuthenticationController();
295+
ParseAuthenticationManager controller =
296+
new ParseAuthenticationManager(getCurrentUserController());
296297
authenticationController.compareAndSet(null, controller);
297298
}
298299
return authenticationController.get();

Parse/src/main/java/com/parse/ParseUser.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public static ParseQuery<ParseUser> getQuery() {
5555
return ParseCorePlugins.getInstance().getCurrentUserController();
5656
}
5757

58-
/* package for tests */ static ParseAuthenticationController getAuthenticationController() {
59-
return ParseCorePlugins.getInstance().getAuthenticationController();
58+
/* package for tests */ static ParseAuthenticationManager getAuthenticationManager() {
59+
return ParseCorePlugins.getInstance().getAuthenticationManager();
6060
}
6161

6262
/** package */ static class State extends ParseObject.State {
@@ -248,7 +248,7 @@ public void remove(String key) {
248248
}
249249

250250
/* package for tests */ void cleanUpAuthData() {
251-
ParseAuthenticationController controller = getAuthenticationController();
251+
ParseAuthenticationManager controller = getAuthenticationManager();
252252
synchronized (mutex) {
253253
Map<String, Map<String, String>> authData = getState().authData();
254254
if (authData.size() == 0) {
@@ -977,7 +977,7 @@ public static void logOut() {
977977
}
978978

979979
/* package */ Task<Void> logOutAsync(boolean revoke) {
980-
ParseAuthenticationController controller = getAuthenticationController();
980+
ParseAuthenticationManager controller = getAuthenticationManager();
981981
List<Task<Void>> tasks = new ArrayList<>();
982982
final String oldSessionToken;
983983

@@ -1073,7 +1073,7 @@ public ParseUser fetchIfNeeded() throws ParseException {
10731073
if (!isCurrentUser()) {
10741074
return;
10751075
}
1076-
boolean success = getAuthenticationController()
1076+
boolean success = getAuthenticationManager()
10771077
.restoreAuthentication(authType, getAuthData(authType));
10781078
if (!success) {
10791079
unlinkFromAsync(authType);
@@ -1118,7 +1118,7 @@ public Task<Void> then(Task<Void> task) throws Exception {
11181118
}
11191119

11201120
/* package */ static void registerAuthenticationProvider(ParseAuthenticationProvider provider) {
1121-
getAuthenticationController().register(provider);
1121+
getAuthenticationManager().register(provider);
11221122
}
11231123

11241124
/* package */ static Task<ParseUser> logInWithAsync(
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
package com.parse;
10+
11+
import org.junit.Before;
12+
import org.junit.Rule;
13+
import org.junit.Test;
14+
import org.junit.rules.ExpectedException;
15+
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
19+
import bolts.Task;
20+
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.verify;
23+
import static org.mockito.Mockito.verifyNoMoreInteractions;
24+
import static org.mockito.Mockito.when;
25+
26+
public class ParseAuthenticationManagerTest {
27+
28+
@Rule
29+
public ExpectedException thrown = ExpectedException.none();
30+
31+
private ParseAuthenticationManager manager;
32+
private ParseCurrentUserController controller;
33+
private ParseAuthenticationProvider provider;
34+
35+
@Before
36+
public void setUp() {
37+
controller = mock(ParseCurrentUserController.class);
38+
manager = new ParseAuthenticationManager(controller);
39+
provider = mock(ParseAuthenticationProvider.class);
40+
when(provider.getAuthType()).thenReturn("test_provider");
41+
}
42+
43+
//region testRegister
44+
45+
@Test
46+
public void testRegisterMultipleShouldThrow() {
47+
when(controller.getAsync(false)).thenReturn(Task.<ParseUser>forResult(null));
48+
ParseAuthenticationProvider provider2 = mock(ParseAuthenticationProvider.class);
49+
when(provider2.getAuthType()).thenReturn("test_provider");
50+
51+
manager.register(provider);
52+
53+
thrown.expect(IllegalStateException.class);
54+
manager.register(provider2);
55+
}
56+
57+
@Test
58+
public void testRegisterAnonymous() {
59+
ParseAuthenticationProvider anonymous = mock(AnonymousAuthenticationProvider.class);
60+
61+
manager.register(anonymous);
62+
verifyNoMoreInteractions(controller);
63+
}
64+
65+
@Test
66+
public void testRegister() {
67+
ParseUser user = mock(ParseUser.class);
68+
when(controller.getAsync(false)).thenReturn(Task.forResult(user));
69+
70+
manager.register(provider);
71+
verify(controller).getAsync(false);
72+
verify(user).synchronizeAuthData("test_provider");
73+
}
74+
75+
//endregion
76+
77+
@Test
78+
public void testRestoreAuthentication() {
79+
when(controller.getAsync(false)).thenReturn(Task.<ParseUser>forResult(null));
80+
manager.register(provider);
81+
82+
Map<String, String> authData = new HashMap<>();
83+
manager.restoreAuthentication("test_provider", authData);
84+
85+
verify(provider).restoreAuthentication(authData);
86+
}
87+
88+
@Test
89+
public void testDeauthenticateAsync() throws ParseException {
90+
when(controller.getAsync(false)).thenReturn(Task.<ParseUser>forResult(null));
91+
when(provider.deauthenticateAsync()).thenReturn(Task.<Void>forResult(null));
92+
manager.register(provider);
93+
94+
ParseTaskUtils.wait(manager.deauthenticateAsync("test_provider"));
95+
96+
verify(provider).deauthenticateAsync();
97+
}
98+
}

0 commit comments

Comments
 (0)