116 lines
4.2 KiB
C#
116 lines
4.2 KiB
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Http;
|
|
using NTP.Api.Auth;
|
|
using NTP.Application.Auth;
|
|
using NTP.Application.Common;
|
|
|
|
namespace NTP.Tests;
|
|
|
|
public class PasswordHasherTests
|
|
{
|
|
[Fact]
|
|
public void Sha1Hex_is_uppercase_hex_of_UPPER_login_plus_password()
|
|
{
|
|
// SHA-1("ABCpass") computed independently.
|
|
var expected = Convert.ToHexString(System.Security.Cryptography.SHA1.HashData("ABCpass"u8.ToArray()));
|
|
Assert.Equal(expected, PasswordHasher.Sha1Hex("abc", "pass"));
|
|
Assert.Equal(40, expected.Length);
|
|
Assert.Equal(expected, expected.ToUpperInvariant());
|
|
}
|
|
|
|
[Fact]
|
|
public void Sha1Hex_known_vector()
|
|
{
|
|
// SHA-1("abc") = A9993E364706816ABA3E25717850C26C9CD0D89D ; login "" + password "abc"
|
|
Assert.Equal("A9993E364706816ABA3E25717850C26C9CD0D89D", PasswordHasher.Sha1Hex("", "abc"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Login_case_does_not_matter()
|
|
=> Assert.Equal(PasswordHasher.Sha1Hex("Abc", "p"), PasswordHasher.Sha1Hex("aBC", "p"));
|
|
|
|
[Fact]
|
|
public void Aseza_accepts_two_variants_other_domains_one()
|
|
{
|
|
Assert.Equal(2, PasswordHasher.Candidates("aseza", "u", "p").Count);
|
|
Assert.Single(PasswordHasher.Candidates("GN", "u", "p"));
|
|
Assert.Contains(PasswordHasher.Sha1Hex("u", "p"), PasswordHasher.Candidates("ASEZA", "u", "p"));
|
|
}
|
|
}
|
|
|
|
public class ClaimsMappingTests
|
|
{
|
|
private static readonly AuthenticatedUser Sample = new(
|
|
Id: 42, UserId: "50034", Name: "Ahmad", RoleCode: "GOP", RoleName: "Gate operator", Domain: "ASEZA",
|
|
LocationId: 5, LocationName: "Gate 1", StkId: 3, CompId: 15, CompanyId: 15, CompanyName: "NAFITH",
|
|
CompanyCode: "NAF001", EmpNo: "1001", Lang: "EN", IsStaff: true);
|
|
|
|
private static CurrentUser Map(AuthenticatedUser u)
|
|
{
|
|
var identity = new ClaimsIdentity(JwtTokenIssuer.CreateClaims(u), "test", AppClaimsNames.Name, AppClaimsNames.Role);
|
|
var ctx = new DefaultHttpContext { User = new ClaimsPrincipal(identity) };
|
|
return new CurrentUser(new HttpContextAccessor { HttpContext = ctx });
|
|
}
|
|
|
|
[Fact]
|
|
public void Roundtrips_all_fields()
|
|
{
|
|
var c = Map(Sample);
|
|
Assert.True(c.IsAuthenticated);
|
|
Assert.Equal(42, c.Id);
|
|
Assert.Equal("50034", c.UserId);
|
|
Assert.Equal("Ahmad", c.Name);
|
|
Assert.Equal("GOP", c.RoleCode);
|
|
Assert.Equal("Gate operator", c.RoleName);
|
|
Assert.Equal("ASEZA", c.Domain);
|
|
Assert.Equal(5, c.LocationId);
|
|
Assert.Equal("Gate 1", c.LocationName);
|
|
Assert.Equal(3, c.StkId);
|
|
Assert.Equal(15, c.CompId);
|
|
Assert.Equal(15, c.CompanyId);
|
|
Assert.Equal("NAF001", c.CompanyCode);
|
|
Assert.Equal("1001", c.EmpNo);
|
|
Assert.Equal("EN", c.Lang);
|
|
Assert.True(c.IsStaff);
|
|
Assert.True(c.IsInRole("gop"));
|
|
Assert.True(c.IsInAnyRole("X", "GOP"));
|
|
}
|
|
|
|
[Fact]
|
|
public void Missing_values_become_null_or_defaults()
|
|
{
|
|
var c = Map(Sample with { LocationId = null, StkId = null, IsStaff = false, Lang = "" });
|
|
Assert.Null(c.LocationId);
|
|
Assert.Null(c.StkId);
|
|
Assert.False(c.IsStaff);
|
|
Assert.Equal("AR", c.Lang);
|
|
}
|
|
|
|
[Fact]
|
|
public void Anonymous_user_is_not_authenticated()
|
|
{
|
|
var c = new CurrentUser(new HttpContextAccessor { HttpContext = new DefaultHttpContext() });
|
|
Assert.False(c.IsAuthenticated);
|
|
Assert.Equal(string.Empty, c.UserId);
|
|
Assert.Null(c.CompanyId);
|
|
}
|
|
}
|
|
|
|
public class ConventionRegistrationTests
|
|
{
|
|
[Fact]
|
|
public void Application_services_are_registered_by_convention()
|
|
{
|
|
var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
|
|
NTP.Application.DependencyInjection.AddApplication(services);
|
|
Assert.Contains(services, d => d.ServiceType == typeof(IAuthService) && d.ImplementationType == typeof(AuthService));
|
|
Assert.Contains(services, d => d.ServiceType == typeof(NTP.Application.Lookups.ILookupService));
|
|
}
|
|
}
|
|
|
|
internal static class AppClaimsNames
|
|
{
|
|
public const string Name = NTP.Application.Abstractions.AppClaims.Name;
|
|
public const string Role = NTP.Application.Abstractions.AppClaims.Role;
|
|
}
|