Пример #1
0
        public void Handler_ReturnsNonNullClaim_WhenExists()
        {
            const string authenticationType = DefaultAuthenticationTypes.ApplicationCookie;
            const string claimType          = ClaimTypes.GivenName;
            string       claimValue         = FakeData.String();
            var          data = new[]
            {
                new Claim(ClaimTypes.Email, FakeData.Email()),
                new Claim(ClaimTypes.NameIdentifier, FakeData.Id()
                          .ToString(CultureInfo.InvariantCulture)),
                new Claim(claimType, claimValue),
            };
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            Expression <Func <ExternalCookieClaims, bool> > expectedQuery = x => x.AuthenticationType == authenticationType;

            queries.Setup(x => x.Execute(It.Is(expectedQuery)))
            .Returns(Task.FromResult(data as IEnumerable <Claim>));
            var handler = new HandleExternalCookieClaimQuery(queries.Object);
            var query   = new ExternalCookieClaim(ClaimTypes.GivenName, authenticationType);

            Claim result = handler.Handle(query).Result;

            Assert.NotNull(result);
            result.Type.ShouldEqual(claimType);
            result.Value.ShouldEqual(claimValue);
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
        }
        public void Handler_ReturnsNullClaim_WhenDoesNotExist()
        {
            const string authenticationType = DefaultAuthenticationTypes.ApplicationCookie;
            var          data = new[]
            {
                new Claim(ClaimTypes.Email, string.Format("{0}@domain.tld", Guid.NewGuid())),
                new Claim(ClaimTypes.NameIdentifier, new Random().Next(3, int.MaxValue)
                          .ToString(CultureInfo.InvariantCulture)),
                new Claim(ClaimTypes.Gender, string.Empty),
            };
            var queries = new Mock <IProcessQueries>(MockBehavior.Strict);
            Expression <Func <ExternalCookieClaims, bool> > expectedQuery = x => x.AuthenticationType == authenticationType;

            queries.Setup(x => x.Execute(It.Is(expectedQuery)))
            .Returns(Task.FromResult(data as IEnumerable <Claim>));
            var handler = new HandleExternalCookieClaimQuery(queries.Object);
            var query   = new ExternalCookieClaim(ClaimTypes.GivenName, authenticationType);

            Claim result = handler.Handle(query).Result;

            result.ShouldBeNull();
            queries.Verify(x => x.Execute(It.Is(expectedQuery)), Times.Once);
        }