public async void Search_200_Result() { var startup = new TestStartup( //This will be invoked before the controller is created so we can modify these mocked services (request, umbCtx, typedContent, serviceContext, searchProvider) => { var mockSearchResults = new Mock <ISearchResults>(); mockSearchResults.Setup(results => results.TotalItemCount).Returns(10); mockSearchResults.Setup(results => results.Skip(It.IsAny <int>())).Returns(new[] { new SearchResult() { Id = 789 }, new SearchResult() { Id = 456 }, }); var mockSearchProvider = Mock.Get(searchProvider); mockSearchProvider.Setup(x => x.CreateSearchCriteria()).Returns(Mock.Of <ISearchCriteria>()); mockSearchProvider.Setup(x => x.Search(It.IsAny <ISearchCriteria>(), It.IsAny <int>())) .Returns(mockSearchResults.Object); var mockMediaService = Mock.Get(serviceContext.MediaService); mockMediaService.Setup(x => x.GetByIds(It.IsAny <IEnumerable <int> >())) .Returns(new[] { ModelMocks.SimpleMockedMedia(789), ModelMocks.SimpleMockedMedia(456) }); }); using (var server = TestServer.Create(builder => startup.Configuration(builder))) { var request = new HttpRequestMessage() { RequestUri = new Uri(string.Format("http://testserver/umbraco/rest/v1/{0}/search?lucene=parentID:\\-1", RouteConstants.MediaSegment)), Method = HttpMethod.Get, }; request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json")); Console.WriteLine(request); var result = await server.HttpClient.SendAsync(request); Console.WriteLine(result); var json = await((StreamContent)result.Content).ReadAsStringAsync(); Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented)); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); } }
public async Task Get_Children_Is_200_With_Params_Result() { var startup = new TestStartup( //This will be invoked before the controller is created so we can modify these mocked services (testServices) => { MockServicesForAuthorizationSuccess(testServices, 123); var mockMediaService = Mock.Get(testServices.ServiceContext.MediaService); mockMediaService.Setup(x => x.GetById(It.IsAny <int>())).Returns(() => ModelMocks.SimpleMockedMedia()); long total = 6; mockMediaService.Setup(x => x.GetPagedChildren(It.IsAny <int>(), It.IsAny <long>(), It.IsAny <int>(), out total, It.IsAny <string>(), Direction.Ascending, It.IsAny <string>())) .Returns(new List <IMedia>(new[] { ModelMocks.SimpleMockedMedia(789), ModelMocks.SimpleMockedMedia(456) })); mockMediaService.Setup(x => x.HasChildren(It.IsAny <int>())).Returns(true); }); using (var server = TestServer.Create(builder => startup.UseDefaultTestSetup(builder))) { var request = new HttpRequestMessage() { RequestUri = new Uri($"http://testserver/umbraco/rest/v1/{RouteConstants.MediaSegment}/123/children?page=2&size=2"), Method = HttpMethod.Get, }; request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json")); Console.WriteLine(request); var result = await server.HttpClient.SendAsync(request); Console.WriteLine(result); var json = await((StreamContent)result.Content).ReadAsStringAsync(); Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented)); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); var djson = JsonConvert.DeserializeObject <JObject>(json); Assert.AreEqual(6, djson["totalResults"].Value <int>()); Assert.AreEqual(2, djson["page"].Value <int>()); Assert.AreEqual(2, djson["pageSize"].Value <int>()); Assert.IsNotNull(djson["_links"]["next"]); Assert.IsNotNull(djson["_links"]["prev"]); } }
public async void Get_Root_Result() { var startup = new TestStartup( //This will be invoked before the controller is created so we can modify these mocked services, (request, umbCtx, typedContent, serviceContext, searchProvider) => { var mockMediaService = Mock.Get(serviceContext.MediaService); mockMediaService.Setup(x => x.GetRootMedia()).Returns(new[] { ModelMocks.SimpleMockedMedia(123, -1), ModelMocks.SimpleMockedMedia(456, -1) }); mockMediaService.Setup(x => x.GetChildren(123)).Returns(new[] { ModelMocks.SimpleMockedMedia(789, 123) }); mockMediaService.Setup(x => x.GetChildren(456)).Returns(new[] { ModelMocks.SimpleMockedMedia(321, 456) }); }); using (var server = TestServer.Create(builder => startup.Configuration(builder))) { var request = new HttpRequestMessage() { RequestUri = new Uri(string.Format("http://testserver/umbraco/rest/v1/{0}", RouteConstants.MediaSegment)), Method = HttpMethod.Get, }; request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json")); Console.WriteLine(request); var result = await server.HttpClient.SendAsync(request); Console.WriteLine(result); var json = await((StreamContent)result.Content).ReadAsStringAsync(); Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented)); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); var asdf = GlobalConfiguration.Configuration; var djson = JsonConvert.DeserializeObject <JObject>(json); Assert.AreEqual("/umbraco/rest/v1/media", djson["_links"]["root"]["href"].Value <string>()); Assert.AreEqual(2, djson["totalResults"].Value <int>()); Assert.AreEqual(2, djson["_links"]["content"].Count()); Assert.AreEqual(2, djson["_embedded"]["content"].Count()); } }
public async Task Get_Root_With_OPTIONS() { var startup = new TestStartup( //This will be invoked before the controller is created so we can modify these mocked services, (testServices) => { var mockMediaService = Mock.Get(testServices.ServiceContext.MediaService); mockMediaService.Setup(x => x.GetRootMedia()).Returns(new[] { ModelMocks.SimpleMockedMedia(123, -1), ModelMocks.SimpleMockedMedia(456, -1) }); mockMediaService.Setup(x => x.GetChildren(123)).Returns(new[] { ModelMocks.SimpleMockedMedia(789, 123) }); mockMediaService.Setup(x => x.GetChildren(456)).Returns(new[] { ModelMocks.SimpleMockedMedia(321, 456) }); }); await Get_Root_With_OPTIONS(startup.UseDefaultTestSetup, RouteConstants.MediaSegment); }
public async void Get_Root_With_OPTIONS() { var startup = new TestStartup( //This will be invoked before the controller is created so we can modify these mocked services, (request, umbCtx, typedContent, serviceContext, searchProvider) => { var mockMediaService = Mock.Get(serviceContext.MediaService); mockMediaService.Setup(x => x.GetRootMedia()).Returns(new[] { ModelMocks.SimpleMockedMedia(123, -1), ModelMocks.SimpleMockedMedia(456, -1) }); mockMediaService.Setup(x => x.GetChildren(123)).Returns(new[] { ModelMocks.SimpleMockedMedia(789, 123) }); mockMediaService.Setup(x => x.GetChildren(456)).Returns(new[] { ModelMocks.SimpleMockedMedia(321, 456) }); }); using (var server = TestServer.Create(builder => startup.Configuration(builder))) { var request = new HttpRequestMessage() { RequestUri = new Uri(string.Format("http://testserver/umbraco/rest/v1/{0}", RouteConstants.MediaSegment)), Method = HttpMethod.Options, }; request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json")); request.Headers.Add("Access-Control-Request-Headers", "accept, authorization"); request.Headers.Add("Access-Control-Request-Method", "GET"); request.Headers.Add("Origin", "http://localhost:12061"); request.Headers.Add("Referer", "http://localhost:12061/browser.html"); Console.WriteLine(request); var result = await server.HttpClient.SendAsync(request); Console.WriteLine(result); var json = await((StreamContent)result.Content).ReadAsStringAsync(); Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented)); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); } }
/// <summary> /// Sets up the services to return the correct data based on the Authorization logic for the non-published content controller /// </summary> /// <param name="testServices"></param> /// <param name="contentIds"></param> /// <remarks> /// Much of this is based on the call to Umbraco Core's ContentController.CheckPermissions which performs quite a few checks. /// Ideally we'd move this authorization logic to an interface so we can mock it instead. /// </remarks> private void MockServicesForAuthorizationSuccess(TestServices testServices, params int[] contentIds) { foreach (var contentId in contentIds) { Mock.Get(testServices.ServiceContext.MediaService) .Setup(x => x.GetById(contentId)) .Returns(ModelMocks.SimpleMockedMedia(contentId)); } Mock.Get(testServices.ServiceContext.EntityService) .Setup(x => x.GetAllPaths(UmbracoObjectTypes.Media, It.IsAny <int[]>())) .Returns((UmbracoObjectTypes objType, int[] ids) => { return(ids.Select(i => new EntityPath { Id = i, Path = i == Constants.System.Root ? "-1" : string.Concat("-1,", i) })); }); }
public async Task Search_200_Result() { var startup = new TestStartup( //This will be invoked before the controller is created so we can modify these mocked services (testServices) => { MockServicesForAuthorizationSuccess(testServices); var mockSearchResults = new Mock <ISearchResults>(); mockSearchResults.Setup(results => results.TotalItemCount).Returns(10); mockSearchResults.Setup(results => results.Skip(It.IsAny <int>())).Returns(new[] { new SearchResult() { Id = 789 }, new SearchResult() { Id = 456 }, }); var mockSearchProvider = Mock.Get(testServices.SearchProvider); mockSearchProvider.Setup(x => x.CreateSearchCriteria()).Returns(Mock.Of <ISearchCriteria>()); mockSearchProvider.Setup(x => x.Search(It.IsAny <ISearchCriteria>(), It.IsAny <int>())) .Returns(mockSearchResults.Object); var mockMediaService = Mock.Get(testServices.ServiceContext.MediaService); mockMediaService.Setup(x => x.GetByIds(It.IsAny <IEnumerable <int> >())) .Returns(new[] { ModelMocks.SimpleMockedMedia(789), ModelMocks.SimpleMockedMedia(456) }); }); await Search_200_Result(startup.UseDefaultTestSetup, RouteConstants.MediaSegment); }
public async Task Get_Root_Result() { var startup = new TestStartup( //This will be invoked before the controller is created so we can modify these mocked services, (testServices) => { MockServicesForAuthorizationSuccess(testServices, 123, 456); var mockMediaService = Mock.Get(testServices.ServiceContext.MediaService); mockMediaService.Setup(x => x.GetRootMedia()).Returns(new[] { ModelMocks.SimpleMockedMedia(123, -1), ModelMocks.SimpleMockedMedia(456, -1) }); mockMediaService.Setup(x => x.GetChildren(123)).Returns(new[] { ModelMocks.SimpleMockedMedia(789, 123) }); mockMediaService.Setup(x => x.GetChildren(456)).Returns(new[] { ModelMocks.SimpleMockedMedia(321, 456) }); }); var djson = await Get_Root_Result(startup.UseDefaultTestSetup, RouteConstants.MediaSegment); Assert.AreEqual(2, djson["_links"]["content"].Count()); Assert.AreEqual(2, djson["_embedded"]["content"].Count()); }
public async Task Get_Metadata_Is_200() { var startup = new TestStartup( //This will be invoked before the controller is created so we can modify these mocked services (testServices) => { MockServicesForAuthorizationSuccess(testServices, 123); var mockMediaService = Mock.Get(testServices.ServiceContext.MediaService); mockMediaService.Setup(x => x.GetById(It.IsAny <int>())).Returns(() => ModelMocks.SimpleMockedMedia()); mockMediaService.Setup(x => x.GetChildren(It.IsAny <int>())).Returns(new List <IMedia>(new[] { ModelMocks.SimpleMockedMedia(789) })); mockMediaService.Setup(x => x.HasChildren(It.IsAny <int>())).Returns(true); var mockTextService = Mock.Get(testServices.ServiceContext.TextService); mockTextService.Setup(x => x.Localize(It.IsAny <string>(), It.IsAny <CultureInfo>(), It.IsAny <IDictionary <string, string> >())) .Returns((string input, CultureInfo culture, IDictionary <string, string> tokens) => input); }); await Get_Metadata_Is_200(startup.UseDefaultTestSetup, RouteConstants.MediaSegment); }
public async Task Get_Id_Result() { var startup = new TestStartup( //This will be invoked before the controller is created so we can modify these mocked services (testServices) => { MockServicesForAuthorizationSuccess(testServices, 123); var mockMediaService = Mock.Get(testServices.ServiceContext.MediaService); mockMediaService.Setup(x => x.GetById(It.IsAny <int>())).Returns(() => ModelMocks.SimpleMockedMedia()); mockMediaService.Setup(x => x.GetChildren(It.IsAny <int>())).Returns(new List <IMedia>(new[] { ModelMocks.SimpleMockedMedia(789) })); mockMediaService.Setup(x => x.HasChildren(It.IsAny <int>())).Returns(true); }); var djson = await Get_Id_Result(startup.UseDefaultTestSetup, RouteConstants.MediaSegment); Assert.AreEqual($"/umbraco/rest/v1/{RouteConstants.MediaSegment}/123", djson["_links"]["self"]["href"].Value <string>()); Assert.AreEqual($"/umbraco/rest/v1/{RouteConstants.MediaSegment}/456", djson["_links"]["parent"]["href"].Value <string>()); Assert.AreEqual($"/umbraco/rest/v1/{RouteConstants.MediaSegment}/123/children{{?page,size,query}}", djson["_links"]["children"]["href"].Value <string>()); Assert.AreEqual($"/umbraco/rest/v1/{RouteConstants.MediaSegment}", djson["_links"]["root"]["href"].Value <string>()); var properties = djson["properties"].ToObject <IDictionary <string, object> >(); Assert.AreEqual(2, properties.Count); Assert.IsTrue(properties.ContainsKey("TestProperty1")); Assert.IsTrue(properties.ContainsKey("testProperty2")); }
public async void Get_Children_Is_200_Response() { var startup = new TestStartup( //This will be invoked before the controller is created so we can modify these mocked services (request, umbCtx, typedContent, serviceContext, searchProvider) => { var mockMediaService = Mock.Get(serviceContext.MediaService); mockMediaService.Setup(x => x.GetById(It.IsAny <int>())).Returns(() => ModelMocks.SimpleMockedMedia()); mockMediaService.Setup(x => x.GetChildren(It.IsAny <int>())).Returns(new List <IMedia>(new[] { ModelMocks.SimpleMockedMedia(789) })); mockMediaService.Setup(x => x.HasChildren(It.IsAny <int>())).Returns(true); }); using (var server = TestServer.Create(builder => startup.Configuration(builder))) { var request = new HttpRequestMessage() { RequestUri = new Uri(string.Format("http://testserver/umbraco/rest/v1/{0}/123/children", RouteConstants.MediaSegment)), Method = HttpMethod.Get, }; request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json")); Console.WriteLine(request); var result = await server.HttpClient.SendAsync(request); Console.WriteLine(result); var json = await((StreamContent)result.Content).ReadAsStringAsync(); Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented)); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); } }
public async void Get_Id_Result() { var startup = new TestStartup( //This will be invoked before the controller is created so we can modify these mocked services (request, umbCtx, typedContent, serviceContext, searchProvider) => { var mockMediaService = Mock.Get(serviceContext.MediaService); mockMediaService.Setup(x => x.GetById(It.IsAny <int>())).Returns(() => ModelMocks.SimpleMockedMedia()); mockMediaService.Setup(x => x.GetChildren(It.IsAny <int>())).Returns(new List <IMedia>(new[] { ModelMocks.SimpleMockedMedia(789) })); mockMediaService.Setup(x => x.HasChildren(It.IsAny <int>())).Returns(true); }); using (var server = TestServer.Create(builder => startup.Configuration(builder))) { var request = new HttpRequestMessage() { RequestUri = new Uri(string.Format("http://testserver/umbraco/rest/v1/{0}/123", RouteConstants.MediaSegment)), Method = HttpMethod.Get, }; request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json")); Console.WriteLine(request); var result = await server.HttpClient.SendAsync(request); Console.WriteLine(result); var json = await((StreamContent)result.Content).ReadAsStringAsync(); Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented)); Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); var djson = JsonConvert.DeserializeObject <JObject>(json); Assert.AreEqual("/umbraco/rest/v1/media/123", djson["_links"]["self"]["href"].Value <string>()); Assert.AreEqual("/umbraco/rest/v1/media/456", djson["_links"]["parent"]["href"].Value <string>()); Assert.AreEqual("/umbraco/rest/v1/media/123/children{?pageIndex,pageSize}", djson["_links"]["children"]["href"].Value <string>()); Assert.AreEqual("/umbraco/rest/v1/media", djson["_links"]["root"]["href"].Value <string>()); var properties = djson["properties"].ToObject <IDictionary <string, object> >(); Assert.AreEqual(2, properties.Count()); Assert.IsTrue(properties.ContainsKey("TestProperty1")); Assert.IsTrue(properties.ContainsKey("testProperty2")); } }