From b7dce9ee081fc87c091b46d1b5eb6b79e539bf13 Mon Sep 17 00:00:00 2001 From: John Niang Date: Wed, 20 Sep 2023 15:45:30 +0800 Subject: [PATCH] Fix unit test errors --- .../app/security/PersonalAccessToken.java | 5 ++++ .../core/extension/endpoint/UserEndpoint.java | 3 ++- .../extension/service/DefaultRoleService.java | 2 +- .../authorization/AuthorityUtils.java | 2 +- .../extension/endpoint/UserEndpointTest.java | 27 ++++++------------- .../security/authentication/pat/PatTest.java | 1 + 6 files changed, 18 insertions(+), 22 deletions(-) diff --git a/api/src/main/java/run/halo/app/security/PersonalAccessToken.java b/api/src/main/java/run/halo/app/security/PersonalAccessToken.java index 1ab26e339c3..333c225b1f1 100644 --- a/api/src/main/java/run/halo/app/security/PersonalAccessToken.java +++ b/api/src/main/java/run/halo/app/security/PersonalAccessToken.java @@ -1,5 +1,7 @@ package run.halo.app.security; +import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED; + import io.swagger.v3.oas.annotations.media.Schema; import java.time.Instant; import java.util.List; @@ -24,6 +26,7 @@ public class PersonalAccessToken extends AbstractExtension { @Schema(name = "PatSpec") public static class Spec { + @Schema(requiredMode = REQUIRED) private String name; private String description; @@ -34,6 +37,7 @@ public static class Spec { private List scopes; + @Schema(requiredMode = REQUIRED) private String username; private boolean revoked; @@ -42,6 +46,7 @@ public static class Spec { private Instant lastUsed; + @Schema(requiredMode = REQUIRED) private String tokenId; } diff --git a/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java b/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java index 6e5a5ec9ca2..20faeb57c01 100644 --- a/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java +++ b/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java @@ -516,7 +516,8 @@ private Mono getUserPermission(ServerRequest request) { .flatMap(roles -> { var up = new UserPermission(); var setRoles = Mono.fromRunnable(() -> up.setRoles(roles)).then(); - var roleNames = roles.stream().map(role -> role.getMetadata().getName()) + var roleNames = roles.stream() + .map(role -> role.getMetadata().getName()) .collect(Collectors.toSet()); var setPermissions = roleService.listPermissions(roleNames) .distinct() diff --git a/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java b/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java index 4e1ca0b271c..567685b4bbc 100644 --- a/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java +++ b/application/src/main/java/run/halo/app/core/extension/service/DefaultRoleService.java @@ -128,7 +128,7 @@ private Flux listDependencies(Set names, Predicate additiona return Flux.fromIterable(dependencies) .filter(dep -> !visited.contains(dep)) - .collect(Collectors.toSet()) + .collect(Collectors.toSet()) .flatMapMany(deps -> listRoles(deps, additionalPredicate)); }) .concatWith(Flux.defer(() -> listAggregatedRoles(visited, additionalPredicate))); diff --git a/application/src/main/java/run/halo/app/security/authorization/AuthorityUtils.java b/application/src/main/java/run/halo/app/security/authorization/AuthorityUtils.java index 497303b4ddc..b00fa310d0d 100644 --- a/application/src/main/java/run/halo/app/security/authorization/AuthorityUtils.java +++ b/application/src/main/java/run/halo/app/security/authorization/AuthorityUtils.java @@ -14,7 +14,7 @@ public enum AuthorityUtils { ; - public static final String SCOPE_PREFIX = "ROLE_"; + public static final String SCOPE_PREFIX = "SCOPE_"; public static final String ROLE_PREFIX = "ROLE_"; diff --git a/application/src/test/java/run/halo/app/core/extension/endpoint/UserEndpointTest.java b/application/src/test/java/run/halo/app/core/extension/endpoint/UserEndpointTest.java index 2d79aa9567a..179aba87ebf 100644 --- a/application/src/test/java/run/halo/app/core/extension/endpoint/UserEndpointTest.java +++ b/application/src/test/java/run/halo/app/core/extension/endpoint/UserEndpointTest.java @@ -1,5 +1,6 @@ package run.halo.app.core.extension.endpoint; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anySet; @@ -436,6 +437,7 @@ void shouldGetPermission() { "rules": [] } """, Role.class); + when(roleService.listPermissions(eq(Set.of("test-A")))).thenReturn(Flux.just(roleA)); when(userService.listRoles(eq("fake-user"))).thenReturn( Flux.fromIterable(List.of(roleA))); when(roleService.listDependenciesFlux(anySet())).thenReturn(Flux.just(roleA)); @@ -444,25 +446,12 @@ void shouldGetPermission() { .exchange() .expectStatus() .isOk() - .expectBody() - .json(""" - { "roles": [{ - "rules": [], - "apiVersion": "v1alpha1", - "kind": "Role", - "metadata": { - "name": "test-A", - "annotations": { - "rbac.authorization.halo.run/ui-permissions": - "[\\"permission-A\\"]" - } - } - }], - "uiPermissions": [ - "permission-A" - ] - } - """); + .expectBody(UserEndpoint.UserPermission.class) + .value(userPermission -> { + assertEquals(Set.of(roleA), userPermission.getRoles()); + assertEquals(List.of(roleA), userPermission.getPermissions()); + assertEquals(Set.of("permission-A"), userPermission.getUiPermissions()); + }); verify(userService, times(1)).listRoles(eq("fake-user")); } diff --git a/application/src/test/java/run/halo/app/security/authentication/pat/PatTest.java b/application/src/test/java/run/halo/app/security/authentication/pat/PatTest.java index 6a8637af71f..82cbd273acf 100644 --- a/application/src/test/java/run/halo/app/security/authentication/pat/PatTest.java +++ b/application/src/test/java/run/halo/app/security/authentication/pat/PatTest.java @@ -24,6 +24,7 @@ void generatePat() { var requestPat = new PersonalAccessToken(); var spec = requestPat.getSpec(); spec.setRoles(List.of("super-role")); + spec.setName("Fake PAT"); webClient.post() .uri("/apis/api.console.security.halo.run/v1alpha1/users/-/personalaccesstokens") .bodyValue(requestPat)