Exemplo n.º 1
0
            public void IsInvalidWhen_MatchesNoPerson()
            {
                var command = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = "*****@*****.**",
                };
                var scenarioOptions = new ScenarioOptions
                {
                    Command = command,
                    Person  = new Person(),
                };
                var validator = CreateValidator(scenarioOptions);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);

                error.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                error.ErrorMessage.ShouldEqual(string.Format(
                                                   ValidateEmailAddress.FailedBecauseValueMatchedNoPerson,
                                                   command.EmailAddress));
                // ReSharper restore PossibleNullReferenceException
            }
Exemplo n.º 2
0
        public void Handle(SendCreatePasswordMessageCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // get the establishment
            var establishment = _entities.Get <Establishment>()
                                .EagerLoad(_entities, new Expression <Func <Establishment, object> >[]
            {
                e => e.Type.Category,
            })
                                .ByEmail(command.EmailAddress);

            // get the person
            var person = _entities.Get <Person>()
                         .EagerLoad(_entities, new Expression <Func <Person, object> >[]
            {
                p => p.Emails,
                p => p.Affiliations,
            })
                         .ByEmail(command.EmailAddress);

            // create the person if they don't yet exist
            if (person == null)
            {
                var createPersonHandler = new CreatePersonHandler(_entities);
                var createPersonCommand = new CreatePersonCommand
                {
                    DisplayName = command.EmailAddress,
                };
                createPersonHandler.Handle(createPersonCommand);
                person = createPersonCommand.CreatedPerson;
            }

            // affiliate person with establishment
            person.AffiliateWith(establishment);

            // add email address if necessary
            if (person.GetEmail(command.EmailAddress) == null)
            {
                person.AddEmail(command.EmailAddress);
            }

            // save changes so nested command can find the correct data
            _unitOfWork.SaveChanges();

            // send the message
            var sendCommand = new SendConfirmEmailMessageCommand
            {
                EmailAddress = command.EmailAddress,
                Intent       = EmailConfirmationIntent.CreatePassword,
                SendFromUrl  = command.SendFromUrl,
            };

            _sendHandler.Handle(sendCommand);
            command.ConfirmationToken = sendCommand.ConfirmationToken;
        }
            public void ReturnsCreatePassword_WhenIntentMatches()
            {
                var command = new SendConfirmEmailMessageCommand
                {
                    Intent = EmailConfirmationIntent.CreatePassword,
                };

                command.TemplateName.ShouldEqual(EmailTemplateName.CreatePasswordConfirmation);
            }
Exemplo n.º 4
0
            public void IsInvalidWhen_EmailAddress_IsNotConfirmed_AndIntentIsResetPassword()
            {
                var command = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = "*****@*****.**",
                    Intent       = EmailConfirmationIntent.ResetPassword,
                };
                var person = new Person
                {
                    User = new User
                    {
                        Name = "username",
                    },
                    Emails = new[]
                    {
                        new EmailAddress
                        {
                            Value = command.EmailAddress
                        },
                    },
                };
                var establishment = new Establishment
                {
                    IsMember     = true,
                    EmailDomains = new Collection <EstablishmentEmailDomain>
                    {
                        new EstablishmentEmailDomain {
                            Value = "@domain.tld"
                        },
                    }
                };
                var scenarioOptions = new ScenarioOptions
                {
                    Command           = command,
                    Person            = person,
                    LocalMemberExists = true,
                    Establishment     = establishment,
                };
                var validator = CreateValidator(scenarioOptions);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);

                error.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                error.ErrorMessage.ShouldEqual(string.Format(
                                                   ValidateEmailAddress.FailedBecauseIsNotConfirmed,
                                                   command.EmailAddress));
                // ReSharper restore PossibleNullReferenceException
            }
Exemplo n.º 5
0
            public void IsInvalidWhen_MatchesSamlEstablishment_AndIntentIsResetPassword()
            {
                var command = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = "*****@*****.**",
                    Intent       = EmailConfirmationIntent.ResetPassword,
                };
                var person = new Person
                {
                    DisplayName = "Adam West",
                    Emails      = new Collection <EmailAddress>
                    {
                        new EmailAddress {
                            Value = command.EmailAddress,
                        },
                    },
                };
                var establishment = new Establishment
                {
                    IsMember     = true,
                    SamlSignOn   = new EstablishmentSamlSignOn(),
                    EmailDomains = new Collection <EstablishmentEmailDomain>
                    {
                        new EstablishmentEmailDomain {
                            Value = "@domain.tld",
                        },
                    },
                };
                var scenarioOptions = new ScenarioOptions
                {
                    Command       = command,
                    Person        = person,
                    Establishment = establishment,
                };
                var validator = CreateValidator(scenarioOptions);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);

                error.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                error.ErrorMessage.ShouldEqual(string.Format(
                                                   ValidateEstablishment.FailedBecauseEstablishmentHasSamlSignOn,
                                                   scenarioOptions.Establishment.RevisionId));
                // ReSharper restore PossibleNullReferenceException
            }
Exemplo n.º 6
0
            public void SetsCommandProperty_ConfirmationToken()
            {
                const string emailAddress = "someone";
                var          command      = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = emailAddress,
                    Intent       = EmailConfirmationIntent.CreatePassword,
                    SendFromUrl  = "test",
                };
                var scenarioOptions = new ScenarioOptions(command);
                var handler         = CreateHandler(scenarioOptions);

                handler.Handle(command);

                command.ConfirmationToken.ShouldEqual(scenarioOptions.OutConfirmation.Token);
            }
Exemplo n.º 7
0
            public void GeneratesSecretCode_WithTwelveCharacters()
            {
                const string emailAddress = "someone";
                var          command      = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = emailAddress,
                    Intent       = EmailConfirmationIntent.ResetPassword,
                    SendFromUrl  = "test",
                };
                var scenarioOptions = new ScenarioOptions(command);
                var handler         = CreateHandler(scenarioOptions);

                handler.Handle(command);

                scenarioOptions.OutConfirmation.SecretCode.Length.ShouldEqual(12);
            }
Exemplo n.º 8
0
            public void ExecutesQuery_ForPerson()
            {
                const string emailAddress = "someone";
                var          command      = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = emailAddress,
                    Intent       = EmailConfirmationIntent.ResetPassword,
                    SendFromUrl  = "test",
                };
                var scenarioOptions = new ScenarioOptions(command);
                var handler         = CreateHandler(scenarioOptions);

                handler.Handle(command);

                scenarioOptions.Entities.Verify(m => m.Get <Person>(),
                                                Times.Once());
            }
Exemplo n.º 9
0
            public void IsValidWhen_MatchesNonSamlMemberEstablishment_AndConfirmedEmail_WithPersonHavingLocalNonSamlUser()
            {
                var command = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = "*****@*****.**",
                };
                var person = new Person
                {
                    User = new User
                    {
                        Name = "username",
                    },
                    Emails = new[]
                    {
                        new EmailAddress
                        {
                            Value       = command.EmailAddress,
                            IsConfirmed = true,
                        },
                    },
                };
                var establishment = new Establishment
                {
                    IsMember     = true,
                    EmailDomains = new Collection <EstablishmentEmailDomain>
                    {
                        new EstablishmentEmailDomain {
                            Value = "@domain.tld"
                        },
                    },
                };
                var scenarioOptions = new ScenarioOptions
                {
                    Command           = command,
                    Person            = person,
                    LocalMemberExists = true,
                    Establishment     = establishment,
                };
                var validator = CreateValidator(scenarioOptions);

                var results = validator.Validate(command);

                var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);

                error.ShouldBeNull();
            }
Exemplo n.º 10
0
            public void InvokesNestedHandler()
            {
                const string emailAddress = "someone";
                var          command      = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = emailAddress,
                    Intent       = EmailConfirmationIntent.ResetPassword,
                    SendFromUrl  = "test",
                };
                var scenarioOptions = new ScenarioOptions(command);
                var handler         = CreateHandler(scenarioOptions);

                handler.Handle(command);

                scenarioOptions.NestedHandler.Verify(m => m.
                                                     Handle(It.Is(NestedCommandBasedOn(scenarioOptions))),
                                                     Times.Once());
            }
Exemplo n.º 11
0
            public void CreatesEmailMessage()
            {
                const string emailAddress = "someone";
                var          command      = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = emailAddress,
                    Intent       = EmailConfirmationIntent.CreatePassword,
                    SendFromUrl  = "test",
                };
                var scenarioOptions = new ScenarioOptions(command);
                var handler         = CreateHandler(scenarioOptions);

                handler.Handle(command);

                scenarioOptions.Entities.Verify(m => m.
                                                Create(It.Is(MessageEntityBasedOn(scenarioOptions))),
                                                Times.Once());
            }
Exemplo n.º 12
0
            public void ExecutesQuery_ToComposeEmailMessage()
            {
                const string emailAddress = "someone";
                var          command      = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = emailAddress,
                    Intent       = EmailConfirmationIntent.CreatePassword,
                    SendFromUrl  = "test",
                };
                var scenarioOptions = new ScenarioOptions(command);
                var handler         = CreateHandler(scenarioOptions);

                handler.Handle(command);

                scenarioOptions.QueryProcessor.Verify(m => m.
                                                      Execute(It.Is(CompositionQueryBasedOn(scenarioOptions))),
                                                      Times.Once());
            }
Exemplo n.º 13
0
            public void IsInvalidWhen_IsNull()
            {
                var command         = new SendConfirmEmailMessageCommand();
                var scenarioOptions = new ScenarioOptions();
                var validator       = CreateValidator(scenarioOptions);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);

                error.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                error.ErrorMessage.ShouldEqual(
                    ValidateEmailAddress.FailedBecauseValueWasEmpty);
                // ReSharper restore PossibleNullReferenceException
            }
Exemplo n.º 14
0
            public void CreatesConfrimation_With_EmailAddres_Intent_AndSecretCode()
            {
                const string emailAddress = "someone";
                var          command      = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = emailAddress,
                    Intent       = EmailConfirmationIntent.CreatePassword,
                    SendFromUrl  = "test",
                };
                var scenarioOptions = new ScenarioOptions(command);
                var handler         = CreateHandler(scenarioOptions);

                handler.Handle(command);

                scenarioOptions.Entities.Verify(m => m.
                                                Create(It.Is(ConfirmationEntityBasedOn(scenarioOptions))),
                                                Times.Once());
                scenarioOptions.OutConfirmation.EmailAddress.ShouldEqual(scenarioOptions.Person.GetEmail(emailAddress));
                scenarioOptions.OutConfirmation.Intent.ShouldEqual(command.Intent);
            }
Exemplo n.º 15
0
 internal ScenarioOptions(SendConfirmEmailMessageCommand command)
 {
     Command = command;
     Person  = new Person
     {
         RevisionId = 6,
         Emails     = new[]
         {
             new EmailAddress
             {
                 Value = command.EmailAddress,
             },
         },
     };
     EmailMessage = new EmailMessage
     {
         ToPerson = Person,
         Number   = 7,
     };
     EmailTemplate = new EmailTemplate();
 }
Exemplo n.º 16
0
            public void IsInvalidWhen_MatchesNoEstablishment()
            {
                var command = new SendConfirmEmailMessageCommand
                {
                    EmailAddress = "*****@*****.**",
                };
                var person = new Person
                {
                    DisplayName = "Adam West",
                    Emails      = new Collection <EmailAddress>
                    {
                        new EmailAddress {
                            Value = command.EmailAddress,
                        },
                    },
                };
                var scenarioOptions = new ScenarioOptions
                {
                    Command       = command,
                    Person        = person,
                    Establishment = new Establishment
                    {
                        EmailDomains = new Collection <EstablishmentEmailDomain>(),
                    },
                };
                var validator = CreateValidator(scenarioOptions);

                var results = validator.Validate(command);

                results.IsValid.ShouldBeFalse();
                results.Errors.Count.ShouldBeInRange(1, int.MaxValue);
                var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);

                error.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                error.ErrorMessage.ShouldEqual(string.Format(
                                                   ValidateEstablishment.FailedBecauseEmailMatchedNoEntity,
                                                   command.EmailAddress));
                // ReSharper restore PossibleNullReferenceException
            }
Exemplo n.º 17
0
 private static Expression <Func <GetEmailTemplateByNameQuery, bool> > EmailTemplateQueryBasedOn(SendConfirmEmailMessageCommand command)
 {
     return(q => q.Name == command.TemplateName.AsSentenceFragment());
 }