|
| 1 | +import json |
| 2 | +from unittest import TestCase |
| 3 | +from unittest.mock import MagicMock, Mock, patch |
| 4 | + |
| 5 | +mocked_modules = { |
| 6 | + "google.cloud.firestore": MagicMock(), |
| 7 | + "google.cloud.firestore_v1": MagicMock(), |
| 8 | + "firebase_admin": MagicMock() |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | +class TestFirestore(TestCase): |
| 13 | + def test_firestore_endpoint_handler_calls_function_with_correct_args(self): |
| 14 | + with patch.dict('sys.modules', mocked_modules): |
| 15 | + from cloudevents.http import CloudEvent |
| 16 | + from firebase_functions import firestore_fn |
| 17 | + from firebase_functions.private import path_pattern |
| 18 | + |
| 19 | + func = Mock(__name__="example_func") |
| 20 | + |
| 21 | + event_type = firestore_fn._event_type_created_with_auth_context |
| 22 | + document_pattern = path_pattern.PathPattern("foo/{bar}") |
| 23 | + raw_event = CloudEvent( |
| 24 | + attributes={ |
| 25 | + "specversion": "1.0", |
| 26 | + "type": event_type, |
| 27 | + "source": "https://example.com/testevent", |
| 28 | + "time": "2023-03-11T13:25:37.403Z", |
| 29 | + "subject": "test_subject", |
| 30 | + "datacontenttype": "application/json", |
| 31 | + "location": "projects/project-id/databases/(default)/documents/foo/{bar}", |
| 32 | + "project": "project-id", |
| 33 | + "namespace": "(default)", |
| 34 | + "document": "foo/{bar}", |
| 35 | + "database": "projects/project-id/databases/(default)", |
| 36 | + "authtype": "unauthenticated", |
| 37 | + "authid": "foo" |
| 38 | + }, |
| 39 | + data=json.dumps({}) |
| 40 | + ) |
| 41 | + |
| 42 | + firestore_fn._firestore_endpoint_handler(func=func, |
| 43 | + event_type=event_type, |
| 44 | + document_pattern=document_pattern, |
| 45 | + raw=raw_event) |
| 46 | + |
| 47 | + func.assert_called_once() |
| 48 | + |
| 49 | + event = func.call_args.args[0] |
| 50 | + self.assertIsNotNone(event) |
| 51 | + self.assertIsInstance(event, firestore_fn.EventWithAuthContext) |
| 52 | + self.assertEqual(event.auth_type, "unauthenticated") |
| 53 | + self.assertEqual(event.auth_id, "foo") |
0 commit comments