Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CAT-24] Add User List Endpoint #21

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ nb-configuration.xml

# Plugin directory
/.quarkus/cli/plugins/

api/cat-data/
4 changes: 4 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-oidc</artifactId>
Expand Down
73 changes: 73 additions & 0 deletions api/src/main/java/org/grnet/cat/api/endpoints/UsersEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

import io.quarkus.security.Authenticated;
import jakarta.inject.Inject;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.enums.SecuritySchemeIn;
import org.eclipse.microprofile.openapi.annotations.enums.SecuritySchemeType;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.security.SecurityRequirement;
import org.eclipse.microprofile.openapi.annotations.security.SecurityScheme;
Expand All @@ -22,9 +29,14 @@
import org.grnet.cat.api.utils.Utility;
import org.grnet.cat.dtos.InformativeResponse;
import org.grnet.cat.dtos.UserProfileDto;
import org.grnet.cat.dtos.pagination.PageResource;
import org.grnet.cat.services.IdentifiedService;
import org.grnet.cat.services.UserService;

import java.util.List;

import static org.eclipse.microprofile.openapi.annotations.enums.ParameterIn.QUERY;

@Path("/v1/users")
@Authenticated
@SecurityScheme(securitySchemeName = "Authentication",
Expand Down Expand Up @@ -133,4 +145,65 @@ public Response profile() {

return Response.ok().entity(userProfile).build();
}

@Tag(name = "User")
@Operation(
summary = "Retrieve a list of available users.",
description = "This endpoint returns a list of users registered in the service. Each user object includes basic information such as their type and unique id. " +
" By default, the first page of 10 Users will be returned. You can tune the default values by using the query parameters page and size.")
@SecurityScheme
@APIResponse(
responseCode = "200",
description = "List of Users.",
content = @Content(schema = @Schema(
type = SchemaType.OBJECT,
implementation = PageableUserProfile.class)))
@APIResponse(
responseCode = "401",
description = "User has not been authenticated.",
content = @Content(schema = @Schema(
type = SchemaType.OBJECT,
implementation = InformativeResponse.class)))
@APIResponse(
responseCode = "403",
description = "User has not been registered on CAT service.",
content = @Content(schema = @Schema(
type = SchemaType.OBJECT,
implementation = InformativeResponse.class)))
@APIResponse(
responseCode = "500",
description = "Internal Server Error.",
content = @Content(schema = @Schema(
type = SchemaType.OBJECT,
implementation = InformativeResponse.class)))
@SecurityRequirement(name = "Authentication")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Registration
public Response usersByPage(@Parameter(name = "page", in = QUERY,
description = "Indicates the page number. Page number must be >= 1.") @DefaultValue("1") @Min(value = 1, message = "Page number must be >= 1.") @QueryParam("page") int page,
@Parameter(name = "size", in = QUERY,
description = "The page size.") @DefaultValue("10") @Min(value = 1, message = "Page size must be between 1 and 100.")
@Max(value = 100, message = "Page size must be between 1 and 100.") @QueryParam("size") int size,
@Context UriInfo uriInfo) {

var userProfile = userService.getUsersByPage(page-1, size, uriInfo);

return Response.ok().entity(userProfile).build();
}

public static class PageableUserProfile extends PageResource<UserProfileDto> {

private List<UserProfileDto> content;

@Override
public List<UserProfileDto> getContent() {
return content;
}

@Override
public void setContent(List<UserProfileDto> content) {
this.content = content;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.grnet.cat.dtos.InformativeResponse;

/**
* This endpoint is responsible for rendering the client.html.
Expand All @@ -32,6 +39,18 @@ public class OidcClientTemplate {
@ConfigProperty(name = "keycloak.server.javascript.adapter")
String keycloakServerJavascriptAdapter;

@Tag(name = "Authentication")
@Operation(
hidden = true,
summary = "Redirects a client to the AAI login page.",
description = "The user is presented with the AAI login page, which is hosted by the authentication server. " +
"This page contains options for selecting the user's identity provider (IdP) or organization. After selecting the identity provider, the user is redirected to the selected IdP's login page to enter their credentials." +
" The user enters their username and password on the IdP's login page. The identity provider verifies the user's credentials and determines whether they are valid. If the authentication is successful, the IdP generates an access token.")
@APIResponse(
responseCode = "200",
description = "Returns an html page containing the obtained access token."
)

@GET
@Produces(MediaType.TEXT_HTML)
public String keycloakClient() {
Expand Down
208 changes: 208 additions & 0 deletions api/src/main/resources/META-INF/resources/index.html

Large diffs are not rendered by default.

36 changes: 33 additions & 3 deletions api/src/test/java/org/grnet/cat/api/UsersEndpointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.keycloak.client.KeycloakTestClient;
import io.restassured.http.ContentType;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.grnet.cat.api.endpoints.UsersEndpoint;
import org.grnet.cat.dtos.InformativeResponse;
import org.grnet.cat.dtos.UserProfileDto;
import org.grnet.cat.dtos.pagination.PageResource;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;
Expand All @@ -16,9 +18,6 @@
@TestHTTPEndpoint(UsersEndpoint.class)
public class UsersEndpointTest {

@ConfigProperty(name = "oidc.user.unique.id")
public static String key;

KeycloakTestClient keycloakClient = new KeycloakTestClient();

@Test
Expand Down Expand Up @@ -97,6 +96,37 @@ public void nonRegisterUserRequestsTheirUserProfile() {
assertEquals("User has not been registered on CAT service. User registration is a prerequisite for accessing this API resource.", informativeResponse.message);
}

@Test
public void fetchAllUsers() {

var response = given()
.auth()
.oauth2(getAccessToken("alice"))
.contentType(ContentType.JSON)
.get()
.thenReturn();

assertEquals(200, response.statusCode());
assertEquals(1, response.body().as(PageResource.class).getNumberOfPage());
}

@Test
public void fetchAllUsersBadRequest() {

var informativeResponse = given()
.auth()
.oauth2(getAccessToken("alice"))
.contentType(ContentType.JSON)
.queryParam("page", 0)
.get()
.then()
.statusCode(400)
.extract()
.as(InformativeResponse.class);

assertEquals("Page number must be >= 1.", informativeResponse.message);
}

protected String getAccessToken(String userName) {
return keycloakClient.getAccessToken(userName);
}
Expand Down
12 changes: 12 additions & 0 deletions data-transfer-object/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.grnet.cat.dtos.pagination;

import lombok.Builder;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

@Builder
@Schema(name="PageLink", description="An object represents the links of paginated entities.")
public class PageLink {

@Schema(
type = SchemaType.STRING,
implementation = String.class,
description = "Uri to paginated entities.",
example = "http://localhost:8080/accounting-system/metric-definitions/61eeab7bb3b68f5c3f8c4c24/metrics?page=1&size=10"
)
public String href;

@Schema(
type = SchemaType.STRING,
implementation = String.class,
description = "Descriptor for how the target resource relates to the current resource.",
example = "first"
)
public String rel;
}
Loading