-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from UdL-EPS-SoftArch/77-fix-update-room-test
77 fix update room test
- Loading branch information
Showing
10 changed files
with
209 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 77 additions & 40 deletions
117
src/main/java/cat/udl/eps/softarch/demo/handler/RoomEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,77 @@ | ||
package cat.udl.eps.softarch.demo.handler; | ||
|
||
|
||
import cat.udl.eps.softarch.demo.domain.Apartment; | ||
import cat.udl.eps.softarch.demo.domain.Owner; | ||
import cat.udl.eps.softarch.demo.domain.Room; | ||
import cat.udl.eps.softarch.demo.repository.ApartmentRepository; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.data.rest.core.annotation.HandleBeforeCreate; | ||
import org.springframework.data.rest.core.annotation.RepositoryEventHandler; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
@RepositoryEventHandler | ||
public class RoomEventHandler { | ||
|
||
final Logger logger = LoggerFactory.getLogger(Room.class); | ||
final ApartmentRepository apartmentRepository; | ||
|
||
public RoomEventHandler(ApartmentRepository apartmentRepository) { | ||
this.apartmentRepository = apartmentRepository; | ||
} | ||
|
||
@HandleBeforeCreate | ||
public void handleBeforeCreate(Room room) { | ||
Owner owner = (Owner) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
room.setOwner(owner); | ||
|
||
List<Apartment> apartmentList = apartmentRepository.findByOwner(owner); | ||
if (!apartmentList.isEmpty()) { | ||
room.setApart(apartmentList.get(0)); | ||
} | ||
logger.info("New room created: {}", room); | ||
} | ||
} | ||
|
||
package cat.udl.eps.softarch.demo.handler; | ||
|
||
|
||
import cat.udl.eps.softarch.demo.domain.Apartment; | ||
import cat.udl.eps.softarch.demo.domain.Owner; | ||
import cat.udl.eps.softarch.demo.domain.Room; | ||
import cat.udl.eps.softarch.demo.repository.ApartmentRepository; | ||
import cat.udl.eps.softarch.demo.repository.RoomRepository; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.data.rest.core.annotation.HandleBeforeCreate; | ||
import org.springframework.data.rest.core.annotation.HandleBeforeDelete; | ||
import org.springframework.data.rest.core.annotation.HandleBeforeSave; | ||
import org.springframework.data.rest.core.annotation.RepositoryEventHandler; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
|
||
@Component | ||
@RepositoryEventHandler | ||
public class RoomEventHandler { | ||
|
||
final Logger logger = LoggerFactory.getLogger(Room.class); | ||
final ApartmentRepository apartmentRepository; | ||
|
||
public RoomEventHandler(ApartmentRepository apartmentRepository, RoomRepository roomRepository) { | ||
this.apartmentRepository = apartmentRepository; | ||
} | ||
|
||
@HandleBeforeSave | ||
public void handleBeforeSave(Room room) { | ||
Owner owner = (Owner) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
Apartment apart = room.getApart(); | ||
Owner roomOwner = apart.getOwner(); | ||
assert roomOwner.getId() != null; | ||
if(!roomOwner.getId().equals(owner.getId())){ | ||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Unauthorized owner."); } | ||
logger.info("New room updated: {}", room); | ||
} | ||
|
||
@HandleBeforeCreate | ||
public void handleBeforeCreate(Room room) { | ||
Owner owner; | ||
try{ | ||
owner = (Owner) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
}catch (Exception e){ | ||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Not owner type."); | ||
} | ||
Apartment apart = room.getApart(); | ||
Owner roomOwner = apart.getOwner(); | ||
assert roomOwner.getId() != null; | ||
if(!roomOwner.getId().equals(owner.getId())){ | ||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Unauthorized owner."); } | ||
logger.info("New room updated: {}", room); | ||
} | ||
|
||
@HandleBeforeDelete | ||
public void handleBeforeDelate(Room room) { | ||
Owner owner; | ||
try{ | ||
owner = (Owner) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
}catch (Exception e){ | ||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Not owner type."); | ||
} | ||
Apartment apart = room.getApart(); | ||
Owner roomOwner = apart.getOwner(); | ||
assert roomOwner.getId() != null; | ||
if(!roomOwner.getId().equals(owner.getId())){ | ||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Unauthorized owner."); } | ||
logger.info("New room updated: {}", room); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,31 @@ Feature: Create Room | |
I must be able to create Room | ||
|
||
Background: | ||
Given There is a registered user with username "demo" and password "password" and email "[email protected]" | ||
Given There is a registered owner with username "owner" and password "password" and email "[email protected]" | ||
Given There is a registered owner with username "owner1" and password "password" and email "[email protected]" | ||
Given There is a apartment with the name "test", floor "2", address "123 Wall Street", postal code "10005", city "New York", country "United States of America", description "..." and a creation date "2024-10-16T10:30:00-04:00" by owner username "owner" | ||
Given There is a apartment with the name "testApartment", floor "0", address "roomAddressX", postal code "postalCodeX", city "cityX", country "countryX", description "roomDescriptionX" and a creation date "2024-10-18T17:44:30.551316+00:00" by owner username "owner1" | ||
|
||
|
||
Scenario: Create a new Room while logged in | ||
Given I login as "owner1" with password "password" | ||
When I create a Room with the surface "30", occupied "false", window "false", desk "false" and bed "false", by owner username "owner1" and the apartment_name "test" | ||
When I create a Room with the surface "30", occupied "false", window "false", desk "false" and bed "false" and the apartment_name "testApartment" | ||
Then The response code is 201 | ||
|
||
Scenario: Create a Room without being logged in | ||
Given I'm not logged in | ||
When I create a Room with the surface "30", occupied "false", window "false", desk "false" and bed "false", by owner username "owner1" and the apartment_name "test" | ||
Then The response code is 401 | ||
When I create a Room with the surface "30", occupied "false", window "false", desk "false" and bed "false" and the apartment_name "testApartment" | ||
Then The response code is 401 | ||
|
||
Scenario: Create Room as a non-owner | ||
Given I login as "demo" with password "password" | ||
When I create a Room with the surface "30", occupied "false", window "false", desk "false" and bed "false" and the apartment_name "testApartment" | ||
Then The response code is 403 | ||
And The error message is "Not owner type." | ||
|
||
|
||
Scenario: Create Room as a other owner | ||
Given I login as "owner" with password "password" | ||
When I create a Room with the surface "30", occupied "false", window "false", desk "false" and bed "false" and the apartment_name "testApartment" | ||
Then The response code is 403 | ||
And The error message is "Unauthorized owner." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.