@@ -147,6 +147,45 @@ def test_auth_login_token_default_instance(self, monkeypatch, cli_fs_runner):
147147 assert "ggshield config set instance" not in result .output
148148 self ._request_mock .assert_all_requests_happened ()
149149
150+ @pytest .mark .parametrize (
151+ ("cmd_line_instance" , "expected_instance" ),
152+ [
153+ ("https://api.gitguardian.com/v1" , "https://dashboard.gitguardian.com" ),
154+ ("https://api.gitguardian.com" , "https://dashboard.gitguardian.com" ),
155+ (
156+ "https://api.eu1.gitguardian.com/v1" ,
157+ "https://dashboard.eu1.gitguardian.com" ,
158+ ),
159+ (
160+ "https://gitguardian.mycorp.local/exposed/" ,
161+ "https://gitguardian.mycorp.local" ,
162+ ),
163+ (
164+ "https://gitguardian.mycorp.local/exposed" ,
165+ "https://gitguardian.mycorp.local" ,
166+ ),
167+ ],
168+ )
169+ def test_api_instance_url (
170+ self , cmd_line_instance , expected_instance , cli_fs_runner
171+ ):
172+ """
173+ GIVEN a valid API token and an instance URL matching GitGuardian API urls
174+ WHEN running the login command
175+ THEN it succeeds
176+ """
177+ token = "mysupertoken"
178+ cmd = ["auth" , "login" , "--method=token" , f"--instance={ cmd_line_instance } " ]
179+ self ._request_mock .add_GET (TOKEN_ENDPOINT , VALID_TOKEN_RESPONSE )
180+ result = cli_fs_runner .invoke (cli , cmd , color = False , input = token + "\n " )
181+ config = Config ()
182+ config_instance_urls = [
183+ instance_config .url for instance_config in config .auth_config .instances
184+ ]
185+ assert_invoke_ok (result )
186+ assert expected_instance in config_instance_urls
187+ self ._request_mock .assert_all_requests_happened ()
188+
150189 @pytest .mark .parametrize (
151190 ("instance" , "suggests" ),
152191 (
@@ -966,3 +1005,23 @@ def test_invalid_instance_url(self, instance_url, cli_fs_runner, monkeypatch):
9661005 exit_code , output = self .run_cmd (cli_fs_runner )
9671006 assert exit_code == ExitCode .USAGE_ERROR , output
9681007 self ._webbrowser_open_mock .assert_not_called ()
1008+
1009+ @pytest .mark .parametrize (
1010+ "instance_url" ,
1011+ [
1012+ "https://api.gitguardian.com/v1" ,
1013+ "https://api.eu1.gitguardian.com/v1" ,
1014+ "https://gitguardian.mycorp.local/exposed/" ,
1015+ ],
1016+ )
1017+ def test_api_instance_url (self , instance_url , cli_fs_runner , monkeypatch ):
1018+ """
1019+ GIVEN an instance URL matching GitGuardian API urls
1020+ WHEN running the login command
1021+ THEN it succeeds
1022+ """
1023+ monkeypatch .setenv ("GITGUARDIAN_INSTANCE" , instance_url )
1024+ self .prepare_mocks (monkeypatch )
1025+ exit_code , output = self .run_cmd (cli_fs_runner )
1026+ assert exit_code == ExitCode .SUCCESS , output
1027+ self ._webbrowser_open_mock .assert_called ()
0 commit comments