Skip to content

Commit 5eba607

Browse files
authored
test: add tests for authenticating-users and gce samples (GoogleCloudPlatform#492)
* Feature: add test * Fix: more tests
1 parent 12201a4 commit 5eba607

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

authenticating-users/main_test.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2022 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 main
16+
17+
18+
def fake_validate(assertion):
19+
if assertion == "Valid":
20+
return "nobody@example.com", "user0001"
21+
else:
22+
return None, None
23+
24+
25+
main.validate_assertion = fake_validate
26+
27+
28+
def test_home_page():
29+
client = main.app.test_client()
30+
31+
# Good request check
32+
r = client.get("/", headers={"X-Goog-IAP-JWT-Assertion": "Valid"})
33+
assert "nobody@example.com" in r.text
34+
35+
# Missing header check
36+
r = client.get("/")
37+
assert "None" in r.text
38+
39+
# Bad header check
40+
r = client.get("/", headers={"X-Goog-IAP-JWT-Assertion": "Not Valid"})
41+
assert "None" in r.text
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==7.1.2

gce/main_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2022 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 main
16+
17+
18+
def test_hello():
19+
main.app.testing = True
20+
client = main.app.test_client()
21+
22+
r = client.get("/")
23+
assert r.status_code == 200
24+
response_text = r.data.decode("utf-8")
25+
assert "Hello, world!" in response_text

0 commit comments

Comments
 (0)