|
| 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' |
0 commit comments