public async Task ForgotPassword_ShouldReturnBadRequest_WhenEmailNotConfirmed() { mapper.Execute(); string testEmail = "testEmail"; string testId = "testId"; var userStore = new Mock <IUserStore <User> >(); var userManagerMock = new Mock <ApplicationUserManager>(userStore.Object); userManagerMock.Setup(x => x.FindByNameAsync(testEmail)) .ReturnsAsync(() => new User() { Email = testEmail, EmailConfirmed = false, Id = testId }); AccountEmailRequestModel request = new AccountEmailRequestModel() { Email = testEmail }; var controller = new AccountController(null, null, null, userManagerMock.Object, null, null); var result = await controller.ForgotPassword(request); Assert.IsInstanceOfType(result, typeof(BadRequestErrorMessageResult)); string responseMessage = ((BadRequestErrorMessageResult)result).Message; Assert.IsTrue(responseMessage.Contains(GlobalConstants.FindingUserError)); userManagerMock.VerifyAll(); }
public async Task <IHttpActionResult> ForgotPassword(AccountEmailRequestModel model) { try { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.Email); if (user == null || !user.EmailConfirmed) { return(BadRequest(GlobalConstants.FindingUserError)); } string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); string encodedCode = HttpUtility.UrlEncode(code); await this.emails.SendEmail(user.Email, GlobalConstants.ResetPasswordSubject, string.Format(GlobalConstants.ResetPasswordBody, encodedCode, $"{GlobalConstants.AppDomainPath}/{GlobalConstants.ResetPasswordPath}"), GlobalConstants.SMTPServer, GlobalConstants.EmailPrimary, GlobalConstants.EmailPrimaryPassword); return(Ok()); } return(BadRequest(ModelState)); } catch (Exception e) { HandlExceptionLogging(e, "", controllerName); return(InternalServerError()); } }
public async Task SendEmailConfirmation_ShouldReturnOkResult() { mapper.Execute(); string testEmail = "testEmail"; string testId = "testId"; string testCode = "testCode"; string testRoute = "/testRoute"; string fullCallbackUrl = $"{GlobalConstants.AppDomainPath}/confirmemail?userid={testId}&code={testCode}"; var usersMock = new Mock <IUsersService>(); usersMock.Setup(x => x.GetByEmail(testEmail)).Returns(() => new User() { Id = testId, Email = testEmail }); var userStore = new Mock <IUserStore <User> >(); var userManagerMock = new Mock <ApplicationUserManager>(userStore.Object); userManagerMock.Setup(x => x.GenerateEmailConfirmationTokenAsync(testId)).ReturnsAsync(testCode); var emailsMock = new Mock <IEmailsService>(); emailsMock.Setup(x => x.SendEmail(testEmail, GlobalConstants.ConfirmEmailSubject, string.Format(GlobalConstants.ConfirmEmailBody, fullCallbackUrl), GlobalConstants.SMTPServer, GlobalConstants.EmailPrimary, GlobalConstants.EmailPrimaryPassword)); var urlMock = new Mock <UrlHelper>(); urlMock.Setup(m => m.Route(It.IsAny <string>(), It.IsAny <object>())).Returns(testRoute); AccountEmailRequestModel request = new AccountEmailRequestModel() { Email = testEmail }; var controller = new ManageUsersController(usersMock.Object, emailsMock.Object, userManagerMock.Object, null) { Url = urlMock.Object }; var result = await controller.SendEmailConfirmation(request); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult <string>)); var responseContent = ((OkNegotiatedContentResult <string>)result).Content; Assert.AreEqual(fullCallbackUrl, responseContent); usersMock.VerifyAll(); userManagerMock.VerifyAll(); emailsMock.VerifyAll(); }
public async Task ForgotPassword_ShouldReturnOkResult() { mapper.Execute(); string testEmail = "testEmail"; string testId = "testId"; string testCode = "testCode"; var userStore = new Mock <IUserStore <User> >(); var userManagerMock = new Mock <ApplicationUserManager>(userStore.Object); userManagerMock.Setup(x => x.FindByNameAsync(testEmail)) .ReturnsAsync(() => new User() { Email = testEmail, EmailConfirmed = true, Id = testId }); userManagerMock.Setup(x => x.GeneratePasswordResetTokenAsync(testId)) .ReturnsAsync(() => testCode); var emailsMock = new Mock <IEmailsService>(); emailsMock.Setup(x => x.SendEmail(testEmail, GlobalConstants.ResetPasswordSubject, string.Format(GlobalConstants.ResetPasswordBody, testCode), GlobalConstants.SMTPServer, GlobalConstants.EmailPrimary, GlobalConstants.EmailPrimaryPassword)); AccountEmailRequestModel request = new AccountEmailRequestModel() { Email = testEmail }; var controller = new AccountController(null, null, emailsMock.Object, userManagerMock.Object, null, null); var result = await controller.ForgotPassword(request); Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult <string>)); var responseContent = ((OkNegotiatedContentResult <string>)result).Content; Assert.AreEqual(responseContent, testCode); userManagerMock.VerifyAll(); emailsMock.VerifyAll(); }
public async Task <IHttpActionResult> SendEmailConfirmation(AccountEmailRequestModel model) { try { User user = this.users.GetByEmail(model.Email); string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); string encodedCode = HttpUtility.UrlEncode(code); string encodedUserId = HttpUtility.UrlEncode(user.Id); string fullCallbackUrl = $"{GlobalConstants.AppDomainPath}/confirmemail?userid={encodedUserId}&code={encodedCode}"; await this.emails.SendEmail(user.Email, GlobalConstants.ConfirmEmailSubject, string.Format(GlobalConstants.ConfirmEmailBody, fullCallbackUrl), GlobalConstants.SMTPServer, GlobalConstants.EmailPrimary, GlobalConstants.EmailPrimaryPassword); return(Ok()); } catch (Exception e) { HandlExceptionLogging(e, "", controllerName); return(InternalServerError()); } }