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

Feature process adoption #91

Merged
merged 2 commits into from
Apr 9, 2024
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
@@ -0,0 +1,66 @@
package cat.udl.eps.softarch.demo.handler;

import cat.udl.eps.softarch.demo.domain.Adoption;
import cat.udl.eps.softarch.demo.exceptions.InvalidPostRequest;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import cat.udl.eps.softarch.demo.exceptions.UnauthorizedAccessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;


@Component
@RepositoryEventHandler()
public class ProcessAdoptionEventHandler {

private static final Logger logger = LoggerFactory.getLogger(ProcessAdoptionEventHandler.class);

// These are the roles that are allowed to create an adoption
private static final String ROLE_USER = "ROLE_USER";
private static final String ROLE_SHELTER_VOLUNTEER = "ROLE_SHELTER_VOLUNTEER";
private static final String ROLE_ADMIN = "ROLE_ADMIN";

// This function is called before creating an adoption
@HandleBeforeCreate
public void handleAdoptionBeforeCreate(Adoption adoption) throws UnauthorizedAccessException, InvalidPostRequest {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

// If the user is not authorized, an exception is thrown
if(!isAuthorized(authentication) ) {
String userName = authentication != null ? authentication.getName() : "anonymous";
String errorMessage = String.format("Unauthorized attempt to create an adoption by user: %s", userName);
logger.error(errorMessage);
throw new UnauthorizedAccessException();
}
// If the pet is already adopted or the pet is null, an exception is thrown
else if (adoption.getPet() == null || adoption.getPet().isAdopted() || adoption.getConfirmed()) {
logger.error("Pet is already adopted or bad request");
throw new InvalidPostRequest();
}
// If the adoption is successful, the adoption is in process

logger.info("Adoption for pet {} created successfully by user {}", adoption.getPet().getName(), authentication.getName());
}


// This function allows to check if the user is authorized to perform the action
private boolean isAuthorized(Authentication authentication) {
if (authentication == null || !authentication.isAuthenticated()) {
return false;
}

List<String> requiredAuthorities = Arrays.asList(ROLE_USER, ROLE_SHELTER_VOLUNTEER, ROLE_ADMIN);

return authentication.getAuthorities().stream()
.anyMatch(grantedAuthority -> requiredAuthorities.contains(grantedAuthority.getAuthority()));
}



}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cat.udl.eps.softarch.demo.steps;


import cat.udl.eps.softarch.demo.domain.Adoption;
import cat.udl.eps.softarch.demo.domain.Pet;
import cat.udl.eps.softarch.demo.repository.AdoptionRepository;
Expand All @@ -25,7 +24,6 @@
@SuppressWarnings("ALL")
public class ProcessAdoptionStepDefs {


@Autowired
StepDefs stepDefs;

Expand All @@ -40,12 +38,10 @@ public class ProcessAdoptionStepDefs {

protected ResultActions result;


@And("I receive a confirmation message for adopting the pet")
public void iReceiveAConfirmationMessageForAdoptingThePet() throws Throwable {
result.andExpect(status().isOk())
.andExpect(jsonPath("$.message", is("Adoption successful")));

}

@Given("There is an available pet with name {string} i want to adopt")
Expand All @@ -61,12 +57,10 @@ public void thereIsAnAvailablePetWithName(String arg0) {
pet.setDescription("description");
pet.setBreed("breed");
petRepository.save(pet);

}

@When("I request to adopt the pet with name {string}")
public void iRequestToAdoptThePetWithName(String arg0) throws Throwable {

Adoption adoption = new Adoption();
adoption.setPet(petRepository.findByName(arg0).get(0));
adoption.setUser(userRepository.findAll().iterator().next());
Expand All @@ -82,11 +76,8 @@ public void iRequestToAdoptThePetWithName(String arg0) throws Throwable {
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());

}



@When("I request to adopt without a pet")
public void iRequestToAdoptWithoutAPet() throws Throwable{
// Proceed with adoption logic
Expand All @@ -97,20 +88,17 @@ public void iRequestToAdoptWithoutAPet() throws Throwable{
adoption.setType("Adoption");
adoption.setEndDate(null);


stepDefs.result = stepDefs.mockMvc.perform(
post("/adoptions")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(adoption))
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());

}

@And("The pet with name {string} is already adopted")
public void thePetWithNameIsAlreadyAdopted(String arg0) {

Pet pet = new Pet();
pet.setName(arg0);
pet.setAdopted(true);
Expand All @@ -121,7 +109,5 @@ public void thePetWithNameIsAlreadyAdopted(String arg0) {
pet.setDescription("description");
pet.setBreed("breed");
petRepository.save(pet);


}
}
Loading