Beispiel #1
0
        public void IsInvalid_WhenPrincipalIdentity_IsClaimsIdentityInstance_WithDifferentNameIdentifierClaim()
        {
            var userId      = FakeData.Id();
            var otherUserId = FakeData.Id(userId);
            var principal   = new Mock <IPrincipal>(MockBehavior.Strict);
            var identity    = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.NameIdentifier, otherUserId.ToString(CultureInfo.InvariantCulture)),
            });

            principal.SetupGet(x => x.Identity).Returns(identity);
            var command = new FakeMustBePrincipalWithUserIdCommand
            {
                Principal = principal.Object,
                UserId    = userId,
            };
            var validator = new FakeMustBePrincipalWithUserIdValidator();

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func <ValidationFailure, bool> principalError = x => x.PropertyName == command.PropertyName(y => y.Principal);

            result.Errors.Count(principalError).ShouldEqual(1);
            result.Errors.Single(principalError).ErrorMessage.ShouldEqual(Resources.Validation_NotAuthorized_UserAction
                                                                          .Replace("{PropertyName}", User.Constraints.Label.ToLower())
                                                                          .Replace("{PropertyValue}", principal.Object.Identity.Name)
                                                                          .Replace("{UserId}", command.UserId.ToString(CultureInfo.InvariantCulture))
                                                                          );
            validator.ShouldHaveValidationErrorFor(x => x.Principal, command);
        }
Beispiel #2
0
        public void Validate_ThrowsNullReferenceException_WhenPrincipal_IsNull()
        {
            var userId  = FakeData.Id();
            var command = new FakeMustBePrincipalWithUserIdCommand
            {
                Principal = null,
                UserId    = userId,
            };
            var validator = new FakeMustBePrincipalWithUserIdValidator();
            var exception = Assert.Throws <NullReferenceException>(
                () => validator.Validate(command));

            exception.ShouldNotBeNull();
        }
Beispiel #3
0
        public void Validate_ThrowsArgumentNullException_WhenPrincipalIdentity_IsNull()
        {
            var userId    = FakeData.Id();
            var principal = new Mock <IPrincipal>(MockBehavior.Strict);

            principal.SetupGet(x => x.Identity).Returns(null as IIdentity);
            var command = new FakeMustBePrincipalWithUserIdCommand
            {
                Principal = principal.Object,
                UserId    = userId,
            };
            var validator = new FakeMustBePrincipalWithUserIdValidator();
            var exception = Assert.Throws <ArgumentNullException>(
                () => validator.Validate(command));

            exception.ShouldNotBeNull();
        }
Beispiel #4
0
        public void IsValid_WhenPrincipalIdentity_IsClaimsIdentityInstance_WithEqualNameIdentifierClaim()
        {
            var userId    = FakeData.Id();
            var principal = new Mock <IPrincipal>(MockBehavior.Strict);
            var identity  = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.NameIdentifier, (userId).ToString(CultureInfo.InvariantCulture)),
            });

            principal.SetupGet(x => x.Identity).Returns(identity);
            var command = new FakeMustBePrincipalWithUserIdCommand
            {
                Principal = principal.Object,
                UserId    = userId,
            };
            var validator = new FakeMustBePrincipalWithUserIdValidator();

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            validator.ShouldNotHaveValidationErrorFor(x => x.Principal, command);
        }