public async Task Index_PostExecutedWithUnAcceptedTermsOfServiceAndIsPkceClient_ViewResultReturned()
        {
            //Arrange
            var id = Guid.NewGuid().ToString();

            using var termsOfServiceController = new TermsOfServiceController(fakeUserManager, termsOfServiceRepository, mockConfiguration, mockIdentityServerInteractionService,
                                                                              mockEventService, mockClientStore, fakeSignInManager)
                  {
                      ControllerContext = new ControllerContext()
                      {
                          HttpContext = new DefaultHttpContext()
                          {
                              User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
                        {
                            new Claim(ClaimTypes.Name, "example name"),
                            new Claim(ClaimTypes.NameIdentifier, id),
                            new Claim("sub", id),
                            new Claim("custom-claim", "example claim value"),
                        }, "mock")),
                          }
                      }
                  };

            fakeUserManager.SetUserModel(userModel);

            var termOfServiceId          = Guid.NewGuid();
            var termsOfServiceInputModel = new TermsOfServiceInputModel()
            {
                Accepted = false,
                InitialAgreementCount = 3,
                ReturnUrl             = RETURN_URL,
                TermsOfServiceId      = termOfServiceId
            };

            mockIdentityServerInteractionService.GetAuthorizationContextAsync(Arg.Any <string>()).Returns(authorizationRequest);

            client.RequirePkce = true;
            mockClientStore.FindEnabledClientByIdAsync(Arg.Any <string>()).Returns(client);

            // Act
            var actionResult = await termsOfServiceController.Index(termsOfServiceInputModel, "accept", RETURN_URL);

            // Assert
            var viewResult = actionResult as ViewResult;

            Assert.NotNull(viewResult);

            var model = viewResult.Model as RedirectViewModel;

            Assert.NotNull(model);
            Assert.True(model.RedirectUrl == RETURN_URL, $"Redirect URL must be '{RETURN_URL}'.");
        }
        public async Task Index_PostExecutedWithInvalidUserContext_ThrowsAuthenticationException()
        {
            //Arrange
            var id = Guid.NewGuid().ToString();

            using var termsOfServiceController = new TermsOfServiceController(fakeUserManager, termsOfServiceRepository, mockConfiguration, mockIdentityServerInteractionService,
                                                                              mockEventService, mockClientStore, fakeSignInManager)
                  {
                      ControllerContext = new ControllerContext()
                      {
                          HttpContext = new DefaultHttpContext()
                          {
                              User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
                        {
                            new Claim(ClaimTypes.Name, "example name"),
                            new Claim(ClaimTypes.NameIdentifier, id),
                            new Claim("sub", id),
                            new Claim("custom-claim", "example claim value"),
                        }, "mock")),
                          }
                      }
                  };

            fakeUserManager.SetUserModel(null);

            var termOfServiceId          = Guid.NewGuid();
            var termsOfServiceInputModel = new TermsOfServiceInputModel()
            {
                Accepted = true,
                InitialAgreementCount = 3,
                ReturnUrl             = RETURN_URL,
                TermsOfServiceId      = termOfServiceId
            };

            Exception caughtException = null;

            try
            {
                var actionResult = await termsOfServiceController.Index(termsOfServiceInputModel, "accept", RETURN_URL);
            }
            catch (Exception ex)
            {
                caughtException = ex;
            }

            // Assert
            Assert.True(caughtException is AuthenticationException, "Invalid login data must throw an AuthenticationException.");
        }
        public async Task Index_PostExecutedWithAcceptedTermsOfService_ViewResultReturned()
        {
            //Arrange
            var id = Guid.NewGuid().ToString();

            using var termsOfServiceController = new TermsOfServiceController(fakeUserManager, termsOfServiceRepository, mockConfiguration, mockIdentityServerInteractionService,
                                                                              mockEventService, mockClientStore, fakeSignInManager)
                  {
                      ControllerContext = new ControllerContext()
                      {
                          HttpContext = new DefaultHttpContext()
                          {
                              User = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
                        {
                            new Claim(ClaimTypes.Name, "example name"),
                            new Claim(ClaimTypes.NameIdentifier, id),
                            new Claim("sub", id),
                            new Claim("custom-claim", "example claim value"),
                        }, "mock")),
                          }
                      }
                  };

            fakeUserManager.SetUserModel(userModel);

            var termOfServiceId          = Guid.NewGuid();
            var termsOfServiceInputModel = new TermsOfServiceInputModel()
            {
                Accepted = true,
                InitialAgreementCount = 3,
                ReturnUrl             = RETURN_URL,
                TermsOfServiceId      = termOfServiceId
            };

            // Act
            var actionResult = await termsOfServiceController.Index(termsOfServiceInputModel, "accept", RETURN_URL);

            // Assert
            var viewResult = actionResult as RedirectToActionResult;

            Assert.NotNull(viewResult);
            Assert.True(viewResult.ActionName == "Index", "Redirect action must be 'Index'.");
            Assert.True(viewResult.RouteValues.Count == 2, "Route value count must be 2.");
            Assert.True(viewResult.RouteValues["returnUrl"].ToString() == RETURN_URL, $"Route value 'returnUrl' must be '{RETURN_URL}'.");
            Assert.True(viewResult.RouteValues["initialAgreementCount"].ToString() == "3", $"Route value 'returnUrl' must be '3'.");
        }
Example #4
0
        public async Task <IActionResult> Index(TermsOfServiceInputModel model, string button, string returnUrl)
        {
            var user = await userManager.GetUserAsync(HttpContext.User);

            if (user == null)
            {
                throw new AuthenticationException("Invalid login data");
            }

            if (button != "accept" || !model.Accepted)
            {
                return(await CancelTokenRequest(model.ReturnUrl));
            }

            await userManager.AgreeToTermsOfServiceAsync(user, model.TermsOfServiceId);

            return(RedirectToAction("Index", new { returnUrl, initialAgreementCount = model.InitialAgreementCount }));
        }