public async Task <IActionResult> OnPostAsync() { if (this.HttpContext.User?.Identity?.Name != null) { string userName = this.HttpContext.User.Identity.Name; if (!Request.Cookies.ContainsKey(UserConstants.SkillsConfirmationCookieName)) { UserProfile userProfile = _userProfileService.GetUser(); UserConfirmModel confirmSkillsModel = new UserConfirmModel() { Name = userProfile.Fullname, Username = userName, }; ValidatedApiResponse <User> confirmResult = await _usersApiClient.ConfirmSkills(userProfile.Id, confirmSkillsModel); if (confirmResult.StatusCode != HttpStatusCode.OK) { throw new InvalidOperationException($"Failed to confirm skills for {userName}"); } SetCookie(); } } return(Redirect("/")); }
public async Task <IActionResult> UpdateHasConfirmedSkills() { if (EnableAuthBypass()) { return(Ok()); } if (!User.Identity.IsAuthenticated) { return(new UnauthorizedResult()); } UserProfile userProfile = _userProfileService.GetUser(); UserConfirmModel confirmSkillsModel = new UserConfirmModel { Name = userProfile.Fullname, Username = User.Identity.Name }; ValidatedApiResponse <User> confirmResult = await _usersApiClient.ConfirmSkills(userProfile.Id, confirmSkillsModel); if (confirmResult.StatusCode == HttpStatusCode.OK) { return(Ok()); } return(confirmResult.ModelState == null?BadRequest("Could not confirm user skills") : BadRequest(confirmResult.ModelState)); }
public void OnPostAsync_GivenCookieDoesNotAlreadyExistsAndCallingConfirmSkillsReturnsBadRequest_ReturnsInternalServerError() { //Arrange const string upn = "*****@*****.**"; IIdentity identity = Substitute.For <IIdentity>(); identity .Name .Returns(upn); ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity); HttpContext httpContext = Substitute.For <HttpContext>(); httpContext .User .Returns(claimsPrincipal); UserProfile userProfile = new UserProfile() { Firstname = "First", Lastname = "Last", Id = "123", UPN = upn }; IUserProfileService userProfileService = CreateUserProfileService(); userProfileService .GetUser() .Returns(userProfile); IUsersApiClient apiClient = CreateUsersApiClient(); apiClient .ConfirmSkills(Arg.Is("123"), Arg.Is <UserConfirmModel>(u => u.Username == upn && u.Name == userProfile.Fullname)) .Returns(new ValidatedApiResponse <User>(HttpStatusCode.BadRequest, (User)null)); ConfirmSkillsModel pageModel = CreatePageModel(apiClient, userProfileService); pageModel.PageContext = new PageContext { HttpContext = httpContext }; //Act Func <Task> test = () => pageModel.OnPostAsync(); //Assert test .Should() .ThrowExactly <InvalidOperationException>() .Which .Message .Should() .Be("Failed to confirm skills for [email protected]"); }
public async Task OnPostAsync_GivenCookieDoesNotAlreadyExistsAndCallingConfirmSkillsReturnsNoContent_SetsCookieReturnsRedirectResult() { //Arrange const string upn = "*****@*****.**"; IIdentity identity = Substitute.For <IIdentity>(); identity .Name .Returns(upn); ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity); HttpContext httpContext = Substitute.For <HttpContext>(); httpContext .User .Returns(claimsPrincipal); UserConfirmModel userConfirmModel = new UserConfirmModel(); User user = new User(); UserProfile userProfile = new UserProfile() { Firstname = "First", Lastname = "Last", Id = "123", UPN = upn }; IUserProfileService userProfileService = CreateUserProfileService(); userProfileService .GetUser() .Returns(userProfile); IUsersApiClient apiClient = CreateUsersApiClient(); apiClient .ConfirmSkills(Arg.Is("123"), Arg.Is <UserConfirmModel>(u => u.Username == upn && u.Name == userProfile.Fullname)) .Returns(new ValidatedApiResponse <User>(HttpStatusCode.OK, user)); ConfirmSkillsModel pageModel = CreatePageModel(apiClient, userProfileService); pageModel.PageContext = new PageContext { HttpContext = httpContext }; //Act IActionResult result = await pageModel.OnPostAsync(); //Assert result .Should() .BeOfType <RedirectResult>() .Which .Url .Should() .Be("/"); }