-
Notifications
You must be signed in to change notification settings - Fork 30
[MOB-11508] Fix Android SDK auth token refresh in background #910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f46c0f9
d0a4547
e8ff15e
f3a783b
e6aa978
4e9af85
3c2537f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,4 +88,34 @@ public void testCallbacks() { | |
verify(callback, never()).onSwitchToBackground(); | ||
} | ||
|
||
@Test | ||
public void testAuthManagerLifecycleRegistration() { | ||
// Create a mock auth handler and initialize IterableApi with auth | ||
IterableAuthHandler mockAuthHandler = mock(IterableAuthHandler.class); | ||
IterableTestUtils.createIterableApiNew(new IterableTestUtils.ConfigBuilderExtender() { | ||
@Override | ||
public IterableConfig.Builder run(IterableConfig.Builder builder) { | ||
return builder.setAuthHandler(mockAuthHandler); | ||
} | ||
}, null); | ||
|
||
IterableApi.getInstance().setEmail("[email protected]"); | ||
IterableAuthManager authManager = IterableApi.getInstance().getAuthManager(); | ||
|
||
// Verify AuthManager is registered as a callback | ||
ActivityController<Activity> activity = Robolectric.buildActivity(Activity.class).create().start().resume(); | ||
Robolectric.flushForegroundThreadScheduler(); | ||
|
||
// Verify we can trigger lifecycle methods without errors | ||
authManager.onSwitchToBackground(); | ||
authManager.onSwitchToForeground(); | ||
|
||
// Test that reset() unregisters the callback | ||
authManager.reset(); | ||
|
||
// After reset, lifecycle methods should still work (no exceptions) | ||
authManager.onSwitchToBackground(); | ||
authManager.onSwitchToForeground(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably a better check would be to trigger parent level onSwitchToBackground to check if authmanager's onSwitchToBackground is actually called or not to check the unregistering of callbacks. |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -480,4 +480,33 @@ public void testRegisterForPushInvokedAfterTokenRefresh() throws InterruptedExce | |
//TODO: Verify if registerForPush is invoked | ||
} | ||
|
||
@Test | ||
public void testAuthTokenRefreshPausesOnBackground() throws Exception { | ||
IterableApi.initialize(getContext(), "apiKey"); | ||
|
||
IterableAuthManager authManager = IterableApi.getInstance().getAuthManager(); | ||
|
||
// Set up a valid token and user to trigger normal expiration refresh | ||
doReturn(validJWT).when(authHandler).onAuthTokenRequested(); | ||
IterableApi.getInstance().setEmail("[email protected]"); | ||
shadowOf(getMainLooper()).runToEndOfTasks(); | ||
|
||
// Request auth token which should set a timer for expiration refresh | ||
authManager.requestNewAuthToken(false); | ||
shadowOf(getMainLooper()).runToEndOfTasks(); | ||
|
||
// The timer might be null if the token is considered expired, so let's test the behavior | ||
// rather than the internal timer state. We'll check that onSwitchToBackground and | ||
// onSwitchToForeground can be called without exceptions | ||
|
||
// Simulate app going to background - should clear any timer | ||
authManager.onSwitchToBackground(); | ||
|
||
// Simulate app coming to foreground - should re-evaluate token state | ||
authManager.onSwitchToForeground(); | ||
shadowOf(getMainLooper()).runToEndOfTasks(); | ||
|
||
// Test passes if no exceptions were thrown and lifecycle methods executed successfully | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we not test anything after onSwitchToForeground? Like if the jwt is being fetched again or not? Or if the timer is no more running. Or probably running as there is new requests SDK is making? |
||
} | ||
|
||
} |
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.