Migrated ControllerContentTests.. Right now the test fails, but the test is correct
Signed-off-by: Bjarke Berg <mail@bergmania.dk>
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Tests.Testing;
|
||||
using Umbraco.Web.BackOffice.Controllers;
|
||||
|
||||
namespace Umbraco.Tests.Integration.TestServerTest.Controllers
|
||||
{
|
||||
[TestFixture]
|
||||
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
||||
public class BackOfficeAssetsControllerTests: UmbracoTestServerTestBase
|
||||
{
|
||||
[Test]
|
||||
public async Task EnsureSuccessStatusCode()
|
||||
{
|
||||
// Arrange
|
||||
var url = PrepareUrl<BackOfficeAssetsController>(x=>x.GetSupportedLocales());
|
||||
|
||||
// Act
|
||||
var response = await Client.GetAsync(url);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,377 @@
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Services;
|
||||
using Umbraco.Tests.Common.Builders;
|
||||
using Umbraco.Tests.Common.Builders.Extensions;
|
||||
using Umbraco.Tests.Testing;
|
||||
using Umbraco.Web.Common.Formatters;
|
||||
using Umbraco.Web.Editors;
|
||||
using Umbraco.Web.Models.ContentEditing;
|
||||
|
||||
namespace Umbraco.Tests.Integration.TestServerTest.Controllers
|
||||
{
|
||||
[TestFixture]
|
||||
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
||||
public class ContentControllerTests : UmbracoTestServerTestBase
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
var localizationService = GetRequiredService<ILocalizationService>();
|
||||
|
||||
//Add another language
|
||||
localizationService.Save(new LanguageBuilder()
|
||||
.WithCultureInfo("da-DK")
|
||||
.WithIsDefault(false)
|
||||
.Build());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns 404 if the content wasn't found based on the ID specified
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Test]
|
||||
public async Task PostSave_Validate_Existing_Content()
|
||||
{
|
||||
var url = PrepareUrl<ContentController>(x => x.PostSave(null));
|
||||
|
||||
var contentService = GetRequiredService<IContentService>();
|
||||
var contentTypeService = GetRequiredService<IContentTypeService>();
|
||||
|
||||
var contentType = new ContentTypeBuilder()
|
||||
.WithId(0)
|
||||
.AddPropertyType()
|
||||
.WithAlias("title")
|
||||
.WithValueStorageType(ValueStorageType.Integer)
|
||||
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
|
||||
.WithName("Title")
|
||||
.Done()
|
||||
.WithContentVariation(ContentVariation.Nothing)
|
||||
.Build();
|
||||
|
||||
contentTypeService.Save(contentType);
|
||||
|
||||
var content = new ContentBuilder()
|
||||
.WithId(0)
|
||||
.WithName("Invariant")
|
||||
.WithContentType(contentType)
|
||||
.AddPropertyData()
|
||||
.WithKeyValue("title", "Cool invariant title")
|
||||
.Done()
|
||||
.Build();
|
||||
contentService.SaveAndPublish(content);
|
||||
|
||||
var model = new ContentItemSaveBuilder()
|
||||
.WithContent(content)
|
||||
.WithId(-1337) // HERE We overwrite the Id, so we don't expect to find it on the server
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var response = await Client.PostAsync(url, new MultipartFormDataContent
|
||||
{
|
||||
{ new StringContent(JsonConvert.SerializeObject(model)), "contentItem" }
|
||||
});
|
||||
|
||||
// Assert
|
||||
|
||||
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
|
||||
// Assert.AreEqual(")]}',\n{\"Message\":\"content was not found\"}", response.Item1.Content.ReadAsStringAsync().Result);
|
||||
//
|
||||
// //var obj = JsonConvert.DeserializeObject<PagedResult<UserDisplay>>(response.Item2);
|
||||
// //Assert.AreEqual(0, obj.TotalItems);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PostSave_Validate_At_Least_One_Variant_Flagged_For_Saving()
|
||||
{
|
||||
var url = PrepareUrl<ContentController>(x => x.PostSave(null));
|
||||
|
||||
var contentService = GetRequiredService<IContentService>();
|
||||
var contentTypeService = GetRequiredService<IContentTypeService>();
|
||||
|
||||
var contentType = new ContentTypeBuilder()
|
||||
.WithId(0)
|
||||
.AddPropertyType()
|
||||
.WithAlias("title")
|
||||
.WithValueStorageType(ValueStorageType.Integer)
|
||||
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
|
||||
.WithName("Title")
|
||||
.Done()
|
||||
.WithContentVariation(ContentVariation.Nothing)
|
||||
.Build();
|
||||
|
||||
contentTypeService.Save(contentType);
|
||||
|
||||
var content = new ContentBuilder()
|
||||
.WithId(0)
|
||||
.WithName("Invariant")
|
||||
.WithContentType(contentType)
|
||||
.AddPropertyData()
|
||||
.WithKeyValue("title", "Cool invariant title")
|
||||
.Done()
|
||||
.Build();
|
||||
contentService.SaveAndPublish(content);
|
||||
|
||||
var model = new ContentItemSaveBuilder()
|
||||
.WithContent(content)
|
||||
.Build();
|
||||
|
||||
// HERE we force the test to fail
|
||||
model.Variants = model.Variants.Select(x=>
|
||||
{
|
||||
x.Save = false;
|
||||
return x;
|
||||
});
|
||||
|
||||
// Act
|
||||
var response = await Client.PostAsync(url, new MultipartFormDataContent
|
||||
{
|
||||
{ new StringContent(JsonConvert.SerializeObject(model)), "contentItem" }
|
||||
});
|
||||
|
||||
// Assert
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
|
||||
Assert.AreEqual(")]}',\n{\"message\":\"No variants flagged for saving\"}", body);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns 404 if any of the posted properties dont actually exist
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Test]
|
||||
public async Task PostSave_Validate_Properties_Exist()
|
||||
{
|
||||
var url = PrepareUrl<ContentController>(x => x.PostSave(null));
|
||||
|
||||
var contentService = GetRequiredService<IContentService>();
|
||||
var contentTypeService = GetRequiredService<IContentTypeService>();
|
||||
|
||||
var contentType = new ContentTypeBuilder()
|
||||
.WithId(0)
|
||||
.AddPropertyType()
|
||||
.WithAlias("title")
|
||||
.WithValueStorageType(ValueStorageType.Integer)
|
||||
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
|
||||
.WithName("Title")
|
||||
.Done()
|
||||
.WithContentVariation(ContentVariation.Nothing)
|
||||
.Build();
|
||||
|
||||
contentTypeService.Save(contentType);
|
||||
|
||||
var content = new ContentBuilder()
|
||||
.WithId(0)
|
||||
.WithName("Invariant")
|
||||
.WithContentType(contentType)
|
||||
.AddPropertyData()
|
||||
.WithKeyValue("title", "Cool invariant title")
|
||||
.Done()
|
||||
.Build();
|
||||
contentService.SaveAndPublish(content);
|
||||
|
||||
var model = new ContentItemSaveBuilder()
|
||||
.WithId(content.Id)
|
||||
.WithContentTypeAlias(content.ContentType.Alias)
|
||||
.AddVariant()
|
||||
.AddProperty()
|
||||
.WithId(2)
|
||||
.WithAlias("doesntexists")
|
||||
.WithValue("Whatever")
|
||||
.Done()
|
||||
.Done()
|
||||
.Build();
|
||||
|
||||
|
||||
// Act
|
||||
var response = await Client.PostAsync(url, new MultipartFormDataContent
|
||||
{
|
||||
{ new StringContent(JsonConvert.SerializeObject(model)), "contentItem" }
|
||||
});
|
||||
|
||||
// Assert
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
|
||||
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
|
||||
|
||||
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PostSave_Simple_Invariant()
|
||||
{
|
||||
var url = PrepareUrl<ContentController>(x => x.PostSave(null));
|
||||
|
||||
var contentService = GetRequiredService<IContentService>();
|
||||
var contentTypeService = GetRequiredService<IContentTypeService>();
|
||||
|
||||
var contentType = new ContentTypeBuilder()
|
||||
.WithId(0)
|
||||
.AddPropertyType()
|
||||
.WithAlias("title")
|
||||
.WithValueStorageType(ValueStorageType.Integer)
|
||||
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
|
||||
.WithName("Title")
|
||||
.Done()
|
||||
.WithContentVariation(ContentVariation.Nothing)
|
||||
.Build();
|
||||
|
||||
contentTypeService.Save(contentType);
|
||||
|
||||
var content = new ContentBuilder()
|
||||
.WithId(0)
|
||||
.WithName("Invariant")
|
||||
.WithContentType(contentType)
|
||||
.AddPropertyData()
|
||||
.WithKeyValue("title", "Cool invariant title")
|
||||
.Done()
|
||||
.Build();
|
||||
contentService.SaveAndPublish(content);
|
||||
var model = new ContentItemSaveBuilder()
|
||||
.WithContent(content)
|
||||
.Build();
|
||||
|
||||
|
||||
// Act
|
||||
var response = await Client.PostAsync(url, new MultipartFormDataContent
|
||||
{
|
||||
{ new StringContent(JsonConvert.SerializeObject(model)), "contentItem" }
|
||||
});
|
||||
|
||||
// Assert
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
|
||||
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
|
||||
|
||||
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, body);
|
||||
var display = JsonConvert.DeserializeObject<ContentItemDisplay>(body);
|
||||
Assert.AreEqual(1, display.Variants.Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PostSave_Validate_Empty_Name()
|
||||
{
|
||||
var url = PrepareUrl<ContentController>(x => x.PostSave(null));
|
||||
|
||||
var contentService = GetRequiredService<IContentService>();
|
||||
var contentTypeService = GetRequiredService<IContentTypeService>();
|
||||
|
||||
var contentType = new ContentTypeBuilder()
|
||||
.WithId(0)
|
||||
.AddPropertyType()
|
||||
.WithAlias("title")
|
||||
.WithValueStorageType(ValueStorageType.Integer)
|
||||
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
|
||||
.WithName("Title")
|
||||
.Done()
|
||||
.WithContentVariation(ContentVariation.Nothing)
|
||||
.Build();
|
||||
|
||||
contentTypeService.Save(contentType);
|
||||
|
||||
var content = new ContentBuilder()
|
||||
.WithId(0)
|
||||
.WithName("Invariant")
|
||||
.WithContentType(contentType)
|
||||
.AddPropertyData()
|
||||
.WithKeyValue("title", "Cool invariant title")
|
||||
.Done()
|
||||
.Build();
|
||||
contentService.SaveAndPublish(content);
|
||||
|
||||
content.Name = null; // Removes the name of one of the variants to force an error
|
||||
var model = new ContentItemSaveBuilder()
|
||||
.WithContent(content)
|
||||
.Build();
|
||||
|
||||
|
||||
// Act
|
||||
var response = await Client.PostAsync(url, new MultipartFormDataContent
|
||||
{
|
||||
{ new StringContent(JsonConvert.SerializeObject(model)), "contentItem" }
|
||||
});
|
||||
|
||||
// Assert
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
|
||||
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
|
||||
|
||||
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
var display = JsonConvert.DeserializeObject<ContentItemDisplay>(body);
|
||||
Assert.AreEqual(1, display.Errors.Count());
|
||||
Assert.IsTrue(display.Errors.ContainsKey("Variants[0].Name"));
|
||||
//ModelState":{"Variants[0].Name":["Required"]}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PostSave_Validate_Variants_Empty_Name()
|
||||
{
|
||||
var url = PrepareUrl<ContentController>(x => x.PostSave(null));
|
||||
|
||||
var contentService = GetRequiredService<IContentService>();
|
||||
var contentTypeService = GetRequiredService<IContentTypeService>();
|
||||
|
||||
var contentType = new ContentTypeBuilder()
|
||||
.WithId(0)
|
||||
.AddPropertyType()
|
||||
.WithAlias("title")
|
||||
.WithValueStorageType(ValueStorageType.Integer)
|
||||
.WithPropertyEditorAlias(Constants.PropertyEditors.Aliases.TextBox)
|
||||
.WithName("Title")
|
||||
.Done()
|
||||
.WithContentVariation(ContentVariation.Culture)
|
||||
.Build();
|
||||
|
||||
contentTypeService.Save(contentType);
|
||||
|
||||
var content = new ContentBuilder()
|
||||
.WithId(0)
|
||||
.WithCultureName("en-US", "English")
|
||||
.WithCultureName("da-DK", "Danish")
|
||||
.WithContentType(contentType)
|
||||
.AddPropertyData()
|
||||
.WithKeyValue("title", "Cool invariant title")
|
||||
.Done()
|
||||
.Build();
|
||||
contentService.SaveAndPublish(content);
|
||||
|
||||
content.CultureInfos[0].Name = null; // Removes the name of one of the variants to force an error
|
||||
var model = new ContentItemSaveBuilder()
|
||||
.WithContent(content)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var response = await Client.PostAsync(url, new MultipartFormDataContent
|
||||
{
|
||||
{ new StringContent(JsonConvert.SerializeObject(model)), "contentItem" }
|
||||
});
|
||||
|
||||
// Assert
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
|
||||
body = body.TrimStart(AngularJsonMediaTypeFormatter.XsrfPrefix);
|
||||
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
var display = JsonConvert.DeserializeObject<ContentItemDisplay>(body);
|
||||
Assert.AreEqual(2, display.Errors.Count());
|
||||
Assert.IsTrue(display.Errors.ContainsKey("Variants[0].Name"));
|
||||
Assert.IsTrue(display.Errors.ContainsKey("_content_variant_en-US_null_"));
|
||||
}
|
||||
|
||||
|
||||
private class TestContentItemSave : ContentItemSave
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Net.Http;
|
||||
using System.Reflection;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Composing;
|
||||
using Umbraco.Extensions;
|
||||
using Umbraco.Tests.Integration.Testing;
|
||||
using Umbraco.Web;
|
||||
using Umbraco.Web.Common.Controllers;
|
||||
using Umbraco.Web.Editors;
|
||||
|
||||
|
||||
namespace Umbraco.Tests.Integration.TestServerTest
|
||||
{
|
||||
[TestFixture]
|
||||
public abstract class UmbracoTestServerTestBase : UmbracoIntegrationTest
|
||||
{
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
Factory = new UmbracoWebApplicationFactory(TestDBConnectionString);
|
||||
Client = Factory.CreateClient(new WebApplicationFactoryClientOptions(){
|
||||
AllowAutoRedirect = false
|
||||
});
|
||||
LinkGenerator = Factory.Services.GetRequiredService<LinkGenerator>();
|
||||
|
||||
}
|
||||
|
||||
protected T GetRequiredService<T>() => Factory.Services.GetRequiredService<T>();
|
||||
protected string PrepareUrl<T>(Expression<Func<T, object>> methodSelector)
|
||||
where T : UmbracoApiController
|
||||
{
|
||||
var url = LinkGenerator.GetUmbracoApiService<T>(methodSelector);
|
||||
|
||||
var umbracoContextFactory = GetRequiredService<IUmbracoContextFactory>();
|
||||
var httpContextAccessor = GetRequiredService<IHttpContextAccessor>();
|
||||
|
||||
httpContextAccessor.HttpContext = new DefaultHttpContext
|
||||
{
|
||||
Request =
|
||||
{
|
||||
Scheme = "https",
|
||||
Host = new HostString("localhost", 80),
|
||||
Path = url,
|
||||
QueryString = new QueryString(string.Empty)
|
||||
}
|
||||
};
|
||||
|
||||
umbracoContextFactory.EnsureUmbracoContext();
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
protected HttpClient Client { get; set; }
|
||||
protected LinkGenerator LinkGenerator { get; set; }
|
||||
protected UmbracoWebApplicationFactory Factory { get; set; }
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
Client.Dispose();
|
||||
|
||||
if (Current.IsInitialized)
|
||||
{
|
||||
typeof(Current).GetProperty(nameof(Current.IsInitialized), BindingFlags.Public | BindingFlags.Static).SetValue(null, false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Security.Claims;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.BackOffice;
|
||||
using Umbraco.Core.Composing;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Mapping;
|
||||
using Umbraco.Core.Models.Membership;
|
||||
using Umbraco.Core.Services;
|
||||
using Umbraco.Web.Common.Security;
|
||||
using Umbraco.Web.UI.BackOffice;
|
||||
|
||||
namespace Umbraco.Tests.Integration.TestServerTest
|
||||
{
|
||||
public class UmbracoWebApplicationFactory : CustomWebApplicationFactory<Startup>
|
||||
{
|
||||
public UmbracoWebApplicationFactory(string testDbConnectionString) :base(testDbConnectionString)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
|
||||
{
|
||||
private readonly string _testDbConnectionString;
|
||||
|
||||
protected CustomWebApplicationFactory(string testDbConnectionString)
|
||||
{
|
||||
_testDbConnectionString = testDbConnectionString;
|
||||
}
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
base.ConfigureWebHost(builder);
|
||||
|
||||
builder.ConfigureTestServices(services =>
|
||||
{
|
||||
services.AddAuthentication("Test").AddScheme<AuthenticationSchemeOptions, TestAuthHandler>("Test", options => {});
|
||||
});
|
||||
|
||||
builder.ConfigureAppConfiguration(x =>
|
||||
{
|
||||
x.AddInMemoryCollection(new Dictionary<string, string>()
|
||||
{
|
||||
["ConnectionStrings:"+ Constants.System.UmbracoConnectionName] = _testDbConnectionString
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
protected override IHostBuilder CreateHostBuilder()
|
||||
{
|
||||
|
||||
var builder = base.CreateHostBuilder();
|
||||
builder.UseUmbraco();
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
|
||||
public class TestAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
||||
{
|
||||
private readonly BackOfficeSignInManager _backOfficeSignInManager;
|
||||
|
||||
private readonly BackOfficeIdentityUser _fakeUser;
|
||||
public TestAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options,
|
||||
ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, BackOfficeSignInManager backOfficeSignInManager, IUserService userService, UmbracoMapper umbracoMapper)
|
||||
: base(options, logger, encoder, clock)
|
||||
{
|
||||
_backOfficeSignInManager = backOfficeSignInManager;
|
||||
|
||||
var user = userService.GetUserById(Constants.Security.SuperUserId);
|
||||
_fakeUser = umbracoMapper.Map<IUser, BackOfficeIdentityUser>(user);
|
||||
_fakeUser.SecurityStamp = "Needed";
|
||||
}
|
||||
|
||||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||
{
|
||||
|
||||
var principal = await _backOfficeSignInManager.CreateUserPrincipalAsync(_fakeUser);
|
||||
var ticket = new AuthenticationTicket(principal, "Test");
|
||||
|
||||
return AuthenticateResult.Success(ticket);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user