@@ -147,7 +147,7 @@ def test_update_config_success(self, mock_update_session_config, mock_opik_confi
147147 url = "http://example.com"
148148 workspace = "workspace1"
149149
150- OpikConfigurator (api_key , workspace , url )._update_config ()
150+ OpikConfigurator (api_key , workspace , url )._update_config (save_to_file = True )
151151
152152 # Ensure config object is created and saved
153153 mock_opik_config .assert_called_with (
@@ -179,7 +179,7 @@ def test_update_config_raises_exception(
179179 workspace = "workspace1"
180180
181181 with pytest .raises (ConfigurationError , match = "Failed to update configuration." ):
182- OpikConfigurator (api_key , workspace , url )._update_config ()
182+ OpikConfigurator (api_key , workspace , url )._update_config (save_to_file = True )
183183
184184 # Ensure save_to_file is not called due to the exception
185185 mock_update_session_config .assert_not_called ()
@@ -201,7 +201,7 @@ def test_update_config_session_update_failure(
201201 workspace = "workspace1"
202202
203203 with pytest .raises (ConfigurationError , match = "Failed to update configuration." ):
204- OpikConfigurator (api_key , workspace , url )._update_config ()
204+ OpikConfigurator (api_key , workspace , url )._update_config (save_to_file = True )
205205
206206 # Ensure config object is created and saved
207207 mock_opik_config .assert_any_call (
@@ -567,6 +567,36 @@ def test_get_api_key_provided_key(self, mock_is_api_key_correct):
567567 assert configurator .api_key == "config_api_key"
568568 assert needs_update is True
569569
570+ @patch ("opik.configurator.configure.LOGGER.warning" )
571+ @patch ("opik.configurator.configure.opik.config.OpikConfig" )
572+ @patch (
573+ "opik.configurator.configure.opik_rest_helpers.is_api_key_correct" ,
574+ return_value = True ,
575+ )
576+ def test_set_api_key__provided_api_key__another_key_already_set_in_config__not_forced__warning_is_shown (
577+ self , mock_is_api_key_correct , mock_opik_config , mock_logger_warning
578+ ):
579+ """
580+ Test that a warning is logged when an API key is provided, but one is already
581+ set in the configuration file and force=False.
582+ """
583+ mock_config_instance = MagicMock ()
584+ mock_config_instance .api_key = "existing_api_key"
585+ mock_opik_config .return_value = mock_config_instance
586+
587+ configurator = OpikConfigurator (
588+ api_key = "new_api_key" , url = OPIK_BASE_URL_LOCAL , force = False
589+ )
590+ needs_update = configurator ._set_api_key ()
591+
592+ mock_logger_warning .assert_called_once_with (
593+ "You already have an API key set in the configuration file. "
594+ "If you want to change it, please use the --force flag or force=True when calling the configure() method. "
595+ "Otherwise, the configuration file will not be updated but the session will use the new API key."
596+ )
597+ assert configurator .api_key == "new_api_key"
598+ assert needs_update is False
599+
570600 @patch (
571601 "opik.configurator.configure.opik_rest_helpers.is_api_key_correct" ,
572602 return_value = True ,
@@ -785,6 +815,34 @@ def test_get_workspace_accept_default__automatic_approvals_enabled__no_approval_
785815 mock_get_default_workspace .assert_called_once_with ()
786816 mock_ask_user_for_approval .assert_not_called ()
787817
818+ @patch ("opik.configurator.configure.opik.config.OpikConfig" )
819+ @patch (
820+ "opik.configurator.configure.OpikConfigurator._get_default_workspace" ,
821+ return_value = "default_workspace" ,
822+ )
823+ @patch ("opik.configurator.configure.ask_user_for_approval" , return_value = True )
824+ def test_get_workspace_accept_default__force_enabled__no_approval_questions_asked (
825+ self , mock_ask_user_for_approval , mock_get_default_workspace , mock_opik_config
826+ ):
827+ """
828+ Test that when force=True, the default workspace is used without asking for user approval.
829+ """
830+ current_config = OpikConfig (workspace = OPIK_WORKSPACE_DEFAULT_NAME )
831+ mock_opik_config .return_value = current_config
832+
833+ configurator = OpikConfigurator (
834+ workspace = None ,
835+ force = True ,
836+ api_key = "valid_api_key" ,
837+ automatic_approvals = False ,
838+ )
839+ needs_update = configurator ._set_workspace ()
840+
841+ assert configurator .workspace == "default_workspace"
842+ assert needs_update is True
843+ mock_get_default_workspace .assert_called_once_with ()
844+ mock_ask_user_for_approval .assert_not_called ()
845+
788846 @patch ("opik.configurator.configure.opik.config.OpikConfig" )
789847 @patch (
790848 "opik.configurator.configure.OpikConfigurator._get_default_workspace" ,
@@ -1193,7 +1251,7 @@ def test_configure_local_uses_local_instance(
11931251 mock_ask_user_for_approval .assert_called_once_with (
11941252 f"Found local Opik instance on: { OPIK_BASE_URL_LOCAL } , do you want to use it? (Y/n)"
11951253 )
1196- mock_update_config .assert_called_once_with ()
1254+ mock_update_config .assert_called_once_with (save_to_file = True )
11971255
11981256 assert configurator .api_key is None
11991257 assert configurator .base_url == OPIK_BASE_URL_LOCAL
@@ -1220,7 +1278,7 @@ def test_configure_local_uses_local_instance__automatic_approvals_enabled__no_ap
12201278 configurator ._configure_local ()
12211279
12221280 mock_ask_user_for_approval .assert_not_called ()
1223- mock_update_config .assert_called_once_with ()
1281+ mock_update_config .assert_called_once_with (save_to_file = True )
12241282
12251283 assert configurator .api_key is None
12261284 assert configurator .base_url == OPIK_BASE_URL_LOCAL
@@ -1271,7 +1329,7 @@ def test_configure_local_uses_local_instance__non_interactive__automatic_approva
12711329 configurator ._configure_local ()
12721330
12731331 mock_ask_user_for_approval .assert_not_called ()
1274- mock_update_config .assert_called_once_with ()
1332+ mock_update_config .assert_called_once_with (save_to_file = True )
12751333
12761334 assert configurator .api_key is None
12771335 assert configurator .base_url == OPIK_BASE_URL_LOCAL
0 commit comments