|
| 1 | +using System.Linq.Expressions; |
| 2 | +using System.Net; |
| 3 | +using System.Text; |
| 4 | +using NUnit.Framework; |
| 5 | +using Umbraco.Cms.Api.Management.Controllers.Document; |
| 6 | +using Umbraco.Cms.Api.Management.ViewModels.Document; |
| 7 | +using Umbraco.Cms.Core; |
| 8 | +using Umbraco.Cms.Core.Actions; |
| 9 | +using Umbraco.Cms.Core.Models; |
| 10 | +using Umbraco.Cms.Core.Models.ContentEditing; |
| 11 | +using Umbraco.Cms.Core.Models.ContentPublishing; |
| 12 | +using Umbraco.Cms.Core.Models.Membership; |
| 13 | +using Umbraco.Cms.Core.Serialization; |
| 14 | +using Umbraco.Cms.Core.Services; |
| 15 | +using Umbraco.Cms.Core.Services.ContentTypeEditing; |
| 16 | +using Umbraco.Cms.Core.Strings; |
| 17 | +using Umbraco.Cms.Tests.Common.Builders; |
| 18 | +using Umbraco.Cms.Tests.Common.TestHelpers; |
| 19 | + |
| 20 | +namespace Umbraco.Cms.Tests.Integration.ManagementApi.Policies; |
| 21 | + |
| 22 | +public class UpdateDocumentTests : ManagementApiTest<UpdateDocumentController> |
| 23 | +{ |
| 24 | + private IUserGroupService UserGroupService => GetRequiredService<IUserGroupService>(); |
| 25 | + |
| 26 | + private IShortStringHelper ShortStringHelper => GetRequiredService<IShortStringHelper>(); |
| 27 | + |
| 28 | + private IJsonSerializer JsonSerializer => GetRequiredService<IJsonSerializer>(); |
| 29 | + |
| 30 | + private ITemplateService TemplateService => GetRequiredService<ITemplateService>(); |
| 31 | + |
| 32 | + private IContentTypeEditingService ContentTypeEditingService => GetRequiredService<IContentTypeEditingService>(); |
| 33 | + |
| 34 | + private IContentEditingService ContentEditingService => GetRequiredService<IContentEditingService>(); |
| 35 | + |
| 36 | + private IContentPublishingService ContentPublishingService => GetRequiredService<IContentPublishingService>(); |
| 37 | + |
| 38 | + private IContentService ContentService => GetRequiredService<IContentService>(); |
| 39 | + |
| 40 | + protected override Expression<Func<UpdateDocumentController, object>> MethodSelector => |
| 41 | + x => x.Update(CancellationToken.None, Guid.Empty, null!); |
| 42 | + |
| 43 | + [Test] |
| 44 | + public async Task UserWithoutPermissionCannotUpdate() |
| 45 | + { |
| 46 | + var userGroup = new UserGroup(ShortStringHelper) |
| 47 | + { |
| 48 | + Name = "Test", |
| 49 | + Alias = "test", |
| 50 | + Permissions = new HashSet<string> { ActionBrowse.ActionLetter }, |
| 51 | + HasAccessToAllLanguages = true, |
| 52 | + StartContentId = -1, |
| 53 | + StartMediaId = -1 |
| 54 | + }; |
| 55 | + userGroup.AddAllowedSection("content"); |
| 56 | + userGroup.AddAllowedSection("media"); |
| 57 | + |
| 58 | + var groupCreationResult = await UserGroupService.CreateAsync(userGroup, Constants.Security.SuperUserKey); |
| 59 | + Assert.IsTrue(groupCreationResult.Success); |
| 60 | + |
| 61 | + await AuthenticateClientAsync(Client, async userService => |
| 62 | + { |
| 63 | + |
| 64 | + var testUserCreateModel = new UserCreateModel |
| 65 | + { |
| 66 | + Email = email, |
| 67 | + Name = "Test Mc.Gee", |
| 68 | + UserName = email, |
| 69 | + UserGroupKeys = new HashSet<Guid> { groupCreationResult.Result.Key }, |
| 70 | + }; |
| 71 | + |
| 72 | + var userCreationResult = |
| 73 | + await userService.CreateAsync(Constants.Security.SuperUserKey, testUserCreateModel, true); |
| 74 | + |
| 75 | + Assert.IsTrue(userCreationResult.Success); |
| 76 | + |
| 77 | + return (userCreationResult.Result.CreatedUser, "1234567890"); |
| 78 | + }); |
| 79 | + |
| 80 | + const string UpdatedName = "NewName"; |
| 81 | + |
| 82 | + var model = await CreateContent(); |
| 83 | + var updateRequestModel = CreateRequestModel(model, UpdatedName); |
| 84 | + |
| 85 | + var response = await GetManagementApiResponse(model, updateRequestModel); |
| 86 | + |
| 87 | + AssertResponse(response, model, HttpStatusCode.Forbidden, model.InvariantName); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public async Task UserWithPermissionCanUpdate() |
| 92 | + { |
| 93 | + // "Default" version creates an editor that has permission to update content. |
| 94 | + await AuthenticateClientAsync(Client, "[email protected]", "1234567890", false); |
| 95 | + |
| 96 | + const string UpdatedName = "NewName"; |
| 97 | + |
| 98 | + var model = await CreateContent(); |
| 99 | + var updateRequestModel = CreateRequestModel(model, UpdatedName); |
| 100 | + |
| 101 | + var response = await GetManagementApiResponse(model, updateRequestModel); |
| 102 | + |
| 103 | + AssertResponse(response, model, HttpStatusCode.OK, UpdatedName); |
| 104 | + } |
| 105 | + |
| 106 | + private async Task<ContentCreateModel> CreateContent() |
| 107 | + { |
| 108 | + var userKey = Constants.Security.SuperUserKey; |
| 109 | + var template = TemplateBuilder.CreateTextPageTemplate(); |
| 110 | + var templateAttempt = await TemplateService.CreateAsync(template, userKey); |
| 111 | + Assert.IsTrue(templateAttempt.Success); |
| 112 | + |
| 113 | + var contentTypeCreateModel = ContentTypeEditingBuilder.CreateSimpleContentType(defaultTemplateKey: template.Key); |
| 114 | + var contentTypeAttempt = await ContentTypeEditingService.CreateAsync(contentTypeCreateModel, userKey); |
| 115 | + Assert.IsTrue(contentTypeAttempt.Success); |
| 116 | + |
| 117 | + var textPage = ContentEditingBuilder.CreateSimpleContent(contentTypeAttempt.Result.Key); |
| 118 | + textPage.TemplateKey = templateAttempt.Result.Key; |
| 119 | + textPage.Key = Guid.NewGuid(); |
| 120 | + var createContentResult = await ContentEditingService.CreateAsync(textPage, userKey); |
| 121 | + Assert.IsTrue(createContentResult.Success); |
| 122 | + |
| 123 | + var publishResult = await ContentPublishingService.PublishAsync( |
| 124 | + createContentResult.Result.Content!.Key, |
| 125 | + [new() { Culture = "*" }], |
| 126 | + userKey); |
| 127 | + |
| 128 | + Assert.IsTrue(publishResult.Success); |
| 129 | + return textPage; |
| 130 | + } |
| 131 | + |
| 132 | + private static UpdateDocumentRequestModel CreateRequestModel(ContentCreateModel model, string name) |
| 133 | + { |
| 134 | + var updateRequestModel = DocumentUpdateHelper.CreateInvariantDocumentUpdateRequestModel(model); |
| 135 | + updateRequestModel.Variants.First().Name = name; |
| 136 | + return updateRequestModel; |
| 137 | + } |
| 138 | + |
| 139 | + private async Task<HttpResponseMessage> GetManagementApiResponse(ContentCreateModel model, UpdateDocumentRequestModel updateRequestModel) |
| 140 | + { |
| 141 | + var url = GetManagementApiUrl<UpdateDocumentController>(x => x.Update(CancellationToken.None, model.Key!.Value, null)); |
| 142 | + var requestBody = new StringContent(JsonSerializer.Serialize(updateRequestModel), Encoding.UTF8, "application/json"); |
| 143 | + return await Client.PutAsync(url, requestBody); |
| 144 | + } |
| 145 | + |
| 146 | + private void AssertResponse(HttpResponseMessage response, ContentCreateModel model, HttpStatusCode expectedStatusCode, string expectedContentName) |
| 147 | + { |
| 148 | + Assert.That(response.StatusCode, Is.EqualTo(expectedStatusCode)); |
| 149 | + var content = ContentService.GetById(model.Key!.Value); |
| 150 | + Assert.IsNotNull(content); |
| 151 | + Assert.That(content.Name, Is.EqualTo(expectedContentName)); |
| 152 | + } |
| 153 | +} |
0 commit comments