public BlockRestrictedIPAddressTaskTests() { httpContext = MvcTestHelper.CreateHttpContext(); bannedIpAddressRepository = new Mock <IBannedIPAddressRepository>(); blockRestrictedIpAddress = new BlockRestrictedIPAddress(httpContext.Object, bannedIpAddressRepository.Object); }
public void ResultFormat_should_be_detected_from_accept_types_when_no_format_is_specified(string accpetTypes, ApiResponseFormat format) { var httpContext = MvcTestHelper.CreateHttpContext(); httpContext.SetupGet(c => c.Request.AcceptTypes).Returns(accpetTypes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); httpContext.SetupGet(c => c.Request.UserHostAddress).Returns("192,168.0.1"); var routeData = new RouteData(); routeData.Values.Add("controller", "dummy"); routeData.Values.Add("action", "Process"); var requestContext = new RequestContext(httpContext.Object, routeData); var valueProvider = new Mock <IValueProvider>(); var controller = new DummyController { ValueProvider = valueProvider.Object }; var controllerContext = new ControllerContext(requestContext, controller); controller.ControllerContext = controllerContext; var bindingContext = new ModelBindingContext { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(DummyCommand)), ValueProvider = valueProvider.Object, ModelName = "command", FallbackToEmptyPrefix = true }; var command = (DummyCommand) new ApiCommandBinder().BindModel(controllerContext, bindingContext); Assert.Equal(format, command.ResponseFormat); }
public void MyProfile_should_be_routed_to_user_profile() { var httpContext = MvcTestHelper.CreateHttpContext("~/MyProfile"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Equal("User", routeData.Controller()); Assert.Equal("Profile", routeData.Action()); }
public void GenerateKey_should_be_routed_to_user_generatekey() { var httpContext = MvcTestHelper.CreateHttpContext("~/GenerateKey"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Equal("User", routeData.Controller()); Assert.Equal("GenerateKey", routeData.Action()); }
public ApiResultTests() { apiResult = new ApiResult(ApiResponseFormat.Text); httpContext = MvcTestHelper.CreateHttpContext(); controllerContext = new ControllerContext(httpContext.Object, new RouteData(), new Mock <ControllerBase>().Object); }
public void Content_folder_should_not_be_routed() { var httpContext = MvcTestHelper.CreateHttpContext("~/Content/mainbg.jpg"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Null(routeData.Controller()); Assert.Null(routeData.Action()); }
public UpdateUserLastActivityAttributeTests() { httpContext = MvcTestHelper.CreateHttpContext(); identity = new Mock <IIdentity>(); userService = new Mock <IUserService>(); attribute = new UpdateUserLastActivityAttribute(userService.Object); }
public void Xrds_should_be_routed_to_authentication_xrds() { var httpContext = MvcTestHelper.CreateHttpContext("~/Xrds"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Equal("Authentication", routeData.Controller()); Assert.Equal("Xrds", routeData.Action()); }
public void LogOff_should_be_routed_to_authentication_logoff() { var httpContext = MvcTestHelper.CreateHttpContext("~/LogOff"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Equal("Authentication", routeData.Controller()); Assert.Equal("LogOff", routeData.Action()); }
public void FavIcon_should_not_be_routed() { var httpContext = MvcTestHelper.CreateHttpContext("~/favicon.ico"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Null(routeData.Controller()); Assert.Null(routeData.Action()); }
public void Home_should_be_routed_to_shorturl_create() { var httpContext = MvcTestHelper.CreateHttpContext("~/"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Equal("ShortUrl", routeData.Controller()); Assert.Equal("Create", routeData.Action()); }
public void Api_create_should_be_routed_to_api_create() { var httpContext = MvcTestHelper.CreateHttpContext("~/Api/Create"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Equal("Api", routeData.Controller()); Assert.Equal("Create", routeData.Action()); }
public void Scripts_folder_should_not_be_routed() { var httpContext = MvcTestHelper.CreateHttpContext("~/Scripts/script.js"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Null(routeData.Controller()); Assert.Null(routeData.Action()); }
public void Axd_files_should_not_be_routed() { var httpContext = MvcTestHelper.CreateHttpContext("~/asset.axd"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Null(routeData.Controller()); Assert.Null(routeData.Action()); }
public void ControlPanel_user_should_be_routed_to_control_panel_user() { var httpContext = MvcTestHelper.CreateHttpContext("~/ControlPanel/User/3"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Equal("ControlPanel", routeData.Controller()); Assert.Equal("User", routeData.Action()); Assert.Equal(3, routeData.Value <int>("id")); }
public void ControlPanel_url_should_be_routed_to_control_panel_url() { var httpContext = MvcTestHelper.CreateHttpContext("~/ControlPanel/Url/a1"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Equal("ControlPanel", routeData.Controller()); Assert.Equal("Url", routeData.Action()); Assert.Equal("a1", routeData.Value <string>("alias")); }
public void Alphanumeric_value_should_be_routed_to_shorturl_visit() { var httpContext = MvcTestHelper.CreateHttpContext("~/a1"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Equal("ShortUrl", routeData.Controller()); Assert.Equal("Visit", routeData.Action()); Assert.Equal("a1", routeData.Value <string>("alias")); }
public void P_should_be_routed_to_shorturl_preview() { var httpContext = MvcTestHelper.CreateHttpContext("~/p/a1"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Equal("ShortUrl", routeData.Controller()); Assert.Equal("Preview", routeData.Action()); Assert.Equal("a1", routeData.Value <string>("alias")); }
public void MyUrls_should_be_routed_to_shorturl_list() { var httpContext = MvcTestHelper.CreateHttpContext("~/MyUrls/5"); var routeData = routes.GetRouteData(httpContext.Object); Assert.Equal("ShortUrl", routeData.Controller()); Assert.Equal("List", routeData.Action()); Assert.Equal(5, routeData.Value <int>("page")); }
public void IsAuthorized_should_return_false_when_current_user_is_not_authenticated() { var identity = new Mock <IIdentity>(); var httpContext = MvcTestHelper.CreateHttpContext(); identity.Setup(i => i.IsAuthenticated).Returns(false); httpContext.SetupGet(c => c.User.Identity).Returns(identity.Object); Assert.False(attribute.IsAuthorized(new AuthorizationContext { HttpContext = httpContext.Object })); }
public void OnResultExecuted_should_add_xrds_location_header_to_response() { var httpContext = MvcTestHelper.CreateHttpContext("/", "~/LogOn", null); var controllerContext = new ControllerContext(httpContext.Object, new RouteData(), new Mock <ControllerBase>().Object); var actionExcutingContext = new ResultExecutedContext(controllerContext, new Mock <ActionResult>().Object, false, null); new RegisterRoutes().Execute(); attribute.OnResultExecuted(actionExcutingContext); httpContext.Verify(c => c.Response.AddHeader(It.IsAny <string>(), It.IsAny <string>())); }
public void Should_be_able_to_bind() { const string IPAddress = "192.168.0.1"; const string Url = "http://dotnetshoutout.com/"; const string Alias = "dns"; var apiKey = Guid.NewGuid().ToString(); var httpContext = MvcTestHelper.CreateHttpContext(); httpContext.SetupGet(c => c.Request.UserHostAddress).Returns(IPAddress); httpContext.SetupGet(c => c.Request.QueryString).Returns(new NameValueCollection { { "ApiKey", apiKey }, { "Url", Url }, { "Alias", Alias }, { "format", "xml" } }); var routeData = new RouteData(); routeData.Values.Add("controller", "dummy"); routeData.Values.Add("action", "Process"); var requestContext = new RequestContext(httpContext.Object, routeData); var controller = new DummyController(); var controllerContext = new ControllerContext(requestContext, controller); controller.ControllerContext = controllerContext; var valueProvider = new NameValueCollectionValueProvider(controllerContext.HttpContext.Request.QueryString, CultureInfo.InvariantCulture); controller.ValueProvider = valueProvider; var bindingContext = new ModelBindingContext { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(DummyCommand)), ValueProvider = valueProvider, ModelName = "command", FallbackToEmptyPrefix = true }; var command = (DummyCommand) new ApiCommandBinder().BindModel(controllerContext, bindingContext); Assert.Equal(apiKey, command.ApiKey); Assert.Equal(Url, command.Url); Assert.Equal(Alias, command.Alias); Assert.Equal(IPAddress, command.IPAddress); Assert.Equal(ApiResponseFormat.Xml, command.ResponseFormat); }
public void IsAuthorized_should_return_false_when_current_user_is_authenticated_but_locked() { var identity = new Mock <IIdentity>(); var httpContext = MvcTestHelper.CreateHttpContext(); var user = new Mock <User>(); user.Setup(u => u.IsLockedOut).Returns(true); user.Setup(u => u.ApiSetting).Returns(new ApiSetting()); userService.Setup(srv => srv.GetByName(It.IsAny <string>())).Returns(new UserDTO(user.Object)); identity.Setup(i => i.IsAuthenticated).Returns(true); httpContext.SetupGet(c => c.User.Identity).Returns(identity.Object); Assert.False(attribute.IsAuthorized(new AuthorizationContext { HttpContext = httpContext.Object })); }
public void IsAuthorized_should_return_true_when_current_user_is_authenticated_has_allowed_role() { var user = new UserDTO(new User { Role = Role.Administrator }); var identity = new Mock <IIdentity>(); var httpContext = MvcTestHelper.CreateHttpContext(); identity.Setup(i => i.IsAuthenticated).Returns(true); identity.Setup(i => i.Name).Returns("FakeUser"); httpContext.SetupGet(c => c.User.Identity).Returns(identity.Object); userService.Setup(r => r.GetByName("FakeUser")).Returns(user); attribute.AllowedRole = Role.Administrator; Assert.True(attribute.IsAuthorized(new AuthorizationContext { HttpContext = httpContext.Object })); }
public void Should_populate_ip_address_url_referrer_and_user_name() { const string IPAddress = "192.168.0.1"; const string Url = "http://dotnetshoutout.com/"; const string UserName = "******"; var httpContext = MvcTestHelper.CreateHttpContext(); httpContext.SetupGet(c => c.Request.UserHostAddress).Returns(IPAddress); httpContext.SetupGet(c => c.Request.Url).Returns(new Uri("http://localhost")); httpContext.SetupGet(c => c.Request.UrlReferrer).Returns(new Uri(Url)); httpContext.SetupGet(c => c.Request.ApplicationPath).Returns(string.Empty); httpContext.SetupGet(c => c.User.Identity.IsAuthenticated).Returns(true); httpContext.SetupGet(c => c.User.Identity.Name).Returns(UserName); var routeData = new RouteData(); routeData.Values.Add("controller", "dummy"); routeData.Values.Add("action", "Process"); var requestContext = new RequestContext(httpContext.Object, routeData); var controller = new DummyController(); var controllerContext = new ControllerContext(requestContext, controller); controller.ControllerContext = controllerContext; var bindingContext = new ModelBindingContext { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(DummyUserCommand)), ValueProvider = new Mock <IValueProvider>().Object, FallbackToEmptyPrefix = true }; var command = (DummyUserCommand) new UserCommandBinder().BindModel(controllerContext, bindingContext); Assert.Equal(IPAddress, command.IPAddress); Assert.Equal(Url, command.Referrer); Assert.Equal(UserName, command.UserName); }
public void Should_be_able_to_bind_for_identifier(string userParameterName, string rememberMeParameterName) { var form = new NameValueCollection { { userParameterName, "joe" }, { rememberMeParameterName, "true" } }; var httpContext = MvcTestHelper.CreateHttpContext(); httpContext.SetupGet(c => c.Request.Form).Returns(form); var routeData = new RouteData(); routeData.Values.Add("controller", "dummy"); routeData.Values.Add("action", "Process"); var requestContext = new RequestContext(httpContext.Object, routeData); var controller = new DummyController(); var controllerContext = new ControllerContext(requestContext, controller); controller.ControllerContext = controllerContext; var valueProvider = new NameValueCollectionValueProvider(controllerContext.HttpContext.Request.Form, CultureInfo.CurrentCulture); controller.ValueProvider = valueProvider; var bindingContext = new ModelBindingContext { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(DummyCommand)), ValueProvider = valueProvider, ModelName = "command", FallbackToEmptyPrefix = true }; var command = (DummyCommand) new LogOnCommandBinder().BindModel(controllerContext, bindingContext); Assert.Equal("joe", command.UserName); Assert.True(command.RememberMe.HasValue && command.RememberMe.Value); }