1414
1515"""Tests for firebase_admin.remote_config."""
1616import json
17+ import pytest
1718import firebase_admin
18- from firebase_admin .remote_config import _REMOTE_CONFIG_ATTRIBUTE , _RemoteConfigService
19+ from firebase_admin import remote_config
20+ from firebase_admin .remote_config import _REMOTE_CONFIG_ATTRIBUTE
21+ from firebase_admin .remote_config import _RemoteConfigService , ServerTemplateData
1922
2023from firebase_admin import _utils
2124from tests import testutils
2225
2326class MockAdapter (testutils .MockAdapter ):
2427 """A Mock HTTP Adapter that Firebase Remote Config with ETag in header."""
2528
26- ETAG = '0 '
29+ ETAG = 'etag '
2730
2831 def __init__ (self , data , status , recorder , etag = ETAG ):
2932 testutils .MockAdapter .__init__ (self , data , status , recorder )
@@ -35,10 +38,15 @@ def send(self, request, **kwargs):
3538 return resp
3639
3740
38- class TestGetServerTemplate :
39- _DEFAULT_APP = firebase_admin .initialize_app (testutils .MockCredential (), name = 'no_project_id' )
40- _RC_INSTANCE = _utils .get_app_service (_DEFAULT_APP ,
41- _REMOTE_CONFIG_ATTRIBUTE , _RemoteConfigService )
41+ class TestRemoteConfigServiceClient :
42+ @classmethod
43+ def setup_class (cls ):
44+ cred = testutils .MockCredential ()
45+ firebase_admin .initialize_app (cred , {'projectId' : 'project-id' })
46+
47+ @classmethod
48+ def teardown_class (cls ):
49+ testutils .cleanup_apps ()
4250
4351 def test_rc_instance_get_server_template (self ):
4452 recorder = []
@@ -50,15 +58,18 @@ def test_rc_instance_get_server_template(self):
5058 'parameterGroups' : {},
5159 'version' : 'test'
5260 })
53- self ._RC_INSTANCE ._client .session .mount (
61+
62+ rc_instance = _utils .get_app_service (firebase_admin .get_app (),
63+ _REMOTE_CONFIG_ATTRIBUTE , _RemoteConfigService )
64+ rc_instance ._client .session .mount (
5465 'https://firebaseremoteconfig.googleapis.com' ,
5566 MockAdapter (response , 200 , recorder ))
5667
57- template = self . _RC_INSTANCE .get_server_template ()
68+ template = rc_instance .get_server_template ()
5869
5970 assert template .parameters == dict (test_key = "test_value" )
6071 assert str (template .version ) == 'test'
61- assert str (template .etag ) == '0 '
72+ assert str (template .etag ) == 'etag '
6273
6374 def test_rc_instance_get_server_template_empty_params (self ):
6475 recorder = []
@@ -68,12 +79,69 @@ def test_rc_instance_get_server_template_empty_params(self):
6879 'version' : 'test'
6980 })
7081
71- self ._RC_INSTANCE ._client .session .mount (
82+ rc_instance = _utils .get_app_service (firebase_admin .get_app (),
83+ _REMOTE_CONFIG_ATTRIBUTE , _RemoteConfigService )
84+ rc_instance ._client .session .mount (
7285 'https://firebaseremoteconfig.googleapis.com' ,
7386 MockAdapter (response , 200 , recorder ))
7487
75- template = self . _RC_INSTANCE .get_server_template ()
88+ template = rc_instance .get_server_template ()
7689
7790 assert template .parameters == {}
7891 assert str (template .version ) == 'test'
79- assert str (template .etag ) == '0'
92+ assert str (template .etag ) == 'etag'
93+
94+
95+ class TestRemoteConfigService :
96+ @classmethod
97+ def setup_class (cls ):
98+ cred = testutils .MockCredential ()
99+ firebase_admin .initialize_app (cred , {'projectId' : 'project-id' })
100+
101+ @classmethod
102+ def teardown_class (cls ):
103+ testutils .cleanup_apps ()
104+
105+ def test_init_server_template (self ):
106+ app = firebase_admin .get_app ()
107+ template_data = {
108+ 'conditions' : [],
109+ 'parameters' : {
110+ 'test_key' : 'test_value'
111+ },
112+ 'parameterGroups' : '' ,
113+ 'version' : '' ,
114+ }
115+
116+ template = remote_config .init_server_template (
117+ app = app ,
118+ template_data = ServerTemplateData ('etag' , template_data ) # Use ServerTemplateData here
119+ )
120+
121+ config = template .evaluate ()
122+ assert config .get_string ('test_key' ) == 'test_value'
123+
124+ @pytest .mark .asyncio
125+ async def test_get_server_template (self ):
126+ app = firebase_admin .get_app ()
127+ rc_instance = _utils .get_app_service (app ,
128+ _REMOTE_CONFIG_ATTRIBUTE , _RemoteConfigService )
129+
130+ recorder = []
131+ response = json .dumps ({
132+ 'parameters' : {
133+ 'test_key' : 'test_value'
134+ },
135+ 'conditions' : [],
136+ 'parameterGroups' : {},
137+ 'version' : 'test'
138+ })
139+
140+ rc_instance ._client .session .mount (
141+ 'https://firebaseremoteconfig.googleapis.com' ,
142+ MockAdapter (response , 200 , recorder ))
143+
144+ template = await remote_config .get_server_template (app = app )
145+
146+ config = template .evaluate ()
147+ assert config .get_string ('test_key' ) == 'test_value'
0 commit comments