14
14
15
15
"""Tests for firebase_admin.remote_config."""
16
16
import json
17
+ import pytest
17
18
import 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
19
22
20
23
from firebase_admin import _utils
21
24
from tests import testutils
22
25
23
26
class MockAdapter (testutils .MockAdapter ):
24
27
"""A Mock HTTP Adapter that Firebase Remote Config with ETag in header."""
25
28
26
- ETAG = '0 '
29
+ ETAG = 'etag '
27
30
28
31
def __init__ (self , data , status , recorder , etag = ETAG ):
29
32
testutils .MockAdapter .__init__ (self , data , status , recorder )
@@ -35,10 +38,15 @@ def send(self, request, **kwargs):
35
38
return resp
36
39
37
40
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 ()
42
50
43
51
def test_rc_instance_get_server_template (self ):
44
52
recorder = []
@@ -50,15 +58,18 @@ def test_rc_instance_get_server_template(self):
50
58
'parameterGroups' : {},
51
59
'version' : 'test'
52
60
})
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 (
54
65
'https://firebaseremoteconfig.googleapis.com' ,
55
66
MockAdapter (response , 200 , recorder ))
56
67
57
- template = self . _RC_INSTANCE .get_server_template ()
68
+ template = rc_instance .get_server_template ()
58
69
59
70
assert template .parameters == dict (test_key = "test_value" )
60
71
assert str (template .version ) == 'test'
61
- assert str (template .etag ) == '0 '
72
+ assert str (template .etag ) == 'etag '
62
73
63
74
def test_rc_instance_get_server_template_empty_params (self ):
64
75
recorder = []
@@ -68,12 +79,69 @@ def test_rc_instance_get_server_template_empty_params(self):
68
79
'version' : 'test'
69
80
})
70
81
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 (
72
85
'https://firebaseremoteconfig.googleapis.com' ,
73
86
MockAdapter (response , 200 , recorder ))
74
87
75
- template = self . _RC_INSTANCE .get_server_template ()
88
+ template = rc_instance .get_server_template ()
76
89
77
90
assert template .parameters == {}
78
91
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