Runway 1.2.0
This commit is contained in:
1 parent
5fa0c1ef9f
commit
9144c206be
90 files changed
+2737
-554
No files matched your search
@@ -71,6 +71,67 @@ class BackupRestoreRecoveryTest {
|
||||
"the scratch work dir is cleared");
|
||||
}
|
||||
|
||||
/** The swap completed but the crash came before the logs were carried: they are merged, older lines first. */
|
||||
@Test
|
||||
void aCompletedSwapGetsItsLogsMergedOnStartup() throws IOException {
|
||||
Path siteDir = layout.siteDir("demo");
|
||||
Files.createDirectories(siteDir.resolve("logs"));
|
||||
Files.writeString(siteDir.resolve("site.yml"), "id: demo\nname: RESTORED\n", StandardCharsets.UTF_8);
|
||||
Files.writeString(siteDir.resolve("logs/audit-2026-10-30.log"), "AFTER\n", StandardCharsets.UTF_8);
|
||||
Path old = workOld("demo");
|
||||
Files.createDirectories(old.resolve("logs"));
|
||||
Files.writeString(old.resolve("site.yml"), "id: demo\nname: BEFORE\n", StandardCharsets.UTF_8);
|
||||
Files.writeString(old.resolve("logs/audit-2026-10-30.log"), "BEFORE\n", StandardCharsets.UTF_8);
|
||||
Files.writeString(old.resolve("logs/audit-2026-10-29.log"), "YESTERDAY\n", StandardCharsets.UTF_8);
|
||||
|
||||
recovery.recover();
|
||||
|
||||
assertEquals("id: demo\nname: RESTORED\n", Files.readString(siteDir.resolve("site.yml")), "the restore stands");
|
||||
assertEquals("BEFORE\nAFTER\n", Files.readString(siteDir.resolve("logs/audit-2026-10-30.log")),
|
||||
"a day both have is joined in order");
|
||||
assertEquals("YESTERDAY\n", Files.readString(siteDir.resolve("logs/audit-2026-10-29.log")));
|
||||
assertFalse(Files.exists(layout.sites().resolve(BackupService.WORK_DIR).resolve("demo")));
|
||||
}
|
||||
|
||||
/**
|
||||
* A log write recreated the site folder between the two renames, and the process died: that folder holds only
|
||||
* logs. It must not count as the site (which would delete `old`, the only copy); the site is rolled forward and
|
||||
* the late lines follow its own.
|
||||
*/
|
||||
@Test
|
||||
void aSiteFolderHoldingOnlyLateLogsIsNotMistakenForTheSite() throws IOException {
|
||||
Path siteDir = layout.siteDir("demo");
|
||||
Files.createDirectories(siteDir.resolve("logs"));
|
||||
Files.writeString(siteDir.resolve("logs/access-2026-10-30.log"), "LATE\n", StandardCharsets.UTF_8);
|
||||
Path old = workOld("demo");
|
||||
Files.createDirectories(old.resolve("logs"));
|
||||
Files.writeString(old.resolve("site.yml"), "id: demo\n", StandardCharsets.UTF_8);
|
||||
Files.writeString(old.resolve("logs/access-2026-10-30.log"), "EARLIER\n", StandardCharsets.UTF_8);
|
||||
|
||||
recovery.recover();
|
||||
|
||||
assertEquals("id: demo\n", Files.readString(siteDir.resolve("site.yml")), "the original site is back");
|
||||
assertEquals("EARLIER\nLATE\n", Files.readString(siteDir.resolve("logs/access-2026-10-30.log")));
|
||||
assertFalse(Files.exists(layout.sites().resolve(BackupService.WORK_DIR).resolve("demo")));
|
||||
}
|
||||
|
||||
/** A rollback put the site back but could not merge the lines written meanwhile: they follow its own. */
|
||||
@Test
|
||||
void lateLinesLeftByARollbackAreMergedAfterTheSitesOwn() throws IOException {
|
||||
Path siteDir = layout.siteDir("demo");
|
||||
Files.createDirectories(siteDir.resolve("logs"));
|
||||
Files.writeString(siteDir.resolve("site.yml"), "id: demo\n", StandardCharsets.UTF_8);
|
||||
Files.writeString(siteDir.resolve("logs/access-2026-10-30.log"), "OWN\n", StandardCharsets.UTF_8);
|
||||
Path late = layout.sites().resolve(BackupService.WORK_DIR).resolve("demo").resolve(BackupService.LATE_DIR);
|
||||
Files.createDirectories(late.resolve("logs"));
|
||||
Files.writeString(late.resolve("logs/access-2026-10-30.log"), "LATE\n", StandardCharsets.UTF_8);
|
||||
|
||||
recovery.recover();
|
||||
|
||||
assertEquals("OWN\nLATE\n", Files.readString(siteDir.resolve("logs/access-2026-10-30.log")));
|
||||
assertFalse(Files.exists(layout.sites().resolve(BackupService.WORK_DIR).resolve("demo")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noWorkDirIsANoOp() {
|
||||
recovery.recover(); // must not throw when sites/.restore-work does not exist
|
||||
|
||||
@@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeFalse;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -492,6 +493,113 @@ class BackupServiceTest {
|
||||
assertTrue(sawContent, "real site content is still archived");
|
||||
}
|
||||
|
||||
/** A restore replaces the content, not the site's history: its audit log and stats stay as they were. */
|
||||
@Test
|
||||
void aRestoreKeepsTheLiveSitesLogsAndStats() throws IOException {
|
||||
ByteArrayOutputStream archive = new ByteArrayOutputStream();
|
||||
svc.backup("demo", archive);
|
||||
write(siteDir.resolve("logs/audit-2026-10-30.log"), "LIVE-AUDIT");
|
||||
write(siteDir.resolve("stats/bandwidth.csv"), "LIVE-STATS");
|
||||
write(siteDir.resolve("templates/_master.html"), "MASTER_BROKEN");
|
||||
|
||||
svc.restore("demo", new ByteArrayInputStream(archive.toByteArray()));
|
||||
|
||||
assertEquals("MASTER_V1", read(siteDir.resolve("templates/_master.html")), "the content is restored");
|
||||
assertEquals("LIVE-AUDIT", read(siteDir.resolve("logs/audit-2026-10-30.log")), "the audit log survives");
|
||||
assertEquals("LIVE-STATS", read(siteDir.resolve("stats/bandwidth.csv")), "the stats survive");
|
||||
}
|
||||
|
||||
/** A backup never writes logs or stats, so whatever an archive carries under those names is discarded. */
|
||||
@Test
|
||||
void aRestoreDiscardsLogsAndStatsCarriedByTheArchive() throws IOException {
|
||||
ByteArrayOutputStream original = new ByteArrayOutputStream();
|
||||
svc.backup("demo", original);
|
||||
ByteArrayOutputStream crafted = new ByteArrayOutputStream();
|
||||
try (ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(original.toByteArray()));
|
||||
ZipOutputStream out = new ZipOutputStream(crafted)) {
|
||||
ZipEntry e;
|
||||
while ((e = in.getNextEntry()) != null) {
|
||||
out.putNextEntry(new ZipEntry(e.getName()));
|
||||
in.transferTo(out);
|
||||
out.closeEntry();
|
||||
}
|
||||
for (String planted : List.of("site/logs/audit-2026-10-30.log", "site/logs/audit-2026-10-29.log",
|
||||
"site/stats/bandwidth.csv")) {
|
||||
out.putNextEntry(new ZipEntry(planted));
|
||||
out.write("PLANTED".getBytes(StandardCharsets.UTF_8));
|
||||
out.closeEntry();
|
||||
}
|
||||
}
|
||||
// The live site has an audit log of its own, and no stats yet.
|
||||
write(siteDir.resolve("logs/audit-2026-10-30.log"), "LIVE-AUDIT");
|
||||
|
||||
svc.restore("demo", new ByteArrayInputStream(crafted.toByteArray()));
|
||||
|
||||
assertEquals("LIVE-AUDIT", read(siteDir.resolve("logs/audit-2026-10-30.log")), "the live log is kept");
|
||||
assertFalse(Files.exists(siteDir.resolve("logs/audit-2026-10-29.log")), "no planted day is added");
|
||||
assertFalse(Files.exists(siteDir.resolve("stats")), "no planted stats appear");
|
||||
}
|
||||
|
||||
private Path work(String siteId) {
|
||||
return layout.sites().resolve(BackupService.WORK_DIR).resolve(siteId);
|
||||
}
|
||||
|
||||
/** A merge that fails is kept, never deleted, and a repeated run finishes it without joining anything twice. */
|
||||
@Test
|
||||
void aFailedLogMergeIsKeptAndFinishedByTheNextRun() throws IOException {
|
||||
write(siteDir.resolve("logs/audit-2026-10-30.log"), "NEW\n");
|
||||
Path old = work("demo").resolve("old");
|
||||
write(old.resolve("site.yml"), "id: demo\n");
|
||||
write(old.resolve("logs/audit-2026-10-29.log"), "YESTERDAY\n");
|
||||
Path unreadable = old.resolve("logs/audit-2026-10-30.log");
|
||||
write(unreadable, "OLD\n");
|
||||
unreadable.toFile().setReadable(false);
|
||||
// As root (some CI containers) a file stays readable whatever its mode: the failure cannot be staged there.
|
||||
assumeFalse(Files.isReadable(unreadable), "the file can be made unreadable");
|
||||
try {
|
||||
assertThrows(IOException.class, () -> BackupService.settle(siteDir, work("demo")));
|
||||
} finally {
|
||||
assertTrue(unreadable.toFile().setReadable(true));
|
||||
}
|
||||
assertTrue(Files.exists(unreadable), "the unmerged log is kept");
|
||||
|
||||
BackupService.settle(siteDir, work("demo"));
|
||||
|
||||
assertEquals("OLD\nNEW\n", read(siteDir.resolve("logs/audit-2026-10-30.log")), "joined once, in order");
|
||||
assertEquals("YESTERDAY\n", read(siteDir.resolve("logs/audit-2026-10-29.log")), "moved once");
|
||||
}
|
||||
|
||||
/** Logs a previous restore could not carry over are merged by the next restore of the site, not deleted. */
|
||||
@Test
|
||||
void theNextRestoreFinishesLogsAPreviousOneLeftBehind() throws IOException {
|
||||
ByteArrayOutputStream archive = new ByteArrayOutputStream();
|
||||
svc.backup("demo", archive);
|
||||
write(work("demo").resolve("old/site.yml"), "id: demo\n");
|
||||
write(work("demo").resolve("old/logs/audit-2026-10-29.log"), "LEFT-BEHIND\n");
|
||||
|
||||
svc.restore("demo", new ByteArrayInputStream(archive.toByteArray()));
|
||||
|
||||
assertEquals("LEFT-BEHIND\n", read(siteDir.resolve("logs/audit-2026-10-29.log")));
|
||||
assertFalse(Files.exists(work("demo")), "the work folder is cleared once settled");
|
||||
}
|
||||
|
||||
/** A new site starts its own logs: a leftover folder under its id (no site.yml) is not adopted. */
|
||||
@Test
|
||||
void aSiteCreatedFromABackupDoesNotAdoptLeftoverLogs() throws IOException {
|
||||
new SiteStore(layout, new YamlStore()).save(new Site("src", "Source", List.of("src.test"), Locale.ENGLISH,
|
||||
List.of(Locale.ENGLISH), "https://src.test", SiteStatus.ACTIVE));
|
||||
write(layout.siteDir("src").resolve("templates/home.html"), "SRC_CONTENT");
|
||||
ByteArrayOutputStream archive = new ByteArrayOutputStream();
|
||||
svc.backup("src", archive);
|
||||
Path stray = layout.siteDir("clone");
|
||||
write(stray.resolve("logs/audit-2026-10-30.log"), "STRAY");
|
||||
|
||||
svc.createFromBackup("clone", "Cloned", List.of("clone.test"), new ByteArrayInputStream(archive.toByteArray()));
|
||||
|
||||
assertTrue(Files.exists(stray.resolve("site.yml")), "the site is created");
|
||||
assertFalse(Files.exists(stray.resolve("logs")), "no leftover log becomes the new site's journal");
|
||||
}
|
||||
|
||||
// ---- a restore must not make every page look edited today ------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
@@ -103,7 +103,9 @@ class AuditViewTest {
|
||||
"permissions.role-inherit-removed", "page.saved", "page.deleted", "page.published",
|
||||
"page.unpublished", "user.created", "user.password-reset", "user.deleted",
|
||||
"plugin.settings-changed", "logs.settings-changed", "site-config.changed", "master.assigned",
|
||||
"master.unassigned", "site.created", "site.hosts-updated", "backup.saved",
|
||||
"master.unassigned", "site.created", "site.hosts-updated", "backup.saved", "backup.deleted",
|
||||
"backup.downloaded", "site.restored", "user.updated", "user.suspended", "user.resumed",
|
||||
"static.uploaded", "static.folder-created", "static.deleted",
|
||||
"platform.kernel-settings-changed", "platform.kernel-setting-adopted")) {
|
||||
assertTrue(en.contains(recorded), "a label for " + recorded);
|
||||
}
|
||||
|
||||
@@ -2,10 +2,17 @@ package mx.quest.runway.console;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.startsWith;
|
||||
import static org.mockito.Mockito.clearInvocations;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
|
||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
||||
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.head;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
@@ -23,6 +30,7 @@ import java.util.Set;
|
||||
import mx.quest.runway.authz.GrantFile;
|
||||
import mx.quest.runway.authz.GrantStore;
|
||||
import mx.quest.runway.backup.BackupService;
|
||||
import mx.quest.runway.log.SiteLogService;
|
||||
import mx.quest.runway.security.PasswordEncoders;
|
||||
import mx.quest.runway.security.PasswordStrength;
|
||||
import mx.quest.runway.security.Roles;
|
||||
@@ -46,6 +54,7 @@ import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.request.RequestPostProcessor;
|
||||
@@ -104,6 +113,8 @@ class TenantBackupsScreenTest {
|
||||
private SiteResolver siteResolver;
|
||||
@Autowired
|
||||
private BackupService backups;
|
||||
@MockitoSpyBean
|
||||
private SiteLogService siteLog;
|
||||
private MockMvc mvc;
|
||||
|
||||
@BeforeEach
|
||||
@@ -189,6 +200,75 @@ class TenantBackupsScreenTest {
|
||||
}
|
||||
}
|
||||
|
||||
// --- the site's audit log ----------------------------------------------------------------------
|
||||
|
||||
/** Each act that took effect is one line in the site's own audit log, with who did it and what. */
|
||||
@Test
|
||||
void everyBackupActIsRecordedInTheSitesAuditLog() throws Exception {
|
||||
MockHttpSession admin = sessionFor("admin");
|
||||
clearInvocations(siteLog);
|
||||
mvc.perform(post("/__admin/backups/create").session(admin).with(csrf()))
|
||||
.andExpect(status().is3xxRedirection());
|
||||
String name = backups.list("acme").get(0).name();
|
||||
verify(siteLog).audit("acme", "admin", "backup.saved", name);
|
||||
|
||||
mvc.perform(get("/__admin/backups/download").session(admin)).andExpect(status().isOk());
|
||||
verify(siteLog).audit("acme", "admin", "backup.downloaded", "full");
|
||||
mvc.perform(get("/__admin/backups/download-transfer").session(admin)).andExpect(status().isOk());
|
||||
verify(siteLog).audit("acme", "admin", "backup.downloaded", "transfer");
|
||||
mvc.perform(get("/__admin/backups/" + name + "/download").session(admin)).andExpect(status().isOk());
|
||||
verify(siteLog).audit("acme", "admin", "backup.downloaded", "full " + name);
|
||||
|
||||
mvc.perform(post("/__admin/backups/" + name + "/restore").session(admin).with(csrf()))
|
||||
.andExpect(flash().attributeExists("flash"));
|
||||
verify(siteLog).audit("acme", "admin", "site.restored", name);
|
||||
byte[] zip = mvc.perform(get("/__admin/backups/download").session(admin)).andReturn()
|
||||
.getResponse().getContentAsByteArray();
|
||||
mvc.perform(multipart("/__admin/backups/restore")
|
||||
.file(new MockMultipartFile("file", "acme.zip", "application/zip", zip))
|
||||
.session(admin).with(csrf()))
|
||||
.andExpect(flash().attributeExists("flash"));
|
||||
verify(siteLog).audit("acme", "admin", "site.restored", "(upload)");
|
||||
|
||||
mvc.perform(post("/__admin/backups/" + name + "/delete").session(admin).with(csrf()))
|
||||
.andExpect(status().is3xxRedirection());
|
||||
verify(siteLog).audit("acme", "admin", "backup.deleted", name);
|
||||
}
|
||||
|
||||
/** A refused, failed or empty act writes nothing: the log says what happened, not what was tried. */
|
||||
@Test
|
||||
void aRefusedOrFailedBackupActIsNotRecorded() throws Exception {
|
||||
String name = storedBackup();
|
||||
MockHttpSession keeper = sessionFor("keeper");
|
||||
MockHttpSession admin = sessionFor("admin");
|
||||
clearInvocations(siteLog);
|
||||
mvc.perform(get("/__admin/backups/download").session(keeper)).andExpect(status().isForbidden());
|
||||
mvc.perform(get("/__admin/backups/" + name + "/download").session(keeper))
|
||||
.andExpect(status().isForbidden());
|
||||
// A HEAD request (a link checker, a preview) takes no copy: answered, not recorded.
|
||||
for (String path : List.of("/__admin/backups/download", "/__admin/backups/download-transfer",
|
||||
"/__admin/backups/" + name + "/download")) {
|
||||
mvc.perform(head(path).session(admin))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(header().string("Content-Disposition", Matchers.containsString("attachment")));
|
||||
}
|
||||
// A range that resumes a download is not a new one.
|
||||
mvc.perform(get("/__admin/backups/" + name + "/download").session(admin).header("Range", "bytes=10-"))
|
||||
.andExpect(status().isPartialContent());
|
||||
mvc.perform(post("/__admin/backups/" + name + "/restore").session(keeper).with(csrf()))
|
||||
.andExpect(status().isForbidden());
|
||||
mvc.perform(post("/__admin/backups/no-such-backup.zip/delete").session(admin).with(csrf()))
|
||||
.andExpect(status().is3xxRedirection());
|
||||
mvc.perform(post("/__admin/backups/no-such-backup.zip/restore").session(admin).with(csrf()))
|
||||
.andExpect(flash().attributeExists("error"));
|
||||
mvc.perform(multipart("/__admin/backups/restore")
|
||||
.file(new MockMultipartFile("file", "junk.zip", "application/zip", new byte[] {1, 2, 3}))
|
||||
.session(admin).with(csrf()))
|
||||
.andExpect(flash().attributeExists("error"));
|
||||
verify(siteLog, never()).audit(anyString(), anyString(), startsWith("backup."), anyString());
|
||||
verify(siteLog, never()).audit(anyString(), anyString(), eq("site.restored"), anyString());
|
||||
}
|
||||
|
||||
/** Without MANAGE_BACKUPS at all, nothing — not even a transfer copy. */
|
||||
@Test
|
||||
void anOperatorWithoutTheGrantGetsNoDownloadOfAnyKind() throws Exception {
|
||||
|
||||
@@ -421,6 +421,41 @@ class PluginContributionPolicyTest {
|
||||
"p2 declared no app-level secret, so the environment's value is not handed out");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theFactoryHandsAPluginItsOwnSavedAppSettingsFromItsOwnFileOnly() throws Exception {
|
||||
assumeTrue(PluginFixture.compilerAvailable());
|
||||
String schema = "settings:\n - { key: apiKey, type: secret, label: l }\n"
|
||||
+ " - { key: host, type: string, label: l }\n";
|
||||
PluginDescriptor d1 = PluginDescriptor.read(new ByteArrayInputStream(("id: p1\nversion: 1.0.0\nmainClass: x.Y\n"
|
||||
+ schema).getBytes(StandardCharsets.UTF_8)));
|
||||
PluginDescriptor d2 = PluginDescriptor.read(new ByteArrayInputStream(("id: p2\nversion: 1.0.0\nmainClass: x.Y\n"
|
||||
+ schema).getBytes(StandardCharsets.UTF_8)));
|
||||
PluginOwnership owners = PluginOwnership.of(new PluginRuntime(
|
||||
List.of(new LoadedPlugin(d1, root.resolve("p1")), new LoadedPlugin(d2, root.resolve("p2"))), loader,
|
||||
Map.of(), false));
|
||||
mx.quest.runway.storage.StorageProperties props = new mx.quest.runway.storage.StorageProperties();
|
||||
props.setExternal(root.resolve("external-saved"));
|
||||
mx.quest.runway.storage.StorageLayout layout = mx.quest.runway.storage.StorageLayout.from(props);
|
||||
java.nio.file.Path p1File = layout.pluginSettingsFile("p1");
|
||||
java.nio.file.Files.createDirectories(p1File.getParent());
|
||||
java.nio.file.Files.writeString(p1File, "runway:\n plugin:\n p1:\n host: saved.example\n"
|
||||
+ " apiKey: p1-saved\n");
|
||||
org.springframework.mock.env.MockEnvironment env = new org.springframework.mock.env.MockEnvironment()
|
||||
.withProperty("runway.plugin.p1.host", "running.example");
|
||||
PluginScopedServices scoped = new PluginScopedServices(owners,
|
||||
new SitePluginConfigService(layout, null, event -> { }), null, layout, 1024, env);
|
||||
|
||||
PluginSettings p1 = scoped.pluginSettings(pointIn(head("p1", "k", false).getClass(), "key"));
|
||||
PluginSettings p2 = scoped.pluginSettings(pointIn(head("p2", "k", false).getClass(), "key"));
|
||||
assertEquals(Optional.of("saved.example"), p1.savedApp("host"), "the file as saved, not the environment");
|
||||
assertEquals(Optional.of("p1-saved"), p1.savedApp("apiKey"), "its own saved secret (plaintext as written)");
|
||||
assertEquals(Optional.empty(), p2.savedApp("host"), "p2 has no file of its own: p1's is never read for it");
|
||||
assertEquals(Optional.empty(), p2.savedApp("apiKey"));
|
||||
|
||||
java.nio.file.Files.writeString(p1File, "runway:\n plugin:\n p1:\n host: [unclosed\n");
|
||||
assertEquals(Optional.empty(), p1.savedApp("host"), "an unparsable file answers empty, never throws");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInjectionPointInAPluginsOwnJarBindsToThatPlugin() throws Exception {
|
||||
assumeTrue(PluginFixture.compilerAvailable());
|
||||
|
||||
@@ -273,4 +273,90 @@ class PluginSettingsImplTest {
|
||||
assertEquals(Optional.empty(), bare.appSecret("password"),
|
||||
"an implementation written against SPI 1.0 still compiles and answers empty");
|
||||
}
|
||||
|
||||
// ---- saved app-level settings (SPI 1.2.0) -------------------------------------------------------
|
||||
|
||||
private void saveAppFile(String plugin, String yaml) throws IOException {
|
||||
Path file = layout.pluginSettingsFile(plugin);
|
||||
Files.createDirectories(file.getParent());
|
||||
Files.writeString(file, yaml);
|
||||
}
|
||||
|
||||
/** As the kernel wires it: the saved values come from the plugin's own settings file. */
|
||||
private PluginSettingsImpl withSavedApp(String plugin) {
|
||||
return new PluginSettingsImpl(plugin, SCHEMA, APP_SCHEMA, environment::get,
|
||||
key -> mx.quest.runway.plugin.PluginSettingsService.saved(layout, plugin).get(key), config, cipher);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSavedAppSettingIsReadFromTheFileNotFromTheRunningEnvironment() throws IOException {
|
||||
environment.put("runway.plugin.database.host", "running.example");
|
||||
environment.put("runway.plugin.database.password", "running-secret");
|
||||
saveAppFile("database", "runway:\n plugin:\n database:\n host: saved.example\n password: "
|
||||
+ cipher.encrypt("saved-secret") + "\n");
|
||||
PluginSettingsImpl settings = withSavedApp("database");
|
||||
assertEquals(Optional.of("saved.example"), settings.savedApp("host"));
|
||||
assertEquals(Optional.of("saved-secret"), settings.savedApp("password"), "a saved secret, decrypted");
|
||||
assertEquals(Optional.of("running-secret"), settings.appSecret("password"), "appSecret keeps the running one");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSaveAfterStartupIsSeenOnTheNextCall() throws IOException {
|
||||
PluginSettingsImpl settings = withSavedApp("database");
|
||||
saveAppFile("database", "runway:\n plugin:\n database:\n host: first.example\n");
|
||||
assertEquals(Optional.of("first.example"), settings.savedApp("host"));
|
||||
saveAppFile("database", "runway:\n plugin:\n database:\n host: second.example\n");
|
||||
assertEquals(Optional.of("second.example"), settings.savedApp("host"), "read from the file each time");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnsavedOrBlankSettingFallsBackToTheDeclaredDefaultExceptASecret() throws IOException {
|
||||
PluginSettingsImpl settings = withSavedApp("database");
|
||||
assertEquals(Optional.of("localhost"), settings.savedApp("host"), "no file: the declared default");
|
||||
assertEquals(Optional.empty(), settings.savedApp("password"), "a secret has no default");
|
||||
saveAppFile("database", "runway:\n plugin:\n database:\n host: ' '\n password: ''\n");
|
||||
assertEquals(Optional.of("localhost"), settings.savedApp("host"));
|
||||
assertEquals(Optional.empty(), settings.savedApp("password"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyTheCallersOwnDeclaredAppKeysAreRead() throws IOException {
|
||||
saveAppFile("database", "runway:\n plugin:\n database:\n undeclared: x\n smtpPassword: y\n");
|
||||
saveAppFile("other", "runway:\n plugin:\n other:\n host: not-mine\n");
|
||||
PluginSettingsImpl settings = withSavedApp("database");
|
||||
assertEquals(Optional.empty(), settings.savedApp("undeclared"));
|
||||
assertEquals(Optional.empty(), settings.savedApp("smtpPassword"), "declared only in siteSettings:");
|
||||
assertEquals(Optional.empty(), settings.savedApp(null));
|
||||
assertEquals(Optional.of("localhost"), settings.savedApp("host"), "another plugin's file is never read");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSavedSecretThatCannotBeDecryptedIsEmptyAndAPlaintextOneIsReturned() throws IOException {
|
||||
PluginSettingsImpl settings = withSavedApp("database");
|
||||
saveAppFile("database", "runway:\n plugin:\n database:\n password: hand-written\n");
|
||||
assertEquals(Optional.of("hand-written"), settings.savedApp("password"));
|
||||
saveAppFile("database", "runway:\n plugin:\n database:\n password: " + cipher.encrypt("s")
|
||||
+ "\n");
|
||||
cipherBroken = true;
|
||||
assertEquals(Optional.empty(), settings.savedApp("password"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theOlderConstructorsAndTheSpiDefaultHaveNoSavedValues() {
|
||||
environment.put("runway.plugin.database.host", "running.example");
|
||||
assertEquals(Optional.of("localhost"), withApp("database").savedApp("host"),
|
||||
"no saved-values lookup: only the declared default");
|
||||
mx.quest.runway.spi.service.PluginSettings bare = new mx.quest.runway.spi.service.PluginSettings() {
|
||||
@Override
|
||||
public SettingsView site(String siteId) {
|
||||
return new SettingsView(java.util.Map.of());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> secret(String siteId, String key) {
|
||||
return Optional.empty();
|
||||
}
|
||||
};
|
||||
assertEquals(Optional.empty(), bare.savedApp("host"), "an implementation written before SPI 1.2.0");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user