Skip to content

Commit cfa32b2

Browse files
authored
Integration Tests: Avoid asserting on errors for permission tests (#20643)
* Added integration tests for PropertyTypeUsageService and adjusted assert in management API permissions test. * Commented or fixed management API integration tests verifying permissions where we were asserting on an error response.
1 parent d8198d2 commit cfa32b2

File tree

13 files changed

+73
-35
lines changed

13 files changed

+73
-35
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
namespace Umbraco.Cms.Core.Persistence.Repositories;
22

3+
/// <summary>
4+
/// Defines repository methods for querying property type usage.
5+
/// </summary>
36
public interface IPropertyTypeUsageRepository
47
{
8+
/// <summary>
9+
/// Determines whether there are any saved property values for the specified content type and property alias.
10+
/// </summary>
511
Task<bool> HasSavedPropertyValuesAsync(Guid contentTypeKey, string propertyAlias);
12+
13+
/// <summary>
14+
/// Determines whether a content type with the specified unique identifier exists.
15+
/// </summary>
616
Task<bool> ContentTypeExistAsync(Guid contentTypeKey);
717
}

src/Umbraco.Core/Services/IPropertyTypeUsageService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Umbraco.Cms.Core.Services;
44

5+
/// <summary>
6+
/// Defines service methods for querying property type usage.
7+
/// </summary>
58
public interface IPropertyTypeUsageService
69
{
710
/// <summary>

src/Umbraco.Core/Services/PropertyTypeUsageService.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44

55
namespace Umbraco.Cms.Core.Services;
66

7+
/// <inheritdoc/>
78
public class PropertyTypeUsageService : IPropertyTypeUsageService
89
{
910
private readonly IPropertyTypeUsageRepository _propertyTypeUsageRepository;
10-
private readonly IContentTypeService _contentTypeService;
1111
private readonly ICoreScopeProvider _scopeProvider;
1212

13+
// TODO (V18): Remove IContentTypeService parameter from constructor.
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="PropertyTypeUsageService"/> class.
17+
/// </summary>
1318
public PropertyTypeUsageService(
1419
IPropertyTypeUsageRepository propertyTypeUsageRepository,
20+
#pragma warning disable IDE0060 // Remove unused parameter
1521
IContentTypeService contentTypeService,
22+
#pragma warning restore IDE0060 // Remove unused parameter
1623
ICoreScopeProvider scopeProvider)
1724
{
1825
_propertyTypeUsageRepository = propertyTypeUsageRepository;
19-
_contentTypeService = contentTypeService;
2026
_scopeProvider = scopeProvider;
2127
}
2228

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
using NPoco;
32
using Umbraco.Cms.Core;
43
using Umbraco.Cms.Core.Persistence.Repositories;
@@ -8,28 +7,26 @@
87

98
namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement;
109

10+
/// <inheritdoc/>
1111
internal sealed class PropertyTypeUsageRepository : IPropertyTypeUsageRepository
1212
{
13-
private static readonly Guid?[] NodeObjectTypes = new Guid?[]
14-
{
13+
private static readonly List<Guid> _nodeObjectTypes =
14+
[
1515
Constants.ObjectTypes.DocumentType, Constants.ObjectTypes.MediaType, Constants.ObjectTypes.MemberType,
16-
};
16+
];
1717

1818
private readonly IScopeAccessor _scopeAccessor;
1919

20-
public PropertyTypeUsageRepository(IScopeAccessor scopeAccessor)
21-
{
22-
_scopeAccessor = scopeAccessor;
23-
}
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="PropertyTypeUsageRepository"/> class.
22+
/// </summary>
23+
public PropertyTypeUsageRepository(IScopeAccessor scopeAccessor) => _scopeAccessor = scopeAccessor;
2424

25+
/// <inheritdoc/>
2526
public Task<bool> HasSavedPropertyValuesAsync(Guid contentTypeKey, string propertyAlias)
2627
{
27-
IUmbracoDatabase? database = _scopeAccessor.AmbientScope?.Database;
28-
29-
if (database is null)
30-
{
31-
throw new InvalidOperationException("A scope is required to query the database");
32-
}
28+
IUmbracoDatabase? database = _scopeAccessor.AmbientScope?.Database
29+
?? throw new InvalidOperationException("A scope is required to query the database");
3330

3431
Sql<ISqlContext> selectQuery = database.SqlContext.Sql()
3532
.SelectAll()
@@ -47,26 +44,21 @@ public Task<bool> HasSavedPropertyValuesAsync(Guid contentTypeKey, string proper
4744
return Task.FromResult(database.ExecuteScalar<bool>(hasValuesQuery));
4845
}
4946

47+
/// <inheritdoc/>
5048
public Task<bool> ContentTypeExistAsync(Guid contentTypeKey)
5149
{
52-
IUmbracoDatabase? database = _scopeAccessor.AmbientScope?.Database;
53-
54-
if (database is null)
55-
{
56-
throw new InvalidOperationException("A scope is required to query the database");
57-
}
50+
IUmbracoDatabase? database = _scopeAccessor.AmbientScope?.Database
51+
?? throw new InvalidOperationException("A scope is required to query the database");
5852

5953
Sql<ISqlContext> selectQuery = database.SqlContext.Sql()
6054
.SelectAll()
6155
.From<NodeDto>("n")
6256
.Where<NodeDto>(n => n.UniqueId == contentTypeKey, "n")
63-
.Where<NodeDto>(n => NodeObjectTypes.Contains(n.NodeObjectType), "n");
57+
.WhereIn<NodeDto>(n => n.NodeObjectType, _nodeObjectTypes, "n");
6458

6559
Sql<ISqlContext> hasValuesQuery = database.SqlContext.Sql()
6660
.SelectAnyIfExists(selectQuery);
6761

6862
return Task.FromResult(database.ExecuteScalar<bool>(hasValuesQuery));
6963
}
70-
71-
7264
}

tests/Umbraco.Tests.Integration/ManagementApi/HealthCheck/ExecuteActionHealthCheckControllerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task Setup()
2727

2828
protected override UserGroupAssertionModel AdminUserGroupAssertionModel => new()
2929
{
30-
ExpectedStatusCode = HttpStatusCode.InternalServerError
30+
ExpectedStatusCode = HttpStatusCode.OK
3131
};
3232

3333
protected override UserGroupAssertionModel EditorUserGroupAssertionModel => new()
@@ -58,7 +58,7 @@ public async Task Setup()
5858
protected override async Task<HttpResponseMessage> ClientRequest()
5959
{
6060
HealthCheckActionRequestModel healthCheckActionRequest =
61-
new() { HealthCheck = new ReferenceByIdModel(_dataIntegrityHealthCheckId), ValueRequired = false };
61+
new() { HealthCheck = new ReferenceByIdModel(_dataIntegrityHealthCheckId), ValueRequired = false, Alias = "fixContentPaths" };
6262
return await Client.PostAsync(Url, JsonContent.Create(healthCheckActionRequest));
6363
}
6464
}

tests/Umbraco.Tests.Integration/ManagementApi/LogViewer/AllLogViewerControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class AllLogViewerControllerTests : ManagementApiUserGroupTestBase<AllLog
1212
// We get the InternalServerError for the admin because it has access, but there is no log file to view
1313
protected override UserGroupAssertionModel AdminUserGroupAssertionModel => new()
1414
{
15-
ExpectedStatusCode = HttpStatusCode.InternalServerError
15+
ExpectedStatusCode = HttpStatusCode.OK
1616
};
1717

1818
protected override UserGroupAssertionModel EditorUserGroupAssertionModel => new()

tests/Umbraco.Tests.Integration/ManagementApi/LogViewer/AllMessageTemplateLogViewerControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class AllMessageTemplateLogViewerControllerTests : ManagementApiUserGroup
1111
// We get the InternalServerError for the admin because it has access, but there is no log file to view
1212
protected override UserGroupAssertionModel AdminUserGroupAssertionModel => new()
1313
{
14-
ExpectedStatusCode = HttpStatusCode.InternalServerError
14+
ExpectedStatusCode = HttpStatusCode.OK
1515
};
1616

1717
protected override UserGroupAssertionModel EditorUserGroupAssertionModel => new()

tests/Umbraco.Tests.Integration/ManagementApi/LogViewer/LogLevelCountLogViewerControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class LogLevelCountLogViewerControllerTests : ManagementApiUserGroupTestB
1111
// We get the InternalServerError for the admin because it has access, but there is no log file to view
1212
protected override UserGroupAssertionModel AdminUserGroupAssertionModel => new()
1313
{
14-
ExpectedStatusCode = HttpStatusCode.InternalServerError
14+
ExpectedStatusCode = HttpStatusCode.OK
1515
};
1616

1717
protected override UserGroupAssertionModel EditorUserGroupAssertionModel => new()

tests/Umbraco.Tests.Integration/ManagementApi/LogViewer/ValidateLogFileSizeLogViewerControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ValidateLogFileSizeLogViewerControllerTests: ManagementApiUserGroup
1111
// We get the InternalServerError for the admin because it has access, but there is no log file to view
1212
protected override UserGroupAssertionModel AdminUserGroupAssertionModel => new()
1313
{
14-
ExpectedStatusCode = HttpStatusCode.InternalServerError
14+
ExpectedStatusCode = HttpStatusCode.OK
1515
};
1616

1717
protected override UserGroupAssertionModel EditorUserGroupAssertionModel => new()

tests/Umbraco.Tests.Integration/ManagementApi/PropertyType/IsUsedPropertyTypeControllerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task Setup()
3636

3737
protected override UserGroupAssertionModel AdminUserGroupAssertionModel => new()
3838
{
39-
ExpectedStatusCode = HttpStatusCode.InternalServerError
39+
ExpectedStatusCode = HttpStatusCode.OK
4040
};
4141

4242
protected override UserGroupAssertionModel EditorUserGroupAssertionModel => new()

0 commit comments

Comments
 (0)