public void WhichShouldNotResolveControllerFilterLogicWithControllerConstructionFunction()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services => services
                          .AddTransient <IInjectedService, InjectedService>());

            var injectedService = new InjectedService();

            MyPipeline
            .Configuration()
            .ShouldMap("/Pipeline/Action?controller=true")
            .To <PipelineController>(c => c.Action())
            .Which(() => new PipelineController(injectedService))
            .ShouldReturn()
            .Ok()
            .AndAlso()
            .ShouldPassForThe <PipelineController>(controller =>
            {
                Assert.Same(injectedService, controller.Service);
                Assert.Null(controller.Data);
            });

            MyApplication.StartsFrom <DefaultStartup>();
        }
        public void WhichShouldOverrideActionFilterValuesInInnerBuilder()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services => services
                          .AddTransient <IInjectedService, InjectedService>());

            const string contextTestKey   = "ControllerFilter";
            const string contextTestValue = "Context Value";

            MyPipeline
            .Configuration()
            .ShouldMap("/Pipeline/Action?controller=true")
            .To <PipelineController>(c => c.Action())
            .Which(controller => controller
                   .WithRouteData(new { ControllerFilter = contextTestValue })
                   .WithControllerContext(context => context.ActionDescriptor.Properties[contextTestKey] = contextTestValue)
                   .WithActionContext(context => context.ModelState.Clear()))
            .ShouldReturn()
            .Ok()
            .AndAlso()
            .ShouldPassForThe <PipelineController>(controller =>
            {
                Assert.Equal(contextTestKey, controller.Data);
                Assert.True(controller.RouteData.Values.ContainsKey(contextTestKey));
                Assert.Equal(contextTestValue, controller.RouteData.Values[contextTestKey]);
                Assert.True(controller.ControllerContext.ActionDescriptor.Properties.ContainsKey(contextTestKey));
                Assert.Equal(contextTestValue, controller.ControllerContext.ActionDescriptor.Properties[contextTestKey]);
                Assert.Empty(controller.ModelState);
            });

            MyApplication.StartsFrom <DefaultStartup>();
        }
        public void WhichShouldResolveCorrectControllerFilterLogicWithExplicitServices()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services => services
                          .AddTransient <IInjectedService, InjectedService>());

            MyPipeline
            .Configuration()
            .ShouldMap("/Pipeline/Action?controller=true")
            .To <PipelineController>(c => c.Action())
            .Which(controller => controller
                   .WithDependencies(new InjectedService()))
            .ShouldReturn()
            .Ok()
            .AndAlso()
            .ShouldPassForThe <PipelineController>(controller =>
            {
                const string testValue = "ControllerFilter";
                Assert.Equal(testValue, controller.Data);
                Assert.True(controller.RouteData.Values.ContainsKey(testValue));
                Assert.True(controller.ControllerContext.ActionDescriptor.Properties.ContainsKey(testValue));
                Assert.True(controller.ModelState.ContainsKey(testValue));
                Assert.NotNull(controller.HttpContext.Features.Get <PipelineController>());
            });

            MyApplication.StartsFrom <DefaultStartup>();
        }
 public void WhichShouldResolveCorrectEmptyAsyncAction()
 {
     MyPipeline
     .Configuration()
     .ShouldMap("/Home/EmptyTask")
     .To <HomeController>(c => c.EmptyTask())
     .Which()
     .ShouldReturnEmpty();
 }
 public void ShouldMapShouldExecuteAuthorizationFiltersAndShouldValidateJustRoutes()
 {
     MyPipeline
     .Configuration()
     .ShouldMap(request => request
                .WithLocation("/Normal/AuthorizedAction")
                .WithUser())
     .To <NormalController>(c => c.AuthorizedAction());
 }
 public void ShouldMapShouldExecuteActionFiltersAndShouldValidateRoutes()
 {
     MyPipeline
     .Configuration()
     .ShouldMap(request => request
                .WithLocation("/Normal/FiltersAction")
                .WithAntiForgeryToken())
     .To <NormalController>(c => c.FiltersAction());
 }
Пример #7
0
 public void PipelineAssertionShouldWorkCorrectlyWithVersioning()
 {
     MyPipeline
     .Configuration()
     .ShouldMap("api/v2.0/versioning")
     .To <VersioningController>(c => c.Index())
     .Which()
     .ShouldReturn()
     .Ok();
 }
Пример #8
0
 public void RouteAssertionShouldWorkCorrectlyWithActionVersioningWhichDoesNotExist()
 {
     MyPipeline
     .Configuration()
     .ShouldMap("api/v3.0/versioning")
     .To <VersioningController>(c => c.SpecificVersion())
     .Which()
     .ShouldReturn()
     .Ok();
 }
 public void GetShouldReturnAllCarAdsWithoutAQuery(int totalCarAds)
 => MyPipeline
 .Configuration()
 .ShouldMap("/CarAds")
 .To <CarAdsController>(c => c.Search(With.Empty <SearchCarAdsQuery>()))
 .Which(instance => instance
        .WithData(A.CollectionOfDummy <CarAd>(totalCarAds)))
 .ShouldReturn()
 .ActionResult <SearchCarAdsOutputModel>(result => result
                                         .Passing(model => model
                                                  .CarAds.Count().Should().Be(totalCarAds)));
 public void GetShouldReturnAvailableCarAdsWithoutAQuery()
 => MyPipeline
 .Configuration()
 .ShouldMap("/CarAds")
 .To <CarAdsController>(c => c.Search(With.Empty <SearchCarAdsQuery>()))
 .Which(instance => instance
        .WithData(CarAdFakes.Data.GetCarAds()))
 .ShouldReturn()
 .ActionResult <SearchCarAdsOutputModel>(result => result
                                         .Passing(model => model
                                                  .CarAds.Count().Should().Be(10)));
 public void ShouldMapShouldExecuteCustomActionFiltersAndShouldValidateRoutes()
 {
     Test.AssertException <RouteAssertionException>(
         () =>
     {
         MyPipeline
         .Configuration()
         .ShouldMap("/Normal/CustomFiltersAction?throw=true")
         .To <NormalController>(c => c.CustomFiltersAction());
     },
         "Expected route '/Normal/CustomFiltersAction' to match CustomFiltersAction action in NormalController but exception was thrown when trying to invoke the pipeline: 'Exception of type 'System.Exception' was thrown.'.");
 }
 public void ShouldMapShouldThrowExceptionIfFiltersAreNotSet()
 {
     Test.AssertException <RouteAssertionException>(
         () =>
     {
         MyPipeline
         .Configuration()
         .ShouldMap("/Normal/FiltersAction")
         .To <NormalController>(c => c.FiltersAction());
     },
         "Expected route '/Normal/FiltersAction' to match FiltersAction action in NormalController but action could not be invoked because of the declared filters - ValidateAntiForgeryTokenAttribute (Action), UnsupportedContentTypeFilter (Global), SaveTempDataAttribute (Global), ControllerActionFilter (Controller). Either a filter is setting the response result before the action itself, or you must set the request properties so that they will pass through the pipeline.");
 }
 public void ShouldMapShouldThrowExceptionIfAuthorizationFiltersAreNotSet()
 {
     Test.AssertException <RouteAssertionException>(
         () =>
     {
         MyPipeline
         .Configuration()
         .ShouldMap("/Normal/AuthorizedAction")
         .To <NormalController>(c => c.AuthorizedAction());
     },
         "Expected route '/Normal/AuthorizedAction' to match AuthorizedAction action in NormalController but exception was thrown when trying to invoke the pipeline: 'No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).'.");
 }
Пример #14
0
        public void SearchShouldReturnAvailableArticlesWithoutAQuery()
        => MyPipeline
        .Configuration()
        .ShouldMap("/Articles")

        .To <ArticlesController>(c => c.Search(With.Empty <SearchArticlesQuery>()))
        .Which(instance => instance
               .WithData(JournalistFakes.Data.GetJournalist()))

        .ShouldReturn()
        .ActionResult <SearchArticlesOutputModel>(result => result
                                                  .Passing(model => model
                                                           .Articles.Count().Should().Be(10)));
 public void WhichShouldResolveCorrectAsyncAction()
 {
     MyPipeline
     .Configuration()
     .ShouldMap("/Home/AsyncMethod")
     .To <HomeController>(c => c.AsyncMethod())
     .Which()
     .ShouldReturn()
     .Ok(ok => ok
         .Passing(result => result
                  .Value
                  .Equals("Test")));
 }
Пример #16
0
        public void SearchShouldReturnAllApartmentAdsWithoutAQuery(int totalApartmentAds)
        => MyPipeline
        .Configuration()
        .ShouldMap("/ApartmentAds")

        .To <ApartmentAdsController>(c => c.Search(With.Empty <SearchApartmentAdsQuery>()))
        .Which(instance => instance
               .WithData(BrokerFakes.Data.GetBroker(totalApartmentAds: totalApartmentAds)))

        .ShouldReturn()
        .ActionResult <SearchApartmentAdsOutputModel>(result => result
                                                      .Passing(model => model
                                                               .ApartmentAds.Count().Should().Be(totalApartmentAds)));
 public void WhichShouldResolveCorrectSyncAction()
 {
     MyPipeline
     .Configuration()
     .ShouldMap("/Home/Contact/1")
     .To <HomeController>(c => c.Contact(1))
     .Which()
     .ShouldReturn()
     .Ok(ok => ok
         .Passing(result => result
                  .Value
                  .Equals(1)));
 }
        public void SearchShouldReturnAprovedReportsWithoutAQuery()
        => MyPipeline
        .Configuration()
        .ShouldMap("/Reports")

        .To <ReportsController>(c => c.Search(With.Empty <SearchReportsQuery>()))
        .Which(instance => instance
               .WithData(ReporterFakes.Data.GetReporter()))

        .ShouldReturn()
        .ActionResult <SearchReportsOutputModel>(result => result
                                                 .Passing(model => model
                                                          .Reports.Count().Should().Be(10)));
Пример #19
0
 public void GetBecomeShouldBeForAuthorizedUsersAndReturnView()
 => MyPipeline
 .Configuration()
 .ShouldMap(request => request
            .WithPath("/Dealers/Become")
            .WithUser())
 .To <DealersController>(c => c.Become())
 .Which()
 .ShouldHave()
 .ActionAttributes(attributes => attributes
                   .RestrictingForAuthorizedRequests())
 .AndAlso()
 .ShouldReturn()
 .View();
Пример #20
0
 public void GetWishlistShouldBeForAuthorizedUsersAndReturnView()
 => MyPipeline
 .Configuration()
 .ShouldMap(request => request
            .WithPath("/Wishlist")
            .WithUser())
 .To <WishlistController>(c => c.Index())
 .Which()
 .ShouldHave()
 .ActionAttributes(attributes => attributes
                   .RestrictingForAuthorizedRequests())
 .AndAlso()
 .ShouldReturn()
 .View();
 public void WhichShouldResolveCorrectActionFilterLogic()
 {
     MyPipeline
     .Configuration()
     .ShouldMap("/Normal/CustomFiltersAction?controller=true")
     .To <NormalController>(c => c.CustomFiltersAction())
     .Which()
     .ShouldReturn()
     .Ok()
     .AndAlso()
     .ShouldPassForThe <NormalController>(controller =>
     {
         Assert.Equal("ActionFilter", controller.Data);
     });
 }
 public void WhichShouldNotResolveCorrectActionResultWhenFilterSetsIt()
 {
     Test.AssertException <RouteAssertionException>(
         () =>
     {
         MyPipeline
         .Configuration()
         .ShouldMap("/Normal/CustomFiltersAction?result=true")
         .To <NormalController>(c => c.CustomFiltersAction())
         .Which()
         .ShouldReturn()
         .Ok();
     },
         "Expected route '/Normal/CustomFiltersAction' to match CustomFiltersAction action in NormalController but action could not be invoked because of the declared filters - CustomActionFilterAttribute (Action), UnsupportedContentTypeFilter (Global), SaveTempDataAttribute (Global), ControllerActionFilter (Controller). Either a filter is setting the response result before the action itself, or you must set the request properties so that they will pass through the pipeline.");
 }
        public void WhichShouldResolveCorrectAsyncActionWithSetup()
        {
            const string testData = "TestData";

            MyPipeline
            .Configuration()
            .ShouldMap("/Home/AsyncMethod")
            .To <HomeController>(c => c.AsyncMethod())
            .Which()
            .WithSetup(c => c.Data = testData)
            .ShouldReturn()
            .Ok(ok => ok
                .Passing(result => result
                         .Value
                         .Equals(testData)));
        }
 public void LoginShouldReturnTokenWhenUserEntersValidCredentials(string email, string password, string token)
 => MyPipeline
 .Configuration()
 .ShouldMap(request => request
            .WithLocation("/Identity/Login")
            .WithMethod(HttpMethod.Post)
            .WithJsonBody(new
 {
     Email    = email,
     Password = password
 }))
 .To <IdentityController>(c => c
                          .Login(new LoginUserCommand(email, password)))
 .Which()
 .ShouldReturn()
 .ActionResult <LoginOutputModel>(result => result
                                  .Passing(model => model.Token.Should().Be(token)));
        public void WhichShouldNotResolveControllerContextWhenWithMethodsAreCalledInInnerBuilder()
        {
            MyApplication
            .StartsFrom <DefaultStartup>()
            .WithServices(services => services
                          .AddTransient <IInjectedService, InjectedService>());

            const string contextTestKey   = "ControllerContext";
            const string contextTestValue = "Context Value";

            MyPipeline
            .Configuration()
            .ShouldMap("/Pipeline/Action?controller=true")
            .To <PipelineController>(c => c.Action())
            .Which(controller => controller
                   .WithHttpContext(context => context.Features.Set(new AnotherInjectedService()))
                   .WithHttpRequest(request => request.WithHeader(contextTestKey, contextTestValue))
                   .WithUser(user => user.WithUsername(contextTestKey))
                   .WithRouteData(new { Id = contextTestKey })
                   .WithControllerContext(context => context.RouteData.Values.Add(contextTestKey, contextTestValue))
                   .WithActionContext(context => context.ModelState.AddModelError(contextTestKey, contextTestValue))
                   .WithTempData(tempData => tempData.WithEntry(contextTestKey, contextTestValue))
                   .WithSetup(controller => controller.HttpContext.Features.Set(new InjectedService())))
            .ShouldReturn()
            .Ok()
            .AndAlso()
            .ShouldPassForThe <PipelineController>(controller =>
            {
                const string testValue = "ControllerFilter";
                Assert.Equal(testValue, controller.Data);
                Assert.True(controller.RouteData.Values.ContainsKey(testValue));
                Assert.True(controller.RouteData.Values.ContainsKey(contextTestKey));
                Assert.True(controller.RouteData.Values.ContainsKey("Id"));
                Assert.True(controller.ControllerContext.ActionDescriptor.Properties.ContainsKey(testValue));
                Assert.True(controller.ModelState.ContainsKey(testValue));
                Assert.True(controller.ModelState.ContainsKey(contextTestKey));
                Assert.NotNull(controller.HttpContext.Features.Get <PipelineController>());
                Assert.NotNull(controller.HttpContext.Features.Get <InjectedService>());
                Assert.NotNull(controller.HttpContext.Features.Get <AnotherInjectedService>());
                Assert.True(controller.HttpContext.Request.Headers.ContainsKey(contextTestKey));
                Assert.True(controller.TempData.ContainsKey(contextTestKey));
                Assert.True(controller.User.Identity.Name == contextTestKey);
            });

            MyApplication.StartsFrom <DefaultStartup>();
        }
Пример #26
0
        public void GetShouldReturnFilteredApartmentAdsWithQuery(string neighborhood)
        => MyPipeline
        .Configuration()
        .ShouldMap($"/ApartmentAds?{nameof(neighborhood)}={neighborhood}")

        .To <ApartmentAdsController>(c => c.Search(new SearchApartmentAdsQuery {
            Neighborhood = neighborhood
        }))
        .Which(instance => instance
               .WithData(ApartmentAdFakes.Data.GetApartmentAds()))

        .ShouldReturn()
        .ActionResult <SearchApartmentAdsOutputModel>(result => result
                                                      .Passing(model =>
        {
            model.ApartmentAds.Count().Should().Be(1);
            model.ApartmentAds.First().Neighborhood.Should().Be(neighborhood);
        }));
        public void SearchShouldReturnFilteredCarAdsWithQuery(string manufacturer)
        => MyPipeline
        .Configuration()
        .ShouldMap($"/CarAds?{nameof(manufacturer)}={manufacturer}")

        .To <CarAdsController>(c => c.Search(new SearchCarAdsQuery {
            Manufacturer = manufacturer
        }))
        .Which(instance => instance
               .WithData(DealerFakes.Data.GetDealer()))

        .ShouldReturn()
        .ActionResult <SearchCarAdsOutputModel>(result => result
                                                .Passing(model =>
        {
            model.CarAds.Count().Should().Be(1);
            model.CarAds.First().Manufacturer.Should().Be(manufacturer);
        }));
Пример #28
0
        public void RegisterShouldReturnOkResult(string email, string password, string name)
        => MyPipeline
        .Configuration()
        .ShouldMap(request => request
                   .WithLocation("/Identity/Register")
                   .WithMethod(HttpMethod.Post)
                   .WithJsonBody(new
        {
            Email    = email,
            Password = password,
            UserName = name
        }))

        .To <IdentityController>(c => c.Register(new RegisterUserCommand(email, password, name)))

        .Which()
        .ShouldReturn()
        .ActionResult();
 public void ShouldMapShouldExecuteActionFiltersAndShouldValidateRoutesAndModelBinding()
 {
     MyPipeline
     .Configuration()
     .ShouldMap(request => request
                .WithLocation("/Normal/FiltersActionWithModelBinding/1")
                .WithMethod(HttpMethod.Post)
                .WithAntiForgeryToken()
                .WithJsonBody(new
     {
         Integer = 1,
         String  = "Text"
     }))
     .To <NormalController>(c => c.FiltersActionWithModelBinding(
                                1,
                                new RequestModel
     {
         Integer = 1,
         String  = "Text"
     }));
 }
Пример #30
0
 public void PostBecomeShouldBeForAuthorizedUsersAndReturnRedirectWithValidModel(
     string dealerName,
     string phoneNumber)
 => MyPipeline
 .Configuration()
 .ShouldMap(request => request
            .WithPath("/Dealers/Become")
            .WithMethod(HttpMethod.Post)
            .WithFormFields(new
 {
     Name        = dealerName,
     PhoneNumber = phoneNumber
 })
            .WithUser()
            .WithAntiForgeryToken())
 .To <DealersController>(c => c.Become(new BecomeDealerFormModel
 {
     Name        = dealerName,
     PhoneNumber = phoneNumber
 }))
 .Which()
 .ShouldHave()
 .ActionAttributes(attributes => attributes
                   .RestrictingForHttpMethod(HttpMethod.Post)
                   .RestrictingForAuthorizedRequests())
 .ValidModelState()
 .Data(data => data
       .WithSet <Dealer>(dealers => dealers
                         .Any(d =>
                              d.Name == dealerName &&
                              d.PhoneNumber == phoneNumber &&
                              d.UserId == TestUser.Identifier)))
 .TempData(tempData => tempData
           .ContainingEntryWithKey(GlobalMessageKey))
 .AndAlso()
 .ShouldReturn()
 .Redirect(redirect => redirect
           .To <CarsController>(c => c.All(With.Any <AllCarsQueryModel>())));