using Anubis.Demo; using Anubis.Platform; namespace Anubis.Tests; /// Startup configuration of the container, and what is left outside it (issue #7, SPEC 15.1, ADR 0022). [TestClass] public sealed class PlatformTests { private static readonly string _key = new('k', 40); private static Dictionary Env(params (string Key, string? Value)[] values) { var env = new Dictionary { ["ANUBIS_DB_ENGINE"] = "sqlite", ["ANUBIS_SECRET_KEY"] = _key, ["ANUBIS_ADMIN_PASSWORD"] = "initial-pass-1", }; foreach (var (key, value) in values) { env[key] = value; } return env; } [TestMethod] [TestCategory(Category.Unit)] public void DockerWithSqliteNeedsNothingElse() { var config = DockerPlatform.Load(Env(), "/data"); Assert.IsTrue(config.IsComplete, string.Join("; ", config.Problems.Select(p => p.What))); Assert.AreEqual(DbEngine.Sqlite, config.Db!.Engine); Assert.AreEqual(Path.Combine("/data", "db", "anubis.sqlite3"), config.Db.SqlitePath); Assert.AreEqual(8787, config.Port); Assert.AreEqual(99, config.Puid); Assert.AreEqual(100, config.Pgid); Assert.IsFalse(config.AllowLive); Assert.AreEqual("Europe/Rome", config.TimeZone.Id); Assert.IsTrue(config.SecretKey.HasKey); Assert.HasCount(32, config.SecretKey.GetOrCreateKey()); } [TestMethod] [TestCategory(Category.Unit)] public void DockerPostgresUsesDefaultsAndNeverShowsThePassword() { var config = DockerPlatform.Load(Env(("ANUBIS_DB_ENGINE", "postgres"), ("ANUBIS_DB_HOST", "192.168.30.23"), ("ANUBIS_DB_PASSWORD", "pg-password-xyz"))); Assert.IsTrue(config.IsComplete); Assert.AreEqual(5432, config.Db!.Port); Assert.AreEqual("anubis", config.Db.Database); Assert.AreEqual("anubis", config.Db.User); Assert.IsFalse(config.Db.Describe().Contains("pg-password-xyz", StringComparison.Ordinal)); Assert.IsFalse(config.Db.ToString().Contains("pg-password-xyz", StringComparison.Ordinal)); } [TestMethod] [TestCategory(Category.Unit)] public void MissingOrWrongValuesBecomeProblemsAndNoDatabaseIsChosen() { var empty = DockerPlatform.Load(new Dictionary()); Assert.IsNull(empty.Db, "senza ANUBIS_DB_ENGINE nessun database viene scelto al posto dell'utente"); CollectionAssert.IsSubsetOf(new[] { "ANUBIS_DB_ENGINE", "ANUBIS_SECRET_KEY", "ANUBIS_ADMIN_PASSWORD" }, empty.Problems.Select(p => p.Setting).ToList()); Assert.IsTrue(empty.Problems.All(p => p.Remedy.Length > 0)); var wrong = DockerPlatform.Load(Env(("ANUBIS_DB_ENGINE", "mysql"))); Assert.IsNull(wrong.Db); Assert.IsFalse(wrong.IsComplete); var postgresWithoutHost = DockerPlatform.Load(Env(("ANUBIS_DB_ENGINE", "postgres"))); Assert.IsNull(postgresWithoutHost.Db, "PostgreSQL incompleto non ripiega su SQLite"); CollectionAssert.IsSubsetOf(new[] { "ANUBIS_DB_HOST", "ANUBIS_DB_PASSWORD" }, postgresWithoutHost.Problems.Select(p => p.Setting).ToList()); var shortKey = DockerPlatform.Load(Env(("ANUBIS_SECRET_KEY", "short"))); Assert.IsTrue(shortKey.Problems.Any(p => p.Setting == "ANUBIS_SECRET_KEY")); var badLive = DockerPlatform.Load(Env(("ANUBIS_ALLOW_LIVE", "yes"))); Assert.IsFalse(badLive.AllowLive); Assert.IsTrue(badLive.Problems.Any(p => p.Setting == "ANUBIS_ALLOW_LIVE")); var badZone = DockerPlatform.Load(Env(("TZ", "Mars/Olympus"))); Assert.IsTrue(badZone.Problems.Any(p => p.Setting == "TZ")); } [TestMethod] [TestCategory(Category.Unit)] public void OptionalKeysAreReadWithTheDemoSuffix() { var config = DockerPlatform.Load(Env(("OKX_API_KEY_DEMO", "demo-key-123456"), ("TELEGRAM_CHAT_ID", "12345"))); Assert.AreEqual("demo-key-123456", config.EnvironmentKeys["OKX_API_KEY_DEMO"]); Assert.AreEqual("12345", config.EnvironmentKeys["TELEGRAM_CHAT_ID"]); Assert.IsFalse(config.EnvironmentKeys.ContainsKey("OKX_API_KEY")); } [TestMethod] [TestCategory(Category.Unit)] public void TheContainerFindsItsDataAndOutsideItTheFolderIsGivenByHand() { // The rule of the server and of the guard alike (ADR 0022): no ANUBIS_PLATFORM, no user folder. var container = new Dictionary { ["DOTNET_RUNNING_IN_CONTAINER"] = "true" }; Assert.AreEqual("/data", DataDirectory.Locate(null, container)); Assert.AreEqual(Path.GetFullPath("dati-di-prova"), DataDirectory.Locate("dati-di-prova", container)); Assert.AreEqual(Path.GetFullPath("dati-di-prova"), DataDirectory.Locate("dati-di-prova", new Dictionary())); var outside = new Dictionary { ["ANUBIS_PLATFORM"] = "docker" }; var refused = Assert.Throws(() => DataDirectory.Locate(null, outside)); Assert.Contains("--data-dir", refused.Message); Assert.Contains("Fuori da un container", refused.Message); } [TestMethod] [TestCategory(Category.Integration)] public void TheDemoStartsFromAnEmptyFolderAndCreatesItsOwnKey() { var folder = TempFolder.Create("demo-key"); try { var data = new DataDirectory(folder); var first = DemoMode.Config(data); Assert.IsTrue(first.IsComplete, string.Join("; ", first.Problems.Select(p => p.What))); Assert.AreEqual(data.SqliteFile, first.Db!.SqlitePath); Assert.IsFalse(first.AllowLive); Assert.IsFalse(first.SecretKey.HasKey, "An empty folder has no key yet."); var key = first.SecretKey.GetOrCreateKey(); Assert.HasCount(32, key); Assert.IsNotEmpty(Directory.EnumerateFiles(Path.Combine(folder, "keys")).ToList(), "The key lives in a file of the demo folder."); CollectionAssert.AreEqual(key, DemoMode.Config(data).SecretKey.GetOrCreateKey(), "The same key at the next start."); Assert.DoesNotContain("Windows", first.SecretKey.Description); } finally { TempFolder.Delete(folder); } } [TestMethod] [TestCategory(Category.Unit)] public void NothingOfTheDesktopIsLeftInTheProduct() { // ADR 0022: the desktop application, its configuration file and its guided first run are gone. string[] words = ["config.toml", "primo-avvio", "PlatformKind", "ANUBIS_PLATFORM", "DesktopPlatform", "ProtectedData"]; var found = Repo.Files() .Where(file => Repo.Relative(file) is var path && (path.StartsWith("src/", StringComparison.Ordinal) || path.StartsWith("tools/", StringComparison.Ordinal) || path.StartsWith("deploy/", StringComparison.Ordinal))) .Where(file => file.Extension is not (".png" or ".ico" or ".ttf" or ".onnx")) .SelectMany(file => words.Where(word => File.ReadAllText(file.FullName).Contains(word, StringComparison.Ordinal)) .Select(word => Repo.Relative(file) + ": " + word)) .ToList(); Assert.IsEmpty(found, string.Join("; ", found)); } [TestMethod] [TestCategory(Category.Unit)] public void TheSolutionHasFiveProjects() { var solution = System.Xml.Linq.XDocument.Load(Path.Combine(Repo.Root.FullName, "Anubis.slnx")); var projects = solution.Descendants("Project").Select(p => (string)p.Attribute("Path")!).Order(StringComparer.Ordinal).ToList(); CollectionAssert.AreEqual(new[] { "src/Anubis.Guard/Anubis.Guard.csproj", "src/Anubis.Server/Anubis.Server.csproj", "src/Anubis/Anubis.csproj", "tests/Anubis.Tests/Anubis.Tests.csproj", "tools/Anubis.Tasks/Anubis.Tasks.csproj", }, projects); } [TestMethod] [TestCategory(Category.Unit)] public void DataFolderHasTheLayoutOfSpec152() { var folder = TempFolder.Create("layout"); try { var data = new DataDirectory(Path.Combine(folder, "data")); data.Ensure(); foreach (var name in new[] { "db", "models", "research", "import", "backups", "logs" }) { Assert.IsTrue(Directory.Exists(Path.Combine(data.Root, name)), name); } Assert.AreEqual(Path.Combine(data.Root, "installation.json"), data.InstallationFile); } finally { TempFolder.Delete(folder); } } }