|
17 | 17 |
|
18 | 18 | from pytest import raises |
19 | 19 |
|
20 | | -from firebase_functions import https_fn, options, params |
| 20 | +from firebase_functions import alerts_fn, https_fn, options, params |
| 21 | +from firebase_functions.alerts import ( |
| 22 | + app_distribution_fn, |
| 23 | + billing_fn, |
| 24 | + crashlytics_fn, |
| 25 | + performance_fn, |
| 26 | +) |
21 | 27 | from firebase_functions.private.serving import functions_as_yaml, merge_required_apis |
22 | 28 |
|
23 | 29 | # pylint: disable=protected-access |
24 | 30 |
|
| 31 | +ALERT_SECRET = params.SecretParam("GITLAB_PERSONAL_ACCESS_TOKEN") |
| 32 | + |
25 | 33 |
|
26 | 34 | @https_fn.on_call() |
27 | 35 | def asamplefunction(_): |
@@ -196,3 +204,104 @@ def test_invoker_with_no_element_throws(): |
196 | 204 | AssertionError, match="HttpsOptions: Invalid option for invoker - must be a non-empty list." |
197 | 205 | ): |
198 | 206 | options.HttpsOptions(invoker=[])._endpoint(func_name="test") |
| 207 | + |
| 208 | + |
| 209 | +def _assert_alert_endpoint_options(endpoint, expected_alert_type, expect_app_id: str | None = None): |
| 210 | + assert endpoint.region == ["europe-west1"] |
| 211 | + assert endpoint.maxInstances == 1 |
| 212 | + assert endpoint.secretEnvironmentVariables == [{"key": "GITLAB_PERSONAL_ACCESS_TOKEN"}] |
| 213 | + assert endpoint.eventTrigger["retry"] is True |
| 214 | + assert endpoint.eventTrigger["eventFilters"]["alerttype"] == expected_alert_type |
| 215 | + if expect_app_id is None: |
| 216 | + assert "appid" not in endpoint.eventTrigger["eventFilters"] |
| 217 | + else: |
| 218 | + assert endpoint.eventTrigger["eventFilters"]["appid"] == expect_app_id |
| 219 | + |
| 220 | + |
| 221 | +def test_crashlytics_options_preserved_in_alert_endpoint(): |
| 222 | + @crashlytics_fn.on_new_fatal_issue_published( |
| 223 | + secrets=[ALERT_SECRET], |
| 224 | + region="europe-west1", |
| 225 | + max_instances=1, |
| 226 | + retry=True, |
| 227 | + app_id="app-123", |
| 228 | + ) |
| 229 | + def sample(_event): |
| 230 | + return None |
| 231 | + |
| 232 | + _assert_alert_endpoint_options( |
| 233 | + sample.__firebase_endpoint__, |
| 234 | + "crashlytics.newFatalIssue", |
| 235 | + expect_app_id="app-123", |
| 236 | + ) |
| 237 | + |
| 238 | + |
| 239 | +def test_app_distribution_options_preserved_in_alert_endpoint(): |
| 240 | + @app_distribution_fn.on_new_tester_ios_device_published( |
| 241 | + secrets=[ALERT_SECRET], |
| 242 | + region="europe-west1", |
| 243 | + max_instances=1, |
| 244 | + retry=True, |
| 245 | + app_id="app-123", |
| 246 | + ) |
| 247 | + def sample(_event): |
| 248 | + return None |
| 249 | + |
| 250 | + _assert_alert_endpoint_options( |
| 251 | + sample.__firebase_endpoint__, |
| 252 | + "appDistribution.newTesterIosDevice", |
| 253 | + expect_app_id="app-123", |
| 254 | + ) |
| 255 | + |
| 256 | + |
| 257 | +def test_performance_options_preserved_in_alert_endpoint(): |
| 258 | + @performance_fn.on_threshold_alert_published( |
| 259 | + secrets=[ALERT_SECRET], |
| 260 | + region="europe-west1", |
| 261 | + max_instances=1, |
| 262 | + retry=True, |
| 263 | + app_id="app-123", |
| 264 | + ) |
| 265 | + def sample(_event): |
| 266 | + return None |
| 267 | + |
| 268 | + _assert_alert_endpoint_options( |
| 269 | + sample.__firebase_endpoint__, |
| 270 | + "performance.threshold", |
| 271 | + expect_app_id="app-123", |
| 272 | + ) |
| 273 | + |
| 274 | + |
| 275 | +def test_billing_options_preserved_in_alert_endpoint(): |
| 276 | + @billing_fn.on_plan_update_published( |
| 277 | + secrets=[ALERT_SECRET], |
| 278 | + region="europe-west1", |
| 279 | + max_instances=1, |
| 280 | + retry=True, |
| 281 | + ) |
| 282 | + def sample(_event): |
| 283 | + return None |
| 284 | + |
| 285 | + _assert_alert_endpoint_options( |
| 286 | + sample.__firebase_endpoint__, |
| 287 | + "billing.planUpdate", |
| 288 | + ) |
| 289 | + |
| 290 | + |
| 291 | +def test_firebase_alert_options_preserved_in_alert_endpoint(): |
| 292 | + @alerts_fn.on_alert_published( |
| 293 | + alert_type=alerts_fn.AlertType.CRASHLYTICS_NEW_FATAL_ISSUE, |
| 294 | + secrets=[ALERT_SECRET], |
| 295 | + region="europe-west1", |
| 296 | + max_instances=1, |
| 297 | + retry=True, |
| 298 | + app_id="app-123", |
| 299 | + ) |
| 300 | + def sample(_event): |
| 301 | + return None |
| 302 | + |
| 303 | + _assert_alert_endpoint_options( |
| 304 | + sample.__firebase_endpoint__, |
| 305 | + "crashlytics.newFatalIssue", |
| 306 | + expect_app_id="app-123", |
| 307 | + ) |
0 commit comments