first commit
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using OnlineBankingAuth.Configuration;
|
||||
using OnlineBankingAuth.Tokens;
|
||||
using Landmark.OLA.Application.Common.Security;
|
||||
using Landmark.OLA.Models.OnlineBanking;
|
||||
|
||||
namespace OnlineBankingAuth.Tests
|
||||
{
|
||||
public class TokenServiceTest
|
||||
{
|
||||
public static SecuritySettings _settings = new SecuritySettings
|
||||
{
|
||||
TokenSecret = "Some fake secret for crypt"
|
||||
};
|
||||
|
||||
//public static IOptions<SecuritySettings> _options = IOptions<SecuritySettings>(_settings);
|
||||
public static IOptions<SecuritySettings> _options = Options.Create<SecuritySettings>(_settings);
|
||||
|
||||
TokenService _tokenService = new TokenService(_options);
|
||||
|
||||
[Fact]
|
||||
public void ShouldGenerateTokenAndRetrieveTokenFromCookie()
|
||||
{
|
||||
var assertion = new DISAMLAssertion
|
||||
{
|
||||
MemberID = "123456",
|
||||
FI_ID = "7890"
|
||||
};
|
||||
var obToken = new OnlineBankingToken(assertion, null, false);
|
||||
var token = _tokenService.CreateOnlineBankingJWT(obToken);
|
||||
|
||||
var cookies = new Dictionary<string, string>
|
||||
{
|
||||
{ Constants.TokenPayload, token },
|
||||
{ Constants.TokenAuthorization, "Role=" + Constants.TokenAuthorizationString }
|
||||
};
|
||||
|
||||
var cookieMock = new Mock<IRequestCookieCollection>();
|
||||
cookieMock.Setup(c => c[It.IsAny<string>()])
|
||||
.Returns((string key) => cookies.TryGetValue(key, out var v) ? v : null);
|
||||
cookieMock.Setup(c => c.ContainsKey(It.IsAny<string>()))
|
||||
.Returns((string key) => cookies.ContainsKey(key));
|
||||
cookieMock.Setup(c => c.TryGetValue(It.IsAny<string>(), out It.Ref<string>.IsAny))
|
||||
.Returns((string key, out string value) =>
|
||||
{
|
||||
var ok = cookies.TryGetValue(key, out var v);
|
||||
value = v;
|
||||
return ok;
|
||||
});
|
||||
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Cookies = cookieMock.Object;
|
||||
|
||||
var retrievedAssertion = _tokenService.GetAuthenticatedDataFromCookie(context);
|
||||
|
||||
Assert.Equal(assertion.FI_ID, retrievedAssertion.FI_ID);
|
||||
Assert.Equal(assertion.MemberID, retrievedAssertion.MemberID);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldRenewCookie()
|
||||
{
|
||||
var assertion = new DISAMLAssertion
|
||||
{
|
||||
MemberID = "123456",
|
||||
FI_ID = "7890"
|
||||
};
|
||||
|
||||
var obToken = new OnlineBankingToken(assertion, null, false);
|
||||
var token = _tokenService.CreateOnlineBankingJWT(obToken);
|
||||
|
||||
var cookies = new Dictionary<string, string>
|
||||
{
|
||||
{ Constants.TokenPayload, token },
|
||||
{ Constants.TokenAuthorization, "Role=" + Constants.TokenAuthorizationString }
|
||||
};
|
||||
|
||||
var cookieMock = new Mock<IRequestCookieCollection>();
|
||||
cookieMock.Setup(c => c[It.IsAny<string>()])
|
||||
.Returns((string key) => cookies.TryGetValue(key, out var v) ? v : null);
|
||||
cookieMock.Setup(c => c.ContainsKey(It.IsAny<string>()))
|
||||
.Returns((string key) => cookies.ContainsKey(key));
|
||||
cookieMock.Setup(c => c.TryGetValue(It.IsAny<string>(), out It.Ref<string>.IsAny))
|
||||
.Returns((string key, out string value) =>
|
||||
{
|
||||
var ok = cookies.TryGetValue(key, out var v);
|
||||
value = v;
|
||||
return ok;
|
||||
});
|
||||
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Cookies = cookieMock.Object;
|
||||
|
||||
var renewedSession = _tokenService.RenewOnlineBankingJWT(context);
|
||||
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
|
||||
var firstEncryptedToken = CookieCryptor.DecryptJwt(token[Constants.TokenPrefix.Length..], _settings.TokenSecret);
|
||||
var firstToken = tokenHandler.ReadJwtToken(firstEncryptedToken);
|
||||
|
||||
var secondEncryptedToken = CookieCryptor.DecryptJwt(renewedSession[Constants.TokenPrefix.Length..], _settings.TokenSecret);
|
||||
var secondToken = tokenHandler.ReadJwtToken(secondEncryptedToken);
|
||||
|
||||
Assert.Equal(firstToken.Payload[Constants.MemberID], secondToken.Payload[Constants.MemberID]);
|
||||
Assert.Equal(firstToken.Payload[Constants.FI_ID], secondToken.Payload[Constants.FI_ID]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user