Files
Academic-Affairs-System/tests/Jiaowu.Api.Tests/TokenServiceTests.cs
T
2026-07-24 12:42:51 +08:00

37 lines
1.1 KiB
C#

using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Jiaowu.Api.Domain.Identity;
using Jiaowu.Api.Infrastructure.Auth;
using Microsoft.Extensions.Options;
namespace Jiaowu.Api.Tests;
public sealed class TokenServiceTests
{
[Fact]
public void Token_contains_identity_and_roles()
{
var options = Options.Create(new JwtOptions
{
Issuer = "tests",
Audience = "tests-web",
Key = "a-test-signing-key-that-is-at-least-32-bytes-long"
});
var user = new ApplicationUser
{
Id = Guid.NewGuid(),
UserName = "teacher01",
DisplayName = "陈老师"
};
var service = new TokenService(options);
var token = new JwtSecurityTokenHandler().ReadJwtToken(
service.Create(user, [SystemRoles.Teacher]));
Assert.Contains(token.Claims, x =>
x.Type == ClaimTypes.Role && x.Value == SystemRoles.Teacher);
Assert.Contains(token.Claims, x =>
x.Type == ClaimTypes.Name && x.Value == "陈老师");
}
}