Skip to content

Commit ebfde36

Browse files
fix(geo): Fix Geo Plugin Auth injection (#2704)
Co-authored-by: Matt Creaser <[email protected]>
1 parent d229be4 commit ebfde36

File tree

8 files changed

+94
-97
lines changed

8 files changed

+94
-97
lines changed

aws-core/src/main/java/com/amplifyframework/auth/CognitoCredentialsProvider.kt

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.amplifyframework.auth
1818
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
1919
import aws.smithy.kotlin.runtime.util.Attributes
2020
import com.amplifyframework.AmplifyException
21+
import com.amplifyframework.annotations.InternalAmplifyApi
2122
import com.amplifyframework.core.Amplify
2223
import com.amplifyframework.core.Consumer
2324
import kotlin.coroutines.resume
@@ -27,14 +28,18 @@ import kotlin.coroutines.suspendCoroutine
2728
/**
2829
* Wrapper to provide credentials from Auth synchronously and asynchronously
2930
*/
30-
open class CognitoCredentialsProvider : AuthCredentialsProvider {
31+
open class CognitoCredentialsProvider @InternalAmplifyApi constructor(
32+
private val authCategory: AuthCategory
33+
) : AuthCredentialsProvider {
34+
35+
constructor() : this(Amplify.Auth)
3136

3237
/**
3338
* Request [Credentials] from the provider.
3439
*/
3540
override suspend fun resolve(attributes: Attributes): Credentials {
3641
return suspendCoroutine { continuation ->
37-
Amplify.Auth.fetchAuthSession(
42+
authCategory.fetchAuthSession(
3843
{ authSession ->
3944
authSession.toAWSAuthSession()?.awsCredentialsResult?.value?.let {
4045
continuation.resume(it.toSdkCredentials())
@@ -58,7 +63,7 @@ open class CognitoCredentialsProvider : AuthCredentialsProvider {
5863
*/
5964
override suspend fun getIdentityId(): String {
6065
return suspendCoroutine { continuation ->
61-
Amplify.Auth.fetchAuthSession(
66+
authCategory.fetchAuthSession(
6267
{ authSession ->
6368
authSession.toAWSAuthSession()?.identityIdResult?.value?.let {
6469
continuation.resume(it)
@@ -78,7 +83,7 @@ open class CognitoCredentialsProvider : AuthCredentialsProvider {
7883
}
7984

8085
override fun getAccessToken(onResult: Consumer<String>, onFailure: Consumer<Exception>) {
81-
Amplify.Auth.fetchAuthSession(
86+
authCategory.fetchAuthSession(
8287
{ session ->
8388
val tokens = session.toAWSAuthSession()?.accessToken
8489
tokens?.let { onResult.accept(tokens) }

aws-geo-location/src/androidTest/java/com/amplifyframework/geo/location/MapsApiTest.kt

+12-24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package com.amplifyframework.geo.location
1717

1818
import android.content.Context
1919
import androidx.test.core.app.ApplicationProvider
20+
import com.amplifyframework.auth.AuthCategory
2021
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
2122
import com.amplifyframework.geo.GeoCategory
2223
import com.amplifyframework.geo.GeoException
@@ -29,24 +30,25 @@ import org.junit.After
2930
import org.junit.Assert.assertNotNull
3031
import org.junit.Assert.assertTrue
3132
import org.junit.Before
32-
import org.junit.BeforeClass
3333
import org.junit.Test
3434

3535
/**
3636
* Tests various functionalities related to Maps API in [AWSLocationGeoPlugin].
3737
*/
3838
class MapsApiTest {
39-
private var geo: SynchronousGeo? = null
39+
private lateinit var geo: SynchronousGeo
40+
private lateinit var auth: SynchronousAuth
4041

4142
/**
4243
* Set up test categories to be used for testing.
4344
*/
4445
@Before
4546
fun setUpBeforeTest() {
46-
// Auth plugin uses default configuration
47-
// Geo plugin uses above auth category to authenticate users
48-
val geoPlugin = AWSLocationGeoPlugin()
47+
val authPlugin = AWSCognitoAuthPlugin()
48+
val authCategory = TestCategory.forPlugin(authPlugin) as AuthCategory
49+
val geoPlugin = AWSLocationGeoPlugin(authCategory = authCategory)
4950
val geoCategory = TestCategory.forPlugin(geoPlugin) as GeoCategory
51+
auth = SynchronousAuth.delegatingTo(authPlugin)
5052
geo = SynchronousGeo.delegatingTo(geoCategory)
5153
}
5254

@@ -66,7 +68,7 @@ class MapsApiTest {
6668
@Test
6769
fun styleDescriptorLoadsProperly() {
6870
signInWithCognito()
69-
val style = geo?.getMapStyleDescriptor(GetMapStyleDescriptorOptions.defaults())
71+
val style = geo.getMapStyleDescriptor(GetMapStyleDescriptorOptions.defaults())
7072
assertNotNull(style)
7173
assertNotNull(style?.json)
7274

@@ -86,31 +88,17 @@ class MapsApiTest {
8688
@Test(expected = GeoException::class)
8789
fun cannotFetchStyleWithoutAuth() {
8890
// should not be authorized to fetch map resource from Amazon Location Service
89-
geo?.getMapStyleDescriptor(GetMapStyleDescriptorOptions.defaults())
91+
geo.getMapStyleDescriptor(GetMapStyleDescriptorOptions.defaults())
9092
}
9193

9294
private fun signInWithCognito() {
9395
val context = ApplicationProvider.getApplicationContext<Context>()
9496
val (username, password) = Credentials.load(context)
95-
auth?.signIn(username, password)
97+
signOutFromCognito() // this ensures a previous test failure doesn't impact current test
98+
auth.signIn(username, password)
9699
}
97100

98101
private fun signOutFromCognito() {
99-
auth?.signOut()
100-
}
101-
102-
companion object {
103-
lateinit var auth: SynchronousAuth
104-
105-
/**
106-
* Set up test categories to be used for testing.
107-
*/
108-
@BeforeClass
109-
@JvmStatic
110-
fun setUp() {
111-
// Auth plugin uses default configuration
112-
auth =
113-
SynchronousAuth.delegatingToCognito(ApplicationProvider.getApplicationContext(), AWSCognitoAuthPlugin())
114-
}
102+
auth.signOut()
115103
}
116104
}

aws-geo-location/src/main/java/com/amplifyframework/geo/location/AWSLocationGeoPlugin.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class AWSLocationGeoPlugin(
6767

6868
@InternalApiWarning
6969
val credentialsProvider: CredentialsProvider by lazy {
70-
CognitoCredentialsProvider()
70+
CognitoCredentialsProvider(authCategory)
7171
}
7272

7373
override fun getPluginKey(): String {

maplibre-adapter/src/androidTest/java/com/amplifyframework/geo/maplibre/AmplifyWrapper.kt

-40
This file was deleted.

maplibre-adapter/src/androidTest/java/com/amplifyframework/geo/maplibre/MapViewStressTest.kt

+25-9
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ package com.amplifyframework.geo.maplibre
1717

1818
import androidx.test.core.app.ApplicationProvider
1919
import androidx.test.ext.junit.rules.ActivityScenarioRule
20+
import com.amplifyframework.auth.AuthCategory
21+
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
2022
import com.amplifyframework.geo.GeoCategory
2123
import com.amplifyframework.geo.location.AWSLocationGeoPlugin
24+
import com.amplifyframework.testutils.sync.SynchronousAuth
2225
import com.amplifyframework.testutils.sync.SynchronousGeo
2326
import com.amplifyframework.testutils.sync.TestCategory
2427
import kotlin.coroutines.resume
@@ -27,6 +30,7 @@ import kotlin.coroutines.suspendCoroutine
2730
import kotlinx.coroutines.CoroutineScope
2831
import kotlinx.coroutines.Dispatchers
2932
import kotlinx.coroutines.runBlocking
33+
import org.junit.After
3034
import org.junit.Assert
3135
import org.junit.Before
3236
import org.junit.Rule
@@ -35,23 +39,35 @@ import org.junit.Test
3539
class MapViewStressTest {
3640
@get:Rule
3741
var rule = ActivityScenarioRule(MapViewTestActivity::class.java)
38-
private var geo: SynchronousGeo? = null
42+
43+
private lateinit var geoCategory: GeoCategory
44+
private lateinit var geo: SynchronousGeo
45+
private lateinit var auth: SynchronousAuth
3946

4047
/**
4148
* Set up test categories to be used for testing.
4249
*/
4350
@Before
4451
fun setUpBeforeTest() {
45-
val geoPlugin = AWSLocationGeoPlugin()
46-
val geoCategory = TestCategory.forPlugin(geoPlugin) as GeoCategory
52+
val authPlugin = AWSCognitoAuthPlugin()
53+
val authCategory = TestCategory.forPlugin(authPlugin) as AuthCategory
54+
val geoPlugin = AWSLocationGeoPlugin(authCategory = authCategory)
55+
geoCategory = TestCategory.forPlugin(geoPlugin) as GeoCategory
56+
auth = SynchronousAuth.delegatingTo(authPlugin)
4757
geo = SynchronousGeo.delegatingTo(geoCategory)
58+
signInWithCognito()
59+
}
60+
61+
@After
62+
fun tearDown() {
63+
signOutFromCognito()
4864
}
4965

5066
/**
5167
* Calls mapView.setStyle 50 times
5268
*/
5369
@Test
54-
fun testMultipleSetStyle() = runBlockingSignedIn(rule) {
70+
fun testMultipleSetStyle() = runBlockingWithConfiguredMapActivity(rule) {
5571
repeat(50) {
5672
val mapStyle = suspendCoroutine { continuation ->
5773
rule.scenario.onActivity { activity ->
@@ -67,26 +83,26 @@ class MapViewStressTest {
6783
}
6884
}
6985

70-
private fun <T> runBlockingSignedIn(
86+
private fun <T> runBlockingWithConfiguredMapActivity(
7187
rule: ActivityScenarioRule<MapViewTestActivity>,
7288
block: suspend CoroutineScope.() -> T
7389
): T {
7490
return runBlocking(Dispatchers.Main) {
7591
rule.scenario.onActivity {
76-
signOutFromCognito() // first sign out to ensure we are in clean state
77-
signInWithCognito()
92+
it.setMapView(geoCategory)
7893
}
7994
block()
8095
}
8196
}
8297

8398
private fun signInWithCognito() {
99+
signOutFromCognito() // this ensures a previous test failure doesn't impact current test
84100
val (username, password) = Credentials.load(ApplicationProvider.getApplicationContext())
85-
val result = AmplifyWrapper.auth.signIn(username, password)
101+
val result = auth.signIn(username, password)
86102
println("SignIn complete: ${result.isSignedIn}")
87103
}
88104

89105
private fun signOutFromCognito() {
90-
AmplifyWrapper.auth.signOut()
106+
auth.signOut()
91107
}
92108
}

maplibre-adapter/src/androidTest/java/com/amplifyframework/geo/maplibre/MapViewTestActivity.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,19 @@
1515

1616
package com.amplifyframework.geo.maplibre
1717

18-
import android.os.Bundle
1918
import androidx.appcompat.app.AppCompatActivity
19+
import com.amplifyframework.geo.GeoCategory
2020
import com.amplifyframework.geo.maplibre.view.MapLibreView
2121

2222
/**
2323
* Activity that initializes MapLibre SDK with adapter on create.
2424
*/
2525
class MapViewTestActivity : AppCompatActivity() {
2626

27-
internal val mapView: MapLibreView by lazy {
28-
MapLibreView(context = applicationContext, geo = AmplifyWrapper.geo)
29-
}
27+
lateinit var mapView: MapLibreView
3028

31-
override fun onCreate(savedInstanceState: Bundle?) {
32-
super.onCreate(savedInstanceState)
29+
fun setMapView(geoCategory: GeoCategory) {
30+
mapView = MapLibreView(context = applicationContext, geo = geoCategory)
3331
setContentView(mapView)
3432
}
3533
}

0 commit comments

Comments
 (0)