Example #1
0
            public void ReturnsView_WhenModelState_IsInvalid()
            {
                var form = new ConfirmEmailForm
                {
                    SecretCode = "wrong",
                };
                var services   = new ConfirmEmailServices(null, null);
                var controller = new ConfirmEmailController(services);

                controller.ModelState.AddModelError("error", String.Empty);

                var result = controller.Post(form);

                result.ShouldNotBeNull();
                result.ShouldBeType <ViewResult>();
                var viewResult = (ViewResult)result;

                viewResult.Model.ShouldNotBeNull();
                viewResult.Model.ShouldBeType <ConfirmEmailForm>();
                var model = (ConfirmEmailForm)viewResult.Model;

                model.ShouldEqual(form);
                model.Intent.ShouldEqual(form.Intent);
                model.SecretCode.ShouldEqual(form.SecretCode);
            }
Example #2
0
            public void ReturnsRedirect_ToCreatePassword_WhenIntentMatches()
            {
                var form = new ConfirmEmailForm
                {
                    Token      = Guid.NewGuid(),
                    Intent     = EmailConfirmationIntent.CreatePassword,
                    SecretCode = "secret",
                };
                var commandHandler = new Mock <IHandleCommands <RedeemEmailConfirmationCommand> >(MockBehavior.Strict);

                commandHandler.Setup(m => m.Handle(It.Is(ConfirmationCommandBasedOn(form))));
                var services   = new ConfirmEmailServices(null, commandHandler.Object);
                var controller = new ConfirmEmailController(services);

                var result = controller.Post(form);

                result.ShouldNotBeNull();
                result.ShouldBeType <RedirectToRouteResult>();
                var routeResult = (RedirectToRouteResult)result;

                routeResult.Permanent.ShouldBeFalse();
                routeResult.RouteValues["area"].ShouldEqual(MVC.Identity.Name);
                routeResult.RouteValues["controller"].ShouldEqual(MVC.Identity.CreatePassword.Name);
                routeResult.RouteValues["action"].ShouldEqual(MVC.Identity.CreatePassword.ActionNames.Get);
                routeResult.RouteValues["token"].ShouldEqual(form.Token);
            }
        public virtual ActionResult Post(ConfirmEmailForm model)
        {
            if (model == null)
            {
                return(HttpNotFound());
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // execute the command
            var command = Mapper.Map <RedeemEmailConfirmationCommand>(model);

            _services.CommandHandler.Handle(command);

            // set temp data values
            TempData.EmailConfirmationTicket(command.Ticket);

            // set feedback message
            SetFeedbackMessage(SuccessMessageForIntent[model.Intent]);

            // redirect to ticketed action
            var redeemedRoute = GetRedeemedRouteValues(model.Token, model.Intent);

            return(RedirectToRoute(redeemedRoute));
        }
            public void MapsToken()
            {
                var source = new ConfirmEmailForm
                {
                    Token = Guid.NewGuid(),
                };

                var destination = Mapper.Map<RedeemEmailConfirmationCommand>(source);

                destination.Token.ShouldEqual(source.Token);
            }
            public void MapsSecretCode()
            {
                var source = new ConfirmEmailForm
                {
                    SecretCode = "its a secret",
                };

                var destination = Mapper.Map<RedeemEmailConfirmationCommand>(source);

                destination.SecretCode.ShouldEqual(source.SecretCode);
            }
Example #6
0
            public void ReturnsTrue_WhenModelStateIsValid()
            {
                var model      = new ConfirmEmailForm();
                var controller = new ConfirmEmailController(null);

                var result = controller.ValidateSecretCode(model);

                result.ShouldNotBeNull();
                result.ShouldBeType <JsonResult>();
                result.JsonRequestBehavior.ShouldEqual(JsonRequestBehavior.DenyGet);
                result.Data.ShouldEqual(true);
            }
Example #7
0
            public void ReturnsErrorMessage_WhenModelStateIsInvalid_ForValueProperty()
            {
                const string errorMessage = "Here is your error message.";
                var          model        = new ConfirmEmailForm();
                var          controller   = new ConfirmEmailController(null);

                controller.ModelState.AddModelError(ConfirmEmailForm.SecretCodePropertyName, errorMessage);

                var result = controller.ValidateSecretCode(model);

                result.ShouldNotBeNull();
                result.ShouldBeType <JsonResult>();
                result.JsonRequestBehavior.ShouldEqual(JsonRequestBehavior.DenyGet);
                result.Data.ShouldEqual(errorMessage);
            }
            public void IsInvalidWhen_IsNull()
            {
                var validated = new ConfirmEmailForm();
                var validator = new ConfirmEmailValidator(null);

                var results = validator.Validate(validated);

                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(
                    ConfirmEmailValidator.FailedBecauseSecretCodeWasEmpty);
                // ReSharper restore PossibleNullReferenceException
            }
Example #9
0
        public async Task <IHttpActionResult> ConfirmEmailAsync(ConfirmEmailForm form)
        {
            return
                (!ModelState.IsValid ? BadRequest(ModelState) : !await verifyUserAuthData(form.UserId, form.UserName, form.Password)
                                    ? BadRequest("Nazwa użytkownika, identyfikator lub hasło są niepoprawne") : !await verifyUserTokenAsync(form.UserId, form.Token)
                                    ? BadRequest("podany token jest niepoprawny") : await activateAccountAsync(form.UserId, form.Token));


            async Task <bool> verifyUserAuthData(string userId, string userName, string password) => await AppUserManager.FindAsync(userName, password) is ApplicationUser user ? user.Id == userId.ToString() : false;

            Task <bool> verifyUserTokenAsync(string userId, string token) => AppUserManager.VerifyUserTokenAsync(userId, "Confirmation", token);

            async Task <IHttpActionResult> activateAccountAsync(string userId, string token)
            {
                IdentityResult identityResult = await AppUserManager.ConfirmEmailAsync(userId, token);

                return(identityResult.Succeeded ? Ok() : (IHttpActionResult)InternalServerError(new Exception("Nieoczekiwany błąd serwera")));
            }
        }
Example #10
0
            public void ExecutesCommand_WhenAction_IsValid()
            {
                var form = new ConfirmEmailForm
                {
                    Token      = Guid.NewGuid(),
                    Intent     = EmailConfirmationIntent.ResetPassword,
                    SecretCode = "secret",
                };
                var commandHandler = new Mock <IHandleCommands <RedeemEmailConfirmationCommand> >(MockBehavior.Strict);

                commandHandler.Setup(m => m.Handle(It.Is(ConfirmationCommandBasedOn(form))));
                var services   = new ConfirmEmailServices(null, commandHandler.Object);
                var controller = new ConfirmEmailController(services);

                controller.Post(form);

                commandHandler.Verify(m =>
                                      m.Handle(It.Is(ConfirmationCommandBasedOn(form))),
                                      Times.Once());
            }
        public virtual ActionResult Post(ConfirmEmailForm model)
        {
            if (model == null) return HttpNotFound();

            if (!ModelState.IsValid) return View(model);

            // execute the command
            var command = Mapper.Map<RedeemEmailConfirmationCommand>(model);
            _services.CommandHandler.Handle(command);

            // set temp data values
            TempData.EmailConfirmationTicket(command.Ticket);

            // set feedback message
            SetFeedbackMessage(SuccessMessageForIntent[model.Intent]);

            // redirect to ticketed action
            var redeemedRoute = GetRedeemedRouteValues(model.Token, model.Intent);
            return RedirectToRoute(redeemedRoute);
        }
            public void IsInvalidWhen_Token_MatchesNoEntity()
            {
                var validated = new ConfirmEmailForm
                {
                    Token = Guid.NewGuid(),
                    SecretCode = "secret",
                };
                var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Query<EmailConfirmation>()).Returns(new EmailConfirmation[] { }.AsQueryable);
                var validator = new ConfirmEmailValidator(entities.Object);

                var results = validator.Validate(validated);

                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(
                    ConfirmEmailValidator.FailedBecauseOfInconsistentData);
                // ReSharper restore PossibleNullReferenceException
            }
Example #13
0
            public void FlashesSuccessMessage_ForCreatePassword_WhenIntentMatches()
            {
                var form = new ConfirmEmailForm
                {
                    Token      = Guid.NewGuid(),
                    Intent     = EmailConfirmationIntent.CreatePassword,
                    SecretCode = "secret",
                };
                var commandHandler = new Mock <IHandleCommands <RedeemEmailConfirmationCommand> >(MockBehavior.Strict);

                commandHandler.Setup(m => m.Handle(It.Is(ConfirmationCommandBasedOn(form))));
                var services   = new ConfirmEmailServices(null, commandHandler.Object);
                var controller = new ConfirmEmailController(services);

                controller.Post(form);

                controller.TempData.ShouldNotBeNull();
                var message = controller.TempData.FeedbackMessage();

                message.ShouldNotBeNull();
                message.ShouldEqual(ConfirmEmailController.SuccessMessageForIntent
                                    [EmailConfirmationIntent.CreatePassword]);
            }
Example #14
0
            public void SetsEmailConfirmationTicket_InTempData_UsingCommandValue()
            {
                var form = new ConfirmEmailForm
                {
                    Token      = Guid.NewGuid(),
                    Intent     = EmailConfirmationIntent.ResetPassword,
                    SecretCode = "secret",
                };
                var commandHandler = new Mock <IHandleCommands <RedeemEmailConfirmationCommand> >(MockBehavior.Strict);

                commandHandler.Setup(m => m.Handle(It.Is(ConfirmationCommandBasedOn(form))))
                .Callback((RedeemEmailConfirmationCommand command) =>
                          command.Ticket = TwoFiftySixLengthString1);
                var services   = new ConfirmEmailServices(null, commandHandler.Object);
                var controller = new ConfirmEmailController(services);

                controller.Post(form);

                controller.TempData.ShouldNotBeNull();
                var ticket = controller.TempData.EmailConfirmationTicket(false);

                ticket.ShouldNotBeNull();
                ticket.ShouldEqual(TwoFiftySixLengthString1);
            }
            public void MapsIntent()
            {
                var source = new ConfirmEmailForm
                {
                    Intent = EmailConfirmationIntent.ResetPassword,
                };

                var destination = Mapper.Map<RedeemEmailConfirmationCommand>(source);

                destination.Intent.ShouldEqual(source.Intent);
            }
            public void ReturnsView_WhenModelState_IsInvalid()
            {
                var form = new ConfirmEmailForm
                {
                    SecretCode = "wrong",
                };
                var services = new ConfirmEmailServices(null, null);
                var controller = new ConfirmEmailController(services);
                controller.ModelState.AddModelError("error", String.Empty);

                var result = controller.Post(form);

                result.ShouldNotBeNull();
                result.ShouldBeType<ViewResult>();
                var viewResult = (ViewResult)result;
                viewResult.Model.ShouldNotBeNull();
                viewResult.Model.ShouldBeType<ConfirmEmailForm>();
                var model = (ConfirmEmailForm)viewResult.Model;
                model.ShouldEqual(form);
                model.Intent.ShouldEqual(form.Intent);
                model.SecretCode.ShouldEqual(form.SecretCode);
            }
            public void ExecutesCommand_WhenAction_IsValid()
            {
                var form = new ConfirmEmailForm
                {
                    Token = Guid.NewGuid(),
                    Intent = EmailConfirmationIntent.ResetPassword,
                    SecretCode = "secret",
                };
                var commandHandler = new Mock<IHandleCommands<RedeemEmailConfirmationCommand>>(MockBehavior.Strict);
                commandHandler.Setup(m => m.Handle(It.Is(ConfirmationCommandBasedOn(form))));
                var services = new ConfirmEmailServices(null, commandHandler.Object);
                var controller = new ConfirmEmailController(services);

                controller.Post(form);

                commandHandler.Verify(m =>
                    m.Handle(It.Is(ConfirmationCommandBasedOn(form))),
                        Times.Once());
            }
            public void FlashesSuccessMessage_ForCreatePassword_WhenIntentMatches()
            {
                var form = new ConfirmEmailForm
                {
                    Token = Guid.NewGuid(),
                    Intent = EmailConfirmationIntent.CreatePassword,
                    SecretCode = "secret",
                };
                var commandHandler = new Mock<IHandleCommands<RedeemEmailConfirmationCommand>>(MockBehavior.Strict);
                commandHandler.Setup(m => m.Handle(It.Is(ConfirmationCommandBasedOn(form))));
                var services = new ConfirmEmailServices(null, commandHandler.Object);
                var controller = new ConfirmEmailController(services);

                controller.Post(form);

                controller.TempData.ShouldNotBeNull();
                var message = controller.TempData.FeedbackMessage();
                message.ShouldNotBeNull();
                message.ShouldEqual(ConfirmEmailController.SuccessMessageForIntent
                    [EmailConfirmationIntent.CreatePassword]);
            }
            public void SetsEmailConfirmationTicket_InTempData_UsingCommandValue()
            {
                var form = new ConfirmEmailForm
                {
                    Token = Guid.NewGuid(),
                    Intent = EmailConfirmationIntent.ResetPassword,
                    SecretCode = "secret",
                };
                var commandHandler = new Mock<IHandleCommands<RedeemEmailConfirmationCommand>>(MockBehavior.Strict);
                commandHandler.Setup(m => m.Handle(It.Is(ConfirmationCommandBasedOn(form))))
                    .Callback((RedeemEmailConfirmationCommand command) =>
                        command.Ticket = TwoFiftySixLengthString1);
                var services = new ConfirmEmailServices(null, commandHandler.Object);
                var controller = new ConfirmEmailController(services);

                controller.Post(form);

                controller.TempData.ShouldNotBeNull();
                var ticket = controller.TempData.EmailConfirmationTicket(false);
                ticket.ShouldNotBeNull();
                ticket.ShouldEqual(TwoFiftySixLengthString1);
            }
            public void ReturnsRedirect_ToCreatePassword_WhenIntentMatches()
            {
                var form = new ConfirmEmailForm
                {
                    Token = Guid.NewGuid(),
                    Intent = EmailConfirmationIntent.CreatePassword,
                    SecretCode = "secret",
                };
                var commandHandler = new Mock<IHandleCommands<RedeemEmailConfirmationCommand>>(MockBehavior.Strict);
                commandHandler.Setup(m => m.Handle(It.Is(ConfirmationCommandBasedOn(form))));
                var services = new ConfirmEmailServices(null, commandHandler.Object);
                var controller = new ConfirmEmailController(services);

                var result = controller.Post(form);

                result.ShouldNotBeNull();
                result.ShouldBeType<RedirectToRouteResult>();
                var routeResult = (RedirectToRouteResult)result;
                routeResult.Permanent.ShouldBeFalse();
                routeResult.RouteValues["area"].ShouldEqual(MVC.Identity.Name);
                routeResult.RouteValues["controller"].ShouldEqual(MVC.Identity.CreatePassword.Name);
                routeResult.RouteValues["action"].ShouldEqual(MVC.Identity.CreatePassword.ActionNames.Get);
                routeResult.RouteValues["token"].ShouldEqual(form.Token);
            }
Example #21
0
 private static Expression <Func <RedeemEmailConfirmationCommand, bool> > ConfirmationCommandBasedOn(ConfirmEmailForm model)
 {
     return(q => q.Token == model.Token && q.SecretCode == model.SecretCode && q.Intent == model.Intent);
 }
 private static Expression<Func<RedeemEmailConfirmationCommand, bool>> ConfirmationCommandBasedOn(ConfirmEmailForm model)
 {
     return q => q.Token == model.Token && q.SecretCode == model.SecretCode && q.Intent == model.Intent;
 }
            public void ReturnsErrorMessage_WhenModelStateIsInvalid_ForValueProperty()
            {
                const string errorMessage = "Here is your error message.";
                var model = new ConfirmEmailForm();
                var controller = new ConfirmEmailController(null);
                controller.ModelState.AddModelError(ConfirmEmailForm.SecretCodePropertyName, errorMessage);

                var result = controller.ValidateSecretCode(model);

                result.ShouldNotBeNull();
                result.ShouldBeType<JsonResult>();
                result.JsonRequestBehavior.ShouldEqual(JsonRequestBehavior.DenyGet);
                result.Data.ShouldEqual(errorMessage);
            }
            public void ReturnsTrue_WhenModelStateIsValid()
            {
                var model = new ConfirmEmailForm();
                var controller = new ConfirmEmailController(null);

                var result = controller.ValidateSecretCode(model);

                result.ShouldNotBeNull();
                result.ShouldBeType<JsonResult>();
                result.JsonRequestBehavior.ShouldEqual(JsonRequestBehavior.DenyGet);
                result.Data.ShouldEqual(true);
            }
 public virtual JsonResult ValidateSecretCode(
     [CustomizeValidator(Properties = ConfirmEmailForm.SecretCodePropertyName)] ConfirmEmailForm model)
 {
     return(ValidateRemote(ConfirmEmailForm.SecretCodePropertyName));
 }
            public void IsInvalidWhen_IsIncorrect()
            {
                var confirmation = new EmailConfirmation(EmailConfirmationIntent.ResetPassword)
                {
                    SecretCode = "secret2",
                };
                var validated = new ConfirmEmailForm
                {
                    Token = confirmation.Token,
                    SecretCode = "secret1",
                    Intent = EmailConfirmationIntent.ResetPassword,
                };
                var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Query<EmailConfirmation>()).Returns(new[] { confirmation }.AsQueryable);
                var validator = new ConfirmEmailValidator(entities.Object);

                var results = validator.Validate(validated);

                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(
                    ConfirmEmailValidator.FailedBecauseSecretCodeWasIncorrect);
                // ReSharper restore PossibleNullReferenceException
            }
            public void IsValidWhen_IsCorrect_AndConfirmationIsNotRedeemed()
            {
                var confirmation = new EmailConfirmation(EmailConfirmationIntent.CreatePassword)
                {
                    SecretCode = "secret1",
                };
                var validated = new ConfirmEmailForm
                {
                    Token = confirmation.Token,
                    SecretCode = "secret1",
                    Intent = EmailConfirmationIntent.CreatePassword,
                };
                var entities = new Mock<IQueryEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Query<EmailConfirmation>()).Returns(new[] { confirmation }.AsQueryable);
                var validator = new ConfirmEmailValidator(entities.Object);

                var results = validator.Validate(validated);

                var error = results.Errors.SingleOrDefault(e => e.PropertyName == PropertyName);
                error.ShouldBeNull();
            }
            public void IgnoresTicket()
            {
                var source = new ConfirmEmailForm();

                var destination = Mapper.Map<RedeemEmailConfirmationCommand>(source);

                destination.Ticket.ShouldBeNull();
            }