Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
test(back-end): Improve token generator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Feb 4, 2021
1 parent 22df166 commit 00c7446
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions Infrastructure.Test/Security/TokenGeneratorTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using Kaizen.Core.Security;
using Kaizen.Infrastructure.Extensions;
using Microsoft.Extensions.Configuration;
Expand All @@ -9,7 +13,7 @@ namespace Infrastructure.Test.Security
[TestFixture]
public class TokenGeneratorTest
{
ITokenGenerator tokenGenerator;
private ITokenGenerator _tokenGenerator;

[OneTimeSetUp]
public void Init()
Expand All @@ -24,20 +28,27 @@ public void Init()
services.ConfigureTokenGenerator();

ServiceProvider serviceProvider = services.BuildServiceProvider();
tokenGenerator = serviceProvider.GetService<ITokenGenerator>();
_tokenGenerator = serviceProvider.GetService<ITokenGenerator>();
}

[Test]
public void CheckTokenGenerator()
{
Assert.IsNotNull(tokenGenerator);
Assert.IsNotNull(_tokenGenerator);
}

[Test]
public void GenerateToken()
public void Generate_And_Verify_Token()
{
string token = tokenGenerator.GenerateToken("admin", "Administrator");
var token = _tokenGenerator.GenerateToken("admin", "Administrator");
Assert.IsTrue(token.Length > 0);

var handler = new JwtSecurityTokenHandler();
var tokenRead = handler.ReadToken(token) as JwtSecurityToken;
Assert.IsNotNull(tokenRead);

var tokenRole = tokenRead.Claims.First(claim => claim.Type == "role").Value;
Assert.AreEqual("Administrator", tokenRole);
}
}
}

0 comments on commit 00c7446

Please sign in to comment.