Skip to content

Commit

Permalink
DateTimeOffset converter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmk committed Aug 23, 2024
1 parent 540b8ee commit 8f34279
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
148 changes: 148 additions & 0 deletions NATS.Jwt.Tests/Models/JwtClaimsDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -111,4 +113,150 @@ public void Deserialize_InvalidJson_ShouldThrowException()

Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<JwtClaimsData>(invalidJson));
}

[Fact]
public void Deserialize_WithNullValues_ShouldSetPropertiesToNull()
{
// Arrange
var json = "{}";

// Act
var claims = JsonSerializer.Deserialize<JwtClaimsData>(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<JwtClaimsData>(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<TestClass>(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<TestClass>(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<InvalidOperationException>(() => JsonSerializer.Deserialize<TestClass>(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; }
}
}
4 changes: 4 additions & 0 deletions NATS.Jwt/NATS.Jwt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" ExcludeAssets="Compile" PrivateAssets="All"/>
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="NATS.Jwt.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100a56954898c64f94b3785de8ff48cf4b9cf7ce5398602da1680f15e3dccc398e8bca151641158df6542afc9623805c0f6ac513a4bcf1b3289f45a27c5eb927711854ebc8a42afce2cbbe7deebde1a431ee53d2bdf64418ff964fc3a2c561e344ba79b30704d916a879afac5e7aea531198afa8a3a1979a4232a9c590f6ee5cfc2"/>
</ItemGroup>

</Project>

0 comments on commit 8f34279

Please sign in to comment.