Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit eedc6d2

Browse filesBrowse files
author
Ace Nassri
authored
Add firebase trigger samples (GoogleCloudPlatform#1651)
* Add firebase trigger samples Change-Id: If677ab6aa788b3c134c11e155ee597537d181cd2 * Downgrade to Python 2.7 to make CI pass Change-Id: I3a20ff5e0fd7d2bb5986552f36570896f1833ee5 * Add tests Change-Id: Id9ea86c970ca3bbe51ead3aa02aeb35ce33486cf
1 parent 26ce641 commit eedc6d2
Copy full SHA for eedc6d2

File tree

Expand file treeCollapse file tree

2 files changed

+156
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+156
-0
lines changed

‎functions/firebase/main.py

Copy file name to clipboard
+71Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START functions_firebase_rtdb]
16+
# [START functions_firebase_firestore]
17+
# [START functions_firebase_auth]
18+
import json
19+
# [END functions_firebase_rtdb]
20+
# [END functions_firebase_firestore]
21+
# [END functions_firebase_auth]
22+
23+
24+
# [START functions_firebase_rtdb]
25+
def hello_rtdb(data, context):
26+
""" Triggered by a change to a Firebase RTDB reference.
27+
Args:
28+
data (dict): The event payload.
29+
context (google.cloud.functions.Context): Metadata for the event.
30+
"""
31+
trigger_resource = context.resource
32+
33+
print('Function triggered by change to: %s' % trigger_resource)
34+
print('Admin?: %s' % data.get("admin", False))
35+
print('Delta:')
36+
print(json.dumps(data["delta"]))
37+
# [END functions_firebase_rtdb]
38+
39+
40+
# [START functions_firebase_firestore]
41+
def hello_firestore(data, context):
42+
""" Triggered by a change to a Firestore document.
43+
Args:
44+
data (dict): The event payload.
45+
context (google.cloud.functions.Context): Metadata for the event.
46+
"""
47+
trigger_resource = context.resource
48+
49+
print('Function triggered by change to: %s' % trigger_resource)
50+
51+
print('\nOld value:')
52+
print(json.dumps(data["oldValue"]))
53+
54+
print('\nNew value:')
55+
print(json.dumps(data["value"]))
56+
# [END functions_firebase_firestore]
57+
58+
59+
# [START functions_firebase_auth]
60+
def hello_auth(data, context):
61+
""" Triggered by creation or deletion of a Firebase Auth user object.
62+
Args:
63+
data (dict): The event payload.
64+
context (google.cloud.functions.Context): Metadata for the event.
65+
"""
66+
print('Function triggered by creation/deletion of user: %s' % data["uid"])
67+
print('Created at: %s' % data["metadata"]["createdAt"])
68+
69+
if 'email' in data:
70+
print('Email: %s' % data["email"])
71+
# [END functions_firebase_auth]

‎functions/firebase/main_test.py

Copy file name to clipboard
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from datetime import datetime
16+
import json
17+
18+
import uuid
19+
20+
import main
21+
22+
23+
class Context(object):
24+
pass
25+
26+
27+
def test_rtdb(capsys):
28+
data_id = str(uuid.uuid4())
29+
resource_id = str(uuid.uuid4())
30+
31+
data = {
32+
'admin': True,
33+
'delta': {'id': data_id}
34+
}
35+
36+
context = Context()
37+
context.resource = resource_id
38+
39+
main.hello_rtdb(data, context)
40+
41+
out, _ = capsys.readouterr()
42+
43+
assert ('Function triggered by change to: %s' % resource_id) in out
44+
assert 'Admin?: True' in out
45+
assert data_id in out
46+
47+
48+
def test_firestore(capsys):
49+
resource_id = str(uuid.uuid4())
50+
51+
context = Context()
52+
context.resource = resource_id
53+
54+
data = {
55+
'oldValue': {'uuid': str(uuid.uuid4())},
56+
'value': {'uuid': str(uuid.uuid4())}
57+
}
58+
59+
main.hello_firestore(data, context)
60+
61+
out, _ = capsys.readouterr()
62+
63+
assert ('Function triggered by change to: %s' % resource_id) in out
64+
assert json.dumps(data['oldValue']) in out
65+
assert json.dumps(data['value']) in out
66+
67+
68+
def test_auth(capsys):
69+
user_id = str(uuid.uuid4())
70+
date_string = datetime.now().isoformat()
71+
email_string = '%s@%s.com' % (uuid.uuid4(), uuid.uuid4())
72+
73+
data = {
74+
'uid': user_id,
75+
'metadata': {'createdAt': date_string},
76+
'email': email_string
77+
}
78+
79+
main.hello_auth(data, None)
80+
81+
out, _ = capsys.readouterr()
82+
83+
assert user_id in out
84+
assert date_string in out
85+
assert email_string in out

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.