@@ -365,9 +365,10 @@ def test_set_last_modified(self, _):
365365
366366 def test_fetch_datafile (self , _ ):
367367 """ Test that fetch_datafile sets config and last_modified based on response. """
368+ sdk_key = 'some_key'
368369 with mock .patch ('optimizely.config_manager.PollingConfigManager.fetch_datafile' ):
369- project_config_manager = config_manager .PollingConfigManager (sdk_key = 'some_key' )
370- expected_datafile_url = 'https://cdn.optimizely.com/datafiles/some_key.json'
370+ project_config_manager = config_manager .PollingConfigManager (sdk_key = sdk_key )
371+ expected_datafile_url = enums . ConfigManager . DATAFILE_URL_TEMPLATE . format ( sdk_key = sdk_key )
371372 test_headers = {'Last-Modified' : 'New Time' }
372373 test_datafile = json .dumps (self .config_dict_with_features )
373374 test_response = requests .Response ()
@@ -397,3 +398,50 @@ def test_is_running(self, _):
397398 with mock .patch ('optimizely.config_manager.PollingConfigManager.fetch_datafile' ):
398399 project_config_manager = config_manager .PollingConfigManager (sdk_key = 'some_key' )
399400 self .assertTrue (project_config_manager .is_running )
401+
402+
403+ @mock .patch ('requests.get' )
404+ class AuthDatafilePollingConfigManagerTest (base .BaseTest ):
405+ def test_init__access_token_none__fails (self , _ ):
406+ """ Test that initialization fails if access_token is None. """
407+ self .assertRaisesRegexp (
408+ optimizely_exceptions .InvalidInputException ,
409+ 'access_token cannot be empty or None.' ,
410+ config_manager .AuthDatafilePollingConfigManager ,
411+ access_token = None
412+ )
413+
414+ def test_set_access_token (self , _ ):
415+ """ Test that access_token is properly set as instance variable. """
416+ access_token = 'some_token'
417+ sdk_key = 'some_key'
418+ with mock .patch ('optimizely.config_manager.AuthDatafilePollingConfigManager.fetch_datafile' ):
419+ project_config_manager = config_manager .AuthDatafilePollingConfigManager (
420+ access_token = access_token , sdk_key = sdk_key )
421+
422+ self .assertEqual (access_token , project_config_manager .access_token )
423+
424+ def test_fetch_datafile (self , _ ):
425+ """ Test that fetch_datafile sets authorization header in request header and sets config based on response. """
426+ access_token = 'some_token'
427+ sdk_key = 'some_key'
428+ with mock .patch ('optimizely.config_manager.AuthDatafilePollingConfigManager.fetch_datafile' ):
429+ project_config_manager = config_manager .AuthDatafilePollingConfigManager (
430+ access_token = access_token , sdk_key = sdk_key )
431+ expected_datafile_url = enums .ConfigManager .AUTHENTICATED_DATAFILE_URL_TEMPLATE .format (sdk_key = sdk_key )
432+ test_datafile = json .dumps (self .config_dict_with_features )
433+ test_response = requests .Response ()
434+ test_response .status_code = 200
435+ test_response ._content = test_datafile
436+
437+ # Call fetch_datafile and assert that request was sent with correct authorization header
438+ with mock .patch ('requests.get' , return_value = test_response ) as mock_request :
439+ project_config_manager .fetch_datafile ()
440+
441+ mock_request .assert_called_once_with (
442+ expected_datafile_url ,
443+ headers = {'Authorization' : 'Bearer {access_token}' .format (access_token = access_token )},
444+ timeout = enums .ConfigManager .REQUEST_TIMEOUT ,
445+ )
446+
447+ self .assertIsInstance (project_config_manager .get_config (), project_config .ProjectConfig )
0 commit comments