Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
henkmollema committed Feb 10, 2018
1 parent cc9f3b6 commit d1e7fcc
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions test/JsonWebTokens.Tests/JsonWebTokenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Jwt.Tests
{
public class JsonWebTokenTests
{
private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

[Fact]
public void EncodeHs256Token_Returns_ExpectedToken()
{
Expand All @@ -25,6 +27,56 @@ public void EncodeHs256Token_Returns_ExpectedToken()
Assert.Equal("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrZXkxIjoxLCJrZXkyIjoidGhlLXZhbHVlIn0.z4nWl_itwSsz1SbxEZkxCmm9MMkIKanFvgGz_gsWIJo", token);
}

[Fact]
public void InvalidSignature_ThrowsException()
{
// Arrange
var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrZXkxIjoxLCJrZXkyIjoidGhlLXZhbHVlIn0.nope";

// Act & Assert
var ex = Assert.Throws<SignatureVerificationException>(() => JsonWebToken.Decode(token, "SOME_SECRET_KEY"));
Assert.Equal("Invalid JWT signature.", ex.Message);
}

[Fact]
public void InvalidKey_ThrowsException()
{
// Arrange
var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrZXkxIjoxLCJrZXkyIjoidGhlLXZhbHVlIn0.z4nWl_itwSsz1SbxEZkxCmm9MMkIKanFvgGz_gsWIJo";

// Act & Assert
var ex = Assert.Throws<SignatureVerificationException>(() => JsonWebToken.Decode(token, "invalid_key"));
Assert.Equal("Invalid JWT signature.", ex.Message);
}

[Fact]
public void InvalidSignature_WithoutVerify_ReturnsPayload()
{
// Arrange
var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrZXkxIjoxLCJrZXkyIjoidGhlLXZhbHVlIn0.nope";

// Act
var jwt = JsonWebToken.DecodeToObject(token, "SOME_SECRET_KEY", verify: false);

// Assert
Assert.Equal(1L, jwt["key1"]);
Assert.Equal("the-value", jwt["key2"]);
}

[Fact]
public void InvalidKey_WithoutVerify_ReturnsPayload()
{
// Arrange
var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrZXkxIjoxLCJrZXkyIjoidGhlLXZhbHVlIn0.z4nWl_itwSsz1SbxEZkxCmm9MMkIKanFvgGz_gsWIJo";

// Act
var jwt = JsonWebToken.DecodeToObject(token, "blah", verify: false);

// Assert
Assert.Equal(1L, jwt["key1"]);
Assert.Equal("the-value", jwt["key2"]);
}

[Fact]
public void DecodeHS256_Verifies_Correctly()
{
Expand Down Expand Up @@ -102,5 +154,54 @@ public void DecodeHS512_Verifies_Correctly()
Assert.Equal(1L, jwt["key1"]);
Assert.Equal("the-value", jwt["key2"]);
}

[Fact]
public void TokenWithExpiration_EncodesAndDecodes()
{
// Arrange
var payload = new Dictionary<string, object>()
{
{ "exp", Math.Round((DateTime.UtcNow.AddHours(1) - UnixEpoch).TotalSeconds) }
};
var secretKey = "SOME_SECRET_KEY";

// Act
var token = JsonWebToken.Encode(payload, secretKey, JwtHashAlgorithm.HS256);
var decoded = JsonWebToken.DecodeToObject(token, secretKey);
Assert.True(decoded.ContainsKey("exp"));
}

[Fact]
public void ExpiredNowToken_ThrowsException()
{
// Arrange
var payload = new Dictionary<string, object>()
{
// A timestamp of now is considered as expired
{ "exp", Math.Round((DateTime.UtcNow - UnixEpoch).TotalSeconds) }
};
var secretKey = "SOME_SECRET_KEY";

// Act
var token = JsonWebToken.Encode(payload, secretKey, JwtHashAlgorithm.HS256);
var ex = Assert.Throws<SignatureVerificationException>(() => JsonWebToken.DecodeToObject(token, secretKey));
Assert.Equal("Token has expired.", ex.Message);
}

[Fact]
public void ExpiredToken_ThrowsException()
{
// Arrange
var payload = new Dictionary<string, object>()
{
{ "exp", Math.Round((DateTime.UtcNow.AddSeconds(-1) - UnixEpoch).TotalSeconds) }
};
var secretKey = "SOME_SECRET_KEY";

// Act
var token = JsonWebToken.Encode(payload, secretKey, JwtHashAlgorithm.HS256);
var ex = Assert.Throws<SignatureVerificationException>(() => JsonWebToken.DecodeToObject(token, secretKey));
Assert.Equal("Token has expired.", ex.Message);
}
}
}

0 comments on commit d1e7fcc

Please sign in to comment.