Skip to content

Commit

Permalink
feat: Update Config validation (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
cynicaljoy authored Nov 14, 2024
1 parent 2ba0716 commit fb3220f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
20 changes: 19 additions & 1 deletion Fauna.Test/Configuration.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ public void ConstructorWorksFine()
Assert.IsTrue(b.DisposeHttpClient);
}

[Test]
public void ConstructorWithSecretVar()
{
string? currentVal = Environment.GetEnvironmentVariable("FAUNA_ENDPOINT");
Environment.SetEnvironmentVariable("FAUNA_SECRET", null);

Configuration config = new Configuration { Secret = "secret" };
Assert.AreEqual("secret", config.Secret);

Environment.SetEnvironmentVariable("FAUNA_SECRET", currentVal);

Assert.DoesNotThrow(() => config.Validate());
}

[Test]
public void ConstructorWithEndpointEnvVar()
{
Expand All @@ -28,6 +42,8 @@ public void ConstructorWithEndpointEnvVar()
Assert.AreEqual("http://localhost:8443/", config.Endpoint.ToString());

Environment.SetEnvironmentVariable("FAUNA_ENDPOINT", currentVal);

Assert.DoesNotThrow(() => config.Validate());
}

[Test]
Expand All @@ -53,6 +69,7 @@ public void ConstructorUsesEnvVar()
Assert.AreEqual("secret", b.Secret);
Assert.AreEqual(Endpoints.Default, b.Endpoint);
Assert.IsTrue(b.DisposeHttpClient);
Assert.DoesNotThrow(() => b.Validate());
}

[Test]
Expand All @@ -63,7 +80,7 @@ public void ConstructorThrowsWithNullSecret()
{
Environment.SetEnvironmentVariable("FAUNA_SECRET", null);
var b = new Configuration();

b.Validate();
});
Environment.SetEnvironmentVariable("FAUNA_SECRET", currentVal);
}
Expand All @@ -76,6 +93,7 @@ public void ConstructorWithHttpClient()
Assert.AreEqual("secret", b.Secret);
Assert.AreEqual(Endpoints.Default, b.Endpoint);
Assert.IsFalse(b.DisposeHttpClient);
Assert.DoesNotThrow(() => b.Validate());
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion Fauna.Test/Connection.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void OneTimeSetup()

var httpClient = new HttpClient(_handlerMock.Object) { BaseAddress = new Uri(TestBaseUri), };

Configuration configuration = new("", httpClient)
Configuration configuration = new("secret", httpClient)
{
Endpoint = new Uri(TestBaseUri),
RetryConfiguration = new RetryConfiguration(MaxRetries, TimeSpan.FromMilliseconds(20))
Expand Down
1 change: 1 addition & 0 deletions Fauna/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public Client(string secret) :
/// <param name="config">The configuration settings for the client.</param>
public Client(Configuration config)
{
config.Validate();
_config = config;
StatsCollector = config.StatsCollector;
_connection = new Connection(config);
Expand Down
14 changes: 9 additions & 5 deletions Fauna/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ public record class Configuration
/// <param name="logger">A logger. If null, a default logger is used.</param>
public Configuration(string secret = "", HttpClient? httpClient = null, ILogger? logger = null)
{
if (string.IsNullOrEmpty(secret) && string.IsNullOrEmpty(Secret))
{
throw new ArgumentNullException(nameof(Secret), "Need to set FAUNA_SECRET environment variable or pass a secret as a parameter when creating the Client.");
}

if (!string.IsNullOrEmpty(secret))
{
Secret = secret;
Expand All @@ -86,4 +81,13 @@ public Configuration(string secret = "", HttpClient? httpClient = null, ILogger?
Logger.Initialize(logger);
}
}

internal void Validate()
{
if (string.IsNullOrEmpty(Secret))
throw new ArgumentNullException(
nameof(Secret),
"Need to set FAUNA_SECRET environment variable or pass a secret as a parameter when creating the Client."
);
}
}

0 comments on commit fb3220f

Please sign in to comment.