-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support a JSON environment config key
- Loading branch information
1 parent
417d3b7
commit 5cb6947
Showing
18 changed files
with
191 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
Presence.Posting.Lib.Tests/EnvironmentConfigReaderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using System.Collections; | ||
using System.Text.Json; | ||
using Presence.Posting.Lib.Config; | ||
using Presence.Posting.Lib.Constants; | ||
using Presence.SocialFormat.Lib.Networks; | ||
|
||
namespace Presence.Posting.Lib.Tests; | ||
|
||
[TestClass] | ||
public class EnvironmentConfigReaderTests | ||
{ | ||
|
||
[TestMethod] | ||
[TestCategory("Unit")] | ||
public void EnvironmentConfigReader_ExtractsIgnoringCase_MultipleAccounts() | ||
{ | ||
var environments = new List<IDictionary>() | ||
{ | ||
new Dictionary<string,string>() | ||
{ | ||
{ ConfigKeys.ACCOUNTS_ENV_KEY, "TEST0,TEST1" }, | ||
{ "TEST0_CONSOLE_PRINTPREFIX", "printprefix:test0" }, | ||
{ "TEST1_AT_ACCOUNTNAME", "account:test1" }, | ||
{ "TEST1_AT_APPPASSWORD", "password:test1" } | ||
}, | ||
new Dictionary<string,string>() | ||
{ | ||
{ ConfigKeys.ACCOUNTS_ENV_KEY, "TEST0,TEST1" }, | ||
{ "TEST0_console_printprefix", "printprefix:test0" }, | ||
{ "TEST1_at_accountname", "account:test1" }, | ||
{ "TEST1_at_apppassword", "password:test1" } | ||
} | ||
}; | ||
|
||
foreach (var env in environments) | ||
{ | ||
var reader = new EnvironmentConfigReader(env); | ||
Assert.AreEqual(2, reader.Count); | ||
Assert.IsTrue(reader.ContainsKey("TEST0")); | ||
Assert.IsTrue(reader.ContainsKey("TEST1")); | ||
Assert.AreEqual("printprefix:test0", reader["TEST0"][SocialNetwork.Console][NetworkCredentialType.PrintPrefix]); | ||
Assert.AreEqual("account:test1", reader["TEST1"][SocialNetwork.AT][NetworkCredentialType.AccountName]); | ||
Assert.AreEqual("password:test1", reader["TEST1"][SocialNetwork.AT][NetworkCredentialType.AppPassword]); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
[TestCategory("Unit")] | ||
public void EnvironmentConfigReader_ExtractsOnly_AccountsInTheAccountsVariable() | ||
{ | ||
var env = new Dictionary<string, string>() | ||
{ | ||
{ ConfigKeys.ACCOUNTS_ENV_KEY, "TEST0,TEST1" }, | ||
{ "TEST0_CONSOLE_PRINTPREFIX", "printprefix:test0" }, | ||
{ "TEST1_AT_ACCOUNTNAME", "account:test1" }, | ||
{ "TEST1_AT_APPPASSWORD", "password:test1" }, | ||
{ "TEST2_AT_ACCOUNTNAME", "account:test2" }, | ||
{ "TEST2_AT_APPPASSWORD", "password:test2" }, | ||
}; | ||
var reader = new EnvironmentConfigReader(env); | ||
Assert.AreEqual(2, reader.Count); | ||
Assert.IsTrue(reader.ContainsKey("TEST0")); | ||
Assert.IsTrue(reader.ContainsKey("TEST1")); | ||
Assert.IsFalse(reader.ContainsKey("TEST2")); | ||
} | ||
|
||
[TestMethod] | ||
[TestCategory("Unit")] | ||
public void EnvironmentConfigReader_CanExtract_JsonConfiguration() | ||
{ | ||
var env = new Dictionary<string, string>() | ||
{ | ||
{ ConfigKeys.JSON_DATA_ENV_KEY, @" | ||
{ | ||
""PRESENCE_ACCOUNTS"": ""TEST0,TEST1"", | ||
""TEST0_CONSOLE_PRINTPREFIX"": ""printprefix:test0"", | ||
""TEST1_AT_ACCOUNTNAME"": ""account:test1"", | ||
""TEST1_AT_APPPASSWORD"": ""password:test1"" | ||
}" } | ||
}; | ||
var reader = new EnvironmentConfigReader(env); | ||
Assert.AreEqual(2, reader.Count, JsonSerializer.Serialize(reader)); | ||
Assert.IsTrue(reader.ContainsKey("TEST0")); | ||
Assert.IsTrue(reader.ContainsKey("TEST1")); | ||
Assert.AreEqual("printprefix:test0", reader["TEST0"][SocialNetwork.Console][NetworkCredentialType.PrintPrefix]); | ||
Assert.AreEqual("account:test1", reader["TEST1"][SocialNetwork.AT][NetworkCredentialType.AccountName]); | ||
Assert.AreEqual("password:test1", reader["TEST1"][SocialNetwork.AT][NetworkCredentialType.AppPassword]); | ||
} | ||
|
||
[TestMethod] | ||
[TestCategory("Unit")] | ||
public void EnvironmentConfigReader_JsonConfiguration_Supersedes_OtherKeys() | ||
{ | ||
var env = new Dictionary<string, string>() | ||
{ | ||
{ | ||
ConfigKeys.JSON_DATA_ENV_KEY, @" | ||
{ | ||
""PRESENCE_ACCOUNTS"": ""TEST0,TEST1"", | ||
""TEST0_CONSOLE_PRINTPREFIX"": ""json:printprefix:test0"", | ||
""TEST1_AT_ACCOUNTNAME"": ""json:account:test1"", | ||
""TEST1_AT_APPPASSWORD"": ""json:password:test1"" | ||
}" | ||
}, | ||
{ "PRESENCE_ACCOUNTS", "TEST0" }, | ||
{ "TEST0_CONSOLE_PRINTPREFIX", "ignored:printprefix:test0" }, | ||
{ "TEST1_AT_ACCOUNTNAME", "ignored:account:test1" }, | ||
{ "TEST1_AT_APPPASSWORD", "ignored:password:test1" } | ||
}; | ||
var reader = new EnvironmentConfigReader(env); | ||
Assert.AreEqual(2, reader.Count, JsonSerializer.Serialize(reader)); | ||
Assert.IsTrue(reader.ContainsKey("TEST0")); | ||
Assert.IsTrue(reader.ContainsKey("TEST1")); | ||
Assert.AreEqual("json:printprefix:test0", reader["TEST0"][SocialNetwork.Console][NetworkCredentialType.PrintPrefix]); | ||
Assert.AreEqual("json:account:test1", reader["TEST1"][SocialNetwork.AT][NetworkCredentialType.AccountName]); | ||
Assert.AreEqual("json:password:test1", reader["TEST1"][SocialNetwork.AT][NetworkCredentialType.AppPassword]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Text.Json; | ||
|
||
namespace Presence.Posting.Lib.Config; | ||
|
||
public class JsonConfigReader | ||
{ | ||
public static IDictionary<string, string?>? ReadJsonConfig(string? json) => | ||
string.IsNullOrWhiteSpace(json) | ||
? null | ||
: JsonSerializer.Deserialize<Dictionary<string, string?>>(json); | ||
// ?.Where(kv => !string.IsNullOrWhiteSpace(kv.Value)) | ||
// .ToDictionary(kv => kv.Key, kv => kv.Value!); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Presence.Posting.Lib.Constants; | ||
|
||
public static class ConfigKeys | ||
{ | ||
public const string JSON_DATA_ENV_KEY = "PRESENCE_CONFIG_JSON"; | ||
public const string ACCOUNTS_ENV_KEY = "PRESENCE_ACCOUNTS"; | ||
|
||
} |
Oops, something went wrong.