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-Delete-Shelter #78

Merged
merged 7 commits into from
Mar 25, 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
Expand Up @@ -33,6 +33,7 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce
.requestMatchers(HttpMethod.POST, "/**/*").authenticated()
.requestMatchers(HttpMethod.PUT, "/**/*").authenticated()
.requestMatchers(HttpMethod.PATCH, "/**/*").authenticated()
.requestMatchers(HttpMethod.DELETE, "/shelters/*").hasRole("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/**/*").authenticated()
.anyRequest().permitAll())
.csrf((csrf) -> csrf.disable())
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cat/udl/eps/softarch/demo/domain/Admin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cat.udl.eps.softarch.demo.domain;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import lombok.Data;
Expand All @@ -15,8 +17,10 @@
public class Admin extends User{

@Override
@JsonValue(value = false)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@ElementCollection
public Collection<? extends GrantedAuthority> getAuthorities(){
public Collection<? extends GrantedAuthority> getAuthorities() {
return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Shelter;
import cat.udl.eps.softarch.demo.repository.ShelterRepository;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.util.List;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

public class DeleteShelterStepDefs {

@Autowired
private StepDefs stepDefs;

@Autowired
private ShelterRepository shelterRepository;


@Given("There is a created shelter with name {string}, email {string} and phone {string}")
public void thereIsACreatedShelterWithNameEmailAndPhone(String name, String email, String phone) {
if (shelterRepository.findByName(name).isEmpty()) {
Shelter shelter = new Shelter();
shelter.setName(name);
shelter.setEmail(email);
shelter.setMobile(phone);
shelter.setLocatedAt(null);

shelterRepository.save(shelter);
}
}

@When("I try to delete Shelter with name {string}")
public void iTryToDeleteShelterWithName(String name) throws Exception {
List<Shelter> shelterList = shelterRepository.findByName(name);

stepDefs.result = stepDefs.mockMvc.perform(
delete("/shelters/{id}", shelterList.isEmpty() ? "0" : shelterList.get(0).getId())
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import cat.udl.eps.softarch.demo.domain.Admin;
import cat.udl.eps.softarch.demo.domain.ShelterVolunteer;
import cat.udl.eps.softarch.demo.domain.User;
import cat.udl.eps.softarch.demo.repository.AdminRepository;
import cat.udl.eps.softarch.demo.repository.ShelterRepository;
import cat.udl.eps.softarch.demo.repository.ShelterVolunteerRepository;
import cat.udl.eps.softarch.demo.repository.UserRepository;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
Expand All @@ -27,6 +32,12 @@ public class RegisterStepDefs {
@Autowired
private UserRepository userRepository;

@Autowired
private AdminRepository adminRepository;

@Autowired
private ShelterVolunteerRepository shelterVolunteerRepository;

@Given("^There is no registered user with username \"([^\"]*)\"$")
public void thereIsNoRegisteredUserWithUsername(String user) {
Assert.assertFalse("User \""
Expand All @@ -46,6 +57,31 @@ public void thereIsARegisteredUserWithUsernameAndPasswordAndEmail(String usernam
}
}


@Given("^There is a registered admin with username \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$")
public void thereIsARegisteredAdminWithUsernameAndPasswordAndEmail(String username, String password, String email) {
if (!adminRepository.existsById(username)) {
Admin user = new Admin();
user.setEmail(email);
user.setId(username);
user.setPassword(password);
user.encodePassword();
userRepository.save(user);
}
}

@Given("^There is a registered shelter volunteer with username \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$")
public void thereIsARegisteredShelterVolunteerWithUsernameAndPasswordAndEmail(String username, String password, String email) {
if (!shelterVolunteerRepository.existsById(username)) {
ShelterVolunteer user = new ShelterVolunteer();
user.setEmail(email);
user.setId(username);
user.setPassword(password);
user.encodePassword();
userRepository.save(user);
}
}

@And("^I can login with username \"([^\"]*)\" and password \"([^\"]*)\"$")
public void iCanLoginWithUsernameAndPassword(String username, String password) throws Throwable {
AuthenticationStepDefs.currentUsername = username;
Expand Down
47 changes: 47 additions & 0 deletions src/test/resources/features/DeleteShelter.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Feature: Delete Shelter
In order to use the app
only admins must be able to delete Shelters

Background:
Given There is a created shelter with name "name", email "[email protected]" and phone "123123123"

Scenario: Delete a Shelter not logged in
Given I'm not logged in
When I try to delete Shelter with name "name"
Then The response code is 401

Scenario: Delete a Shelter as user
Given There is a registered user with username "user" and password "password" and email "[email protected]"
Given I login as "user" with password "password"
When I try to delete Shelter with name "name"
Then The response code is 403

Scenario: Delete a Shelter as ShelterVolunteer
Given There is a registered shelter volunteer with username "ShelterVolunteer" and password "password" and email "[email protected]"
Given I login as "ShelterVolunteer" with password "password"
When I try to delete Shelter with name "name"
Then The response code is 403

Scenario: Delete a Shelter as admin
Given There is a registered admin with username "admin" and password "password" and email "[email protected]"
Given I login as "admin" with password "password"
When I try to delete Shelter with name "name"
Then The response code is 200

Scenario: Delete a Shelter that does not exist as user
Given There is a registered user with username "user" and password "password" and email "[email protected]"
Given I login as "user" with password "password"
When I try to delete Shelter with name "name"
Then The response code is 403

Scenario: Delete a Shelter that does not exist as ShelterVolunteer
Given There is a registered shelter volunteer with username "ShelterVolunteer" and password "password" and email "[email protected]"
Given I login as "ShelterVolunteer" with password "password"
When I try to delete Shelter with name "name"
Then The response code is 403

Scenario: Delete a Shelter that does not exist as admin
Given There is a registered admin with username "admin" and password "password" and email "[email protected]"
Given I login as "admin" with password "password"
When I try to delete Shelter with name "unregistered"
Then The response code is 404
Loading