diff --git a/src/main/resources/ui/index.html b/src/main/resources/ui/index.html index a49b5930ae..f3c382dd13 100644 --- a/src/main/resources/ui/index.html +++ b/src/main/resources/ui/index.html @@ -469,18 +469,37 @@ } // ── Connect ────────────────────────────────────────── - var SNIPPETS = { - shell: "export AWS_ENDPOINT_URL=" + endpoint + "\nexport AWS_ACCESS_KEY_ID=test\nexport AWS_SECRET_ACCESS_KEY=test\nexport AWS_DEFAULT_REGION=us-east-1", - cli: "aws --endpoint-url " + endpoint + " s3 mb s3://demo\naws --endpoint-url " + endpoint + " s3 ls", - boto3: 'import boto3\n\ns3 = boto3.client(\n "s3",\n endpoint_url="' + endpoint + '",\n region_name="us-east-1",\n aws_access_key_id="test",\n aws_secret_access_key="test",\n)' - }; + var activeSnippet = "shell"; + var connectRegion = "us-east-1"; + + function renderSnippet() { + var snippets = { + shell: "export AWS_ENDPOINT_URL=" + endpoint + "\nexport AWS_ACCESS_KEY_ID=test\nexport AWS_SECRET_ACCESS_KEY=test\nexport AWS_DEFAULT_REGION=" + connectRegion, + cli: "aws --endpoint-url " + endpoint + " s3 mb s3://demo\naws --endpoint-url " + endpoint + " s3 ls", + boto3: 'import boto3\n\ns3 = boto3.client(\n "s3",\n endpoint_url="' + endpoint + '",\n region_name="' + connectRegion + '",\n aws_access_key_id="test",\n aws_secret_access_key="test",\n)' + }; + $("snippet").textContent = snippets[activeSnippet]; + } + + function renderConnect(info) { + var defaults = info && info.defaults; + if (!defaults) { return; } + if (defaults.account_id) { $("f-account").textContent = defaults.account_id; } + if (defaults.region && defaults.region !== connectRegion) { + connectRegion = defaults.region; + $("f-region").textContent = connectRegion; + renderSnippet(); + } + } + $("f-endpoint").textContent = endpoint; - $("snippet").textContent = SNIPPETS.shell; + renderSnippet(); document.querySelectorAll(".tab").forEach(function (tab) { tab.addEventListener("click", function () { document.querySelectorAll(".tab").forEach(function (t) { t.setAttribute("aria-selected", String(t === tab)); }); - $("snippet").textContent = SNIPPETS[tab.dataset.snippet]; + activeSnippet = tab.dataset.snippet; + renderSnippet(); }); }); @@ -633,7 +652,11 @@ function refresh() { if (refreshing) { return; } refreshing = true; - Promise.allSettled([getJson("/_floci/health"), getJson("/_floci/init"), getJson("/_floci/info"), getJson("/_floci/ui/status")]) + var info = getJson("/_floci/info").then(function (manifest) { + renderConnect(manifest); + return manifest; + }); + Promise.allSettled([getJson("/_floci/health"), getJson("/_floci/init"), info, getJson("/_floci/ui/status")]) .then(function (results) { var v = results.map(function (r) { return r.status === "fulfilled" ? r.value : null; }); var health = v[0], init = v[1]; diff --git a/src/test/java/io/floci/aws/services/floci/ui/DashboardConnectUsesConfiguredDefaultsTest.java b/src/test/java/io/floci/aws/services/floci/ui/DashboardConnectUsesConfiguredDefaultsTest.java new file mode 100644 index 0000000000..92f5309351 --- /dev/null +++ b/src/test/java/io/floci/aws/services/floci/ui/DashboardConnectUsesConfiguredDefaultsTest.java @@ -0,0 +1,80 @@ +package io.floci.aws.services.floci.ui; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.regex.Pattern; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Guards the dashboard's Connect panel, which shows the region and account an SDK should use and + * the Shell and boto3 snippets built from them: they come from the defaults {@code /_floci/info} + * reports, as soon as it answers, and the built-in values stay when it does not report them. + */ +class DashboardConnectUsesConfiguredDefaultsTest { + + private static final Path LANDING_PAGE = Path.of("src", "main", "resources", "ui", "index.html"); + private static final Pattern REGION_LABEL_READ = Pattern.compile("\\(\"f-region\"\\)\\.textContent\\s*(?:[^=\\s]|==)"); + private static final Pattern INFO_RENDERED_ON_ARRIVAL = + Pattern.compile("getJson\\(\"/_floci/info\"\\)\\.then\\(function \\(manifest\\) \\{\\s*renderConnect\\(manifest\\);"); + + // Catches: a snippet that hardcodes us-east-1, so an emulator on another region hands out + // commands and code that point the SDK at the wrong one. + @Test + void snippetsUseTheRegionTheManifestReports() { + String content = readLandingPage(); + + assertTrue(content.contains("AWS_DEFAULT_REGION=\" + connectRegion"), + "the Shell snippet must take its region from connectRegion"); + assertTrue(content.contains("region_name=\"' + connectRegion"), + "the boto3 snippet must take its region from connectRegion"); + assertFalse(content.contains("AWS_DEFAULT_REGION=us-east-1") || content.contains("region_name=\"us-east-1\""), + "no snippet may hardcode the built-in region"); + } + + // Catches: a snippet that reads the region back from the panel's label, so any text added to + // the label lands in the generated commands and code. + @Test + void snippetsNeverReadTheRegionBackFromThePanelLabel() { + assertFalse(REGION_LABEL_READ.matcher(readLandingPage()).find(), + "index.html must only write the region label, never read it back"); + } + + // Catches: the Connect panel waiting for every dashboard request to settle, so a slow + // /_floci/ui/status holds back the region and account the manifest already gave. + @Test + void theManifestIsAppliedAsSoonAsInfoAnswers() { + assertTrue(INFO_RENDERED_ON_ARRIVAL.matcher(readLandingPage()).find(), + "index.html must render the Connect defaults in /_floci/info's own callback"); + } + + // Catches: a manifest without defaults, or the region or account missing from them, blanking + // the built-in values shown before it arrived. + @Test + void theBuiltInValuesStayWhenTheManifestOmitsThem() { + String content = readLandingPage(); + + assertTrue(content.contains("
us-east-1
") + && content.contains("
000000000000
") + && content.contains("var connectRegion = \"us-east-1\";"), + "the panel and the snippets must start from the built-in region and account"); + assertTrue(content.contains("if (!defaults) { return; }") + && content.contains("if (defaults.account_id) {") + && content.contains("if (defaults.region && defaults.region !== connectRegion) {"), + "the panel must change a value only when the manifest reports it"); + } + + private static String readLandingPage() { + try { + return Files.readString(LANDING_PAGE, StandardCharsets.UTF_8); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } +}