Skip to content

Commit

Permalink
Merge pull request #80 from UdL-EPS-SoftArch/Feature-Edit-Shelter
Browse files Browse the repository at this point in the history
Feature - Edit shelter
  • Loading branch information
rogargon authored Apr 4, 2024
2 parents cb86c1c + a9e17e0 commit 3401676
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce
.requestMatchers(HttpMethod.DELETE, "/shelters/*").hasRole("ADMIN")
.requestMatchers(HttpMethod.DELETE, "/pets/*").hasAnyRole("SHELTER_VOLUNTEER", "ADMIN")
.requestMatchers(HttpMethod.DELETE, "/**/*").authenticated()
.requestMatchers("/shelters/**").hasAuthority("ROLE_ADMIN")
.requestMatchers(HttpMethod.POST, "/shelters/**").hasAuthority("ROLE_ADMIN")
.requestMatchers(HttpMethod.POST, "/**/*").authenticated()

.anyRequest().permitAll())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,113 @@
package cat.udl.eps.softarch.demo.steps;

import cat.udl.eps.softarch.demo.domain.Shelter;
import cat.udl.eps.softarch.demo.domain.User;
import cat.udl.eps.softarch.demo.domain.ShelterVolunteer;
import cat.udl.eps.softarch.demo.domain.Admin;


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;
import io.cucumber.java.en.When;
import org.json.JSONObject;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.nio.charset.StandardCharsets;
import java.util.List;

import static com.jayway.jsonpath.internal.path.PathCompiler.fail;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;

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

import org.json.JSONObject;

public class EditShelterStepDefs {

@Autowired
ShelterRepository shelterRepository;
User user = new User();
@Autowired
UserRepository userRepository;
@And("^There a shelter with name \"([^\"]*)\" email \"([^\"]*)\" and mobile \"([^\"]*)\"$")
public void thereAShelterWithNameEmailAndMobile(String name, String email, String mobile) {
@Autowired
AdminRepository adminRepository;
@Autowired
ShelterVolunteerRepository shelterVolunteerRepository;
@Autowired
StepDefs stepDefs;
@And("^There is already a shelter with name \"([^\"]*)\" email \"([^\"]*)\" and mobile \"([^\"]*)\"$")
public void thereIsAlreadyAShelterWithNameEmailAndMobile(String name, String email, String mobile) {
Shelter shelter = new Shelter();
shelter.setName(name);
shelter.setEmail(email);
shelter.setMobile(mobile);
shelterRepository.save(shelter);
}

@When("^I update the shelter with name \"([^\"]*)\" to name \"([^\"]*)\" email \"([^\"]*)\" and mobile \"([^\"]*)\"$")
public void iUpdateTheShelterWithNameToNameEmailAndMobile(String prevName, String newName, String email, String mobile)
throws Throwable {
Shelter shelter = shelterRepository.findByName(prevName).get(0);
if (shelter != null) {
shelter = shelterRepository.findByName(prevName).get(0);
}
stepDefs.result = stepDefs.mockMvc.perform(patch(shelter.getUri())
.contentType(MediaType.APPLICATION_JSON)
.content(new JSONObject().put("name", newName)
.put("email", email)
.put("mobile", mobile).toString())
.characterEncoding(StandardCharsets.UTF_8)
.accept(MediaType.APPLICATION_JSON)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());
}

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

@And("^I get the shelter with name \"([^\"]*)\"")
public void iGetTheShelterWithName(String newname) throws Exception {
List<Shelter> shelters = shelterRepository.findByName(newname);
Shelter shelter = shelters.get(0);
stepDefs.result = stepDefs.mockMvc.perform(get("/shelters/" + shelter.getId())
.accept(MediaType.APPLICATION_JSON)).andDo(print());
JSONObject jsonObject = new JSONObject(stepDefs.result.andReturn().getResponse().getContentAsString());
jsonObject.getString("name");
try {
String actualName = jsonObject.getString("name");
Assert.assertEquals(newname, actualName);
} catch (Exception e) {
fail("Key not found");
}
}


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

}
}
}
2 changes: 1 addition & 1 deletion src/test/resources/features/DeletePet.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Feature:
Feature: Delete Pet
in order to use the app
As a Shelter Volunteer
I must be able to delete a Pet
Expand Down
53 changes: 53 additions & 0 deletions src/test/resources/features/EditShelter.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Feature: Edit Shelter
In order to update shelter information
As a shelter manager
I want to edit a shelter

Background:
Given There is a registered user with username "user" and password "existing" and email "[email protected]"
Given There is a registered already admin with username "admin" and password "admin" and email "[email protected]"
Given There is a registered volunteer with username "volunteer" and password "volunteer"
And There is already a shelter with name "Shelter 1" email "[email protected]" and mobile "999999999"

Scenario: Edit shelter without being logged in
Given I'm not logged in
When I update the shelter with name "Shelter 1" to name "Another Shelter" email "[email protected]" and mobile "123123123"
Then The response code is 401
And The error message is "Unauthorized"

Scenario: Edit shelter with user
Given I login as "client" with password "existing"
When I update the shelter with name "Shelter 1" to name "Another Shelter" email "[email protected]" and mobile "123123123"
Then The response code is 401
And The error message is "Unauthorized"

Scenario: Edit shelter with admin
Given I login as "admin" with password "admin"
When I update the shelter with name "Shelter 1" to name "Another Shelter" email "[email protected]" and mobile "123123123"
And I get the shelter with name "Another Shelter"
Then The response code is 200

Scenario: Edit shelter with volunteer
Given I login as "volunteer" with password "volunteer"
When I update the shelter with name "Shelter 1" to name "Another Shelter" email "[email protected]" and mobile "123123123"
And I get the shelter with name "Another Shelter"
Then The response code is 200

Scenario: Edit shelter with missing new name
Given I login as "admin" with password "admin"
When I update the shelter with name "Shelter 1" to name "" email "[email protected]" and mobile "123123123"
Then The response code is 400
And The error message is "must not be blank"

Scenario: Edit shelter with missing new email
Given I login as "admin" with password "admin"
When I update the shelter with name "Shelter 1" to name "Another Shelter" email "" and mobile "123123123"
Then The response code is 400
And The error message is "must not be blank"

Scenario: Edit shelter with missing new mobile
Given I login as "admin" with password "admin"
When I update the shelter with name "Shelter 1" to name "Another Shelter" email "[email protected]" and mobile ""
Then The response code is 400
And The error message is "must not be blank"

9 changes: 2 additions & 7 deletions src/test/resources/features/KickUserFromShelter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Feature: Kick ShelterVolunteer from Shelter

Background:
Given There is a registered user with username "user" and password "pass" and email "[email protected]"
And There a shelter with name "shelter1" email "[email protected]" and mobile "12345678"
And There is already a shelter with name "shelter1" email "[email protected]" and mobile "12345678"
And There is a shelter volunteer with username "volunteer" and password "pass" in the shelter "shelter1"
And There is a shelter volunteer with username "volunteer1" and password "pass" in the shelter "shelter1"
And There a shelter with name "shelter2" email "[email protected]" and mobile "12345679"
And There is already a shelter with name "shelter2" email "[email protected]" and mobile "12345679"
And There is a shelter volunteer with username "volunteer2" and password "pass" in the shelter "shelter2"

Scenario: Kick volunteer from shelter
Expand All @@ -17,18 +17,13 @@ Feature: Kick ShelterVolunteer from Shelter
Then The response code is 200
And I cannot login with username "volunteer1" and password "pass"



Scenario: Kick volunteer from shelter as user

And I can login with username "user" and password "pass"
When I kick user "volunteer1" from shelter "shelter"
Then The response code is 403
And I can login with username "volunteer1" and password "pass"


Scenario: Kick volunteer from shelter as volunteer in another shelter

Given I can login with username "volunteer" and password "pass"
When I kick user "volunteer2" from shelter "shelter"
Then The response code is 412
Expand Down

0 comments on commit 3401676

Please sign in to comment.