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-Create-Shelter #76

Merged
merged 24 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2c21c40
Created CreateShelter.feature file
SebastianJitaru29 Mar 5, 2024
0efd547
Created CreateShelterStepDefs
SebastianJitaru29 Mar 5, 2024
ebd512e
First scenario implemented
SebastianJitaru29 Mar 5, 2024
d80f3e3
Merge branch 'main' into Feature-Create-Shelter
SebastianJitaru29 Mar 5, 2024
acce524
Changes to Create Shelter Step Defs
SebastianJitaru29 Mar 5, 2024
5eca35a
Changes to CreateShelter.feature
SebastianJitaru29 Mar 5, 2024
ef3a4a4
Changes to ShelterRepository, Completed test scenario1
SebastianJitaru29 Mar 5, 2024
61ba33c
KickUserFromShelter feature file created
SebastianJitaru29 Mar 6, 2024
5e5e289
Deleted other feature file from branch
SebastianJitaru29 Mar 6, 2024
543264a
Changes to CreateShelter feature and steps file
SebastianJitaru29 Mar 6, 2024
69bc951
Merge branch 'main' into Feature-Create-Shelter
SebastianJitaru29 Mar 6, 2024
17bf93a
Changes to CrudShelter.feature file
SebastianJitaru29 Mar 6, 2024
6ded836
Changes to CrudShelter.feature file2
SebastianJitaru29 Mar 7, 2024
932dbca
Solve merge conflicts
SebastianJitaru29 Mar 7, 2024
76d32a9
Merge branch 'main' into Feature-Create-Shelter
SebastianJitaru29 Mar 13, 2024
361dad6
Merge branch 'main' into Feature-Create-Shelter
SebastianJitaru29 Mar 16, 2024
f3ca085
Basic tests corrected
SebastianJitaru29 Mar 16, 2024
620c742
Added new scenarios
SebastianJitaru29 Mar 16, 2024
d28e5a3
Removed empty funcitons in step def
SebastianJitaru29 Mar 16, 2024
fad2ff3
Created Shelter event handler and added new scenarios
SebastianJitaru29 Mar 20, 2024
d25590d
Solved pull request erros
SebastianJitaru29 Mar 20, 2024
b94a6f8
admin registered posted to mvc
SebastianJitaru29 Mar 21, 2024
051ccbc
Solved ShelterHandler values inicialization
SebastianJitaru29 Mar 26, 2024
c138612
WebSecurityConfig changed so only admin can post to /shelters/**
SebastianJitaru29 Mar 26, 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 @@ -30,11 +30,13 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce
.requestMatchers(HttpMethod.GET, "/identity").authenticated()
.requestMatchers(HttpMethod.POST, "/users").anonymous()
.requestMatchers(HttpMethod.POST, "/users/*").denyAll()
.requestMatchers(HttpMethod.POST, "/**/*").authenticated()
.requestMatchers(HttpMethod.PUT, "/**/*").authenticated()
.requestMatchers(HttpMethod.PATCH, "/**/*").authenticated()
.requestMatchers(HttpMethod.DELETE, "/**/*").authenticated()
.anyRequest().permitAll())
.requestMatchers("/shelters/**").hasAuthority("ROLE_ADMIN")
.requestMatchers(HttpMethod.POST, "/**/*").authenticated()

.anyRequest().permitAll())
.csrf((csrf) -> csrf.disable())
.sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.cors((cors) -> cors.configurationSource(corsConfigurationSource()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cat.udl.eps.softarch.demo.handler;

import cat.udl.eps.softarch.demo.domain.Shelter;
import cat.udl.eps.softarch.demo.repository.ShelterRepository;
import org.springframework.data.rest.core.annotation.HandleAfterSave;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.stereotype.Component;

import java.time.ZonedDateTime;

@Component
@RepositoryEventHandler
public class ShelterEventHandler {
final ShelterRepository shelterRepository;

public ShelterEventHandler(ShelterRepository shelterRepository) {
this.shelterRepository = shelterRepository;
}

@HandleBeforeCreate
public void handleShelterPreCreate(Shelter shelter) {
shelter.setCreatedAt(ZonedDateTime.now());
shelter.setUpdatedAt(ZonedDateTime.now());

}
@HandleAfterSave
public void handleShelterPostSave(Shelter shelter) {
shelter.setUpdatedAt(ZonedDateTime.now());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package cat.udl.eps.softarch.demo.steps;
import static org.hamcrest.Matchers.is;
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 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.*;
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 jakarta.validation.constraints.AssertTrue;
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.time.ZonedDateTime;

public class CreateShelterStepDefs {
@Autowired

private StepDefs stepDefs;
@Autowired
private ShelterRepository shelterRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private ShelterVolunteerRepository ShelterVolunteerRepository;

@Autowired
private AdminRepository adminRepository;

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

}
}

@When("^I create a shelter with a name \"([^\"]*)\", email \"([^\"]*)\" and phone \"([^\"]*)\" and location \"([^\"]*)\"$")
public void iCreateAShelterWithAName(String name, String email, String mobile, String location) throws Throwable {;
Shelter newshelter = new Shelter();
newshelter.setName(name);
newshelter.setEmail(email);
newshelter.setMobile(mobile);
newshelter.setCreatedAt(ZonedDateTime.now());
newshelter.setUpdatedAt(ZonedDateTime.now());
//Following code is doing a post request to /shelters storing the response in stepDefs.result to test REST functionalty
stepDefs.result = stepDefs.mockMvc.perform(
post("/shelters")
.contentType(MediaType.APPLICATION_JSON)
.content(stepDefs.mapper.writeValueAsString(newshelter))
.characterEncoding(StandardCharsets.UTF_8)
.with(AuthenticationStepDefs.authenticate()))
.andDo(print());

}

@And("^There is (\\d+) Shelter created$")
public void thereIsShelterCreated(int sheltersCreatedNum) {
Assert.assertEquals("Shelters created", sheltersCreatedNum, shelterRepository.count());
}

@Given("^There is no shelter registered with the name \"([^\"]*)\"$")
public void thereIsNoShelterRegisteredWithTheName(String name) {
Assert.assertTrue("Shelter with name \""
+ name + "\" shouldn't exist", shelterRepository.findByName(name).isEmpty());
}

@Given("^There is a registered volunteer with name \"([^\"]*)\" and password \"([^\"]*)\" and email \"([^\"]*)\"$")
public void thereIsARegisteredVolunteerWithNameAndPasswordAndEmail(String name, String password, String email) {
if(!ShelterVolunteerRepository.existsById(name)) {
ShelterVolunteer volunteer = new ShelterVolunteer();
volunteer.setEmail(email);
volunteer.setId(name);
volunteer.setPassword(password);
volunteer.encodePassword();
ShelterVolunteerRepository.save(volunteer);
}
}
}
62 changes: 62 additions & 0 deletions src/test/resources/features/CreateShelter.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Feature: Create Shelter
In order to provide temporary housing for animals
As a Admin
I want to create shelters

Background:
Given There is a registered user with username "user" and password "password" and email "[email protected]"
Given There is a registered admin with name "admin" and password "password" and email "[email protected]"
Given There is a registered volunteer with name "volunteer" and password "password" and email "[email protected]"


Scenario: Create a shelter without being logged in
Given I'm not logged in
When I create a shelter with a name "name", email "[email protected]" and phone "123123123" and location "location"
Then The response code is 401
And The error message is "Unauthorized"
And There is 0 Shelter created

Scenario: Create shelter as Admin
Given I login as "admin" with password "password"
When I create a shelter with a name "name", email "[email protected]" and phone "123123123" and location "location"
Then The response code is 201

Scenario: Create shelter as Volunteer
Given I login as "volunteer" with password "password"
When I create a shelter with a name "name", email "[email protected]" and phone "123123123" and location "location"
Then The response code is 403
And The error message is "Forbidden"
And There is 0 Shelter created

Scenario: Create shelter as Client
Given I login as "client" with password "password"
When I create a shelter with a name "name", email "[email protected]" and phone "123123123" and location "location"
Then The response code is 401
And The error message is "Unauthorized"
And There is 0 Shelter created

Scenario: Create shelter with missing name
Given I login as "admin" with password "password"
When I create a shelter with a name "", email "[email protected]" and phone "123123123" and location "location"
Then The response code is 400
And There is 0 Shelter created

Scenario: Create shelter with missing email
Given I login as "admin" with password "password"
When I create a shelter with a name "name", email "" and phone "123123123" and location "location"
Then The response code is 400
And There is 0 Shelter created

Scenario: Create shelter with missing mobile
Given I login as "admin" with password "password"
When I create a shelter with a name "name", email "[email protected]" and phone "" and location "location"
Then The response code is 400
And There is 0 Shelter created

Scenario: Create a shelter that already exists:
Given I login as "admin" with password "password"
When I create a shelter with a name "name", email "[email protected]" and phone "123123123" and location "location"
Then The response code is 201
When I create a shelter with a name "name", email "[email protected]" and phone "123123123" and location "location"
Then The response code is 409
And There is 1 Shelter created
Loading