Skip to content

Commit

Permalink
Fixed organizations settings
Browse files Browse the repository at this point in the history
  • Loading branch information
jezzsantos committed Mar 18, 2024
1 parent 91a5740 commit 3cecb90
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text.Json;
using ApiHost1;
using Application.Interfaces.Services;
using Application.Persistence.Shared;
Expand Down Expand Up @@ -62,9 +61,9 @@ public async Task WhenDeliverProvisioning_ThenDelivers()
}, req => req.SetJWTBearerToken(login.AccessToken));

organization.Content.Value.Settings!.Count.Should().Be(3);
organization.Content.Value.Settings["aname1"].As<JsonElement>().GetString().Should().Be("avalue");
organization.Content.Value.Settings["aname2"].As<JsonElement>().GetString().Should().Be("99");
organization.Content.Value.Settings["aname3"].As<JsonElement>().GetString().Should().Be("True");
organization.Content.Value.Settings["aname1"].Should().Be("avalue");
organization.Content.Value.Settings["aname2"].Should().Be("99");
organization.Content.Value.Settings["aname3"].Should().Be("True");
#endif
}

Expand Down Expand Up @@ -107,9 +106,9 @@ public async Task WhenDrainAllProvisioningsAndSomeWithUnknownTenancies_ThenDrain
}, req => req.SetJWTBearerToken(login.AccessToken));

organization.Content.Value.Settings!.Count.Should().Be(3);
organization.Content.Value.Settings["aname1"].As<JsonElement>().GetString().Should().Be("avalue1");
organization.Content.Value.Settings["aname2"].As<JsonElement>().GetString().Should().Be("99");
organization.Content.Value.Settings["aname3"].As<JsonElement>().GetString().Should().Be("True");
organization.Content.Value.Settings["aname1"].Should().Be("avalue1");
organization.Content.Value.Settings["aname2"].Should().Be("99");
organization.Content.Value.Settings["aname3"].Should().Be("True");
}
#endif

Expand Down
4 changes: 3 additions & 1 deletion src/Application.Resources.Shared/Organization.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Application.Interfaces.Resources;
using JetBrains.Annotations;

namespace Application.Resources.Shared;

Expand All @@ -13,9 +14,10 @@ public class Organization : IIdentifiableResource
public required string Id { get; set; }
}

[UsedImplicitly]
public class OrganizationWithSettings : Organization
{
public required Dictionary<string, object?> Settings { get; set; }
public required Dictionary<string, string> Settings { get; set; }
}

public enum OrganizationOwnership
Expand Down
33 changes: 32 additions & 1 deletion src/Common.UnitTests/Extensions/StringExtensionsSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public void WhenToIntAndNotMatches_ThenThrows()
}

[Fact]
public void WhenToIntAndMatchesLowercase_ThenReturnsTrue()
public void WhenToIntAndMatches_ThenReturns()
{
var result = "9".ToInt();

Expand All @@ -395,6 +395,37 @@ public void WhenToIntOrDefaultAndNotMatches_ThenReturnsDefault()
result.Should().Be(9);
}

[Fact]
public void WhenToDoubleAndEmpty_ThenReturnsMinusOne()
{
var result = "".ToDouble();

result.Should().Be(-1);
}

[Fact]
public void WhenToDoubleAndNotMatches_ThenThrows()
{
"notavalue".Invoking(x => x.ToDouble())
.Should().Throw<FormatException>();
}

[Fact]
public void WhenToDoubleAndMatchesInteger_ThenReturns()
{
var result = "9".ToDouble();

result.Should().Be(9D);
}

[Fact]
public void WhenToDoubleAndMatchesFloating_ThenReturns()
{
var result = "9.009".ToDouble();

result.Should().Be(9.009D);
}

[Fact]
public void WhenWithoutTrailingSlashWithSlash_ThenReturnsPathWithoutSlash()
{
Expand Down
13 changes: 13 additions & 0 deletions src/Common/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,19 @@ public static string ToCamelCase(this string value)
}
#endif
#if COMMON_PROJECT || ANALYZERS_NONPLATFORM
/// <summary>
/// Converts the <see cref="value" /> to a floating value
/// </summary>
public static double ToDouble(this string? value)
{
if (value.HasNoValue())
{
return -1;
}

return double.Parse(value);
}

/// <summary>
/// Converts the <see cref="value" /> to a integer value
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public class GetOrganizationSettingsResponse : IWebResponse
{
public Organization? Organization { get; set; }

public Dictionary<string, object?>? Settings { get; set; }
public Dictionary<string, string>? Settings { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ await _application.CreateOrganizationAsync(_caller.Object, "auserid", "aname",
&& org.Ownership == Ownership.Personal
&& org.CreatedById == "auserid"
&& org.Settings.Properties.Count == 1
&& org.Settings.Properties["aname"].Value == "avalue"
&& org.Settings.Properties["aname"].Value.As<string>() == "avalue"
&& org.Settings.Properties["aname"].IsEncrypted == false
), It.IsAny<CancellationToken>()));
_tenantSettingsService.Verify(tss =>
Expand Down Expand Up @@ -106,7 +106,7 @@ await _application.CreateSharedOrganizationAsync(_caller.Object, "aname",
&& org.Ownership == Ownership.Shared
&& org.CreatedById == "acallerid"
&& org.Settings.Properties.Count == 1
&& org.Settings.Properties["aname"].Value == "avalue"
&& org.Settings.Properties["aname"].Value.As<string>() == "avalue"
&& org.Settings.Properties["aname"].IsEncrypted == false
), It.IsAny<CancellationToken>()));
_tenantSettingsService.Verify(tss =>
Expand Down Expand Up @@ -219,10 +219,10 @@ public async Task WhenChangeSettings_ThenReturnsSettings()
&& o.Ownership == Ownership.Personal
&& o.CreatedById == "auserid"
&& o.Settings.Properties.Count == 4
&& o.Settings.Properties["aname1"].Value == "anewvalue"
&& o.Settings.Properties["aname2"].Value == "99"
&& o.Settings.Properties["aname3"].Value == "True"
&& o.Settings.Properties["aname4"].Value == "anoldvalue"
&& o.Settings.Properties["aname1"].Value.As<string>() == "anewvalue"
&& o.Settings.Properties["aname2"].Value.As<double>().Equals(99D)
&& o.Settings.Properties["aname3"].Value.As<bool>() == true
&& o.Settings.Properties["aname4"].Value.As<string>() == "anoldvalue"
), It.IsAny<CancellationToken>()));
_tenantSettingsService.Verify(tss =>
tss.CreateForTenantAsync(It.IsAny<ICallerContext>(), It.IsAny<string>(), It.IsAny<CancellationToken>()),
Expand Down
5 changes: 3 additions & 2 deletions src/OrganizationsApplication/OrganizationsApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ public static OrganizationWithSettings ToOrganizationWithSettings(this Organizat
{
var dto = organization.ToOrganization().Convert<Organization, OrganizationWithSettings>();
dto.Settings =
organization.Settings.Properties.ToDictionary(pair => pair.Key, pair => (object?)pair.Value.Value);
organization.Settings.Properties.ToDictionary(pair => pair.Key,
pair => pair.Value.Value.ToString() ?? string.Empty);
return dto;
}

Expand All @@ -222,7 +223,7 @@ public static Result<Settings, Error> ToSettings(this TenantSettings tenantSetti
continue;
}

var value = tenantSetting.Value.ToString()!;
var value = tenantSetting.Value;
var setting = Setting.Create(value, tenantSetting.IsEncrypted);
if (!setting.IsSuccessful)
{
Expand Down
113 changes: 110 additions & 3 deletions src/OrganizationsDomain.UnitTests/SettingSpec.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Domain.Interfaces.Services;
using FluentAssertions;
using Moq;
using UnitTesting.Common;
using Xunit;

Expand All @@ -8,22 +10,127 @@ namespace OrganizationsDomain.UnitTests;
public class SettingSpec
{
[Fact]
public void WhenCreateWithEmptyValue_ThenReturnsSetting()
public void WhenCreateWithEmptyStringValue_ThenReturnsSetting()
{
var result = Setting.Create(string.Empty, true);

result.Should().BeSuccess();
result.Value.Value.Should().BeEmpty();
result.Value.Value.As<string>().Should().BeEmpty();
result.Value.ValueType.Should().Be(SettingValueType.String);
result.Value.IsEncrypted.Should().BeTrue();
}

[Fact]
public void WhenCreateWithNonEmptyValue_ThenReturnsSetting()
public void WhenCreateWithStringValue_ThenReturnsSetting()
{
var result = Setting.Create("aname", true);

result.Should().BeSuccess();
result.Value.Value.Should().Be("aname");
result.Value.ValueType.Should().Be(SettingValueType.String);
result.Value.IsEncrypted.Should().BeTrue();
}

[Fact]
public void WhenCreateWithIntegerValue_ThenReturnsSetting()
{
var result = Setting.Create(99);

result.Should().BeSuccess();
result.Value.Value.Should().Be(99);
result.Value.ValueType.Should().Be(SettingValueType.Number);
result.Value.IsEncrypted.Should().BeFalse();
}

[Fact]
public void WhenCreateWithFloatValue_ThenReturnsSetting()
{
var result = Setting.Create(99.99);

result.Should().BeSuccess();
result.Value.Value.Should().Be(99.99);
result.Value.ValueType.Should().Be(SettingValueType.Number);
result.Value.IsEncrypted.Should().BeFalse();
}

[Fact]
public void WhenCreateWithBooleanValue_ThenReturnsSetting()
{
var result = Setting.Create(true);

result.Should().BeSuccess();
result.Value.Value.Should().Be(true);
result.Value.ValueType.Should().Be(SettingValueType.Boolean);
result.Value.IsEncrypted.Should().BeFalse();
}

[Fact]
public void WhenFromAndEncryptedString_ThenReturns()
{
var tenantSettingService = new Mock<ITenantSettingService>();
tenantSettingService.Setup(x => x.Decrypt(It.IsAny<string>())).Returns("adecryptedvalue");

var result = Setting.From("astringvalue", SettingValueType.String, true, tenantSettingService.Object);

result.Value.Value.Should().Be("adecryptedvalue");
result.Value.Value.Should().BeOfType<string>();
result.Value.ValueType.Should().Be(SettingValueType.String);
tenantSettingService.Verify(x => x.Decrypt("astringvalue"));
}

[Fact]
public void WhenFromAndUnencryptedString_ThenReturns()
{
var tenantSettingService = new Mock<ITenantSettingService>();
tenantSettingService.Setup(x => x.Decrypt(It.IsAny<string>())).Returns("adecryptedvalue");

var result = Setting.From("astringvalue", SettingValueType.String, false, tenantSettingService.Object);

result.Value.Value.Should().Be("astringvalue");
result.Value.Value.Should().BeOfType<string>();
result.Value.ValueType.Should().Be(SettingValueType.String);
tenantSettingService.Verify(x => x.Decrypt(It.IsAny<string>()), Times.Never);
}

[Fact]
public void WhenFromAndInteger_ThenReturns()
{
var tenantSettingService = new Mock<ITenantSettingService>();
tenantSettingService.Setup(x => x.Decrypt(It.IsAny<string>())).Returns("adecryptedvalue");

var result = Setting.From("99", SettingValueType.Number, false, tenantSettingService.Object);

result.Value.Value.Should().Be(99D);
result.Value.Value.Should().BeOfType<double>();
result.Value.ValueType.Should().Be(SettingValueType.Number);
tenantSettingService.Verify(x => x.Decrypt(It.IsAny<string>()), Times.Never);
}

[Fact]
public void WhenFromAndFloat_ThenReturns()
{
var tenantSettingService = new Mock<ITenantSettingService>();
tenantSettingService.Setup(x => x.Decrypt(It.IsAny<string>())).Returns("adecryptedvalue");

var result = Setting.From("99.99", SettingValueType.Number, false, tenantSettingService.Object);

result.Value.Value.Should().Be(99.99D);
result.Value.Value.Should().BeOfType<double>();
result.Value.ValueType.Should().Be(SettingValueType.Number);
tenantSettingService.Verify(x => x.Decrypt(It.IsAny<string>()), Times.Never);
}

[Fact]
public void WhenFromAndBoolean_ThenReturns()
{
var tenantSettingService = new Mock<ITenantSettingService>();
tenantSettingService.Setup(x => x.Decrypt(It.IsAny<string>())).Returns("adecryptedvalue");

var result = Setting.From("True", SettingValueType.Boolean, false, tenantSettingService.Object);

result.Value.Value.Should().Be(true);
result.Value.Value.Should().BeOfType<bool>();
result.Value.ValueType.Should().Be(SettingValueType.Boolean);
tenantSettingService.Verify(x => x.Decrypt(It.IsAny<string>()), Times.Never);
}
}
19 changes: 15 additions & 4 deletions src/OrganizationsDomain/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ public static Created Create(Identifier id, Ownership ownership, Identifier crea

public sealed class SettingCreated : IDomainEvent
{
public static SettingCreated Create(Identifier id, string name, string value, bool isEncrypted)
public static SettingCreated Create(Identifier id, string name, string value, SettingValueType valueType,
bool isEncrypted)
{
return new SettingCreated
{
RootId = id,
Name = name,
Value = value,
StringValue = value,
ValueType = valueType,
IsEncrypted = isEncrypted,
OccurredUtc = DateTime.UtcNow
};
Expand All @@ -48,7 +50,9 @@ public static SettingCreated Create(Identifier id, string name, string value, bo

public required string Name { get; set; }

public required string Value { get; set; }
public required string StringValue { get; set; }

public required SettingValueType ValueType { get; set; }

public required string RootId { get; set; }

Expand All @@ -57,27 +61,34 @@ public static SettingCreated Create(Identifier id, string name, string value, bo

public sealed class SettingUpdated : IDomainEvent
{
public static SettingUpdated Create(Identifier id, string name, string from, string to, bool isEncrypted)
public static SettingUpdated Create(Identifier id, string name, string from, SettingValueType fromType,
string to, SettingValueType toType, bool isEncrypted)
{
return new SettingUpdated
{
RootId = id,
Name = name,
From = from,
FromType = fromType,
To = to,
ToType = toType,
IsEncrypted = isEncrypted,
OccurredUtc = DateTime.UtcNow
};
}

public required string From { get; set; }

public required SettingValueType FromType { get; set; }

public required bool IsEncrypted { get; set; }

public required string Name { get; set; }

public required string To { get; set; }

public required SettingValueType ToType { get; set; }

public required string RootId { get; set; }

public required DateTime OccurredUtc { get; set; }
Expand Down
Loading

0 comments on commit 3cecb90

Please sign in to comment.