|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using NUnit.Framework; |
| 3 | +using Umbraco.Cms.Api.Management.Factories; |
| 4 | +using Umbraco.Cms.Api.Management.Mapping.Permissions; |
| 5 | +using Umbraco.Cms.Api.Management.ViewModels; |
| 6 | +using Umbraco.Cms.Api.Management.ViewModels.UserGroup; |
| 7 | +using Umbraco.Cms.Api.Management.ViewModels.UserGroup.Permissions; |
| 8 | +using Umbraco.Cms.Core; |
| 9 | +using Umbraco.Cms.Core.Models; |
| 10 | +using Umbraco.Cms.Core.Services; |
| 11 | +using Umbraco.Cms.Core.Services.ContentTypeEditing; |
| 12 | +using Umbraco.Cms.Core.Services.OperationStatus; |
| 13 | +using Umbraco.Cms.Infrastructure.Persistence.Mappers; |
| 14 | +using Umbraco.Cms.Tests.Common.Builders; |
| 15 | +using Umbraco.Cms.Tests.Common.TestHelpers; |
| 16 | +using Umbraco.Cms.Tests.Common.Testing; |
| 17 | +using Umbraco.Cms.Tests.Integration.Testing; |
| 18 | + |
| 19 | +namespace Umbraco.Cms.Tests.Integration.ManagementApi.Factories; |
| 20 | + |
| 21 | +[TestFixture] |
| 22 | +[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)] |
| 23 | +public class UserGroupPresentationFactoryTests : UmbracoIntegrationTest |
| 24 | +{ |
| 25 | + public IUserGroupPresentationFactory UserGroupPresentationFactory => GetRequiredService<IUserGroupPresentationFactory>(); |
| 26 | + public IUserGroupService UserGroupService => GetRequiredService<IUserGroupService>(); |
| 27 | + public ITemplateService TemplateService => GetRequiredService<ITemplateService>(); |
| 28 | + public IContentTypeEditingService ContentTypeEditingService => GetRequiredService<IContentTypeEditingService>(); |
| 29 | + |
| 30 | + public IContentEditingService ContentEditingService => GetRequiredService<IContentEditingService>(); |
| 31 | + |
| 32 | + protected override void ConfigureTestServices(IServiceCollection services) |
| 33 | + { |
| 34 | + services.AddTransient<IUserGroupPresentationFactory, UserGroupPresentationFactory>(); |
| 35 | + services.AddSingleton<IPermissionPresentationFactory, PermissionPresentationFactory>(); |
| 36 | + services.AddSingleton<DocumentPermissionMapper>(); |
| 37 | + services.AddSingleton<IPermissionMapper>(x=>x.GetRequiredService<DocumentPermissionMapper>()); |
| 38 | + services.AddSingleton<IPermissionPresentationMapper>(x=>x.GetRequiredService<DocumentPermissionMapper>()); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + [Test] |
| 43 | + public async Task Can_Map_Create_Model_And_Create() |
| 44 | + { |
| 45 | + var updateModel = new CreateUserGroupRequestModel() |
| 46 | + { |
| 47 | + Alias = "testAlias", |
| 48 | + FallbackPermissions = new HashSet<string>(), |
| 49 | + HasAccessToAllLanguages = true, |
| 50 | + Languages = new List<string>(), |
| 51 | + Name = "Test Name", |
| 52 | + Sections = new [] {"Umb.Section.Content"}, |
| 53 | + Permissions = new HashSet<IPermissionPresentationModel>() |
| 54 | + }; |
| 55 | + |
| 56 | + var attempt = await UserGroupPresentationFactory.CreateAsync(updateModel); |
| 57 | + Assert.IsTrue(attempt.Success); |
| 58 | + |
| 59 | + var userGroupCreateAttempt = await UserGroupService.CreateAsync(attempt.Result, Constants.Security.SuperUserKey); |
| 60 | + |
| 61 | + var userGroup = userGroupCreateAttempt.Result; |
| 62 | + |
| 63 | + Assert.Multiple(() => |
| 64 | + { |
| 65 | + Assert.IsTrue(userGroupCreateAttempt.Success); |
| 66 | + Assert.IsNotNull(userGroup); |
| 67 | + Assert.IsEmpty(userGroup.GranularPermissions); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public async Task Cannot_Create_UserGroup_With_Unexisting_Document_Reference() |
| 73 | + { |
| 74 | + var updateModel = new CreateUserGroupRequestModel() |
| 75 | + { |
| 76 | + Alias = "testAlias", |
| 77 | + FallbackPermissions = new HashSet<string>(), |
| 78 | + HasAccessToAllLanguages = true, |
| 79 | + Languages = new List<string>(), |
| 80 | + Name = "Test Name", |
| 81 | + Sections = new [] {"Umb.Section.Content"}, |
| 82 | + Permissions = new HashSet<IPermissionPresentationModel>() |
| 83 | + { |
| 84 | + new DocumentPermissionPresentationModel() |
| 85 | + { |
| 86 | + Document = new ReferenceByIdModel(Guid.NewGuid()), |
| 87 | + Verbs = new HashSet<string>() |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + var attempt = await UserGroupPresentationFactory.CreateAsync(updateModel); |
| 93 | + Assert.IsTrue(attempt.Success); |
| 94 | + |
| 95 | + var userGroupCreateAttempt = await UserGroupService.CreateAsync(attempt.Result, Constants.Security.SuperUserKey); |
| 96 | + |
| 97 | + Assert.Multiple(() => |
| 98 | + { |
| 99 | + Assert.IsFalse(userGroupCreateAttempt.Success); |
| 100 | + Assert.AreEqual(UserGroupOperationStatus.DocumentPermissionKeyNotFound, userGroupCreateAttempt.Status); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + [Test] |
| 105 | + public async Task Can_Create_Usergroup_With_Empty_Granluar_Permissions_For_Document() |
| 106 | + { |
| 107 | + var contentKey = await CreateContent(); |
| 108 | + |
| 109 | + var updateModel = new CreateUserGroupRequestModel() |
| 110 | + { |
| 111 | + Alias = "testAlias", |
| 112 | + FallbackPermissions = new HashSet<string>(), |
| 113 | + HasAccessToAllLanguages = true, |
| 114 | + Languages = new List<string>(), |
| 115 | + Name = "Test Name", |
| 116 | + Sections = new [] {"Umb.Section.Content"}, |
| 117 | + Permissions = new HashSet<IPermissionPresentationModel> |
| 118 | + { |
| 119 | + new DocumentPermissionPresentationModel() |
| 120 | + { |
| 121 | + Document = new ReferenceByIdModel(contentKey), |
| 122 | + Verbs = new HashSet<string>() |
| 123 | + } |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + var attempt = await UserGroupPresentationFactory.CreateAsync(updateModel); |
| 128 | + Assert.IsTrue(attempt.Success); |
| 129 | + |
| 130 | + var userGroupCreateAttempt = await UserGroupService.CreateAsync(attempt.Result, Constants.Security.SuperUserKey); |
| 131 | + var userGroup = userGroupCreateAttempt.Result; |
| 132 | + |
| 133 | + Assert.Multiple(() => |
| 134 | + { |
| 135 | + Assert.IsTrue(userGroupCreateAttempt.Success); |
| 136 | + Assert.IsNotNull(userGroup); |
| 137 | + Assert.IsNotEmpty(userGroup.GranularPermissions); |
| 138 | + Assert.AreEqual(contentKey, userGroup.GranularPermissions.First().Key); |
| 139 | + Assert.AreEqual(string.Empty, userGroup.GranularPermissions.First().Permission); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + private async Task<Guid> CreateContent() |
| 144 | + { |
| 145 | + // NOTE Maybe not the best way to create/save test data as we are using the services, which are being tested. |
| 146 | + var template = TemplateBuilder.CreateTextPageTemplate("defaultTemplate"); |
| 147 | + await TemplateService.CreateAsync(template, Constants.Security.SuperUserKey); |
| 148 | + // Create and Save ContentType "umbTextpage" -> 1051 (template), 1052 (content type) |
| 149 | + var contentTypeCreateModel = ContentTypeEditingBuilder.CreateSimpleContentType("umbTextpage", "Textpage", defaultTemplateKey: template.Key); |
| 150 | + var contentTypeAttempt = await ContentTypeEditingService.CreateAsync(contentTypeCreateModel, Constants.Security.SuperUserKey); |
| 151 | + Assert.IsTrue(contentTypeAttempt.Success); |
| 152 | + |
| 153 | + var contentTypeResult = contentTypeAttempt.Result; |
| 154 | + var contentTypeUpdateModel = ContentTypeUpdateHelper.CreateContentTypeUpdateModel(contentTypeResult); contentTypeUpdateModel.AllowedContentTypes = new[] |
| 155 | + { |
| 156 | + new ContentTypeSort(contentTypeResult.Key, 0, contentTypeCreateModel.Alias), |
| 157 | + }; |
| 158 | + var updatedContentTypeResult = await ContentTypeEditingService.UpdateAsync(contentTypeResult, contentTypeUpdateModel, Constants.Security.SuperUserKey); |
| 159 | + Assert.IsTrue(updatedContentTypeResult.Success); |
| 160 | + |
| 161 | + // Create and Save Content "Homepage" based on "umbTextpage" -> 1053 |
| 162 | + var textPage = ContentEditingBuilder.CreateSimpleContent(updatedContentTypeResult.Result.Key); |
| 163 | + var createContentResultTextPage = await ContentEditingService.CreateAsync(textPage, Constants.Security.SuperUserKey); |
| 164 | + Assert.IsTrue(createContentResultTextPage.Success); |
| 165 | + |
| 166 | + return createContentResultTextPage.Result.Content.Key; |
| 167 | + } |
| 168 | +} |
0 commit comments