Skip to content

Commit 95a4d35

Browse files
committed
Kotlin implementation for Https callable functions
1 parent 4f4ede7 commit 95a4d35

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

firebase-functions/ktx/ktx.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ dependencies {
5959
testImplementation(libs.junit)
6060
testImplementation(libs.truth)
6161
testImplementation(libs.androidx.test.core)
62+
testImplementation(project(":firebase-functions"))
6263
}

firebase-functions/ktx/src/main/kotlin/com/google/firebase/functions/ktx/Functions.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ import com.google.firebase.FirebaseApp
1919
import com.google.firebase.components.Component
2020
import com.google.firebase.components.ComponentRegistrar
2121
import com.google.firebase.functions.FirebaseFunctions
22+
import com.google.firebase.functions.HttpsCallableOptions
23+
import com.google.firebase.functions.HttpsCallableReference
2224
import com.google.firebase.ktx.Firebase
2325
import com.google.firebase.platforminfo.LibraryVersionComponent
26+
import java.net.URL
2427

2528
/** Returns the [FirebaseFunctions] instance of the default [FirebaseApp]. */
2629
val Firebase.functions: FirebaseFunctions
@@ -45,3 +48,15 @@ class FirebaseFunctionsKtxRegistrar : ComponentRegistrar {
4548
override fun getComponents(): List<Component<*>> =
4649
listOf(LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME))
4750
}
51+
52+
fun FirebaseFunctions.getHttpsCallable(name: String, init: HttpsCallableOptions.Builder.() -> Unit) : HttpsCallableReference {
53+
val builder = HttpsCallableOptions.Builder()
54+
builder.init()
55+
return getHttpsCallable(name, builder.build())
56+
}
57+
58+
fun FirebaseFunctions.getHttpsCallableFromUrl(url: URL, init: HttpsCallableOptions.Builder.() -> Unit) : HttpsCallableReference {
59+
val builder = HttpsCallableOptions.Builder()
60+
builder.init()
61+
return getHttpsCallableFromUrl(url, builder.build())
62+
}

firebase-functions/ktx/src/test/kotlin/com/google/firebase/functions/ktx/FunctionsTests.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import com.google.common.truth.Truth.assertThat
1919
import com.google.firebase.FirebaseApp
2020
import com.google.firebase.FirebaseOptions
2121
import com.google.firebase.functions.FirebaseFunctions
22+
import com.google.firebase.functions.HttpsCallableReference
23+
import com.google.firebase.functions.TestVisibilityUtil
2224
import com.google.firebase.ktx.Firebase
2325
import com.google.firebase.ktx.app
2426
import com.google.firebase.ktx.initialize
@@ -28,6 +30,8 @@ import org.junit.Before
2830
import org.junit.Test
2931
import org.junit.runner.RunWith
3032
import org.robolectric.RobolectricTestRunner
33+
import java.lang.reflect.Field
34+
import java.net.URL
3135

3236
const val APP_ID = "APP_ID"
3337
const val API_KEY = "API_KEY"
@@ -100,3 +104,38 @@ class LibraryVersionTest : BaseTestCase() {
100104
assertThat(publisher.userAgent).contains(LIBRARY_NAME)
101105
}
102106
}
107+
108+
@RunWith(RobolectricTestRunner::class)
109+
class AppCheckLimitedUseTest : BaseTestCase() {
110+
@Test
111+
fun `FirebaseFunctions#getHttpsCallable should build callable with FAC settings (when true)`() {
112+
val callable = Firebase.functions.getHttpsCallable("function") {
113+
limitedUseAppCheckTokens = true
114+
};
115+
assertThat(TestVisibilityUtil.refUsesLimitedUseFacTokens(callable)).isEqualTo(true)
116+
}
117+
118+
// @Test
119+
// fun `FirebaseFunctions#getHttpsCallable should build callable with FAC settings (when false)`() {
120+
// val callable = Firebase.functions.getHttpsCallable("function") {
121+
// limitedUseAppCheckTokens = false
122+
// };
123+
// assertLimitedUseFACTokens(callable, false)
124+
// }
125+
//
126+
// @Test
127+
// fun `FirebaseFunctions#getHttpsCallableFromUrl callable with FAC settings (when true)`() {
128+
// val callable = Firebase.functions.getHttpsCallableFromUrl(URL()) {
129+
// limitedUseAppCheckTokens = true
130+
// };
131+
// assertLimitedUseFACTokens(callable, true)
132+
// }
133+
//
134+
// @Test
135+
// fun `FirebaseFunctions#getHttpsCallable should build callable with FAC settings (when false)`() {
136+
// val callable = Firebase.functions.getHttpsCallable("function") {
137+
// limitedUseAppCheckTokens = false
138+
// };
139+
// assertLimitedUseFACTokens(callable, false)
140+
// }
141+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.google.firebase.functions;
2+
3+
/** Exposes fields for testing from Kotlin, which is in a different package */
4+
public final class TestVisibilityUtil {
5+
private TestVisibilityUtil() {}
6+
7+
/**
8+
* Returns true if the {@link HttpsCallableReference} is configured to use FAC limited-use tokens.
9+
*/
10+
public static boolean refUsesLimitedUseFacTokens(HttpsCallableReference ref) {
11+
return ref.options.getLimitedUseAppCheckTokens();
12+
}
13+
}

0 commit comments

Comments
 (0)