Skip to content

Commit 20749d7

Browse files
author
Pijush Chakraborty
committed
Adding unit tests for init_server_template and get_server_template
1 parent f5db7db commit 20749d7

File tree

2 files changed

+88
-21
lines changed

2 files changed

+88
-21
lines changed

firebase_admin/remote_config.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ def __init__(self, app: App = None, default_config: Optional[Dict[str, str]] = N
112112

113113
async def load(self):
114114
"""Fetches the server template and caches the data."""
115-
self._cache = await self._rc_service.getServerTemplate()
115+
self._cache = self._rc_service.get_server_template()
116116

117-
def evaluate(self, context):
117+
def evaluate(self):
118118
# Logic to process the cached template into a ServerConfig here.
119-
# TODO: Add Condition evaluator.
120-
self._evaluator = _ConditionEvaluator(self._cache.conditions, context)
119+
# TODO: Add and validate Condition evaluator.
120+
self._evaluator = _ConditionEvaluator(self._cache.parameters)
121121
return ServerConfig(config_values=self._evaluator.evaluate())
122122

123123
def set(self, template: ServerTemplateData):
@@ -194,13 +194,12 @@ def _handle_remote_config_error(cls, error: Any):
194194
class _ConditionEvaluator:
195195
"""Internal class that facilitates sending requests to the Firebase Remote
196196
Config backend API."""
197-
def __init__(self, context, conditions):
198-
self._context = context
199-
self._conditions = conditions
197+
def __init__(self, parameters):
198+
self._parameters = parameters
200199

201200
def evaluate(self):
202-
# TODO: Write evaluator
203-
return {}
201+
# TODO: Write logic for evaluator
202+
return self._parameters
204203

205204

206205
async def get_server_template(app: App = None, default_config: Optional[Dict[str, str]] = None):

tests/test_remote_config.py

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@
1414

1515
"""Tests for firebase_admin.remote_config."""
1616
import json
17+
import pytest
1718
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
1922

2023
from firebase_admin import _utils
2124
from tests import testutils
2225

2326
class 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

Comments
 (0)