public void ValidateTokenPayload_WhenTokenIsNull_ExpectResultWithArgumentNullException()
        {
            var result = new TestJwtPayloadTokenHandler().TestValidateTokenPayload(null, new TokenValidationParameters());

            result.IsValid.Should().BeFalse();
            result.Exception.Should().BeOfType <ArgumentNullException>();
        }
        public void ValidateTokenPayload_WhenTokenValidationParametersAreNull_ExpectResultWithArgumentNullException()
        {
            var token = CreateMockToken().Object;

            var result = new TestJwtPayloadTokenHandler().TestValidateTokenPayload(token, null);

            result.IsValid.Should().BeFalse();
            result.Exception.Should().BeOfType <ArgumentNullException>();
        }
        public void GetSigningKeys_WhenKeyResolverReturnsKey_ExpectKeyFromResolver()
        {
            var expectedKey = new RsaSecurityKey(RSA.Create());

            var handler = new TestJwtPayloadTokenHandler();
            var keys    = handler.TestGetSigningKeys("test", new TokenValidationParameters
            {
                IssuerSigningKeyResolver = (token, securityToken, kid, parameters) => new[] { expectedKey },
                IssuerSigningKey         = new RsaSecurityKey(RSA.Create())
            }).ToList();

            keys.Count.Should().Be(1);
            keys.Should().Contain(expectedKey);
        }
        public void CreateClaimsIdentity_WhenTokenHasNoIssuer_ExpectClaimsToUseDefaultIssuer()
        {
            var mockToken = new Mock <MockableJwtPayloadSecurityToken>();

            mockToken.Setup(x => x.Issuer).Returns((string)null);
            mockToken.Setup(x => x.Claims).Returns(new List <Claim> {
                new Claim("sub", "123")
            });

            var handler = new TestJwtPayloadTokenHandler();

            var identity = handler.TestCreateClaimsIdentity(mockToken.Object, new TokenValidationParameters());

            identity.Claims.All(x => x.Issuer == ClaimsIdentity.DefaultIssuer).Should().BeTrue();
            identity.Claims.All(x => x.OriginalIssuer == ClaimsIdentity.DefaultIssuer).Should().BeTrue();
        }
        public void GetSigningKeys_WheKeysInParameters_ExpectAllKeys()
        {
            var expectedKey1 = new RsaSecurityKey(RSA.Create());
            var expectedKey2 = new RsaSecurityKey(RSA.Create());

            var handler = new TestJwtPayloadTokenHandler();
            var keys    = handler.TestGetSigningKeys("test", new TokenValidationParameters
            {
                IssuerSigningKeyResolver = (token, securityToken, kid, parameters) => new List <SecurityKey>(),
                IssuerSigningKey         = expectedKey1,
                IssuerSigningKeys        = new[] { expectedKey2 }
            }).ToList();

            keys.Count.Should().Be(2);
            keys.Should().Contain(expectedKey1);
            keys.Should().Contain(expectedKey2);
        }
        public void CreateClaimsIdentity_WhenTokenHasClaimsWithProperties_ExpectPropertiesPersisted()
        {
            var expectedProperty  = new KeyValuePair <string, string>("test", "test_val");
            var claimWithProperty = new Claim("sub", "123")
            {
                Properties = { expectedProperty }
            };

            var mockToken = new Mock <MockableJwtPayloadSecurityToken>();

            mockToken.Setup(x => x.Issuer).Returns((string)null);
            mockToken.Setup(x => x.Claims).Returns(new List <Claim> {
                claimWithProperty
            });

            var handler = new TestJwtPayloadTokenHandler();

            var identity = handler.TestCreateClaimsIdentity(mockToken.Object, new TokenValidationParameters());

            var mappedClaim = identity.Claims.Single(x => x.Type == claimWithProperty.Type && x.Value == claimWithProperty.Value);

            mappedClaim.Properties.Should().Contain(expectedProperty);
        }
        public void CreateClaimsIdentity_WhenTokenValidationParametersAreNull_ExpectArgumentNullException()
        {
            var handler = new TestJwtPayloadTokenHandler();

            Assert.Throws <ArgumentNullException>(() => handler.TestCreateClaimsIdentity(new MockableJwtPayloadSecurityToken(), null));
        }
        public void CreateClaimsIdentity_WhenTokenIsNull_ExpectArgumentNullException()
        {
            var handler = new TestJwtPayloadTokenHandler();

            Assert.Throws <ArgumentNullException>(() => handler.TestCreateClaimsIdentity(null, new TokenValidationParameters()));
        }