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 - Edit shelter #80

Merged
merged 20 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ac21fee
Created .feature for EditShelter feature
SebastianJitaru29 Mar 6, 2024
9eb9453
Merge branch 'main' into Feature-Edit-Shelter
SebastianJitaru29 Mar 6, 2024
7f1aabc
Added step defs
SebastianJitaru29 Mar 13, 2024
704c586
Merge branch 'main' into Feature-Edit-Shelter
SebastianJitaru29 Mar 13, 2024
7003e62
Merge branch 'main' into Feature-Edit-Shelter
SebastianJitaru29 Mar 24, 2024
4a58258
New Scenarios implemeted
SebastianJitaru29 Mar 24, 2024
037d2aa
Added checking if updated shelter has new name
SebastianJitaru29 Apr 2, 2024
45be26c
Solving Pull request maven errors
SebastianJitaru29 Apr 2, 2024
8cd9277
Troubleshoot new error json string can't be null or empty
SebastianJitaru29 Apr 2, 2024
2609391
Troubleshoot new error json string can't be null or empty 2
SebastianJitaru29 Apr 2, 2024
edf7016
Troubleshoot new error json string can't be null or empty 3
SebastianJitaru29 Apr 3, 2024
bde078b
Testing where the error is
SebastianJitaru29 Apr 3, 2024
abcbd25
Testing where the error is
SebastianJitaru29 Apr 3, 2024
610e6be
Util search key function implemented
SebastianJitaru29 Apr 3, 2024
f8021a9
Util search key function implemented
SebastianJitaru29 Apr 3, 2024
4c01610
using jsonobject
SebastianJitaru29 Apr 3, 2024
f9773cd
Merge remote-tracking branch 'origin/main' into Feature-Edit-Shelter
rogargon Apr 4, 2024
20b0b9e
Add missing feature title
rogargon Apr 4, 2024
58e1db0
Fix merge conflicts
rogargon Apr 4, 2024
a9e17e0
Fix web security issue blocking all HTTP methods for shelters
rogargon Apr 4, 2024
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 @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

You should also check that the update has been materialised in the backend. For that you can add an additional step where a GET is performed using the shelter ID and the you check in the returned JSON, using JSONPath, that the updated name is returned now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just pushed the implementation of the specified step and changes in the .feature file


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
Loading