|
| 1 | +# Copyright 2020 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 | +import json |
| 16 | +import pytest |
| 17 | +import subprocess |
| 18 | +from urllib import request |
| 19 | +import os |
| 20 | + |
| 21 | +# Setting up variables for testing |
| 22 | +GCLOUD_PROJECT = os.environ["GCLOUD_PROJECT"] |
| 23 | + |
| 24 | +@pytest.fixture() |
| 25 | +def services(): |
| 26 | + # Change into parent directory to access renderer and editor directories |
| 27 | + os.chdir("..") |
| 28 | + |
| 29 | + # Build and Deploy Cloud Run Services |
| 30 | + subprocess.run( |
| 31 | + ["gcloud", "builds", "submit", "--config", |
| 32 | + "e2e_test/test_setup.yaml", "--quiet"] |
| 33 | + ) |
| 34 | + |
| 35 | + # Get the URL for the editor and the token |
| 36 | + editor = subprocess.run( |
| 37 | + [ |
| 38 | + "gcloud", |
| 39 | + "run", |
| 40 | + "--platform=managed", |
| 41 | + "--region=us-central1", |
| 42 | + "services", |
| 43 | + "describe", |
| 44 | + "editor", |
| 45 | + "--format=value(status.url)", |
| 46 | + ], |
| 47 | + stdout=subprocess.PIPE, |
| 48 | + ).stdout.strip() |
| 49 | + |
| 50 | + token = subprocess.run( |
| 51 | + ["gcloud", "auth", "print-identity-token"], stdout=subprocess.PIPE |
| 52 | + ).stdout.strip() |
| 53 | + |
| 54 | + yield editor, token |
| 55 | + |
| 56 | + |
| 57 | +def test_end_to_end(services): |
| 58 | + editor = services[0].decode() + "/render" |
| 59 | + token = services[1].decode() |
| 60 | + data = json.dumps({"data": "**strong text**"}) |
| 61 | + |
| 62 | + req = request.Request( |
| 63 | + editor, |
| 64 | + data=data.encode(), |
| 65 | + headers={ |
| 66 | + "Authorization": f"Bearer {token}", |
| 67 | + "Content-Type": "application/json", |
| 68 | + }, |
| 69 | + ) |
| 70 | + |
| 71 | + response = request.urlopen(req) |
| 72 | + assert response.status == 200 |
| 73 | + |
| 74 | + body = response.read() |
| 75 | + assert "<p><strong>strong text</strong></p>" in body.decode() |
| 76 | +# tear_down() |
| 77 | + |
| 78 | + |
| 79 | +# def tear_down(): |
| 80 | +# subprocess.run(["gcloud", "run", "services", "delete", "editor", "--quiet"]) |
| 81 | +# subprocess.run( |
| 82 | +# ["gcloud", "run", "services", "delete", "renderer", "--quiet"] |
| 83 | +# ) |
0 commit comments