Skip to content

Commit

Permalink
Added missing graph endpoints in mock
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Oct 16, 2023
1 parent 6017c6f commit 3367391
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import provisioning.model.ResourceType;
import provisioning.repository.ProvisioningRepository;

import java.util.List;
import java.util.Map;
import java.util.UUID;

Expand Down Expand Up @@ -53,11 +54,13 @@ public ResponseEntity<Map<String, Object>> createUser(@RequestBody Map<String, O
"/graph/users"
));
String inviteRedirectUrl = (String) user.get("inviteRedirectUrl");
String sub = inviteRedirectUrl.substring(inviteRedirectUrl.lastIndexOf("/") + 1);
List<String> parts = List.of(inviteRedirectUrl.split("/"));
String manageId = parts.get(parts.size() - 2);
String userId = parts.get(parts.size() - 1);
String id = UUID.randomUUID().toString();
return ResponseEntity.ok(Map.of(
"invitedUser", Map.of("id", id),
"inviteRedeemUrl", this.mockServerBaseUrl + "/graph/accept/" + sub,
"inviteRedeemUrl", String.format("%s/graph/accept/%s/%s", this.mockServerBaseUrl, manageId, userId),
"id", id));
}

Expand All @@ -76,6 +79,21 @@ public ResponseEntity<Map<String, Object>> getUser() {
return ResponseEntity.ok(Map.of("id", UUID.randomUUID().toString()));
}

@DeleteMapping("/users")
public ResponseEntity<Void> deleteUser() {
LOG.info("/graph/users DELETE ");

provisioningRepository.save(new Provisioning(
ProvisioningType.graph,
objectMapper.valueToTree(null),
HttpMethod.DELETE,
ResourceType.USERS,
"/graph/users"
));

return ResponseEntity.status(201).build();
}

@PatchMapping("/users")
public ResponseEntity<Map<String, Object>> updateUser(@RequestBody Map<String, Object> user) {
LOG.info("/graph/users PATCH " + user);
Expand All @@ -91,11 +109,11 @@ public ResponseEntity<Map<String, Object>> updateUser(@RequestBody Map<String, O
return ResponseEntity.ok(user);
}

@GetMapping("/accept/{sub}")
public View accept(@PathVariable("sub") String sub) {
@GetMapping("/accept/{manageId}/{userId}")
public View accept(@PathVariable("manageId") String manageId, @PathVariable("userId") String userId) {
LOG.info("/graph/accept GET ");

return new RedirectView(this.inviteServerMSRedirectUrl + "/" + sub);
return new RedirectView(String.format("%s/%s/%s", this.inviteServerMSRedirectUrl, manageId, userId));
}

}
29 changes: 29 additions & 0 deletions provisioning-mock/src/test/java/provisioning/api/AbstractTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package provisioning.api;

import io.restassured.RestAssured;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import provisioning.repository.ProvisioningRepository;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class AbstractTest {

@LocalServerPort
protected int port;

@Autowired
protected ProvisioningRepository provisioningRepository;

@BeforeEach
protected void beforeEach() {
RestAssured.port = port;
provisioningRepository.deleteAllInBatch();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package provisioning.api;

import io.restassured.common.mapper.TypeRef;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.UUID;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class GraphControllerTest extends AbstractTest {

@Test
void createUser() {
Map<String, Object> result = given()
.when()
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.body(Map.of("inviteRedirectUrl", "http://localhost/api/v1/invitations/ms-accept-return/1/2"))
.post("/graph/users")
.as(new TypeRef<>() {
});
assertNotNull(result.get("id"));
assertNotNull(((Map) result.get("invitedUser")).get("id"));
assertEquals("http://localhost:8081/graph/accept/1/2", result.get("inviteRedeemUrl"));

assertEquals(1, provisioningRepository.count());
}

@Test
void getUser() {
Map<String, Object> result = given()
.when()
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.get("/graph/users")
.as(new TypeRef<>() {
});
assertNotNull(result.get("id"));
assertEquals(1, provisioningRepository.count());
}

@Test
void updateUser() {
Map<String, Object> result = given()
.when()
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.body(Map.of("id", UUID.randomUUID().toString()))
.patch("/graph/users")
.as(new TypeRef<>() {
});
assertNotNull(result.get("id"));

assertEquals(1, provisioningRepository.count());
}

@Test
void deleteUser() {
given()
.when()
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.delete("/graph/users")
.then()
.statusCode(201);

assertEquals(1, provisioningRepository.count());
}

@Test
void accept() {
given()
.redirects()
.follow(false)
.when()
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.get("/graph/accept/1/2")
.then()
.header("Location", "http://localhost:8080/api/v1/users/ms-accept-return/1/2");
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package provisioning;
package provisioning.api;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import provisioning.repository.ProvisioningRepository;

import java.util.Map;
import java.util.UUID;
Expand All @@ -18,21 +10,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SCIMControllerTest {

@LocalServerPort
protected int port;

@Autowired
protected ProvisioningRepository provisioningRepository;

@BeforeEach
protected void beforeEach() {
RestAssured.port = port;
provisioningRepository.deleteAllInBatch();
}
class SCIMControllerTest extends AbstractTest {

@Test
void user() {
Expand Down
1 change: 0 additions & 1 deletion server/src/main/java/access/provision/Provisioning.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ private void invariant() {
assert scimPassword != null : "scimPassword is null";
}
case graph -> {
assert graphUrl != null: "graphUrl is null";
assert graphClientId != null: "graphClientId is null";
assert graphSecret != null: "graphSecret is null";
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/main/java/access/provision/graph/GraphClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpStatus;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;

Expand Down Expand Up @@ -121,7 +122,7 @@ private GraphServiceClient<Request> getRequestGraphServiceClient(Provisioning pr

private static void replaceGraphUrl(String graphUrl, BaseRequest buildRequest) {
//hack to enable testing
if (graphUrl.startsWith("http://") || graphUrl.contains("mock")) {
if (StringUtils.hasText(graphUrl) && (graphUrl.startsWith("http://") || graphUrl.contains("mock"))) {
Field field = ReflectionUtils.findField(BaseRequest.class, "requestUrl");
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, buildRequest, graphUrl);
Expand Down
5 changes: 2 additions & 3 deletions server/src/test/java/access/provision/ProvisioningTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ void provisioningEva() {
@Test
void provisioningGraph() {
this.assertInvariant(this.provisioningMap(ProvisioningType.graph));
this.assertInvariant(this.provisioningMap(ProvisioningType.graph, "graph_url"));
this.assertInvariant(this.provisioningMap(ProvisioningType.graph, "graph_url", "graph_client_id"));
this.provisioningMap(ProvisioningType.graph, "graph_url", "graph_client_id", "graph_secret");
this.assertInvariant(this.provisioningMap(ProvisioningType.graph,"graph_client_id"));
this.provisioningMap(ProvisioningType.graph, "graph_client_id", "graph_secret");
}

private void assertInvariant(Map<String, Object> provider) {
Expand Down

0 comments on commit 3367391

Please sign in to comment.