2023-03-02 20:04:23 +01:00
|
|
|
|
using AutoFixture;
|
2023-03-07 19:46:52 +01:00
|
|
|
|
using Bit.Core.Context;
|
2023-03-02 20:04:23 +01:00
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
using Bit.Core.Settings;
|
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
2024-01-18 15:47:34 +01:00
|
|
|
|
using LaunchDarkly.Sdk.Server.Interfaces;
|
2023-03-07 19:46:52 +01:00
|
|
|
|
using NSubstitute;
|
2023-03-02 20:04:23 +01:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Test.Services;
|
|
|
|
|
|
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
public class LaunchDarklyFeatureServiceTests
|
|
|
|
|
{
|
2023-12-20 20:47:14 +01:00
|
|
|
|
private const string _fakeFeatureKey = "somekey";
|
|
|
|
|
private const string _fakeSdkKey = "somesdkkey";
|
2023-11-13 20:46:39 +01:00
|
|
|
|
|
|
|
|
|
private static SutProvider<LaunchDarklyFeatureService> GetSutProvider(IGlobalSettings globalSettings)
|
2023-03-02 20:04:23 +01:00
|
|
|
|
{
|
2023-11-13 20:46:39 +01:00
|
|
|
|
globalSettings.ProjectName = "LaunchDarkly Tests";
|
|
|
|
|
|
2024-01-18 15:47:34 +01:00
|
|
|
|
var currentContext = Substitute.For<ICurrentContext>();
|
|
|
|
|
currentContext.UserId.Returns(Guid.NewGuid());
|
|
|
|
|
|
|
|
|
|
var client = Substitute.For<ILdClient>();
|
|
|
|
|
|
2023-03-02 20:04:23 +01:00
|
|
|
|
var fixture = new Fixture();
|
|
|
|
|
return new SutProvider<LaunchDarklyFeatureService>(fixture)
|
2023-11-13 20:46:39 +01:00
|
|
|
|
.SetDependency(globalSettings)
|
2024-01-18 15:47:34 +01:00
|
|
|
|
.SetDependency(currentContext)
|
|
|
|
|
.SetDependency(client)
|
2023-03-02 20:04:23 +01:00
|
|
|
|
.Create();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 19:46:52 +01:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public void DefaultFeatureValue_WhenSelfHost(string key)
|
|
|
|
|
{
|
2023-11-13 20:46:39 +01:00
|
|
|
|
var sutProvider = GetSutProvider(new Settings.GlobalSettings { SelfHosted = true });
|
2023-03-07 19:46:52 +01:00
|
|
|
|
|
2024-01-18 15:47:34 +01:00
|
|
|
|
Assert.False(sutProvider.Sut.IsEnabled(key));
|
2023-03-07 19:46:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void DefaultFeatureValue_NoSdkKey()
|
|
|
|
|
{
|
2023-11-13 20:46:39 +01:00
|
|
|
|
var sutProvider = GetSutProvider(new Settings.GlobalSettings());
|
2023-03-07 19:46:52 +01:00
|
|
|
|
|
2024-01-18 15:47:34 +01:00
|
|
|
|
Assert.False(sutProvider.Sut.IsEnabled(_fakeFeatureKey));
|
2023-03-07 19:46:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact(Skip = "For local development")]
|
|
|
|
|
public void FeatureValue_Boolean()
|
|
|
|
|
{
|
2023-12-20 20:47:14 +01:00
|
|
|
|
var settings = new Settings.GlobalSettings { LaunchDarkly = { SdkKey = _fakeSdkKey } };
|
2023-03-07 19:46:52 +01:00
|
|
|
|
|
|
|
|
|
var sutProvider = GetSutProvider(settings);
|
|
|
|
|
|
2024-01-18 15:47:34 +01:00
|
|
|
|
Assert.False(sutProvider.Sut.IsEnabled(_fakeFeatureKey));
|
2023-03-07 19:46:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact(Skip = "For local development")]
|
|
|
|
|
public void FeatureValue_Int()
|
|
|
|
|
{
|
2023-12-20 20:47:14 +01:00
|
|
|
|
var settings = new Settings.GlobalSettings { LaunchDarkly = { SdkKey = _fakeSdkKey } };
|
2023-03-07 19:46:52 +01:00
|
|
|
|
|
|
|
|
|
var sutProvider = GetSutProvider(settings);
|
|
|
|
|
|
2024-01-18 15:47:34 +01:00
|
|
|
|
Assert.Equal(0, sutProvider.Sut.GetIntVariation(_fakeFeatureKey));
|
2023-03-07 19:46:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact(Skip = "For local development")]
|
|
|
|
|
public void FeatureValue_String()
|
|
|
|
|
{
|
2023-12-20 20:47:14 +01:00
|
|
|
|
var settings = new Settings.GlobalSettings { LaunchDarkly = { SdkKey = _fakeSdkKey } };
|
2023-03-07 19:46:52 +01:00
|
|
|
|
|
|
|
|
|
var sutProvider = GetSutProvider(settings);
|
|
|
|
|
|
2024-01-18 15:47:34 +01:00
|
|
|
|
Assert.Null(sutProvider.Sut.GetStringVariation(_fakeFeatureKey));
|
2023-03-07 19:46:52 +01:00
|
|
|
|
}
|
2023-03-10 14:11:11 +01:00
|
|
|
|
|
|
|
|
|
[Fact(Skip = "For local development")]
|
|
|
|
|
public void GetAll()
|
|
|
|
|
{
|
2023-11-13 20:46:39 +01:00
|
|
|
|
var sutProvider = GetSutProvider(new Settings.GlobalSettings());
|
2023-03-10 14:11:11 +01:00
|
|
|
|
|
2024-01-18 15:47:34 +01:00
|
|
|
|
var results = sutProvider.Sut.GetAll();
|
2023-03-10 14:11:11 +01:00
|
|
|
|
|
|
|
|
|
Assert.NotNull(results);
|
|
|
|
|
Assert.NotEmpty(results);
|
|
|
|
|
}
|
2023-03-02 20:04:23 +01:00
|
|
|
|
}
|