|
|
@ -63,7 +63,8 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
public void setup() throws Exception {
|
|
|
|
public void setup() throws Exception {
|
|
|
|
this.server = new MockWebServer();
|
|
|
|
this.server = new MockWebServer();
|
|
|
|
this.builder = WebClient.builder().baseUrl(this.server.url("/").toString());
|
|
|
|
this.builder = WebClient.builder().baseUrl(this.server.url("/").toString());
|
|
|
|
this.securityService = new ReactiveCloudFoundrySecurityService(this.builder, CLOUD_CONTROLLER);
|
|
|
|
this.securityService = new ReactiveCloudFoundrySecurityService(this.builder,
|
|
|
|
|
|
|
|
CLOUD_CONTROLLER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
@After
|
|
|
@ -76,12 +77,15 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
String responseBody = "{\"read_sensitive_data\": true,\"read_basic_data\": true}";
|
|
|
|
String responseBody = "{\"read_sensitive_data\": true,\"read_basic_data\": true}";
|
|
|
|
prepareResponse(response -> response.setBody(responseBody)
|
|
|
|
prepareResponse(response -> response.setBody(responseBody)
|
|
|
|
.setHeader("Content-Type", "application/json"));
|
|
|
|
.setHeader("Content-Type", "application/json"));
|
|
|
|
StepVerifier.create(this.securityService.getAccessLevel("my-access-token", "my-app-id"))
|
|
|
|
StepVerifier
|
|
|
|
.consumeNextWith(
|
|
|
|
.create(this.securityService.getAccessLevel("my-access-token",
|
|
|
|
accessLevel -> assertThat(accessLevel).isEqualTo(AccessLevel.FULL))
|
|
|
|
"my-app-id"))
|
|
|
|
|
|
|
|
.consumeNextWith(accessLevel -> assertThat(accessLevel)
|
|
|
|
|
|
|
|
.isEqualTo(AccessLevel.FULL))
|
|
|
|
.expectComplete().verify();
|
|
|
|
.expectComplete().verify();
|
|
|
|
expectRequest(request -> {
|
|
|
|
expectRequest(request -> {
|
|
|
|
assertThat(request.getHeader(HttpHeaders.AUTHORIZATION)).isEqualTo("bearer my-access-token");
|
|
|
|
assertThat(request.getHeader(HttpHeaders.AUTHORIZATION))
|
|
|
|
|
|
|
|
.isEqualTo("bearer my-access-token");
|
|
|
|
assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER_PERMISSIONS);
|
|
|
|
assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER_PERMISSIONS);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -92,12 +96,15 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
String responseBody = "{\"read_sensitive_data\": false,\"read_basic_data\": true}";
|
|
|
|
String responseBody = "{\"read_sensitive_data\": false,\"read_basic_data\": true}";
|
|
|
|
prepareResponse(response -> response.setBody(responseBody)
|
|
|
|
prepareResponse(response -> response.setBody(responseBody)
|
|
|
|
.setHeader("Content-Type", "application/json"));
|
|
|
|
.setHeader("Content-Type", "application/json"));
|
|
|
|
StepVerifier.create(this.securityService.getAccessLevel("my-access-token", "my-app-id"))
|
|
|
|
StepVerifier
|
|
|
|
.consumeNextWith(
|
|
|
|
.create(this.securityService.getAccessLevel("my-access-token",
|
|
|
|
accessLevel -> assertThat(accessLevel).isEqualTo(AccessLevel.RESTRICTED))
|
|
|
|
"my-app-id"))
|
|
|
|
|
|
|
|
.consumeNextWith(accessLevel -> assertThat(accessLevel)
|
|
|
|
|
|
|
|
.isEqualTo(AccessLevel.RESTRICTED))
|
|
|
|
.expectComplete().verify();
|
|
|
|
.expectComplete().verify();
|
|
|
|
expectRequest(request -> {
|
|
|
|
expectRequest(request -> {
|
|
|
|
assertThat(request.getHeader(HttpHeaders.AUTHORIZATION)).isEqualTo("bearer my-access-token");
|
|
|
|
assertThat(request.getHeader(HttpHeaders.AUTHORIZATION))
|
|
|
|
|
|
|
|
.isEqualTo("bearer my-access-token");
|
|
|
|
assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER_PERMISSIONS);
|
|
|
|
assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER_PERMISSIONS);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -105,15 +112,18 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void getAccessLevelWhenTokenIsNotValidShouldThrowException() throws Exception {
|
|
|
|
public void getAccessLevelWhenTokenIsNotValidShouldThrowException() throws Exception {
|
|
|
|
prepareResponse(response -> response.setResponseCode(401));
|
|
|
|
prepareResponse(response -> response.setResponseCode(401));
|
|
|
|
StepVerifier.create(this.securityService.getAccessLevel("my-access-token", "my-app-id"))
|
|
|
|
StepVerifier.create(
|
|
|
|
.consumeErrorWith(
|
|
|
|
this.securityService.getAccessLevel("my-access-token", "my-app-id"))
|
|
|
|
throwable -> {
|
|
|
|
.consumeErrorWith(throwable -> {
|
|
|
|
assertThat(throwable).isInstanceOf(CloudFoundryAuthorizationException.class);
|
|
|
|
assertThat(throwable)
|
|
|
|
assertThat(((CloudFoundryAuthorizationException) throwable).getReason()).isEqualTo(Reason.INVALID_TOKEN);
|
|
|
|
.isInstanceOf(CloudFoundryAuthorizationException.class);
|
|
|
|
})
|
|
|
|
assertThat(
|
|
|
|
.verify();
|
|
|
|
((CloudFoundryAuthorizationException) throwable).getReason())
|
|
|
|
|
|
|
|
.isEqualTo(Reason.INVALID_TOKEN);
|
|
|
|
|
|
|
|
}).verify();
|
|
|
|
expectRequest(request -> {
|
|
|
|
expectRequest(request -> {
|
|
|
|
assertThat(request.getHeader(HttpHeaders.AUTHORIZATION)).isEqualTo("bearer my-access-token");
|
|
|
|
assertThat(request.getHeader(HttpHeaders.AUTHORIZATION))
|
|
|
|
|
|
|
|
.isEqualTo("bearer my-access-token");
|
|
|
|
assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER_PERMISSIONS);
|
|
|
|
assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER_PERMISSIONS);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -121,15 +131,18 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
public void getAccessLevelWhenForbiddenShouldThrowException() throws Exception {
|
|
|
|
public void getAccessLevelWhenForbiddenShouldThrowException() throws Exception {
|
|
|
|
prepareResponse(response -> response.setResponseCode(403));
|
|
|
|
prepareResponse(response -> response.setResponseCode(403));
|
|
|
|
StepVerifier.create(this.securityService.getAccessLevel("my-access-token", "my-app-id"))
|
|
|
|
StepVerifier.create(
|
|
|
|
.consumeErrorWith(
|
|
|
|
this.securityService.getAccessLevel("my-access-token", "my-app-id"))
|
|
|
|
throwable -> {
|
|
|
|
.consumeErrorWith(throwable -> {
|
|
|
|
assertThat(throwable).isInstanceOf(CloudFoundryAuthorizationException.class);
|
|
|
|
assertThat(throwable)
|
|
|
|
assertThat(((CloudFoundryAuthorizationException) throwable).getReason()).isEqualTo(Reason.ACCESS_DENIED);
|
|
|
|
.isInstanceOf(CloudFoundryAuthorizationException.class);
|
|
|
|
})
|
|
|
|
assertThat(
|
|
|
|
.verify();
|
|
|
|
((CloudFoundryAuthorizationException) throwable).getReason())
|
|
|
|
|
|
|
|
.isEqualTo(Reason.ACCESS_DENIED);
|
|
|
|
|
|
|
|
}).verify();
|
|
|
|
expectRequest(request -> {
|
|
|
|
expectRequest(request -> {
|
|
|
|
assertThat(request.getHeader(HttpHeaders.AUTHORIZATION)).isEqualTo("bearer my-access-token");
|
|
|
|
assertThat(request.getHeader(HttpHeaders.AUTHORIZATION))
|
|
|
|
|
|
|
|
.isEqualTo("bearer my-access-token");
|
|
|
|
assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER_PERMISSIONS);
|
|
|
|
assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER_PERMISSIONS);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -138,15 +151,18 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
public void getAccessLevelWhenCloudControllerIsNotReachableThrowsException()
|
|
|
|
public void getAccessLevelWhenCloudControllerIsNotReachableThrowsException()
|
|
|
|
throws Exception {
|
|
|
|
throws Exception {
|
|
|
|
prepareResponse(response -> response.setResponseCode(500));
|
|
|
|
prepareResponse(response -> response.setResponseCode(500));
|
|
|
|
StepVerifier.create(this.securityService.getAccessLevel("my-access-token", "my-app-id"))
|
|
|
|
StepVerifier.create(
|
|
|
|
.consumeErrorWith(
|
|
|
|
this.securityService.getAccessLevel("my-access-token", "my-app-id"))
|
|
|
|
throwable -> {
|
|
|
|
.consumeErrorWith(throwable -> {
|
|
|
|
assertThat(throwable).isInstanceOf(CloudFoundryAuthorizationException.class);
|
|
|
|
assertThat(throwable)
|
|
|
|
assertThat(((CloudFoundryAuthorizationException) throwable).getReason()).isEqualTo(Reason.SERVICE_UNAVAILABLE);
|
|
|
|
.isInstanceOf(CloudFoundryAuthorizationException.class);
|
|
|
|
})
|
|
|
|
assertThat(
|
|
|
|
.verify();
|
|
|
|
((CloudFoundryAuthorizationException) throwable).getReason())
|
|
|
|
|
|
|
|
.isEqualTo(Reason.SERVICE_UNAVAILABLE);
|
|
|
|
|
|
|
|
}).verify();
|
|
|
|
expectRequest(request -> {
|
|
|
|
expectRequest(request -> {
|
|
|
|
assertThat(request.getHeader(HttpHeaders.AUTHORIZATION)).isEqualTo("bearer my-access-token");
|
|
|
|
assertThat(request.getHeader(HttpHeaders.AUTHORIZATION))
|
|
|
|
|
|
|
|
.isEqualTo("bearer my-access-token");
|
|
|
|
assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER_PERMISSIONS);
|
|
|
|
assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER_PERMISSIONS);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -173,11 +189,13 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
StepVerifier.create(this.securityService.fetchTokenKeys())
|
|
|
|
StepVerifier.create(this.securityService.fetchTokenKeys())
|
|
|
|
.consumeNextWith(
|
|
|
|
.consumeNextWith(tokenKeys -> assertThat(tokenKeys.get("test-key"))
|
|
|
|
tokenKeys -> assertThat(tokenKeys.get("test-key")).isEqualTo(tokenKeyValue))
|
|
|
|
.isEqualTo(tokenKeyValue))
|
|
|
|
.expectComplete().verify();
|
|
|
|
.expectComplete().verify();
|
|
|
|
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/my-cloud-controller.com/info"));
|
|
|
|
expectRequest(request -> assertThat(request.getPath())
|
|
|
|
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/my-uaa.com/token_keys"));
|
|
|
|
.isEqualTo("/my-cloud-controller.com/info"));
|
|
|
|
|
|
|
|
expectRequest(request -> assertThat(request.getPath())
|
|
|
|
|
|
|
|
.isEqualTo("/my-uaa.com/token_keys"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -192,11 +210,12 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
StepVerifier.create(this.securityService.fetchTokenKeys())
|
|
|
|
StepVerifier.create(this.securityService.fetchTokenKeys())
|
|
|
|
.consumeNextWith(
|
|
|
|
.consumeNextWith(tokenKeys -> assertThat(tokenKeys).hasSize(0))
|
|
|
|
tokenKeys -> assertThat(tokenKeys).hasSize(0))
|
|
|
|
|
|
|
|
.expectComplete().verify();
|
|
|
|
.expectComplete().verify();
|
|
|
|
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/my-cloud-controller.com/info"));
|
|
|
|
expectRequest(request -> assertThat(request.getPath())
|
|
|
|
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/my-uaa.com/token_keys"));
|
|
|
|
.isEqualTo("/my-cloud-controller.com/info"));
|
|
|
|
|
|
|
|
expectRequest(request -> assertThat(request.getPath())
|
|
|
|
|
|
|
|
.isEqualTo("/my-uaa.com/token_keys"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -209,12 +228,14 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
response.setResponseCode(500);
|
|
|
|
response.setResponseCode(500);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
StepVerifier.create(this.securityService.fetchTokenKeys())
|
|
|
|
StepVerifier.create(this.securityService.fetchTokenKeys())
|
|
|
|
.consumeErrorWith(
|
|
|
|
.consumeErrorWith(throwable -> assertThat(
|
|
|
|
throwable -> assertThat(((CloudFoundryAuthorizationException) throwable)
|
|
|
|
((CloudFoundryAuthorizationException) throwable).getReason())
|
|
|
|
.getReason()).isEqualTo(Reason.SERVICE_UNAVAILABLE))
|
|
|
|
.isEqualTo(Reason.SERVICE_UNAVAILABLE))
|
|
|
|
.verify();
|
|
|
|
.verify();
|
|
|
|
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/my-cloud-controller.com/info"));
|
|
|
|
expectRequest(request -> assertThat(request.getPath())
|
|
|
|
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/my-uaa.com/token_keys"));
|
|
|
|
.isEqualTo("/my-cloud-controller.com/info"));
|
|
|
|
|
|
|
|
expectRequest(request -> assertThat(request.getPath())
|
|
|
|
|
|
|
|
.isEqualTo("/my-uaa.com/token_keys"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -224,11 +245,12 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
StepVerifier.create(this.securityService.getUaaUrl())
|
|
|
|
StepVerifier.create(this.securityService.getUaaUrl())
|
|
|
|
.consumeNextWith(
|
|
|
|
.consumeNextWith(uaaUrl -> assertThat(uaaUrl).isEqualTo(UAA_URL))
|
|
|
|
uaaUrl -> assertThat(uaaUrl).isEqualTo(UAA_URL))
|
|
|
|
|
|
|
|
.expectComplete().verify();
|
|
|
|
.expectComplete().verify();
|
|
|
|
//this.securityService.getUaaUrl().block(); //FIXME subscribe again to check that it isn't called again
|
|
|
|
// this.securityService.getUaaUrl().block(); //FIXME subscribe again to check that
|
|
|
|
expectRequest(request -> assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER + "/info"));
|
|
|
|
// it isn't called again
|
|
|
|
|
|
|
|
expectRequest(request -> assertThat(request.getPath())
|
|
|
|
|
|
|
|
.isEqualTo(CLOUD_CONTROLLER + "/info"));
|
|
|
|
expectRequestCount(1);
|
|
|
|
expectRequestCount(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -237,13 +259,15 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
throws Exception {
|
|
|
|
throws Exception {
|
|
|
|
prepareResponse(response -> response.setResponseCode(500));
|
|
|
|
prepareResponse(response -> response.setResponseCode(500));
|
|
|
|
StepVerifier.create(this.securityService.getUaaUrl())
|
|
|
|
StepVerifier.create(this.securityService.getUaaUrl())
|
|
|
|
.consumeErrorWith(
|
|
|
|
.consumeErrorWith(throwable -> {
|
|
|
|
throwable -> {
|
|
|
|
assertThat(throwable)
|
|
|
|
assertThat(throwable).isInstanceOf(CloudFoundryAuthorizationException.class);
|
|
|
|
.isInstanceOf(CloudFoundryAuthorizationException.class);
|
|
|
|
assertThat(((CloudFoundryAuthorizationException) throwable).getReason()).isEqualTo(Reason.SERVICE_UNAVAILABLE);
|
|
|
|
assertThat(
|
|
|
|
})
|
|
|
|
((CloudFoundryAuthorizationException) throwable).getReason())
|
|
|
|
.verify();
|
|
|
|
.isEqualTo(Reason.SERVICE_UNAVAILABLE);
|
|
|
|
expectRequest(request -> assertThat(request.getPath()).isEqualTo(CLOUD_CONTROLLER + "/info"));
|
|
|
|
}).verify();
|
|
|
|
|
|
|
|
expectRequest(request -> assertThat(request.getPath())
|
|
|
|
|
|
|
|
.isEqualTo(CLOUD_CONTROLLER + "/info"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void prepareResponse(Consumer<MockResponse> consumer) {
|
|
|
|
private void prepareResponse(Consumer<MockResponse> consumer) {
|
|
|
@ -252,7 +276,8 @@ public class ReactiveCloudFoundrySecurityServiceTests {
|
|
|
|
this.server.enqueue(response);
|
|
|
|
this.server.enqueue(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void expectRequest(Consumer<RecordedRequest> consumer) throws InterruptedException {
|
|
|
|
private void expectRequest(Consumer<RecordedRequest> consumer)
|
|
|
|
|
|
|
|
throws InterruptedException {
|
|
|
|
consumer.accept(this.server.takeRequest());
|
|
|
|
consumer.accept(this.server.takeRequest());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|