コード例 #1
0
        public void ToGetAll_Passing_Claims_Returns_Claim_Value_Int_List()
        {
            var claimTypeRole     = ClaimTypes.Role;
            var permissionId_add  = new Random().Next();
            var permissionId_edit = new Random().Next();

            var claimsPrincipal = new ClaimsPrincipal();

            claimsPrincipal.ToAdd(authenticationType, (claimTypeRole, permissionId_add), (claimTypeRole, permissionId_edit));
            Thread.CurrentPrincipal = claimsPrincipal;

            var fakeHttpContext = FakeHttpContext.Configure(x =>
            {
                x.User = claimsPrincipal;
            });

            var claimsPrincipalActual        = fakeHttpContext.User.ToGetAll <int>(claimTypeRole);
            var threadCurrentPrincipalActual = ((ClaimsPrincipal)Thread.CurrentPrincipal).ToGetAll <int>(claimTypeRole);

            if (claimsPrincipalActual != null)
            {
                Assert.IsTrue(claimsPrincipalActual.Count() == 2);
                Assert.IsTrue(claimsPrincipalActual.Contains(permissionId_add));
                Assert.IsTrue(claimsPrincipalActual.Contains(permissionId_edit));
            }

            if (threadCurrentPrincipalActual != null)
            {
                Assert.IsTrue(threadCurrentPrincipalActual.Count() == 2);
                Assert.IsTrue(threadCurrentPrincipalActual.Contains(permissionId_add));
                Assert.IsTrue(threadCurrentPrincipalActual.Contains(permissionId_edit));
            }
        }
コード例 #2
0
        public void ToGet_Passing_Claims_Returns_Claim_Value_Object()
        {
            var fakeObject = new FakeUser
            {
                Id          = Guid.NewGuid(),
                Name        = Guid.NewGuid().ToString(),
                Age         = new Random().Next(),
                Permissions = new string[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }
            };

            var claimsPrincipal = new ClaimsPrincipal();

            claimsPrincipal.ToAdd(authenticationType, (claimTypeUserData, fakeObject));
            Thread.CurrentPrincipal = claimsPrincipal;

            var fakeHttpContext = FakeHttpContext.Configure(x =>
            {
                x.User = claimsPrincipal;
            });

            var claimsPrincipalActual        = fakeHttpContext.User.ToGet <FakeUser>(claimTypeUserData);
            var threadCurrentPrincipalActual = ((ClaimsPrincipal)Thread.CurrentPrincipal).ToGet <FakeUser>(claimTypeUserData);

            Assert.AreEqual(fakeObject.Id, claimsPrincipalActual?.Id);
            Assert.AreEqual(fakeObject.Name, claimsPrincipalActual?.Name);
            Assert.AreEqual(fakeObject.Age, claimsPrincipalActual?.Age);
            CollectionAssert.AreEqual(fakeObject.Permissions, claimsPrincipalActual?.Permissions);

            Assert.AreEqual(fakeObject.Id, threadCurrentPrincipalActual?.Id);
            Assert.AreEqual(fakeObject.Name, threadCurrentPrincipalActual?.Name);
            Assert.AreEqual(fakeObject.Age, threadCurrentPrincipalActual?.Age);
            CollectionAssert.AreEqual(fakeObject.Permissions, threadCurrentPrincipalActual?.Permissions);
        }
コード例 #3
0
        public void ToIPv6_Passing_HttpContext_IP_Value_Returns_IP()
        {
            var ipExpected = new System.Net.IPAddress(int.MaxValue);

            var httpContext = FakeHttpContext.Configure(x =>
            {
                x.Connection.RemoteIpAddress = ipExpected;
            });

            Assert.AreEqual(ipExpected.MapToIPv6().ToString(), httpContext.ToIPv6());
        }
コード例 #4
0
        public void ToGetAll_Passing_Claims_Returns_Claim_Value_Object_List()
        {
            var fakeObject = new FakeUser
            {
                Id          = Guid.NewGuid(),
                Name        = Guid.NewGuid().ToString(),
                Age         = new Random().Next(),
                Permissions = new string[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }
            };

            var fakeObject1 = new FakeUser
            {
                Id          = Guid.NewGuid(),
                Name        = Guid.NewGuid().ToString(),
                Age         = new Random().Next(),
                Permissions = new string[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }
            };

            var claimsPrincipal = new ClaimsPrincipal();

            claimsPrincipal.ToAdd(authenticationType, (claimTypeUserData, fakeObject), (claimTypeUserData, fakeObject1));
            Thread.CurrentPrincipal = claimsPrincipal;

            var fakeHttpContext = FakeHttpContext.Configure(x =>
            {
                x.User = claimsPrincipal;
            });

            var claimsPrincipalActual        = fakeHttpContext.User.ToGetAll <FakeUser>(claimTypeUserData);
            var threadCurrentPrincipalActual = ((ClaimsPrincipal)Thread.CurrentPrincipal).ToGetAll <FakeUser>(claimTypeUserData);

            if (claimsPrincipalActual != null)
            {
                var resultItemClaimsPrincipalActual = claimsPrincipalActual.First(x => x?.Id == fakeObject.Id);

                Assert.IsTrue(claimsPrincipalActual.Count() == 2);
                Assert.AreEqual(fakeObject.Id, resultItemClaimsPrincipalActual?.Id);
                Assert.AreEqual(fakeObject.Name, resultItemClaimsPrincipalActual?.Name);
                Assert.AreEqual(fakeObject.Age, resultItemClaimsPrincipalActual?.Age);
                CollectionAssert.AreEqual(fakeObject.Permissions, resultItemClaimsPrincipalActual?.Permissions);
            }

            if (threadCurrentPrincipalActual != null)
            {
                var resultItemThreadCurrentPrincipalActual = threadCurrentPrincipalActual.First(x => x?.Id == fakeObject1.Id);
                Assert.IsTrue(threadCurrentPrincipalActual.Count() == 2);
                Assert.AreEqual(fakeObject1.Id, resultItemThreadCurrentPrincipalActual?.Id);
                Assert.AreEqual(fakeObject1.Name, resultItemThreadCurrentPrincipalActual?.Name);
                Assert.AreEqual(fakeObject1.Age, resultItemThreadCurrentPrincipalActual?.Age);
                CollectionAssert.AreEqual(fakeObject1.Permissions, resultItemThreadCurrentPrincipalActual?.Permissions);
            }
        }
コード例 #5
0
        public void ToAuthenticationType_Passing_Null_ClaimsPrincipal_Returns_ApplicationException()
        {
            ClaimsPrincipal?claimsPrincipal = null;

            var fakeHttpContext = FakeHttpContext.Configure(x =>
            {
                x.User = claimsPrincipal;
            });

            var claimsPrincipalActual = fakeHttpContext.User;

            Assert.ThrowsException <ApplicationException>(() => claimsPrincipalActual.ToAuthenticationType());
        }
コード例 #6
0
        public void ToAuthenticationType_Passing_Claims_Returns_Null()
        {
            var claimsPrincipal = new ClaimsPrincipal();

            Thread.CurrentPrincipal = claimsPrincipal;

            var fakeHttpContext = FakeHttpContext.Configure(x =>
            {
                x.User = claimsPrincipal;
            });

            var claimsPrincipalActual        = fakeHttpContext.User;
            var threadCurrentPrincipalActual = (ClaimsPrincipal)Thread.CurrentPrincipal;

            Assert.IsNull(claimsPrincipalActual.ToAuthenticationType());
            Assert.IsNull(threadCurrentPrincipalActual.ToAuthenticationType());
        }
コード例 #7
0
        public void ToIsAuthenticated_Passing_Claims_Returns_True()
        {
            var claimsPrincipal = new ClaimsPrincipal();

            claimsPrincipal.ToAdd(authenticationType, (claimTypeName, claimValueName), (claimTypeId, claimValueId.ToString()));
            Thread.CurrentPrincipal = claimsPrincipal;

            var fakeHttpContext = FakeHttpContext.Configure(x =>
            {
                x.User = claimsPrincipal;
            });

            var claimsPrincipalActual        = fakeHttpContext.User;
            var threadCurrentPrincipalActual = (ClaimsPrincipal)Thread.CurrentPrincipal;

            Assert.IsTrue(claimsPrincipalActual.ToIsAuthenticated());
            Assert.IsTrue(threadCurrentPrincipalActual.ToIsAuthenticated());
        }
コード例 #8
0
        public void ToGet_Passing_Claims_Returns_Claim_Value_Int()
        {
            var claimsPrincipal = new ClaimsPrincipal();

            claimsPrincipal.ToAdd(authenticationType, (claimTypeId, claimValueId));
            Thread.CurrentPrincipal = claimsPrincipal;

            var fakeHttpContext = FakeHttpContext.Configure(x =>
            {
                x.User = claimsPrincipal;
            });

            var claimsPrincipalActual        = fakeHttpContext.User.ToGetValue <int>(claimTypeId);
            var threadCurrentPrincipalActual = ((ClaimsPrincipal)Thread.CurrentPrincipal).ToGetValue <int>(claimTypeId);

            Assert.AreEqual(claimValueId, claimsPrincipalActual);
            Assert.AreEqual(claimValueId, threadCurrentPrincipalActual);
        }
コード例 #9
0
        public void ToUserName_Passing_Valid_UserName_ClaimsPrincipal_Returns_String_UserName_Value()
        {
            var claimsPrincipal = new ClaimsPrincipal();

            claimsPrincipal.ToAdd(authenticationType, (claimTypeName, claimValueName));
            Thread.CurrentPrincipal = claimsPrincipal;

            var fakeHttpContext = FakeHttpContext.Configure(x =>
            {
                x.User = claimsPrincipal;
            });

            var claimsPrincipalActual        = fakeHttpContext.User;
            var threadCurrentPrincipalActual = (ClaimsPrincipal)Thread.CurrentPrincipal;

            Assert.AreEqual(claimValueName, claimsPrincipalActual.ToUserName());
            Assert.AreEqual(claimValueName, threadCurrentPrincipalActual.ToUserName());
        }