public void TestThatValidateTokenThrowsSecurityExceptionWhenBuildOnIdentityBuilderFailsWithException()
        {
            var fixture = new Fixture();

            fixture.Customize <IIdentity>(e => e.FromFactory(() => MockRepository.GenerateMock <IIdentity>()));

            var userNamePasswordValidatorMock = MockRepository.GenerateMock <UserNamePasswordValidator>();
            var error = fixture.Create <Exception>();
            var identityBuilderMock = MockRepository.GenerateMock <IIdentityBuilder>();

            identityBuilderMock.Stub(m => m.Build(Arg <SecurityToken> .Is.NotNull, Arg <IDictionary <string, string> > .Is.Anything))
            .Throw(error)
            .Repeat.Any();

            var userNameSecurityToken = new UserNameSecurityToken(fixture.Create <string>(), fixture.Create <string>());

            var userNameAsMailAddressSecurityTokenAuthenticator = new UserNameAsMailAddressSecurityTokenAuthenticator(userNamePasswordValidatorMock, identityBuilderMock);

            Assert.That(userNameAsMailAddressSecurityTokenAuthenticator, Is.Not.Null);

            var exception = Assert.Throws <SecurityException>(() => userNameAsMailAddressSecurityTokenAuthenticator.ValidateToken(userNameSecurityToken));

            Assert.That(exception, Is.Not.Null);
            Assert.That(exception.Message, Is.Not.Null);
            Assert.That(exception.Message, Is.Not.Empty);
            Assert.That(exception.Message, Is.EqualTo(Resource.GetExceptionMessage(ExceptionMessage.SecurityTokenCouldNotBeValidated)));
            Assert.That(exception.InnerException, Is.Not.Null);
            Assert.That(exception.InnerException, Is.EqualTo(error));
        }
        public void TestThatValidateTokenReturnsReadOnlyCollectionOfAuthorizationPoliciesContainingUserNameAsMailAddressAuthorizationPolicy()
        {
            var fixture = new Fixture();

            var userNamePasswordValidatorMock = MockRepository.GenerateMock <UserNamePasswordValidator>();
            var identity            = MockRepository.GenerateMock <IIdentity>();
            var identityBuilderMock = MockRepository.GenerateMock <IIdentityBuilder>();

            identityBuilderMock.Stub(m => m.Build(Arg <SecurityToken> .Is.NotNull, Arg <IDictionary <string, string> > .Is.Anything))
            .Return(identity)
            .Repeat.Any();

            var userNameSecurityToken = new UserNameSecurityToken(fixture.Create <string>(), fixture.Create <string>());

            var userNameAsMailAddressSecurityTokenAuthenticator = new UserNameAsMailAddressSecurityTokenAuthenticator(userNamePasswordValidatorMock, identityBuilderMock);

            Assert.That(userNameAsMailAddressSecurityTokenAuthenticator, Is.Not.Null);

            var result = userNameAsMailAddressSecurityTokenAuthenticator.ValidateToken(userNameSecurityToken);

            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.Not.Empty);

            var userNameAsMailAddressAuthorizationPolicy = result.OfType <UserNameAsMailAddressAuthorizationPolicy>().SingleOrDefault();

            Assert.That(userNameAsMailAddressAuthorizationPolicy, Is.Not.Null);
            // ReSharper disable PossibleNullReferenceException
            Assert.That(userNameAsMailAddressAuthorizationPolicy.PrimaryIdentity, Is.Not.Null);
            Assert.That(userNameAsMailAddressAuthorizationPolicy.PrimaryIdentity, Is.EqualTo(identity));
            // ReSharper restore PossibleNullReferenceException
        }
        public void TestThatValidateTokenCallsValidateOnUserNamePasswordValidator()
        {
            var fixture = new Fixture();

            fixture.Customize <IIdentity>(e => e.FromFactory(() => MockRepository.GenerateMock <IIdentity>()));

            var userNamePasswordValidatorMock = MockRepository.GenerateMock <UserNamePasswordValidator>();
            var identityBuilderMock           = MockRepository.GenerateMock <IIdentityBuilder>();

            identityBuilderMock.Stub(m => m.Build(Arg <SecurityToken> .Is.NotNull, Arg <IDictionary <string, string> > .Is.Anything))
            .Return(fixture.Create <IIdentity>())
            .Repeat.Any();

            var userName = fixture.Create <string>();
            var password = fixture.Create <string>();
            var userNameSecurityToken = new UserNameSecurityToken(userName, password);

            var userNameAsMailAddressSecurityTokenAuthenticator = new UserNameAsMailAddressSecurityTokenAuthenticator(userNamePasswordValidatorMock, identityBuilderMock);

            Assert.That(userNameAsMailAddressSecurityTokenAuthenticator, Is.Not.Null);

            userNameAsMailAddressSecurityTokenAuthenticator.ValidateToken(userNameSecurityToken);

            userNamePasswordValidatorMock.AssertWasCalled(m => m.Validate(Arg <string> .Is.Equal(userName), Arg <string> .Is.Equal(password)));
        }
        public void TestThatValidateTokenCallsBuildOnIdentityBuilder()
        {
            var fixture = new Fixture();

            fixture.Customize <IIdentity>(e => e.FromFactory(() => MockRepository.GenerateMock <IIdentity>()));

            var userName = fixture.Create <string>();

            var userNamePasswordValidatorMock = MockRepository.GenerateMock <UserNamePasswordValidator>();
            var identityBuilderMock           = MockRepository.GenerateMock <IIdentityBuilder>();

            identityBuilderMock.Stub(m => m.Build(Arg <SecurityToken> .Is.NotNull, Arg <IDictionary <string, string> > .Is.NotNull))
            .WhenCalled(e =>
            {
                var identityProperites = (IDictionary <string, string>)e.Arguments.ElementAt(1);
                Assert.That(identityProperites, Is.Not.Null);
                Assert.That(identityProperites.Count, Is.EqualTo(1));
                Assert.That(identityProperites.ElementAt(0).Key, Is.Not.Null);
                Assert.That(identityProperites.ElementAt(0).Key, Is.Not.Empty);
                Assert.That(identityProperites.ElementAt(0).Key, Is.EqualTo(ClaimTypes.Email));
                Assert.That(identityProperites.ElementAt(0).Value, Is.Not.Null);
                Assert.That(identityProperites.ElementAt(0).Value, Is.Not.Empty);
                Assert.That(identityProperites.ElementAt(0).Value, Is.EqualTo(userName));
            })
            .Return(fixture.Create <IIdentity>())
            .Repeat.Any();

            var userNameSecurityToken = new UserNameSecurityToken(userName, fixture.Create <string>());

            var userNameAsMailAddressSecurityTokenAuthenticator = new UserNameAsMailAddressSecurityTokenAuthenticator(userNamePasswordValidatorMock, identityBuilderMock);

            Assert.That(userNameAsMailAddressSecurityTokenAuthenticator, Is.Not.Null);

            userNameAsMailAddressSecurityTokenAuthenticator.ValidateToken(userNameSecurityToken);

            identityBuilderMock.AssertWasCalled(m => m.Build(Arg <SecurityToken> .Is.Equal(userNameSecurityToken), Arg <IDictionary <string, string> > .Is.NotNull));
        }
        public void TestThatValidateTokenThrowsSecurityExceptionWhenValidateOnUserNamePasswordValidatorFailsWithSecurityException()
        {
            var fixture = new Fixture();

            var error = new SecurityException(fixture.Create <string>());
            var userNamePasswordValidatorMock = MockRepository.GenerateMock <UserNamePasswordValidator>();

            userNamePasswordValidatorMock.Stub(m => m.Validate(Arg <string> .Is.Anything, Arg <string> .Is.Anything))
            .Throw(error)
            .Repeat.Any();

            var identityBuilderMock = MockRepository.GenerateMock <IIdentityBuilder>();

            var userNameSecurityToken = new UserNameSecurityToken(fixture.Create <string>(), fixture.Create <string>());

            var userNameAsMailAddressSecurityTokenAuthenticator = new UserNameAsMailAddressSecurityTokenAuthenticator(userNamePasswordValidatorMock, identityBuilderMock);

            Assert.That(userNameAsMailAddressSecurityTokenAuthenticator, Is.Not.Null);

            var exception = Assert.Throws <SecurityException>(() => userNameAsMailAddressSecurityTokenAuthenticator.ValidateToken(userNameSecurityToken));

            Assert.That(exception, Is.Not.Null);
            Assert.That(exception, Is.EqualTo(error));
        }
        public void TestThatValidateTokenReturnsReadOnlyCollectionOfAuthorizationPolicies()
        {
            var fixture = new Fixture();

            fixture.Customize <IIdentity>(e => e.FromFactory(() => MockRepository.GenerateMock <IIdentity>()));

            var userNamePasswordValidatorMock = MockRepository.GenerateMock <UserNamePasswordValidator>();
            var identityBuilderMock           = MockRepository.GenerateMock <IIdentityBuilder>();

            identityBuilderMock.Stub(m => m.Build(Arg <SecurityToken> .Is.NotNull, Arg <IDictionary <string, string> > .Is.Anything))
            .Return(fixture.Create <IIdentity>())
            .Repeat.Any();

            var userNameSecurityToken = new UserNameSecurityToken(fixture.Create <string>(), fixture.Create <string>());

            var userNameAsMailAddressSecurityTokenAuthenticator = new UserNameAsMailAddressSecurityTokenAuthenticator(userNamePasswordValidatorMock, identityBuilderMock);

            Assert.That(userNameAsMailAddressSecurityTokenAuthenticator, Is.Not.Null);

            var result = userNameAsMailAddressSecurityTokenAuthenticator.ValidateToken(userNameSecurityToken);

            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.Not.Empty);
        }