using System.Linq.Expressions; using System.Net; using System.Net.Http.Json; using NUnit.Framework; using Umbraco.Cms.Api.Management.Controllers.Document; using Umbraco.Cms.Api.Management.ViewModels.Sorting; using Umbraco.Cms.Core; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models.ContentEditing; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Tests.Common.Builders; namespace Umbraco.Cms.Tests.Integration.ManagementApi.Document; public class SortDocumentControllerTests : ManagementApiUserGroupTestBase { private IContentEditingService ContentEditingService => GetRequiredService(); private ITemplateService TemplateService => GetRequiredService(); private IContentTypeService ContentTypeService => GetRequiredService(); private Guid _contentKey; [SetUp] public async Task Setup() { // Template var template = TemplateBuilder.CreateTextPageTemplate(Guid.NewGuid().ToString()); await TemplateService.CreateAsync(template, Constants.Security.SuperUserKey); // Content Type var contentType = ContentTypeBuilder.CreateTextPageContentType(defaultTemplateId: template.Id, name: Guid.NewGuid().ToString(), alias: Guid.NewGuid().ToString()); contentType.AllowedAsRoot = true; contentType.AllowedContentTypes = [new ContentTypeSort(contentType.Key, 0, contentType.Alias)]; await ContentTypeService.CreateAsync(contentType, Constants.Security.SuperUserKey); // Content var createModel = new ContentCreateModel { ContentTypeKey = contentType.Key, TemplateKey = template.Key, ParentKey = Constants.System.RootKey, Variants = new List { new() { Name = Guid.NewGuid().ToString() } }, }; var response = await ContentEditingService.CreateAsync(createModel, Constants.Security.SuperUserKey); _contentKey = response.Result.Content.Key; } protected override Expression> MethodSelector => x => x.Sort(CancellationToken.None, null); protected override UserGroupAssertionModel AdminUserGroupAssertionModel => new() { ExpectedStatusCode = HttpStatusCode.OK }; protected override UserGroupAssertionModel EditorUserGroupAssertionModel => new() { ExpectedStatusCode = HttpStatusCode.OK }; protected override UserGroupAssertionModel SensitiveDataUserGroupAssertionModel => new() { ExpectedStatusCode = HttpStatusCode.Forbidden }; protected override UserGroupAssertionModel TranslatorUserGroupAssertionModel => new() { ExpectedStatusCode = HttpStatusCode.Forbidden }; protected override UserGroupAssertionModel WriterUserGroupAssertionModel => new() { ExpectedStatusCode = HttpStatusCode.Forbidden }; protected override UserGroupAssertionModel UnauthorizedUserGroupAssertionModel => new() { ExpectedStatusCode = HttpStatusCode.Unauthorized }; protected override async Task ClientRequest() { SortingRequestModel sortingRequestModel = new() { Parent = null, Sorting = new[] { new ItemSortingRequestModel { Id = _contentKey, SortOrder = 0 } }, }; return await Client.PutAsync(Url, JsonContent.Create(sortingRequestModel)); } }