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

✨ (#220): Add endpoint to get my org #221

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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.hopps.org.rest;

import app.hopps.org.jpa.Member;
import app.hopps.org.jpa.MemberRepository;
import app.hopps.org.jpa.Organization;
import app.hopps.org.jpa.OrganizationRepository;
import app.hopps.org.rest.RestValidator.ValidationResult;
Expand All @@ -12,12 +13,15 @@
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.SecurityContext;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
Expand All @@ -28,6 +32,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Map;

@Path("/organization")
Expand All @@ -36,17 +41,18 @@ public class OrganizationResource {

private static final Logger LOG = LoggerFactory.getLogger(OrganizationResource.class);

private final Validator validator;
private final Process<? extends Model> process;
private final OrganizationRepository organizationRepository;
@Inject
Validator validator;

@Inject
public OrganizationResource(Validator validator, @Named("NewOrganization") Process<? extends Model> process,
OrganizationRepository organizationRepository) {
this.validator = validator;
this.process = process;
this.organizationRepository = organizationRepository;
}
@Named("NewOrganization")
Process<? extends Model> process;

@Inject
OrganizationRepository organizationRepository;

@Inject
MemberRepository memberRepository;

@GET
@Path("{slug}")
Expand All @@ -63,6 +69,29 @@ public Response getOrganizationBySlug(@PathParam("slug") String slug) {
return Response.ok(organization).build();
}

@GET
@Path("/my")
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Get my organization", description = "Retrieves the details of an organization the current user is assigned to.")
@APIResponse(responseCode = "200", description = "Own organization retrieved successfully", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = Organization.class)))
@APIResponse(responseCode = "404", description = "Organization not found for user")
public Organization getMyOrganization(@Context SecurityContext securityContext) {
3thr3n marked this conversation as resolved.
Show resolved Hide resolved

String userName = securityContext.getUserPrincipal().getName();
Member me = memberRepository.findByEmail(userName);
if (me == null) {
throw new NotFoundException("Organization of User not found");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wäre gut, wenn wir auch eine Fehlermeldung bekommen statt nur den code. Quarkus ist ja leider so schlau und lässt die Fehlermeldung weg aus der Response.
Gerne mal in Zeile 142 und 143 schauen.

}

Collection<Organization> orgs = me.getOrganizations();
if (orgs.size() > 1) {
throw new IllegalStateException(
"More than one organization is currently not implemented. User: " + userName);
}

return orgs.stream().findFirst().orElseThrow();
}

@GET
@Path("{slug}/members")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package app.hopps.org.rest;

import app.hopps.org.jpa.Organization;
import app.hopps.org.jpa.OrganizationRepository;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.security.TestSecurity;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.MediaType;
import org.flywaydb.core.Flyway;
import org.instancio.Instancio;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.SQLException;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;
import static org.slf4j.LoggerFactory.getLogger;

@QuarkusTest
@TestHTTPEndpoint(OrganizationResource.class)
class OrganizationResourceAuthorizedTests {

@Inject
Flyway flyway;

@BeforeEach
public void cleanDatabase() throws Exception {
flyway.clean();
flyway.migrate();
}

@Test
@DisplayName("should return Organization of current user")
@TestSecurity(user = "[email protected]")
void shouldReturnMyOrg() {

given()
.contentType(MediaType.APPLICATION_JSON)
.when()
.get("my")
.then()
.statusCode(200)
.body("name", is("Grünes Herz e.V."));
}
}
Loading