Skip to content

Commit

Permalink
fix: makeAPI urls from GitGuardian work with GGShield
Browse files Browse the repository at this point in the history
  • Loading branch information
Séverine Bonnechère committed Jan 8, 2025
1 parent dcd87b4 commit 759457d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- It is now possible to login using https://api.eu1.gitguardian.com/v1 or https://api.gitguardian.com/v1 urls.
11 changes: 11 additions & 0 deletions ggshield/cmd/auth/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ def login_cmd(
"--scopes is reserved for the web login method.", param_hint="scopes"
)

# make GitGuardian API urls work
instance = translate_API_url_to_dashboard_url(instance)

if method == "token":
token_login(config, instance)
return 0
Expand All @@ -169,6 +172,14 @@ def login_cmd(
return 1


def translate_API_url_to_dashboard_url(url: Optional[str]) -> Optional[str]:
if url == "https://api.gitguardian.com/v1":
return "https://dashboard.gitguardian.com"
if url == "https://api.eu1.gitguardian.com/v1":
return "https://dashboard.eu1.gitguardian.com"
return url


def token_login(config: Config, instance: Optional[str]) -> None:
if instance:
config.cmdline_instance_name = instance
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/cmd/auth/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,36 @@ def test_auth_login_token_default_instance(self, monkeypatch, cli_fs_runner):
assert "ggshield config set instance" not in result.output
self._request_mock.assert_all_requests_happened()

@pytest.mark.parametrize(
("cmd_line_instance", "expected_instance"),
[
("https://api.gitguardian.com/v1", "https://dashboard.gitguardian.com"),
(
"https://api.eu1.gitguardian.com/v1",
"https://dashboard.eu1.gitguardian.com",
),
],
)
def test_api_instance_url(
self, cmd_line_instance, expected_instance, cli_fs_runner
):
"""
GIVEN a valid API token and an instance URL matching GitGuardian API urls
WHEN running the login command
THEN it succeeds
"""
token = "mysupertoken"
cmd = ["auth", "login", "--method=token", f"--instance={cmd_line_instance}"]
self._request_mock.add_GET(TOKEN_ENDPOINT, VALID_TOKEN_RESPONSE)
result = cli_fs_runner.invoke(cli, cmd, color=False, input=token + "\n")
config = Config()
config_instance_urls = [
instance_config.url for instance_config in config.auth_config.instances
]
assert_invoke_ok(result)
assert expected_instance in config_instance_urls
self._request_mock.assert_all_requests_happened()

@pytest.mark.parametrize(
("instance", "suggests"),
(
Expand Down Expand Up @@ -966,3 +996,22 @@ def test_invalid_instance_url(self, instance_url, cli_fs_runner, monkeypatch):
exit_code, output = self.run_cmd(cli_fs_runner)
assert exit_code == ExitCode.USAGE_ERROR, output
self._webbrowser_open_mock.assert_not_called()

@pytest.mark.parametrize(
"instance_url",
[
"https://api.gitguardian.com/v1",
"https://api.eu1.gitguardian.com/v1",
],
)
def test_api_instance_url(self, instance_url, cli_fs_runner, monkeypatch):
"""
GIVEN an instance URL matching GitGuardian API urls
WHEN running the login command
THEN it succeeds
"""
monkeypatch.setenv("GITGUARDIAN_INSTANCE", instance_url)
self.prepare_mocks(monkeypatch)
exit_code, output = self.run_cmd(cli_fs_runner)
assert exit_code == ExitCode.SUCCESS, output
self._webbrowser_open_mock.assert_called()

0 comments on commit 759457d

Please sign in to comment.