Skip to content

Commit

Permalink
Fixed event serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 committed Jan 30, 2024
1 parent c5ccb02 commit 7f11fbc
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public record ApiKeyUpdatedEvent : DomainEvent, INotification
/// <summary>
/// Gets or sets the custom attribute modifications of the API key.
/// </summary>
public Dictionary<string, string?> CustomAttributes { get; } = [];
public Dictionary<string, string?> CustomAttributes { get; init; } = [];

/// <summary>
/// Gets a value indicating whether or not the API key is being modified.
/// </summary>
[JsonIgnore]
public bool HasChanges => DisplayName != null || Description != null || ExpiresOn != null || CustomAttributes.Count > 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public record OneTimePasswordUpdatedEvent : DomainEvent, INotification
/// <summary>
/// Gets or sets the custom attribute modifications of the One-Time Password (OTP).
/// </summary>
public Dictionary<string, string?> CustomAttributes { get; } = [];
public Dictionary<string, string?> CustomAttributes { get; init; } = [];

/// <summary>
/// Gets a value indicating whether or not the One-Time Password (OTP) is being modified.
/// </summary>
[JsonIgnore]
public bool HasChanges => CustomAttributes.Count > 0;
}
3 changes: 2 additions & 1 deletion src/Logitar.Identity.Domain/Roles/Events/RoleUpdatedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public record RoleUpdatedEvent : DomainEvent, INotification
/// <summary>
/// Gets or sets the custom attribute modifications of the role.
/// </summary>
public Dictionary<string, string?> CustomAttributes { get; } = [];
public Dictionary<string, string?> CustomAttributes { get; init; } = [];

/// <summary>
/// Gets a value indicating whether or not the role is being modified.
/// </summary>
[JsonIgnore]
public bool HasChanges => DisplayName != null || Description != null || CustomAttributes.Count > 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public record SessionUpdatedEvent : DomainEvent, INotification
/// <summary>
/// Gets or sets the custom attribute modifications of the session.
/// </summary>
public Dictionary<string, string?> CustomAttributes { get; } = [];
public Dictionary<string, string?> CustomAttributes { get; init; } = [];

/// <summary>
/// Gets a value indicating whether or not the session is being modified.
/// </summary>
[JsonIgnore]
public bool HasChanges => CustomAttributes.Count > 0;
}
3 changes: 2 additions & 1 deletion src/Logitar.Identity.Domain/Users/Events/UserUpdatedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ public record UserUpdatedEvent : DomainEvent, INotification
/// <summary>
/// Gets or sets the custom attribute modifications of the user.
/// </summary>
public Dictionary<string, string?> CustomAttributes { get; } = [];
public Dictionary<string, string?> CustomAttributes { get; init; } = [];

/// <summary>
/// Gets a value indicating whether or not the user is being modified.
/// </summary>
[JsonIgnore]
public bool HasChanges => FirstName != null || MiddleName != null || LastName != null || FullName != null || Nickname != null
|| Birthdate != null || Gender != null || Locale != null || TimeZone != null
|| Picture != null || Profile != null || Website != null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Bogus;

namespace Logitar.Identity.Domain.ApiKeys.Events;

[Trait(Traits.Category, Categories.Unit)]
public class ApiKeyUpdatedEventTests
{
private readonly Faker _faker = new();

[Fact(DisplayName = "It should be serializable and deserializable.")]
public void It_should_be_serializable_and_deserializable()
{
ApiKeyUpdatedEvent @event = new();
@event.CustomAttributes.Add("Owner", _faker.Person.UserName);
@event.CustomAttributes.Add("SubSystem", "Identity");

string json = JsonSerializer.Serialize(@event);
Assert.DoesNotContain("haschanges", json.ToLower());

ApiKeyUpdatedEvent? deserialized = JsonSerializer.Deserialize<ApiKeyUpdatedEvent>(json);
Assert.NotNull(deserialized);
Assert.Equal(@event.CustomAttributes, deserialized.CustomAttributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Using Include="System.IdentityModel.Tokens.Jwt" />
<Using Include="System.Reflection" />
<Using Include="System.Security.Claims" />
<Using Include="System.Text.Json" />
<Using Include="Xunit" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Logitar.Identity.Domain.Passwords.Events;

[Trait(Traits.Category, Categories.Unit)]
public class OneTimePasswordUpdatedEventTests
{
[Fact(DisplayName = "It should be serializable and deserializable.")]
public void It_should_be_serializable_and_deserializable()
{
OneTimePasswordUpdatedEvent @event = new();
@event.CustomAttributes.Add("Purpose", "reset_password");
@event.CustomAttributes.Add("UserId", Guid.NewGuid().ToString());

string json = JsonSerializer.Serialize(@event);
Assert.DoesNotContain("haschanges", json.ToLower());

OneTimePasswordUpdatedEvent? deserialized = JsonSerializer.Deserialize<OneTimePasswordUpdatedEvent>(json);
Assert.NotNull(deserialized);
Assert.Equal(@event.CustomAttributes, deserialized.CustomAttributes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Logitar.Identity.Domain.Roles.Events;

[Trait(Traits.Category, Categories.Unit)]
public class RoleUpdatedEventTests
{
[Fact(DisplayName = "It should be serializable and deserializable.")]
public void It_should_be_serializable_and_deserializable()
{
RoleUpdatedEvent @event = new();
@event.CustomAttributes.Add("manage_users", bool.FalseString);
@event.CustomAttributes.Add("configuration", bool.TrueString);

string json = JsonSerializer.Serialize(@event);
Assert.DoesNotContain("haschanges", json.ToLower());

RoleUpdatedEvent? deserialized = JsonSerializer.Deserialize<RoleUpdatedEvent>(json);
Assert.NotNull(deserialized);
Assert.Equal(@event.CustomAttributes, deserialized.CustomAttributes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Bogus;

namespace Logitar.Identity.Domain.Sessions.Events;

[Trait(Traits.Category, Categories.Unit)]
public class SessionUpdatedEventTests
{
private readonly Faker _faker = new();

[Fact(DisplayName = "It should be serializable and deserializable.")]
public void It_should_be_serializable_and_deserializable()
{
SessionUpdatedEvent @event = new();
@event.CustomAttributes.Add("AdditionalInformation", $@"{{""User-Agent"":""{_faker.Internet.UserAgent()}""}}");
@event.CustomAttributes.Add("IpAddress", _faker.Internet.Ip());

string json = JsonSerializer.Serialize(@event);
Assert.DoesNotContain("haschanges", json.ToLower());

SessionUpdatedEvent? deserialized = JsonSerializer.Deserialize<SessionUpdatedEvent>(json);
Assert.NotNull(deserialized);
Assert.Equal(@event.CustomAttributes, deserialized.CustomAttributes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Bogus;

namespace Logitar.Identity.Domain.Users.Events;

[Trait(Traits.Category, Categories.Unit)]
public class UserUpdatedEventTests
{
private readonly Faker _faker = new();

[Fact(DisplayName = "It should be serializable and deserializable.")]
public void It_should_be_serializable_and_deserializable()
{
UserUpdatedEvent @event = new();
@event.CustomAttributes.Add("HealthInsuranceNumber", _faker.Person.BuildHealthInsuranceNumber());
@event.CustomAttributes.Add("JobTitle", "Sales Manager");

string json = JsonSerializer.Serialize(@event);
Assert.DoesNotContain("haschanges", json.ToLower());

UserUpdatedEvent? deserialized = JsonSerializer.Deserialize<UserUpdatedEvent>(json);
Assert.NotNull(deserialized);
Assert.Equal(@event.CustomAttributes, deserialized.CustomAttributes);
}
}
20 changes: 20 additions & 0 deletions tests/Logitar.Identity.Tests/PersonExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Bogus;

namespace Logitar.Identity;

public static class PersonExtensions
{
private static readonly Random _random = new();

public static string BuildHealthInsuranceNumber(this Person person)
{
StringBuilder hin = new();
hin.Append(person.LastName[..3].ToUpper());
hin.Append(person.FirstName[..1].ToUpper());
hin.Append(person.DateOfBirth.Year % 100);
hin.Append(person.DateOfBirth.Month.ToString("00"));
hin.Append(person.DateOfBirth.Day.ToString("00"));
hin.Append(_random.Next(0, 99).ToString("00"));
return hin.ToString();
}
}

0 comments on commit 7f11fbc

Please sign in to comment.