diff --git a/background/app/app.yaml b/background/app/app.yaml index fb63451a..02c0651c 100644 --- a/background/app/app.yaml +++ b/background/app/app.yaml @@ -13,5 +13,5 @@ # limitations under the License. # [START getting_started_background_config] -runtime: python37 +runtime: python312 # [END getting_started_background_config] diff --git a/background/app/main.py b/background/app/main.py index 5474d54c..fad93d46 100644 --- a/background/app/main.py +++ b/background/app/main.py @@ -21,8 +21,8 @@ import os from flask import Flask, redirect, render_template, request -from google.cloud import firestore -from google.cloud import pubsub +from google.cloud import firestore, pubsub +from markupsafe import escape app = Flask(__name__) @@ -61,15 +61,13 @@ def translate(): language (form field 'lang'), by sending a PubSub message to a topic. """ source_string = request.form.get("v", "") - to_language = request.form.get("lang", "") + to_language = escape(request.form.get("lang", "")) if source_string == "": - error_message = "Empty value" - return error_message, 400 + return "Invalid request, you must provide a value.", 400 if to_language not in ACCEPTABLE_LANGUAGES: - error_message = "Unsupported language: {}".format(to_language) - return error_message, 400 + return f"Unsupported language: {to_language}", 400 message = { "Original": source_string, @@ -78,11 +76,11 @@ def translate(): "OriginalLanguage": "", } - topic_name = "projects/{}/topics/{}".format( - os.getenv("GOOGLE_CLOUD_PROJECT"), "translate" + topic_name = ( + f"projects/{os.getenv('GOOGLE_CLOUD_PROJECT')}/topics/translate" ) publisher.publish( - topic=topic_name, data=json.dumps(message).encode("utf8") + topic=topic_name, data=json.dumps(message).encode("utf-8") ) return redirect("/") diff --git a/background/app/main_test.py b/background/app/main_test.py index f3bf28c2..41b529d3 100644 --- a/background/app/main_test.py +++ b/background/app/main_test.py @@ -16,8 +16,7 @@ import uuid import google.auth -from google.cloud import firestore -from google.cloud import pubsub +from google.cloud import firestore, pubsub, storage import main import pytest @@ -39,7 +38,10 @@ def clear_collection(collection): for doc in collection.stream(): doc.reference.delete() + bucket_name = 'system-test-bucket' client = firestore.Client() + storage_client = storage.Client() + bucket = storage_client.bucket(bucket_name) translations = client.collection("translations") clear_collection(translations) translations.add( @@ -51,6 +53,7 @@ def clear_collection(collection): }, document_id="test translation", ) + assert bucket in locals() yield client diff --git a/background/app/requirements.txt b/background/app/requirements.txt index 7cfba806..f70d16b7 100644 --- a/background/app/requirements.txt +++ b/background/app/requirements.txt @@ -1,3 +1,3 @@ -google-cloud-firestore==2.11.1 -google-cloud-pubsub==2.16.1 -flask==2.2.5 +google-cloud-firestore==2.18.0 +google-cloud-pubsub==2.23.0 +flask==3.0.3