Skip to content

Commit 16c4ddd

Browse files
committed
Added more tests
1 parent b11f82c commit 16c4ddd

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

services/src/test/java/io/scalecube/services/AuthTest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ void afterEach() {
101101
class SecuredTests {
102102

103103
@ParameterizedTest(name = "[{index}] {0}")
104-
@MethodSource("successfulAuthenticationMethodSource")
105-
void successfulAuthentication(String test, SuccessArgs args) {
104+
@MethodSource("authenticateSuccessfullyMethodSource")
105+
void authenticateSuccessfully(String test, SuccessArgs args) {
106106
serviceCall =
107107
serviceCall(
108108
($, serviceRole) -> credentials(args.tokenSupplier.apply(serviceRole)),
@@ -118,7 +118,7 @@ private record SuccessArgs(
118118
String serviceRole,
119119
List<String> allowedRoles) {}
120120

121-
private static Stream<Arguments> successfulAuthenticationMethodSource() {
121+
private static Stream<Arguments> authenticateSuccessfullyMethodSource() {
122122
return Stream.of(
123123
arguments(
124124
"Authenticate by service role only",
@@ -213,7 +213,7 @@ private static Stream<Arguments> failedAuthenticationSource() {
213213
class CompositeSecuredTests {
214214

215215
@Test
216-
void successfulAuthentication() {
216+
void authenticateSuccessfully() {
217217
final var role = "invoker";
218218
final var tokenCredentials = new TokenCredentials(VALID_TOKEN, role, null);
219219

@@ -274,6 +274,22 @@ private static Stream<Arguments> failedAuthenticationSource() {
274274
}
275275
}
276276

277+
@Nested
278+
class ServiceRoleSecuredTests {
279+
280+
@Test
281+
void authenticateSuccessfully() {
282+
final var role = "admin";
283+
final var tokenCredentials =
284+
new TokenCredentials(VALID_TOKEN, role, List.of("read", "write"));
285+
286+
serviceCall = serviceCall((sr, r) -> credentials(tokenCredentials), role, null);
287+
288+
StepVerifier.create(serviceCall.api(SecuredService.class).invokeWithAllowedRoleAnnotation())
289+
.verifyComplete();
290+
}
291+
}
292+
277293
private static Mono<byte[]> credentials(TokenCredentials tokenCredentials) {
278294
return Mono.fromCallable(
279295
() -> {

services/src/test/java/io/scalecube/services/sut/security/SecuredService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ public interface SecuredService {
1414
@Secured
1515
@ServiceMethod
1616
Mono<Void> invokeWithRoleOrPermissions();
17+
18+
// Services secured by annotations in method body
19+
20+
@Secured
21+
@ServiceMethod
22+
Mono<Void> invokeWithAllowedRoleAnnotation();
1723
}

services/src/test/java/io/scalecube/services/sut/security/SecuredServiceImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.scalecube.services.sut.security;
22

33
import io.scalecube.services.RequestContext;
4+
import io.scalecube.services.auth.AllowedRole;
45
import io.scalecube.services.exceptions.ForbiddenException;
56
import reactor.core.publisher.Mono;
67

@@ -33,4 +34,14 @@ public Mono<Void> invokeWithRoleOrPermissions() {
3334
})
3435
.then();
3536
}
37+
38+
// Services secured by annotations in method body
39+
40+
@AllowedRole(
41+
name = "admin",
42+
permissions = {"read", "write"})
43+
@Override
44+
public Mono<Void> invokeWithAllowedRoleAnnotation() {
45+
return RequestContext.deferSecured().then();
46+
}
3647
}

0 commit comments

Comments
 (0)