Skip to content

Commit

Permalink
Adding unit tests for init_server_template and get_server_template
Browse files Browse the repository at this point in the history
  • Loading branch information
Pijush Chakraborty committed Nov 7, 2024
1 parent f5db7db commit 20749d7
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 21 deletions.
17 changes: 8 additions & 9 deletions firebase_admin/remote_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ def __init__(self, app: App = None, default_config: Optional[Dict[str, str]] = N

async def load(self):
"""Fetches the server template and caches the data."""
self._cache = await self._rc_service.getServerTemplate()
self._cache = self._rc_service.get_server_template()

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

def set(self, template: ServerTemplateData):
Expand Down Expand Up @@ -194,13 +194,12 @@ def _handle_remote_config_error(cls, error: Any):
class _ConditionEvaluator:
"""Internal class that facilitates sending requests to the Firebase Remote
Config backend API."""
def __init__(self, context, conditions):
self._context = context
self._conditions = conditions
def __init__(self, parameters):
self._parameters = parameters

def evaluate(self):
# TODO: Write evaluator
return {}
# TODO: Write logic for evaluator
return self._parameters


async def get_server_template(app: App = None, default_config: Optional[Dict[str, str]] = None):
Expand Down
92 changes: 80 additions & 12 deletions tests/test_remote_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@

"""Tests for firebase_admin.remote_config."""
import json
import pytest
import firebase_admin
from firebase_admin.remote_config import _REMOTE_CONFIG_ATTRIBUTE, _RemoteConfigService
from firebase_admin import remote_config
from firebase_admin.remote_config import _REMOTE_CONFIG_ATTRIBUTE
from firebase_admin.remote_config import _RemoteConfigService, ServerTemplateData

from firebase_admin import _utils
from tests import testutils

class MockAdapter(testutils.MockAdapter):
"""A Mock HTTP Adapter that Firebase Remote Config with ETag in header."""

ETAG = '0'
ETAG = 'etag'

def __init__(self, data, status, recorder, etag=ETAG):
testutils.MockAdapter.__init__(self, data, status, recorder)
Expand All @@ -35,10 +38,15 @@ def send(self, request, **kwargs):
return resp


class TestGetServerTemplate:
_DEFAULT_APP = firebase_admin.initialize_app(testutils.MockCredential(), name='no_project_id')
_RC_INSTANCE = _utils.get_app_service(_DEFAULT_APP,
_REMOTE_CONFIG_ATTRIBUTE, _RemoteConfigService)
class TestRemoteConfigServiceClient:
@classmethod
def setup_class(cls):
cred = testutils.MockCredential()
firebase_admin.initialize_app(cred, {'projectId': 'project-id'})

@classmethod
def teardown_class(cls):
testutils.cleanup_apps()

def test_rc_instance_get_server_template(self):
recorder = []
Expand All @@ -50,15 +58,18 @@ def test_rc_instance_get_server_template(self):
'parameterGroups': {},
'version': 'test'
})
self._RC_INSTANCE._client.session.mount(

rc_instance = _utils.get_app_service(firebase_admin.get_app(),
_REMOTE_CONFIG_ATTRIBUTE, _RemoteConfigService)
rc_instance._client.session.mount(
'https://firebaseremoteconfig.googleapis.com',
MockAdapter(response, 200, recorder))

template = self._RC_INSTANCE.get_server_template()
template = rc_instance.get_server_template()

assert template.parameters == dict(test_key="test_value")
assert str(template.version) == 'test'
assert str(template.etag) == '0'
assert str(template.etag) == 'etag'

def test_rc_instance_get_server_template_empty_params(self):
recorder = []
Expand All @@ -68,12 +79,69 @@ def test_rc_instance_get_server_template_empty_params(self):
'version': 'test'
})

self._RC_INSTANCE._client.session.mount(
rc_instance = _utils.get_app_service(firebase_admin.get_app(),
_REMOTE_CONFIG_ATTRIBUTE, _RemoteConfigService)
rc_instance._client.session.mount(
'https://firebaseremoteconfig.googleapis.com',
MockAdapter(response, 200, recorder))

template = self._RC_INSTANCE.get_server_template()
template = rc_instance.get_server_template()

assert template.parameters == {}
assert str(template.version) == 'test'
assert str(template.etag) == '0'
assert str(template.etag) == 'etag'


class TestRemoteConfigService:
@classmethod
def setup_class(cls):
cred = testutils.MockCredential()
firebase_admin.initialize_app(cred, {'projectId': 'project-id'})

@classmethod
def teardown_class(cls):
testutils.cleanup_apps()

def test_init_server_template(self):
app = firebase_admin.get_app()
template_data = {
'conditions': [],
'parameters': {
'test_key': 'test_value'
},
'parameterGroups': '',
'version': '',
}

template = remote_config.init_server_template(
app=app,
template_data=ServerTemplateData('etag', template_data) # Use ServerTemplateData here
)

config = template.evaluate()
assert config.get_string('test_key') == 'test_value'

@pytest.mark.asyncio
async def test_get_server_template(self):
app = firebase_admin.get_app()
rc_instance = _utils.get_app_service(app,
_REMOTE_CONFIG_ATTRIBUTE, _RemoteConfigService)

recorder = []
response = json.dumps({
'parameters': {
'test_key': 'test_value'
},
'conditions': [],
'parameterGroups': {},
'version': 'test'
})

rc_instance._client.session.mount(
'https://firebaseremoteconfig.googleapis.com',
MockAdapter(response, 200, recorder))

template = await remote_config.get_server_template(app=app)

config = template.evaluate()
assert config.get_string('test_key') == 'test_value'

0 comments on commit 20749d7

Please sign in to comment.