Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
xin9le committed Sep 26, 2022
1 parent 3edb842 commit d670f06
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions tests/LitJWT.Tests/DecodeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ public void VerifyExp()
var decodeResult = decoder.TryDecode(result, x => JsonConvert.DeserializeObject<Payload>(Encoding.UTF8.GetString(x)), out var decodedPayload);
decodeResult.Should().Be(DecodeResult.Success);
}

{
var nested = new Payload { Bar = 2, Foo = "foo2" };
var payload = new PayloadExp { Bar = 1, Foo = "foo", Nested = nested, exp = (DateTimeOffset.UtcNow - TimeSpan.FromSeconds(10)).ToUnixTimeSeconds() };
var jwt = encoder.Encode(payload, null);

var decodeResult = decoder.TryDecode(jwt, out PayloadExp decodedPayload);
decodeResult.Should().Be(DecodeResult.FailedVerifyExpire);
}

{
var nested = new Payload { Bar = 2, Foo = "foo2" };
var payload = new PayloadExp { Bar = 1, Foo = "foo", Nested = nested, exp = (DateTimeOffset.UtcNow + TimeSpan.FromSeconds(10)).ToUnixTimeSeconds() };
var jwt = encoder.Encode(payload, null);

var decodeResult = decoder.TryDecode(jwt, out PayloadExp decodedPayload);
decodeResult.Should().Be(DecodeResult.Success);
}
}

[Fact]
Expand Down Expand Up @@ -144,6 +162,24 @@ public void VerifyNbf()

decodeResult.Should().Be(DecodeResult.Success);
}

{
var nested = new Payload { Bar = 2, Foo = "foo2" };
var payload = new PayloadNbf { Bar = 1, Foo = "foo", Nested = nested, nbf = (DateTimeOffset.UtcNow + TimeSpan.FromSeconds(10)).ToUnixTimeSeconds() };
var jwt = encoder.Encode(payload, null);

var decodeResult = decoder.TryDecode(jwt, out PayloadNbf decodedPayload);
decodeResult.Should().Be(DecodeResult.FailedVerifyNotBefore);
}

{
var nested = new Payload { Bar = 2, Foo = "foo2" };
var payload = new PayloadNbf { Bar = 1, Foo = "foo", Nested = nested, nbf = (DateTimeOffset.UtcNow - TimeSpan.FromSeconds(10)).ToUnixTimeSeconds() };
var jwt = encoder.Encode(payload, null);

var decodeResult = decoder.TryDecode(jwt, out PayloadNbf decodedPayload);
decodeResult.Should().Be(DecodeResult.Success);
}
}


Expand Down Expand Up @@ -232,6 +268,24 @@ public void VerifyExpUtf8()
var decodeResult = decoder.TryDecode(result, x => JsonConvert.DeserializeObject<Payload>(Encoding.UTF8.GetString(x)), out var decodedPayload);
decodeResult.Should().Be(DecodeResult.Success);
}

{
var nested = new Payload { Bar = 2, Foo = "foo2" };
var payload = new PayloadExp { Bar = 1, Foo = "foo", Nested = nested, exp = (DateTimeOffset.UtcNow - TimeSpan.FromSeconds(10)).ToUnixTimeSeconds() };
var jwt = encoder.EncodeAsUtf8Bytes(payload, null);

var decodeResult = decoder.TryDecode(jwt, out PayloadExp decodedPayload);
decodeResult.Should().Be(DecodeResult.FailedVerifyExpire);
}

{
var nested = new Payload { Bar = 2, Foo = "foo2" };
var payload = new PayloadExp { Bar = 1, Foo = "foo", Nested = nested, exp = (DateTimeOffset.UtcNow + TimeSpan.FromSeconds(10)).ToUnixTimeSeconds() };
var jwt = encoder.EncodeAsUtf8Bytes(payload, null);

var decodeResult = decoder.TryDecode(jwt, out PayloadExp decodedPayload);
decodeResult.Should().Be(DecodeResult.Success);
}
}

[Fact]
Expand Down Expand Up @@ -259,6 +313,26 @@ public void VerifyNbfUtf8()

var decodeResult = decoder.TryDecode(result, x => JsonConvert.DeserializeObject<Payload>(Encoding.UTF8.GetString(x)), out var decodedPayload);

decodeResult.Should().Be(DecodeResult.Success);
}
{
var nested = new Payload { Bar = 2, Foo = "foo2" };
var payload = new PayloadNbf { Bar = 1, Foo = "foo", Nested = nested, nbf = (DateTimeOffset.UtcNow + TimeSpan.FromSeconds(10)).ToUnixTimeSeconds() };
var jwt = encoder.EncodeAsUtf8Bytes(payload, null);


var decodeResult = decoder.TryDecode(jwt, out PayloadNbf decodedPayload);

decodeResult.Should().Be(DecodeResult.FailedVerifyNotBefore);
}
{
var nested = new Payload { Bar = 2, Foo = "foo2" };
var payload = new PayloadNbf { Bar = 1, Foo = "foo", Nested = nested, nbf = (DateTimeOffset.UtcNow - TimeSpan.FromSeconds(10)).ToUnixTimeSeconds() };
var jwt = encoder.EncodeAsUtf8Bytes(payload, null);


var decodeResult = decoder.TryDecode(jwt, out PayloadNbf decodedPayload);

decodeResult.Should().Be(DecodeResult.Success);
}
}
Expand Down

0 comments on commit d670f06

Please sign in to comment.