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;
|
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-11-13 20:46:39 +01:00
|
|
|
|
private const string _fakeKey = "somekey";
|
|
|
|
|
private const string _fakeValue = "somevalue";
|
|
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
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)
|
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
|
|
|
|
|
|
|
|
|
var currentContext = Substitute.For<ICurrentContext>();
|
|
|
|
|
currentContext.UserId.Returns(Guid.NewGuid());
|
|
|
|
|
|
|
|
|
|
Assert.False(sutProvider.Sut.IsEnabled(key, currentContext));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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
|
|
|
|
|
|
|
|
|
var currentContext = Substitute.For<ICurrentContext>();
|
|
|
|
|
currentContext.UserId.Returns(Guid.NewGuid());
|
|
|
|
|
|
2023-11-13 20:46:39 +01:00
|
|
|
|
Assert.False(sutProvider.Sut.IsEnabled(_fakeKey, currentContext));
|
2023-03-07 19:46:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact(Skip = "For local development")]
|
|
|
|
|
public void FeatureValue_Boolean()
|
|
|
|
|
{
|
2023-11-13 20:46:39 +01:00
|
|
|
|
var settings = new Settings.GlobalSettings { LaunchDarkly = { SdkKey = _fakeValue } };
|
2023-03-07 19:46:52 +01:00
|
|
|
|
|
|
|
|
|
var sutProvider = GetSutProvider(settings);
|
|
|
|
|
|
|
|
|
|
var currentContext = Substitute.For<ICurrentContext>();
|
|
|
|
|
currentContext.UserId.Returns(Guid.NewGuid());
|
|
|
|
|
|
2023-11-13 20:46:39 +01:00
|
|
|
|
Assert.False(sutProvider.Sut.IsEnabled(_fakeKey, currentContext));
|
2023-03-07 19:46:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact(Skip = "For local development")]
|
|
|
|
|
public void FeatureValue_Int()
|
|
|
|
|
{
|
2023-11-13 20:46:39 +01:00
|
|
|
|
var settings = new Settings.GlobalSettings { LaunchDarkly = { SdkKey = _fakeValue } };
|
2023-03-07 19:46:52 +01:00
|
|
|
|
|
|
|
|
|
var sutProvider = GetSutProvider(settings);
|
|
|
|
|
|
|
|
|
|
var currentContext = Substitute.For<ICurrentContext>();
|
|
|
|
|
currentContext.UserId.Returns(Guid.NewGuid());
|
|
|
|
|
|
2023-11-13 20:46:39 +01:00
|
|
|
|
Assert.Equal(0, sutProvider.Sut.GetIntVariation(_fakeKey, currentContext));
|
2023-03-07 19:46:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact(Skip = "For local development")]
|
|
|
|
|
public void FeatureValue_String()
|
|
|
|
|
{
|
2023-11-13 20:46:39 +01:00
|
|
|
|
var settings = new Settings.GlobalSettings { LaunchDarkly = { SdkKey = _fakeValue } };
|
2023-03-07 19:46:52 +01:00
|
|
|
|
|
|
|
|
|
var sutProvider = GetSutProvider(settings);
|
|
|
|
|
|
|
|
|
|
var currentContext = Substitute.For<ICurrentContext>();
|
|
|
|
|
currentContext.UserId.Returns(Guid.NewGuid());
|
|
|
|
|
|
2023-11-13 20:46:39 +01:00
|
|
|
|
Assert.Null(sutProvider.Sut.GetStringVariation(_fakeKey, currentContext));
|
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
|
|
|
|
|
|
|
|
|
var currentContext = Substitute.For<ICurrentContext>();
|
|
|
|
|
currentContext.UserId.Returns(Guid.NewGuid());
|
|
|
|
|
|
|
|
|
|
var results = sutProvider.Sut.GetAll(currentContext);
|
|
|
|
|
|
|
|
|
|
Assert.NotNull(results);
|
|
|
|
|
Assert.NotEmpty(results);
|
|
|
|
|
}
|
2023-03-02 20:04:23 +01:00
|
|
|
|
}
|