Skip to content

fix(geo): Fix Geo Plugin Auth injection #2704

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

Merged
merged 8 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.amplifyframework.auth
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
import aws.smithy.kotlin.runtime.util.Attributes
import com.amplifyframework.AmplifyException
import com.amplifyframework.annotations.InternalAmplifyApi
import com.amplifyframework.core.Amplify
import com.amplifyframework.core.Consumer
import kotlin.coroutines.resume
Expand All @@ -27,14 +28,18 @@ import kotlin.coroutines.suspendCoroutine
/**
* Wrapper to provide credentials from Auth synchronously and asynchronously
*/
open class CognitoCredentialsProvider : AuthCredentialsProvider {
open class CognitoCredentialsProvider @InternalAmplifyApi constructor(
private val authCategory: AuthCategory
) : AuthCredentialsProvider {

constructor() : this(Amplify.Auth)

/**
* Request [Credentials] from the provider.
*/
override suspend fun resolve(attributes: Attributes): Credentials {
return suspendCoroutine { continuation ->
Amplify.Auth.fetchAuthSession(
authCategory.fetchAuthSession(
{ authSession ->
authSession.toAWSAuthSession()?.awsCredentialsResult?.value?.let {
continuation.resume(it.toSdkCredentials())
Expand All @@ -58,7 +63,7 @@ open class CognitoCredentialsProvider : AuthCredentialsProvider {
*/
override suspend fun getIdentityId(): String {
return suspendCoroutine { continuation ->
Amplify.Auth.fetchAuthSession(
authCategory.fetchAuthSession(
{ authSession ->
authSession.toAWSAuthSession()?.identityIdResult?.value?.let {
continuation.resume(it)
Expand All @@ -78,7 +83,7 @@ open class CognitoCredentialsProvider : AuthCredentialsProvider {
}

override fun getAccessToken(onResult: Consumer<String>, onFailure: Consumer<Exception>) {
Amplify.Auth.fetchAuthSession(
authCategory.fetchAuthSession(
{ session ->
val tokens = session.toAWSAuthSession()?.accessToken
tokens?.let { onResult.accept(tokens) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.amplifyframework.geo.location

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.amplifyframework.auth.AuthCategory
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
import com.amplifyframework.geo.GeoCategory
import com.amplifyframework.geo.GeoException
Expand All @@ -29,24 +30,25 @@ import org.junit.After
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test

/**
* Tests various functionalities related to Maps API in [AWSLocationGeoPlugin].
*/
class MapsApiTest {
private var geo: SynchronousGeo? = null
private lateinit var geo: SynchronousGeo
private lateinit var auth: SynchronousAuth

/**
* Set up test categories to be used for testing.
*/
@Before
fun setUpBeforeTest() {
// Auth plugin uses default configuration
// Geo plugin uses above auth category to authenticate users
val geoPlugin = AWSLocationGeoPlugin()
val authPlugin = AWSCognitoAuthPlugin()
val authCategory = TestCategory.forPlugin(authPlugin) as AuthCategory
val geoPlugin = AWSLocationGeoPlugin(authCategory = authCategory)
val geoCategory = TestCategory.forPlugin(geoPlugin) as GeoCategory
auth = SynchronousAuth.delegatingTo(authPlugin)
geo = SynchronousGeo.delegatingTo(geoCategory)
}

Expand All @@ -66,7 +68,7 @@ class MapsApiTest {
@Test
fun styleDescriptorLoadsProperly() {
signInWithCognito()
val style = geo?.getMapStyleDescriptor(GetMapStyleDescriptorOptions.defaults())
val style = geo.getMapStyleDescriptor(GetMapStyleDescriptorOptions.defaults())
assertNotNull(style)
assertNotNull(style?.json)

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

private fun signInWithCognito() {
val context = ApplicationProvider.getApplicationContext<Context>()
val (username, password) = Credentials.load(context)
auth?.signIn(username, password)
signOutFromCognito() // this ensures a previous test failure doesn't impact current test
auth.signIn(username, password)
}

private fun signOutFromCognito() {
auth?.signOut()
}

companion object {
lateinit var auth: SynchronousAuth

/**
* Set up test categories to be used for testing.
*/
@BeforeClass
@JvmStatic
fun setUp() {
// Auth plugin uses default configuration
auth =
SynchronousAuth.delegatingToCognito(ApplicationProvider.getApplicationContext(), AWSCognitoAuthPlugin())
}
auth.signOut()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AWSLocationGeoPlugin(

@InternalApiWarning
val credentialsProvider: CredentialsProvider by lazy {
CognitoCredentialsProvider()
CognitoCredentialsProvider(authCategory)
}

override fun getPluginKey(): String {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ package com.amplifyframework.geo.maplibre

import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.amplifyframework.auth.AuthCategory
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
import com.amplifyframework.geo.GeoCategory
import com.amplifyframework.geo.location.AWSLocationGeoPlugin
import com.amplifyframework.testutils.sync.SynchronousAuth
import com.amplifyframework.testutils.sync.SynchronousGeo
import com.amplifyframework.testutils.sync.TestCategory
import kotlin.coroutines.resume
Expand All @@ -27,6 +30,7 @@ import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
Expand All @@ -35,23 +39,35 @@ import org.junit.Test
class MapViewStressTest {
@get:Rule
var rule = ActivityScenarioRule(MapViewTestActivity::class.java)
private var geo: SynchronousGeo? = null

private lateinit var geoCategory: GeoCategory
private lateinit var geo: SynchronousGeo
private lateinit var auth: SynchronousAuth

/**
* Set up test categories to be used for testing.
*/
@Before
fun setUpBeforeTest() {
val geoPlugin = AWSLocationGeoPlugin()
val geoCategory = TestCategory.forPlugin(geoPlugin) as GeoCategory
val authPlugin = AWSCognitoAuthPlugin()
val authCategory = TestCategory.forPlugin(authPlugin) as AuthCategory
val geoPlugin = AWSLocationGeoPlugin(authCategory = authCategory)
geoCategory = TestCategory.forPlugin(geoPlugin) as GeoCategory
auth = SynchronousAuth.delegatingTo(authPlugin)
geo = SynchronousGeo.delegatingTo(geoCategory)
signInWithCognito()
}

@After
fun tearDown() {
signOutFromCognito()
}

/**
* Calls mapView.setStyle 50 times
*/
@Test
fun testMultipleSetStyle() = runBlockingSignedIn(rule) {
fun testMultipleSetStyle() = runBlockingWithConfiguredMapActivity(rule) {
repeat(50) {
val mapStyle = suspendCoroutine { continuation ->
rule.scenario.onActivity { activity ->
Expand All @@ -67,26 +83,26 @@ class MapViewStressTest {
}
}

private fun <T> runBlockingSignedIn(
private fun <T> runBlockingWithConfiguredMapActivity(
rule: ActivityScenarioRule<MapViewTestActivity>,
block: suspend CoroutineScope.() -> T
): T {
return runBlocking(Dispatchers.Main) {
rule.scenario.onActivity {
signOutFromCognito() // first sign out to ensure we are in clean state
signInWithCognito()
it.setMapView(geoCategory)
}
block()
}
}

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

private fun signOutFromCognito() {
AmplifyWrapper.auth.signOut()
auth.signOut()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@

package com.amplifyframework.geo.maplibre

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.amplifyframework.geo.GeoCategory
import com.amplifyframework.geo.maplibre.view.MapLibreView

/**
* Activity that initializes MapLibre SDK with adapter on create.
*/
class MapViewTestActivity : AppCompatActivity() {

internal val mapView: MapLibreView by lazy {
MapLibreView(context = applicationContext, geo = AmplifyWrapper.geo)
}
lateinit var mapView: MapLibreView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
fun setMapView(geoCategory: GeoCategory) {
mapView = MapLibreView(context = applicationContext, geo = geoCategory)
setContentView(mapView)
}
}
Loading