From 8f34279d4fbd29e15d98618f840e3b27b70ee20f Mon Sep 17 00:00:00 2001 From: Ziya Suzen Date: Fri, 23 Aug 2024 18:43:54 +0100 Subject: [PATCH] DateTimeOffset converter tests --- NATS.Jwt.Tests/Models/JwtClaimsDataTests.cs | 148 ++++++++++++++++++++ NATS.Jwt/NATS.Jwt.csproj | 4 + 2 files changed, 152 insertions(+) diff --git a/NATS.Jwt.Tests/Models/JwtClaimsDataTests.cs b/NATS.Jwt.Tests/Models/JwtClaimsDataTests.cs index 06304a9..ab90945 100644 --- a/NATS.Jwt.Tests/Models/JwtClaimsDataTests.cs +++ b/NATS.Jwt.Tests/Models/JwtClaimsDataTests.cs @@ -3,6 +3,8 @@ using System; using System.Text.Json; +using System.Text.Json.Serialization; +using NATS.Jwt.Internal; using NATS.Jwt.Models; using Xunit; @@ -111,4 +113,150 @@ public void Deserialize_InvalidJson_ShouldThrowException() Assert.Throws(() => JsonSerializer.Deserialize(invalidJson)); } + + [Fact] + public void Deserialize_WithNullValues_ShouldSetPropertiesToNull() + { + // Arrange + var json = "{}"; + + // Act + var claims = JsonSerializer.Deserialize(json); + + // Assert + Assert.Null(claims.Audience); + Assert.Null(claims.Id); + Assert.Null(claims.IssuedAt); + Assert.Null(claims.Issuer); + Assert.Null(claims.Name); + Assert.Null(claims.Subject); + Assert.Null(claims.Expires); + Assert.Null(claims.NotBefore); + } + + [Fact] + public void Serialize_WithNullValues_ShouldOmitNullProperties() + { + // Arrange + var claims = new JwtClaimsData(); + + // Act + var json = JsonSerializer.Serialize(claims); + + // Assert + Assert.Equal("{}", json); + } + + [Fact] + public void Deserialize_WithUnixTimestamps_ShouldConvertCorrectly() + { + // Arrange + var json = @"{ + ""iat"": 1609459200, + ""exp"": 1609545600, + ""nbf"": 1609372800 + }"; + + // Act + var claims = JsonSerializer.Deserialize(json); + + // Assert + Assert.Equal(new DateTimeOffset(2021, 1, 1, 0, 0, 0, TimeSpan.Zero), claims.IssuedAt); + Assert.Equal(new DateTimeOffset(2021, 1, 2, 0, 0, 0, TimeSpan.Zero), claims.Expires); + Assert.Equal(new DateTimeOffset(2020, 12, 31, 0, 0, 0, TimeSpan.Zero), claims.NotBefore); + } + + [Fact] + public void NatsJsonDateTimeOffsetConverter_Read_ValidNumber_ShouldConvertCorrectly() + { + // Arrange + var json = @"{""Timestamp"": 1609459200}"; + var options = new JsonSerializerOptions + { + Converters = { new NatsJsonDateTimeOffsetConverter() } + }; + + // Act + var result = JsonSerializer.Deserialize(json, options); + + // Assert + Assert.Equal(new DateTimeOffset(2021, 1, 1, 0, 0, 0, TimeSpan.Zero), result.Timestamp); + } + + [Fact] + public void NatsJsonDateTimeOffsetConverter_Read_NullValue_ShouldReturnNull() + { + // Arrange + var json = @"{""Timestamp"": null}"; + var options = new JsonSerializerOptions + { + Converters = { new NatsJsonDateTimeOffsetConverter() } + }; + + // Act + var result = JsonSerializer.Deserialize(json, options); + + // Assert + Assert.Null(result.Timestamp); + } + + [Fact] + public void NatsJsonDateTimeOffsetConverter_Read_InvalidTokenType_ShouldThrowException() + { + // Arrange + var json = @"{""Timestamp"": ""not a number""}"; + var options = new JsonSerializerOptions + { + Converters = { new NatsJsonDateTimeOffsetConverter() }, + }; + + // Act & Assert + Assert.Throws(() => JsonSerializer.Deserialize(json, options)); + } + + [Fact] + public void NatsJsonDateTimeOffsetConverter_Write_ValidValue_ShouldWriteNumber() + { + // Arrange + var testObject = new TestClass + { + Timestamp = new DateTimeOffset(2021, 1, 1, 0, 0, 0, TimeSpan.Zero) + }; + var options = new JsonSerializerOptions + { + Converters = { new NatsJsonDateTimeOffsetConverter() } + }; + + // Act + var json = JsonSerializer.Serialize(testObject, options); + + // Assert + Assert.Equal(@"{""Timestamp"":1609459200}", json); + } + + [Fact] + public void NatsJsonDateTimeOffsetConverter_Write_NullValue_ShouldWriteNull() + { + // Arrange + var testObject = new TestClass + { + Timestamp = null + }; + var options = new JsonSerializerOptions + { + Converters = { new NatsJsonDateTimeOffsetConverter() } + }; + + // Act + var json = JsonSerializer.Serialize(testObject, options); + + // Assert + Assert.Equal(@"{""Timestamp"":null}", json); + } + + private class TestClass + { + [JsonConverter(typeof(NatsJsonDateTimeOffsetConverter))] + public DateTimeOffset? Timestamp { get; set; } + } } diff --git a/NATS.Jwt/NATS.Jwt.csproj b/NATS.Jwt/NATS.Jwt.csproj index 2eca8e4..b7f0846 100644 --- a/NATS.Jwt/NATS.Jwt.csproj +++ b/NATS.Jwt/NATS.Jwt.csproj @@ -37,4 +37,8 @@ + + + +