Skip to content

Commit 8eefb95

Browse files
committed
Revert "chore(auth): Fix failing integration tests (#3032)"
This reverts commit 4cf9f34.
1 parent bddf064 commit 8eefb95

File tree

4 files changed

+152
-49
lines changed

4 files changed

+152
-49
lines changed

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/usecases/FetchAuthSessionUseCase.kt

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ import com.amplifyframework.auth.exceptions.SignedOutException
3030
import com.amplifyframework.auth.exceptions.UnknownException
3131
import com.amplifyframework.auth.options.AuthFetchSessionOptions
3232
import com.amplifyframework.auth.plugins.core.AuthHubEventEmitter
33-
import com.amplifyframework.statemachine.StateMachineEvent
3433
import com.amplifyframework.statemachine.codegen.data.AmplifyCredential
3534
import com.amplifyframework.statemachine.codegen.errors.SessionError
3635
import com.amplifyframework.statemachine.codegen.events.AuthorizationEvent
3736
import com.amplifyframework.statemachine.codegen.states.AuthorizationState
3837
import kotlinx.coroutines.flow.first
3938
import kotlinx.coroutines.flow.mapNotNull
40-
import kotlinx.coroutines.flow.onStart
4139

4240
internal class FetchAuthSessionUseCase(
4341
private val stateMachine: AuthStateMachine,
@@ -46,82 +44,80 @@ internal class FetchAuthSessionUseCase(
4644
suspend fun execute(options: AuthFetchSessionOptions = AuthFetchSessionOptions.defaults()): AWSCognitoAuthSession =
4745
when (val authZState = stateMachine.getCurrentState().authZState) {
4846
is AuthorizationState.Configured -> {
49-
listenForSessionEstablished(
50-
eventToSend = AuthorizationEvent(AuthorizationEvent.EventType.FetchUnAuthSession)
51-
)
47+
stateMachine.send(AuthorizationEvent(AuthorizationEvent.EventType.FetchUnAuthSession))
48+
listenForSessionEstablished()
5249
}
5350
is AuthorizationState.SessionEstablished -> {
5451
val credential = authZState.amplifyCredential
5552
if (!credential.isValid() || options.forceRefresh) {
56-
listenForSessionEstablished(eventToSend = refreshSessionEvent(credential))
53+
sendRefreshSessionEvent(credential)
54+
listenForSessionEstablished()
5755
} else {
5856
credential.getCognitoSession()
5957
}
6058
}
6159
is AuthorizationState.Error -> {
6260
val error = authZState.exception
6361
if (error is SessionError) {
64-
listenForSessionEstablished(eventToSend = refreshSessionEvent(error.amplifyCredential))
62+
sendRefreshSessionEvent(error.amplifyCredential)
63+
listenForSessionEstablished()
6564
} else {
6665
throw InvalidStateException()
6766
}
6867
}
6968
else -> throw InvalidStateException()
7069
}
7170

72-
private fun refreshSessionEvent(credential: AmplifyCredential): StateMachineEvent =
71+
private fun sendRefreshSessionEvent(credential: AmplifyCredential) {
7372
if (credential is AmplifyCredential.IdentityPoolFederated) {
74-
AuthorizationEvent(
75-
AuthorizationEvent.EventType.StartFederationToIdentityPool(
76-
credential.federatedToken,
77-
credential.identityId,
78-
credential
73+
stateMachine.send(
74+
AuthorizationEvent(
75+
AuthorizationEvent.EventType.StartFederationToIdentityPool(
76+
credential.federatedToken,
77+
credential.identityId,
78+
credential
79+
)
7980
)
8081
)
8182
} else {
82-
AuthorizationEvent(AuthorizationEvent.EventType.RefreshSession(credential))
83+
stateMachine.send(AuthorizationEvent(AuthorizationEvent.EventType.RefreshSession(credential)))
8384
}
85+
}
8486

85-
private suspend fun listenForSessionEstablished(eventToSend: StateMachineEvent): AWSCognitoAuthSession {
86-
val session = stateMachine.stateTransitions
87-
.onStart {
88-
stateMachine.send(eventToSend)
89-
}
90-
.mapNotNull { authState ->
91-
when (val authZState = authState.authZState) {
92-
is AuthorizationState.SessionEstablished -> authZState.amplifyCredential.getCognitoSession()
93-
is AuthorizationState.Error -> {
94-
when (val error = authZState.exception) {
95-
is SessionError -> {
96-
when (val innerException = error.exception) {
97-
is SignedOutException -> error.amplifyCredential.getCognitoSession(innerException)
98-
is ServiceException -> error.amplifyCredential.getCognitoSession(innerException)
99-
is NotAuthorizedException -> error.amplifyCredential.getCognitoSession(
100-
innerException
101-
)
102-
is SessionExpiredException -> {
103-
emitter.sendHubEvent(AuthChannelEventName.SESSION_EXPIRED.toString())
104-
error.amplifyCredential.getCognitoSession(innerException)
105-
}
106-
else -> {
107-
val errorResult = UnknownException("Fetch auth session failed.", innerException)
108-
error.amplifyCredential.getCognitoSession(errorResult)
109-
}
87+
private suspend fun listenForSessionEstablished(): AWSCognitoAuthSession {
88+
val session = stateMachine.stateTransitions.mapNotNull { authState ->
89+
when (val authZState = authState.authZState) {
90+
is AuthorizationState.SessionEstablished -> authZState.amplifyCredential.getCognitoSession()
91+
is AuthorizationState.Error -> {
92+
when (val error = authZState.exception) {
93+
is SessionError -> {
94+
when (val innerException = error.exception) {
95+
is SignedOutException -> error.amplifyCredential.getCognitoSession(innerException)
96+
is ServiceException -> error.amplifyCredential.getCognitoSession(innerException)
97+
is NotAuthorizedException -> error.amplifyCredential.getCognitoSession(innerException)
98+
is SessionExpiredException -> {
99+
emitter.sendHubEvent(AuthChannelEventName.SESSION_EXPIRED.toString())
100+
error.amplifyCredential.getCognitoSession(innerException)
101+
}
102+
else -> {
103+
val errorResult = UnknownException("Fetch auth session failed.", innerException)
104+
error.amplifyCredential.getCognitoSession(errorResult)
110105
}
111106
}
112-
is ConfigurationException -> {
113-
val errorResult = InvalidAccountTypeException(error)
114-
AmplifyCredential.Empty.getCognitoSession(errorResult)
115-
}
116-
else -> {
117-
val errorResult = UnknownException("Fetch auth session failed.", error)
118-
AmplifyCredential.Empty.getCognitoSession(errorResult)
119-
}
107+
}
108+
is ConfigurationException -> {
109+
val errorResult = InvalidAccountTypeException(error)
110+
AmplifyCredential.Empty.getCognitoSession(errorResult)
111+
}
112+
else -> {
113+
val errorResult = UnknownException("Fetch auth session failed.", error)
114+
AmplifyCredential.Empty.getCognitoSession(errorResult)
120115
}
121116
}
122-
else -> null // no-op
123117
}
124-
}.first()
118+
else -> null // no-op
119+
}
120+
}.first()
125121

126122
return session
127123
}

configuration/instrumentation-tests.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def module_backends = [
99
'aws-storage-s3' : 'NONE',
1010
'aws-analytics-pinpoint': 'NONE',
1111
'aws-predictions' : 'NONE',
12+
'core' : 'NONE',
1213
'aws-auth-cognito' : 'AuthIntegrationTests'
1314
]
1415

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amplifyframework.devmenu;
17+
18+
import androidx.annotation.RawRes;
19+
import androidx.fragment.app.testing.FragmentScenario;
20+
import androidx.navigation.NavDestination;
21+
import androidx.navigation.Navigation;
22+
import androidx.navigation.testing.TestNavHostController;
23+
import androidx.test.core.app.ApplicationProvider;
24+
import androidx.test.espresso.action.ViewActions;
25+
import androidx.test.espresso.matcher.ViewMatchers;
26+
27+
import com.amplifyframework.core.R;
28+
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
32+
import static androidx.test.espresso.Espresso.onView;
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertNotNull;
35+
36+
/**
37+
* Tests the navigation behavior for {@link DevMenuMainFragment}.
38+
*/
39+
public final class DevMenuMainViewInstrumentationTest {
40+
// A navigation host controller for testing.
41+
private TestNavHostController navHostController;
42+
43+
/**
44+
* Go to the main screen of the developer menu.
45+
*/
46+
@Before
47+
public void resetView() {
48+
FragmentScenario<DevMenuMainFragment> mainMenuScenario =
49+
FragmentScenario.launchInContainer(DevMenuMainFragment.class);
50+
mainMenuScenario.onFragment(fragment -> {
51+
navHostController = new TestNavHostController(ApplicationProvider.getApplicationContext());
52+
navHostController.setGraph(R.navigation.dev_menu_nav_graph);
53+
Navigation.setViewNavController(fragment.requireView(), navHostController);
54+
});
55+
}
56+
57+
58+
/**
59+
* Tests that the app is navigated to the environment information
60+
* screen when the view environment information button is pressed.
61+
*/
62+
@Test
63+
public void testNavigationToEnvInfoScreen() {
64+
testNavigationOnButtonPress(R.id.env_button, R.id.environment_fragment);
65+
}
66+
67+
/**
68+
* Tests that the app is navigated to the device information
69+
* screen when the view device information button is pressed.
70+
*/
71+
@Test
72+
public void testNavigationToDeviceInfoScreen() {
73+
testNavigationOnButtonPress(R.id.device_button, R.id.device_fragment);
74+
}
75+
76+
/**
77+
* Tests that the app is navigated to the logs screen when
78+
* the view logs button is pressed.
79+
*/
80+
@Test
81+
public void testNavigationToLogsScreen() {
82+
testNavigationOnButtonPress(R.id.logs_button, R.id.logs_fragment);
83+
}
84+
85+
/**
86+
* Tests that the app is navigated to the file issue screen
87+
* when the file issue button is pressed.
88+
*/
89+
@Test
90+
public void testNavigationToFileIssueScreen() {
91+
testNavigationOnButtonPress(R.id.file_issue_button, R.id.file_issue_fragment);
92+
}
93+
94+
/**
95+
* Test that the app is navigated to the destination with the given ID when
96+
* the button with the given ID is pressed.
97+
* @param buttonId ID of the button pressed
98+
* @param destinationId ID of the navigation destination
99+
*/
100+
private void testNavigationOnButtonPress(@RawRes int buttonId, @RawRes int destinationId) {
101+
onView(ViewMatchers.withId(buttonId)).perform(ViewActions.click());
102+
NavDestination curDestination = navHostController.getCurrentDestination();
103+
assertNotNull(curDestination);
104+
assertEquals(curDestination.getId(), destinationId);
105+
}
106+
}

0 commit comments

Comments
 (0)