Skip to content

Commit 00aafa2

Browse files
fix!: throw exception on startup when using local evaluation without server key (#141)
* Throw exception when trying to instantiate client in local eval without server key * Fix test muddle * Update fixture api key
1 parent 4f760b9 commit 00aafa2

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

Flagsmith.Client.Test/Fixtures.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Flagsmith.FlagsmithClientTest
88
{
99
internal class Fixtures
1010
{
11-
public static string ApiKey => "test_key";
11+
public static string ApiKey => "ser.test_key";
1212
public static string ApiUrl => "http://test_url/";
1313
public static AnalyticsProcessorTest GetAnalyticalProcessorTest() => new(new HttpClient(), ApiKey, ApiUrl);
1414
public static JObject JsonObject = JObject.Parse(@"{

Flagsmith.Client.Test/FlagsmithTest.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -523,6 +523,24 @@ public void TestCannotCreateFlagsmithClientInRemoteEvaluationWithoutAPIKey()
523523
Assert.Equal("ValueError: environmentKey is required", exception.Message);
524524
}
525525

526+
[Fact]
527+
public void TestCannotCreateFlagsmithClientInLocalEvaluationWithoutServerAPIKey()
528+
{
529+
// When
530+
Action createFlagsmith = () => new FlagsmithClient(
531+
environmentKey: "foobar",
532+
enableClientSideEvaluation: true
533+
);
534+
535+
// Then
536+
var exception = Assert.Throws<Exception>(() => createFlagsmith());
537+
Assert.Equal
538+
(
539+
"ValueError: In order to use local evaluation, please generate a server key in the environment settings page.",
540+
exception.Message
541+
);
542+
}
543+
526544
[Fact]
527545
/// <summary>
528546
/// Test that analytics data is consistent with concurrent calls to get flags.
@@ -554,7 +572,7 @@ public async Task TestAnalyticsDataConsistencyWithConcurrentCallsToGetFlags()
554572
featuresDictionary.TryAdd($"Feature_{i}", 0);
555573
}
556574

557-
// When
575+
// When
558576
var tasks = new Task[numberOfThreads];
559577

560578
// Create numberOfThreads threads.
@@ -566,13 +584,13 @@ public async Task TestAnalyticsDataConsistencyWithConcurrentCallsToGetFlags()
566584
// Prepare an array of feature names of length callsPerThread.
567585
for (int j = 0; j < callsPerThread; j++)
568586
{
569-
// The feature names are randomly selected from the featuresDictionary and added to the
570-
// list of features, which represents the features that have been evaluated.
587+
// The feature names are randomly selected from the featuresDictionary and added to the
588+
// list of features, which represents the features that have been evaluated.
571589
string featureName = $"Feature_{new Random().Next(1, featuresDictionary.Count + 1)}";
572590
features[j] = featureName;
573591

574592
// The relevant key in the featuresDictionary is incremented to simulate an evaluation
575-
// to track for that feature.
593+
// to track for that feature.
576594
featuresDictionary[featureName]++;
577595
}
578596

Flagsmith.FlagsmithClient/FlagsmithClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ public FlagsmithClient(
150150
{
151151
if (!EnvironmentKey!.StartsWith("ser."))
152152
{
153-
Console.WriteLine(
154-
"In order to use local evaluation, please generate a server key in the environment settings page."
153+
throw new Exception(
154+
"ValueError: In order to use local evaluation, please generate a server key in the environment settings page."
155155
);
156156
}
157157

@@ -399,4 +399,4 @@ private IFlags GetIdentityFlagsFromDocument(string identifier, List<ITrait>? tra
399399

400400
~FlagsmithClient() => _pollingManager?.StopPoll();
401401
}
402-
}
402+
}

0 commit comments

Comments
 (0)