8000 Added integration tests for auth module (#34) · carsongee/firebase-admin-python@1190591 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1190591

Browse files
authored
Added integration tests for auth module (firebase#34)
* Added integration tests for auth * Exiting on errors
1 parent 6dd88b4 commit 1190591

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
.tox/
66
*.egg-info/
77
*~
8+
scripts/cert.json
9+
scripts/apikey.txt

CONTRIBUTING.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@ You can also get a code coverage report by launching pytest as follows:
158158
pytest --cov=firebase_admin --cov=tests
159159
```
160160

161+
### Integration Testing
162+
163+
A suite of integration tests are available under the `integration/` directory.
164+
These tests are designed to run against an actual Firebase project. Create a new
165+
project in the [Firebase Console](https://console.firebase.google.com), if you
166+
do not already have one suitable for running the tests aginst. Then obtain the
167+
following credentials from the project:
168+
169+
1. *Service account certificate*: This can be downloaded as a JSON file from
170+
the "Settings > Service Accounts" tab of the Firebase console.
171+
2. *Web API key*: This is displayed in the "Settings > General" tab of the
172+
console. Copy it and save to a new text file.
173+
174+
Now you can invoke the integration test suite as follows:
175+
176+
```
177+
pytest integration/ --cert path/to/service_acct.json --apikey path/to/apikey.txt
178+
```
179+
161180
### Testing in Different Environments
162181

163182
Sometimes we want to run unit tests in multiple environments (e.g. different Python versions), and
@@ -213,6 +232,7 @@ pyenv local 2.7.6 3.3.0 pypy2-5.6.0
213232
Here are some highlights of the directory structure and notable source files
214233

215234
* `firebase_admin/` - Source directory for the `firebase_admin` module.
235+
* `integration/` - Integration tests.
216236
* `tests/` - Unit tests.
217237
* `data/` - Provides mocks for several variables as well as mock service account keys.
218238
* `.github/` - Contribution instructions as well as issue and pull request templates.

integration/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
def pytest_addoption(parser):
2525
parser.addoption(
2626
'--cert', action='store', help='Service account certificate file for integration tests.')
27+
parser.addoption(
28+
'--apikey', action='store', help='API key file for integration tests.')
2729

2830
def _get_cert_path(request):
2931
cert = request.config.getoption('--cert')
@@ -48,3 +50,13 @@ def default_app(request):
4850
cred = credentials.Certificate(cert_path)
4951
ops = {'dbURL' : 'https://{0}.firebaseio.com'.format(project_id)}
5052
return firebase_admin.initialize_app(cred, ops)
53+
54+
@pytest.fixture(scope='session')
55+
def api_key(request):
56+
path = request.config.getoption('--apikey')
57+
if not path:
58+
raise ValueError('API key file not specified. Make sure to specify the "--apikey" '
59+
'command-line option.')
60+
with open(path) as keyfile:
61+
return keyfile.read().strip()
62+

integration/test_auth.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2017 Google Inc.
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+
"""Integration tests for firebase_admin.auth module."""
16+
import requests
17+
18+
from firebase_admin import auth
19+
20+
21+
_id_toolkit_url = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken'
22+
23+
24+
def _sign_in(custom_token, api_key):
25+
body = {'token' : custom_token, 'returnSecureToken' : True}
26+
params = {'key' : api_key}
27+
resp = requests.request('post', _id_toolkit_url, params=params, json=body)
28+
resp.raise_for_status()
29+
return resp.json().get('idToken')
30+
31+
def test_custom_token(api_key):
32+
custom_token = auth.create_custom_token('user1')
33+
id_token = _sign_in(custom_token, api_key)
34+
claims = auth.verify_id_token(id_token)
35+
assert claims['uid'] == 'user1'
36+
37+
def test_custom_token_with_claims(api_key):
38+
dev_claims = {'premium' : True, 'subscription' : 'silver'}
39+
custom_token = auth.create_custom_token('user2', dev_claims)
40+
id_token = _sign_in(custom_token, api_key)
41+
claims = auth.verify_id_token(id_token)
42+
assert claims['uid'] == 'user2'
43+
assert claims['premium'] is True
44+
assert claims['subscription'] == 'silver'

scripts/prepare_release.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ if [[ `git status --porcelain` ]]; then
7575
exit 1
7676
fi
7777

78+
if [[ ! -e "cert.json" ]]; then
79+
echo "[ERROR] cert.json file is required to run integration tests."
80+
exit 1
81+
fi
82+
83+
if [[ ! -e "apikey.txt" ]]; then
84+
echo "[ERROR] apikey.txt file is required to run integration tests."
85+
exit 1
86+
fi
87+
7888
HOST=$(uname)
7989
echo "[INFO] Updating version number in firebase_admin/__init__.py"
8090
if [ $HOST == "Darwin" ]; then
@@ -86,4 +96,7 @@ fi
8696
echo "[INFO] Running unit tests"
8797
tox --workdir ..
8898

99+
echo "[INFO] Running integration tests"
100+
pytest ../integration --cert cert.json --apikey apikey.txt
101+
89102
echo "[INFO] This repo has been prepared for a release. Create a branch and commit the changes."

0 commit comments

Comments
 (0)
0