|
15 | 15 | """Tests for firebase_admin.db."""
|
16 | 16 | import collections
|
17 | 17 | import json
|
| 18 | +import os |
18 | 19 | import sys
|
19 | 20 | import time
|
20 | 21 |
|
|
28 | 29 | from tests import testutils
|
29 | 30 |
|
30 | 31 |
|
| 32 | +_EMULATOR_HOST_ENV_VAR = 'FIREBASE_DATABASE_EMULATOR_HOST' |
| 33 | + |
| 34 | + |
31 | 35 | class MockAdapter(testutils.MockAdapter):
|
32 | 36 | """A mock HTTP adapter that mimics RTDB server behavior."""
|
33 | 37 |
|
@@ -702,52 +706,70 @@ def test_no_db_url(self):
|
702 | 706 | 'url,emulator_host,expected_base_url,expected_namespace',
|
703 | 707 | [
|
704 | 708 | # Production URLs with no override:
|
705 |
| - ('https://test.firebaseio.com', None, 'https://test.firebaseio.com', 'test'), |
706 |
| - ('https://test.firebaseio.com/', None, 'https://test.firebaseio.com', 'test'), |
| 709 | + ('https://test.firebaseio.com', None, 'https://test.firebaseio.com', None), |
| 710 | + ('https://test.firebaseio.com/', None, 'https://test.firebaseio.com', None), |
707 | 711 |
|
708 | 712 | # Production URLs with emulator_host override:
|
709 | 713 | ('https://test.firebaseio.com', 'localhost:9000', 'http://localhost:9000', 'test'),
|
710 | 714 | ('https://test.firebaseio.com/', 'localhost:9000', 'http://localhost:9000', 'test'),
|
711 | 715 |
|
712 |
| - # Emulator URLs with no override. |
| 716 | + # Emulator URL with no override. |
713 | 717 | ('http://localhost:8000/?ns=test', None, 'http://localhost:8000', 'test'),
|
| 718 | +
|
714 | 719 | # emulator_host is ignored when the original URL is already emulator.
|
715 | 720 | ('http://localhost:8000/?ns=test', 'localhost:9999', 'http://localhost:8000', 'test'),
|
716 | 721 | ]
|
717 | 722 | )
|
718 | 723 | def test_parse_db_url(self, url, emulator_host, expected_base_url, expected_namespace):
|
719 |
| - base_url, namespace = db._DatabaseService._parse_db_url(url, emulator_host) |
720 |
| - assert base_url == expected_base_url |
721 |
| - assert namespace == expected_namespace |
722 |
| - |
723 |
| - @pytest.mark.parametrize('url,emulator_host', [ |
724 |
| - ('', None), |
725 |
| - (None, None), |
726 |
| - (42, None), |
727 |
| - ('test.firebaseio.com', None), # Not a URL. |
728 |
| - ('http://test.firebaseio.com', None), # Use of non-HTTPs in production URLs. |
729 |
| - ('ftp://test.firebaseio.com', None), # Use of non-HTTPs in production URLs. |
730 |
| - ('https://example.com', None), # Invalid RTDB URL. |
731 |
| - ('http://localhost:9000/', None), # No ns specified. |
732 |
| - ('http://localhost:9000/?ns=', None), # No ns specified. |
733 |
| - ('http://localhost:9000/?ns=test1&ns=test2', None), # Two ns parameters specified. |
734 |
| - ('ftp://localhost:9000/?ns=test', None), # Neither HTTP or HTTPS. |
| 724 | + if emulator_host: |
| 725 | + os.environ[_EMULATOR_HOST_ENV_VAR] = emulator_host |
| 726 | + |
| 727 | + try: |
| 728 | + firebase_admin.initialize_app(testutils.MockCredential(), {'databaseURL' : url}) |
| 729 | + ref = db.reference() |
| 730 | + assert ref._client._base_url == expected_base_url |
| 731 | + assert ref._client.params.get('ns') == expected_namespace |
| 732 | + if expected_base_url.startswith('http://localhost'): |
| 733 | + assert isinstance(ref._client.credential, db._EmulatorAdminCredentials) |
| 734 | + else: |
| 735 | + assert isinstance(ref._client.credential, testutils.MockGoogleCredential) |
| 736 | + finally: |
| 737 | + if _EMULATOR_HOST_ENV_VAR in os.environ: |
| 738 | + del os.environ[_EMULATOR_HOST_ENV_VAR] |
| 739 | + |
| 740 | + @pytest.mark.parametrize('url', [ |
| 741 | + '', |
| 742 | + None, |
| 743 | + 42, |
| 744 | + 'test.firebaseio.com', # Not a URL. |
| 745 | + 'http://test.firebaseio.com', # Use of non-HTTPs in production URLs. |
| 746 | + 'ftp://test.firebaseio.com', # Use of non-HTTPs in production URLs. |
| 747 | + 'http://localhost:9000/', # No ns specified. |
| 748 | + 'http://localhost:9000/?ns=', # No ns specified. |
| 749 | + 'http://localhost:9000/?ns=test1&ns=test2', # Two ns parameters specified. |
| 750 | + 'ftp://localhost:9000/?ns=test', # Neither HTTP or HTTPS. |
735 | 751 | ])
|
736 |
| - def test_parse_db_url_errors(self, url, emulator_host): |
| 752 | + def test_parse_db_url_errors(self, url): |
| 753 | + firebase_admin.initialize_app(testutils.MockCredential(), {'databaseURL' : url}) |
737 | 754 | with pytest.raises(ValueError):
|
738 |
| - db._DatabaseService._parse_db_url(url, emulator_host) |
| 755 | + db.reference() |
739 | 756 |
|
740 | 757 | @pytest.mark.parametrize('url', [
|
741 |
| - 'https://test.firebaseio.com', 'https://test.firebaseio.com/' |
| 758 | + 'https://test.firebaseio.com', 'https://test.firebaseio.com/', |
| 759 | + 'https://test.eu-west1.firebasdatabase.app', 'https://test.eu-west1.firebasdatabase.app/' |
742 | 760 | ])
|
743 | 761 | def test_valid_db_url(self, url):
|
744 | 762 | firebase_admin.initialize_app(testutils.MockCredential(), {'databaseURL' : url})
|
745 | 763 | ref = db.reference()
|
746 |
| - assert ref._client.base_url == 'https://test.firebaseio.com' |
| 764 | + expected_url = url |
| 765 | + if url.endswith('/'): |
| 766 | + expected_url = url[:-1] |
| 767 | + assert ref._client.base_url == expected_url |
747 | 768 | assert 'auth_variable_override' not in ref._client.params
|
| 769 | + assert 'ns' not in ref._client.params |
748 | 770 |
|
749 | 771 | @pytest.mark.parametrize('url', [
|
750 |
| - None, '', 'foo', 'http://test.firebaseio.com', 'https://google.com', |
| 772 | + None, '', 'foo', 'http://test.firebaseio.com', 'http://test.firebasedatabase.app', |
751 | 773 | True, False, 1, 0, dict(), list(), tuple(), _Object()
|
752 | 774 | ])
|
753 | 775 | def test_invalid_db_url(self, url):
|
|
0 commit comments