Skip to content

Commit 524cc6d

Browse files
committed
End to end testing
1 parent 4b8fc5f commit 524cc6d

File tree

12 files changed

+320
-135
lines changed

12 files changed

+320
-135
lines changed

run/markdown-preview/e2e_test.py

Lines changed: 0 additions & 104 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
# )
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pytest==5.3.2; python_version > "3.0"
2+
pytest==5.3.2; python_version < "3.0"
3+
google-auth==1.14.0
4+
google-api-python-client==1.8.0
File renamed without changes.

run/markdown-preview/editor/editor_test.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
15+
import main
1616
import os
1717
import pytest
18-
import main
1918

2019

2120
@pytest.fixture
@@ -26,19 +25,18 @@ def client():
2625

2726
def test_editor_handler(client):
2827
os.environ["EDITOR_UPSTREAM_RENDER_URL"] = "http://testing.local"
29-
r = client.get('/')
28+
r = client.get("/")
3029
body = r.data.decode()
3130

3231
assert r.status_code == 200
3332
assert "<title>Markdown Editor</title>" in body
3433
assert "This UI allows a user to write Markdown text" in body
3534

36-
3735
def test_render_handler_errors(client):
3836
r = client.get("/render")
3937
assert r.status_code == 405
4038

4139
with pytest.raises(Exception) as e:
4240
client.post("/render", data="**markdown**")
4341

44-
assert "Invalid JSON" in str(e.value)
42+
assert "Invalid JSON" in str(e.value)

run/markdown-preview/editor/main.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414

1515
from flask import Flask, render_template, request
1616
import os
17+
import sys
1718

1819
import render
1920

2021
app = Flask(__name__)
2122

2223

23-
@app.route('/', methods=['GET'])
24+
@app.route("/", methods=["GET"])
2425
def index():
2526
# Render the default template
2627
f = open('templates/markdown.md')
27-
default = f.read()
28-
return render_template('index.html', default=default)
28+
return render_template("index.html", default=f.read())
2929

3030

31-
@app.route('/render', methods=['POST'])
31+
@app.route("/render", methods=["POST"])
3232
def render_handler():
3333
body = request.get_json()
3434
if not body:
@@ -39,9 +39,9 @@ def render_handler():
3939
return parsed_markdown
4040

4141

42-
if __name__ == '__main__':
43-
PORT = int(os.getenv('PORT')) if os.getenv('PORT') else 8080
42+
if __name__ == "__main__":
43+
PORT = int(os.getenv("PORT")) if os.getenv("PORT") else 8080
4444

4545
# This is used when running locally. Gunicorn is used to run the
4646
# application on Cloud Run. See entrypoint in Dockerfile.
47-
app.run(host='127.0.0.1', port=PORT, debug=True)
47+
app.run(host="127.0.0.1", port=PORT, debug=True)

run/markdown-preview/editor/render.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import os
16+
import sys
1617
import urllib
1718

1819

@@ -23,15 +24,20 @@ def new_request(data):
2324
and Cloud Functions.
2425
"""
2526

26-
url = os.environ.get('EDITOR_UPSTREAM_RENDER_URL')
27-
unauthenticated = os.environ.get('EDITOR_UPSTREAM_UNAUTHENTICATED')
27+
url = os.environ.get("EDITOR_UPSTREAM_RENDER_URL")
28+
unauthenticated = os.environ.get("EDITOR_UPSTREAM_UNAUTHENTICATED", False)
29+
print("Unauthenticated: ", unauthenticated)
30+
print("Authenticated: ", not unauthenticated)
2831

2932
req = urllib.request.Request(url, data=data.encode())
3033

3134
if not unauthenticated:
35+
print("if statement")
3236
token = get_token(url)
3337
req.add_header("Authorization", f"Bearer {token}")
3438

39+
sys.stdout.flush()
40+
3541
response = urllib.request.urlopen(req)
3642
return response.read()
3743

@@ -40,10 +46,13 @@ def get_token(url):
4046
"""
4147
Retrieves the IAM ID Token credential for the url.
4248
"""
43-
token_url = (f"http://metadata.google.internal/computeMetadata/v1/instance/"
44-
f"service-accounts/default/identity?audience={url}")
45-
token_req = urllib.request.Request(token_url,
46-
headers={'Metadata-Flavor': 'Google'})
49+
token_url = (
50+
f"http://metadata.google.internal/computeMetadata/v1/instance/"
51+
f"service-accounts/default/identity?audience={url}"
52+
)
53+
token_req = urllib.request.Request(
54+
token_url, headers={"Metadata-Flavor": "Google"}
55+
)
4756
token_response = urllib.request.urlopen(token_req)
4857
token = token_response.read()
4958
return token.decode()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==5.3.2

run/markdown-preview/renderer/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
app = Flask(__name__)
2121

2222

23-
@app.route('/', methods=['POST'])
23+
@app.route("/", methods=["POST"])
2424
def index():
2525
data = request.get_data(as_text=True)
2626
return markdown.markdown(data)
2727

2828

29-
if __name__ == '__main__':
30-
PORT = int(os.getenv('PORT')) if os.getenv('PORT') else 8080
29+
if __name__ == "__main__":
30+
PORT = int(os.getenv("PORT")) if os.getenv("PORT") else 8080
3131

3232
# This is used when running locally. Gunicorn is used to run the
3333
# application on Cloud Run. See entrypoint in Dockerfile.
34-
app.run(host='127.0.0.1', port=PORT, debug=True)
34+
app.run(host="127.0.0.1", port=PORT, debug=True)

run/markdown-preview/renderer/renderer_test.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
15+
import main
1616
import os
1717
import pytest
18-
import main
1918

2019

2120
@pytest.fixture
@@ -26,17 +25,18 @@ def client():
2625

2726
def test_markdown_handler(client):
2827
data_input = "**strong text**"
29-
r = client.post('/', data=data_input)
30-
28+
r = client.post("/", data=data_input)
29+
3130
assert r.status_code == 200
3231
assert "<p><strong>strong text</strong></p>" == r.data.decode()
3332

3433
data_input = (
35-
'<a onblur="alert(secret)" href="http://www.google.com">Google</a>')
34+
'<a onblur="alert(secret)" href="http://www.google.com">Google</a>'
35+
)
3636
expect = (
3737
'<p><a onblur="alert(secret)"'
38-
' href="http://www.google.com">Google</a></p>')
39-
r = client.post('/', data=data_input)
38+
' href="http://www.google.com">Google</a></p>'
39+
)
40+
r = client.post("/", data=data_input)
4041
assert r.status_code == 200
41-
assert expect in r.data.decode()
42-
42+
assert expect in r.data.decode()

0 commit comments

Comments
 (0)