diff --git a/functions/firebase/main.py b/functions/firebase/main.py index 0e24d93d748f..8febbfc366c6 100644 --- a/functions/firebase/main.py +++ b/functions/firebase/main.py @@ -24,6 +24,10 @@ # [END functions_firebase_firestore] # [END functions_firebase_auth] +# [START functions_firebase_reactive] +from google.cloud import firestore +# [END functions_firebase_reactive] + # [START functions_firebase_rtdb] def hello_rtdb(data, context): @@ -75,6 +79,28 @@ def hello_auth(data, context): # [END functions_firebase_auth] +# [START functions_firebase_reactive] +client = firestore.Client() + + +# Converts strings added to /messages/{pushId}/original to uppercase +def make_upper_case(data, context): + path_parts = context.resource.split('/documents/')[1].split('/') + collection_path = path_parts[0] + document_path = '/'.join(path_parts[1:]) + + affected_doc = client.collection(collection_path).document(document_path) + + cur_value = data["value"]["fields"]["original"]["stringValue"] + new_value = cur_value.upper() + print(f'Replacing value: {cur_value} --> {new_value}') + + affected_doc.set({ + u'original': new_value + }) +# [END functions_firebase_reactive] + + # [START functions_firebase_analytics] def hello_analytics(data, context): print(data) diff --git a/functions/firebase/main_test.py b/functions/firebase/main_test.py index ff10bf672e60..d4404834a1e9 100644 --- a/functions/firebase/main_test.py +++ b/functions/firebase/main_test.py @@ -12,8 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. +from collections import UserDict from datetime import datetime import json +import uuid + +from mock import MagicMock, patch import main @@ -76,6 +80,43 @@ def test_auth(capsys): assert 'Email: me@example.com' in out +@patch('main.client') +def test_make_upper_case(firestore_mock, capsys): + + firestore_mock.collection = MagicMock(return_value=firestore_mock) + firestore_mock.document = MagicMock(return_value=firestore_mock) + firestore_mock.set = MagicMock(return_value=firestore_mock) + + user_id = str(uuid.uuid4()) + date_string = datetime.now().isoformat() + email_string = '%s@%s.com' % (uuid.uuid4(), uuid.uuid4()) + + data = { + 'uid': user_id, + 'metadata': {'createdAt': date_string}, + 'email': email_string, + 'value': { + 'fields': { + 'original': { + 'stringValue': 'foobar' + } + } + } + } + + context = UserDict() + context.resource = '/documents/some_collection/path/some/path' + + main.make_upper_case(data, context) + + out, _ = capsys.readouterr() + + assert 'Replacing value: foobar --> FOOBAR' in out + firestore_mock.collection.assert_called_with('some_collection') + firestore_mock.document.assert_called_with('path/some/path') + firestore_mock.set.assert_called_with({'original': 'FOOBAR'}) + + def test_analytics(capsys): timestamp = int(datetime.utcnow().timestamp()) diff --git a/functions/firebase/requirements.txt b/functions/firebase/requirements.txt new file mode 100644 index 000000000000..dac97c81fed7 --- /dev/null +++ b/functions/firebase/requirements.txt @@ -0,0 +1 @@ +google-cloud-firestore==0.29.0