Beispiel #1
0
        public async Task Supports_Post()
        {
            var startup = new TestStartup(
                //This will be invoked before the controller is created so we can modify these mocked services
                (testServices) =>
            {
                ContentServiceMocks.SetupMocksForPost(testServices.ServiceContext);
            });

            using (var server = TestServer.Create(app =>
            {
                var httpConfig = startup.UseTestWebApiConfiguration(app);
                app.AuthenticateEverything();
                app.UseUmbracoRestApi(startup.ApplicationContext, new UmbracoRestApiOptions
                {
                    //customize the authz policies, in this case we want to allow everything
                    CustomAuthorizationPolicyCallback = (policyName, defaultPolicy) => (builder => builder.RequireAssertion(context => true))
                });
                app.UseWebApi(httpConfig);
            }))
            {
                var request = new HttpRequestMessage()
                {
                    RequestUri = new Uri($"http://testserver/umbraco/rest/v1/{RouteConstants.ContentSegment}"),
                    Method     = HttpMethod.Post,
                };

                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json"));
                request.Headers.Add("Origin", "http://*****:*****@"{
  ""contentTypeAlias"": ""testType"",
  ""parentId"": """ + 456.ToGuid() + @""",
  ""templateId"": """ + 9.ToGuid() + @""",
  ""name"": ""Home"",
  ""properties"": {
    ""TestProperty1"": ""property value1"",
    ""testProperty2"": ""property value2""
  }
}", Encoding.UTF8, "application/json");

                Console.WriteLine(request);
                var result = await server.HttpClient.SendAsync(request);

                Console.WriteLine(result);

                //CORS
                Assert.IsTrue(result.Headers.Contains("Access-Control-Allow-Origin"));
                var acao = result.Headers.GetValues("Access-Control-Allow-Origin");
                Assert.AreEqual(1, acao.Count());
                Assert.AreEqual("http://localhost:12061", acao.First());

                //Creation
                var json = await((StreamContent)result.Content).ReadAsStringAsync();
                Console.Write(JsonConvert.SerializeObject(JsonConvert.DeserializeObject(json), Formatting.Indented));

                Assert.AreEqual(HttpStatusCode.Created, result.StatusCode);
            }
        }
        public async Task Put_Is_200_Response_With_Published()
        {
            var startup = new TestStartup(
                //This will be invoked before the controller is created so we can modify these mocked services
                (testServices) =>
            {
                MockServicesForAuthorizationSuccess(testServices, 456);
                ContentServiceMocks.SetupMocksForPost(testServices.ServiceContext);
                var mockContentService = Mock.Get(testServices.ServiceContext.ContentService);
                mockContentService.Setup(x => x.SaveAndPublishWithStatus(It.IsAny <IContent>(), It.IsAny <int>(), It.IsAny <bool>()))
                .Returns(Attempt <PublishStatus> .Succeed);
            });

            await base.Put_Is_200_Response(startup.UseDefaultTestSetup, RouteConstants.ContentSegment, new StringContent(@"{
  ""contentTypeAlias"": ""testType"",
  ""parentId"": 456,
  ""templateId"": 9,
  ""published"": true,
  ""name"": ""Home"",
  ""properties"": {
    ""TestProperty1"": ""property value1"",
    ""testProperty2"": ""property value2""
  }
}", Encoding.UTF8, "application/json"));
        }
        public async Task Post_Is_400_Validation_Property_Required()
        {
            var startup = new TestStartup(
                //This will be invoked before the controller is created so we can modify these mocked services
                (testServices) =>
            {
                MockServicesForAuthorizationSuccess(testServices, 456);
                ContentServiceMocks.SetupMocksForPost(testServices.ServiceContext);

                var mockPropertyEditor = Mock.Get(PropertyEditorResolver.Current);
                mockPropertyEditor.Setup(x => x.GetByAlias("testEditor")).Returns(new ModelMocks.SimplePropertyEditor());
            });

            using (var server = TestServer.Create(builder => startup.UseDefaultTestSetup(builder)))
            {
                var request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(string.Format("http://testserver/umbraco/rest/v1/{0}", RouteConstants.ContentSegment)),
                    Method     = HttpMethod.Post,
                };

                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json"));
                //NOTE: it is missing
                request.Content = new StringContent(@"{
    ""name"": ""test"",  
    ""contentTypeAlias"": ""test"",
  ""parentId"": 456,
  ""templateId"": 9,
  ""properties"": {
    ""TestProperty1"": """",
    ""testProperty2"": ""property value2""
  }
}", Encoding.UTF8, "application/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.BadRequest, result.StatusCode);

                var djson = JsonConvert.DeserializeObject <JObject>(json);

                Assert.AreEqual(1, djson["totalResults"].Value <int>());
                Assert.AreEqual("content.properties.TestProperty1.value", djson["_embedded"]["errors"][0]["logRef"].Value <string>());
            }
        }
        public async Task Post_Is_201_Response()
        {
            var startup = new TestStartup(
                //This will be invoked before the controller is created so we can modify these mocked services
                (testServices) =>
            {
                MockServicesForAuthorizationSuccess(testServices, 456);
                ContentServiceMocks.SetupMocksForPost(testServices.ServiceContext);
            });

            await base.Post_Is_201_Response(startup.UseDefaultTestSetup, RouteConstants.ContentSegment, new StringContent(@"{
  ""contentTypeAlias"": ""testType"",
  ""parentId"": 456,
  ""templateId"": 9,
  ""name"": ""Home"",
  ""properties"": {
    ""TestProperty1"": ""property value1"",
    ""testProperty2"": ""property value2""
  }
}", Encoding.UTF8, "application/json"));
        }