-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtest_firestore_fn.py
123 lines (106 loc) · 4.1 KB
/
test_firestore_fn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
This module contains tests for the firestore_fn module.
"""
import json
from unittest import TestCase
from unittest.mock import MagicMock, Mock, patch
mocked_modules = {
"google.cloud.firestore": MagicMock(),
"google.cloud.firestore_v1": MagicMock(),
"firebase_admin": MagicMock()
}
class TestFirestore(TestCase):
"""
firestore_fn tests.
"""
def test_firestore_endpoint_handler_calls_function_with_correct_args(self):
with patch.dict("sys.modules", mocked_modules):
from cloudevents.http import CloudEvent
from firebase_functions.firestore_fn import _event_type_created_with_auth_context as event_type, \
_firestore_endpoint_handler as firestore_endpoint_handler, AuthEvent
from firebase_functions.private import path_pattern
func = Mock(__name__="example_func")
document_pattern = path_pattern.PathPattern("foo/{bar}")
attributes = {
"specversion":
"1.0",
"type":
event_type,
"source":
"https://example.com/testevent",
"time":
"2023-03-11T13:25:37.403Z",
"subject":
"test_subject",
"datacontenttype":
"application/json",
"location":
"projects/project-id/databases/(default)/documents/foo/{bar}",
"project":
"project-id",
"namespace":
"(default)",
"document":
"foo/{bar}",
"database":
"projects/project-id/databases/(default)",
"authtype":
"unauthenticated",
"authid":
"foo"
}
raw_event = CloudEvent(attributes=attributes, data=json.dumps({}))
firestore_endpoint_handler(func=func,
event_type=event_type,
document_pattern=document_pattern,
raw=raw_event)
func.assert_called_once()
event = func.call_args.args[0]
self.assertIsNotNone(event)
self.assertIsInstance(event, AuthEvent)
self.assertEqual(event.auth_type, "unauthenticated")
self.assertEqual(event.auth_id, "foo")
def test_calls_init_function(self):
with patch.dict("sys.modules", mocked_modules):
from firebase_functions import firestore_fn, core
from cloudevents.http import CloudEvent
func = Mock(__name__="example_func")
hello = None
@core.init
def init():
nonlocal hello
hello = "world"
attributes = {
"specversion":
"1.0",
# pylint: disable=protected-access
"type":
firestore_fn._event_type_created,
"source":
"https://example.com/testevent",
"time":
"2023-03-11T13:25:37.403Z",
"subject":
"test_subject",
"datacontenttype":
"application/json",
"location":
"projects/project-id/databases/(default)/documents/foo/{bar}",
"project":
"project-id",
"namespace":
"(default)",
"document":
"foo/{bar}",
"database":
"projects/project-id/databases/(default)",
"authtype":
"unauthenticated",
"authid":
"foo"
}
raw_event = CloudEvent(attributes=attributes, data=json.dumps({}))
decorated_func = firestore_fn.on_document_created(
document="/foo/{bar}")(func)
decorated_func(raw_event)
self.assertEqual(hello, "world")