Skip to content

Commit

Permalink
Fix unit test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnNiang committed Sep 20, 2023
1 parent d703168 commit b7dce9e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -34,6 +37,7 @@ public static class Spec {

private List<String> scopes;

@Schema(requiredMode = REQUIRED)
private String username;

private boolean revoked;
Expand All @@ -42,6 +46,7 @@ public static class Spec {

private Instant lastUsed;

@Schema(requiredMode = REQUIRED)
private String tokenId;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,8 @@ private Mono<ServerResponse> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private Flux<Role> listDependencies(Set<String> names, Predicate<Role> additiona

return Flux.fromIterable(dependencies)
.filter(dep -> !visited.contains(dep))
.collect(Collectors.toSet())
.collect(Collectors.<String>toSet())
.flatMapMany(deps -> listRoles(deps, additionalPredicate));
})
.concatWith(Flux.defer(() -> listAggregatedRoles(visited, additionalPredicate)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_";

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b7dce9e

Please sign in to comment.