diff --git a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialCreateService.java b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialCreateService.java index 03d665f..8369ea4 100644 --- a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialCreateService.java +++ b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialCreateService.java @@ -25,7 +25,6 @@ import pl.sknikod.kodemybackend.infrastructure.store.TagStore; import pl.sknikod.kodemybackend.infrastructure.store.TypeStore; import pl.sknikod.kodemycommons.exception.content.ExceptionUtil; -import pl.sknikod.kodemycommons.security.AuthFacade; import pl.sknikod.kodemycommons.util.PrincipalUtil; import java.util.List; diff --git a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialGetByIdService.java b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialGetByIdService.java index 8e580d9..10a3dd6 100644 --- a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialGetByIdService.java +++ b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialGetByIdService.java @@ -4,7 +4,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import pl.sknikod.kodemybackend.infrastructure.module.material.model.SingleMaterialResponse; -import pl.sknikod.kodemybackend.infrastructure.store.GradeStore; import pl.sknikod.kodemybackend.infrastructure.store.MaterialStore; import pl.sknikod.kodemycommons.exception.content.ExceptionUtil; diff --git a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialManageService.java b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialManageService.java index 66e212c..1b2dafb 100644 --- a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialManageService.java +++ b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialManageService.java @@ -26,7 +26,7 @@ public Page manage( PageRequest pageRequest ) { return materialStore.findAll(createFindAllFilters(materialFilterSearchParams, pageRequest)) - .map(tuple -> tuple.map2(users -> + .map(tuple -> tuple.map2(users -> users.stream().collect(Collectors.toMap(UserStore.User::getId, UserStore.User::getUsername))) ) .mapTry(tuple -> tuple._1.map(object -> MaterialPageable.map( diff --git a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialStatusService.java b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialStatusService.java index 90b0a49..da708f2 100644 --- a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialStatusService.java +++ b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialStatusService.java @@ -8,8 +8,8 @@ import pl.sknikod.kodemybackend.infrastructure.store.MaterialStore; import pl.sknikod.kodemycommons.exception.Validation400Exception; import pl.sknikod.kodemycommons.exception.content.ExceptionUtil; -import pl.sknikod.kodemycommons.security.AuthFacade; import pl.sknikod.kodemycommons.security.UserPrincipal; +import pl.sknikod.kodemycommons.util.PrincipalUtil; import static pl.sknikod.kodemybackend.infrastructure.module.material.model.MaterialStatusUtil.getAuthorityForStatusChange; import static pl.sknikod.kodemybackend.infrastructure.module.material.model.MaterialStatusUtil.getPossibleStatuses; @@ -18,6 +18,7 @@ @AllArgsConstructor public class MaterialStatusService { private final MaterialStore materialStore; + private final PrincipalUtil principalUtil; public Material.MaterialStatus update(Long materialId, Material.MaterialStatus newStatus) { return materialStore.findById(materialId, true) @@ -42,7 +43,7 @@ public StatusesToChangeResponse showStatusesToChange(Long materialId) { } private boolean canUserUpdateStatus(SimpleGrantedAuthority neededAuthority, Material material) { - UserPrincipal userPrincipal = AuthFacade.getCurrentUserPrincipal().get(); + UserPrincipal userPrincipal = principalUtil.getPrincipal().get(); return userPrincipal.getAuthorities().contains(neededAuthority) || isOwnerStatusUpdatePossible(neededAuthority, material, userPrincipal.getId()); } diff --git a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialUpdateService.java b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialUpdateService.java index 7948ec8..850d4e7 100644 --- a/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialUpdateService.java +++ b/kodemy-backend/src/main/java/pl/sknikod/kodemybackend/infrastructure/module/material/MaterialUpdateService.java @@ -20,7 +20,7 @@ import pl.sknikod.kodemybackend.infrastructure.store.TagStore; import pl.sknikod.kodemybackend.infrastructure.store.TypeStore; import pl.sknikod.kodemycommons.exception.content.ExceptionUtil; -import pl.sknikod.kodemycommons.security.AuthFacade; +import pl.sknikod.kodemycommons.util.PrincipalUtil; import java.util.List; import java.util.Set; @@ -38,6 +38,7 @@ public class MaterialUpdateService { private final MaterialStore materialStore; private static final SimpleGrantedAuthority CAN_AUTO_APPROVED_MATERIAL = new SimpleGrantedAuthority("CAN_AUTO_APPROVED_MATERIAL"); + private final PrincipalUtil principalUtil; public MaterialUpdateResponse update(Long materialId, MaterialUpdateRequest body) { var material = materialStore.findById(materialId, true) @@ -70,7 +71,7 @@ private Material updateEntity( material.setCategory(category); material.setType(type); material.setTags(tags); - material.setStatus(validateStatus(AuthFacade.getCurrentUserPrincipal().get().getAuthorities(), material.getStatus())); + material.setStatus(validateStatus(principalUtil.getPrincipal().get().getAuthorities(), material.getStatus())); return material; } diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialControllerSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialControllerSpec.groovy index d78dca6..cfc12e1 100644 --- a/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialControllerSpec.groovy +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialControllerSpec.groovy @@ -1,6 +1,5 @@ package unit.infrastructure.module.material - import org.spockframework.util.Assert import org.springframework.data.domain.PageImpl import org.springframework.data.domain.PageRequest diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialStatusServiceSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialStatusServiceSpec.groovy new file mode 100644 index 0000000..4d14e33 --- /dev/null +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialStatusServiceSpec.groovy @@ -0,0 +1,44 @@ +package unit.infrastructure.module.material + +import factory.MaterialFactory +import io.vavr.Tuple2 +import io.vavr.control.Try +import org.spockframework.util.Assert +import org.springframework.security.core.authority.SimpleGrantedAuthority +import pl.sknikod.kodemybackend.infrastructure.database.Material +import pl.sknikod.kodemybackend.infrastructure.module.material.MaterialStatusService +import pl.sknikod.kodemybackend.infrastructure.store.MaterialStore +import pl.sknikod.kodemycommons.security.UserPrincipal +import pl.sknikod.kodemycommons.util.PrincipalUtil +import spock.lang.Specification + +class MaterialStatusServiceSpec extends Specification { + def materialStore = Mock(MaterialStore) + def principalUtil = Mock(PrincipalUtil) + + def material = MaterialFactory.create() + + def materialStatusService = new MaterialStatusService(materialStore, principalUtil) + + def "shouldUpdate"() { + given: + material.status = Material.MaterialStatus.PENDING + materialStore.findById(1L, true) + >> Try.success(new MaterialStore.FindByIdObject( + material, "user", 3.0D, new ArrayList() + )) + materialStore.changeStatus(1L, Material.MaterialStatus.APPROVED) + >> Try.success(new Tuple2<>(1L, Material.MaterialStatus.APPROVED)) + principalUtil.getPrincipal() >> Optional.of(new UserPrincipal( + 1L, "name", new ArrayList( + List.of(new SimpleGrantedAuthority("CAN_APPROVED_MATERIAL")) + ) + )) + + when: + def result = materialStatusService.update(1L, Material.MaterialStatus.APPROVED) + + then: + Assert.that(result == Material.MaterialStatus.APPROVED) + } +} diff --git a/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialUpdateServiceSpec.groovy b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialUpdateServiceSpec.groovy new file mode 100644 index 0000000..cf2e9ea --- /dev/null +++ b/kodemy-backend/src/test/groovy/unit/infrastructure/module/material/MaterialUpdateServiceSpec.groovy @@ -0,0 +1,75 @@ +package unit.infrastructure.module.material + +import factory.MaterialFactory +import io.vavr.control.Try +import org.spockframework.util.Assert +import org.springframework.security.core.authority.SimpleGrantedAuthority +import pl.sknikod.kodemybackend.infrastructure.database.Material +import pl.sknikod.kodemybackend.infrastructure.module.material.MaterialUpdateService +import pl.sknikod.kodemybackend.infrastructure.store.CategoryStore +import pl.sknikod.kodemybackend.infrastructure.store.MaterialStore +import pl.sknikod.kodemybackend.infrastructure.store.TagStore +import pl.sknikod.kodemybackend.infrastructure.store.TypeStore +import pl.sknikod.kodemycommons.security.UserPrincipal +import pl.sknikod.kodemycommons.util.PrincipalUtil +import spock.lang.Specification + +class MaterialUpdateServiceSpec extends Specification { + def updateMaterialMapper = Mock(MaterialUpdateService.MaterialUpdateMapper) + def categoryStore = Mock(CategoryStore) + def typeStore = Mock(TypeStore) + def tagStore = Mock(TagStore) + def materialStore = Mock(MaterialStore) + def principalUtil = Mock(PrincipalUtil) + + def material = MaterialFactory.create() + + def materialUpdateService = new MaterialUpdateService( + updateMaterialMapper, + categoryStore, + typeStore, + tagStore, + materialStore, + principalUtil + ) + + def "shouldUpdate"() { + given: + materialStore.findById(1L, true) + >> Try.success(new MaterialStore.FindByIdObject( + material, "user", 3.0D, new ArrayList() + )) + categoryStore.findById(1L) >> Try.success(material.category) + typeStore.findById(1L) >> Try.success(material.type) + tagStore.findAllByIdIn(List.of(1L)) >> Try.success(material.tags) + materialStore.update(_ as Material) >> Try.success(material) + def response = new MaterialUpdateService.MaterialUpdateResponse( + material.id, material.title, + material.description, material.link, + new MaterialUpdateService.MaterialUpdateResponse.BaseDetails( + material.category.id, material.category.name + ), + new MaterialUpdateService.MaterialUpdateResponse.BaseDetails( + material.type.id, material.type.name + ), + Set.of(new MaterialUpdateService.MaterialUpdateResponse.BaseDetails( + 1L, "name" + ))) + updateMaterialMapper.map(_ as Material) + >> response + principalUtil.getPrincipal() >> Optional.of(new UserPrincipal( + 1L, "name", List.of(new SimpleGrantedAuthority( + "CAN_AUTO_APPROVED_MATERIAL" + ))) + ) + + when: + def result = materialUpdateService.update(1L, + new MaterialUpdateService.MaterialUpdateRequest( + "title", "desc", "link", 1L, 1L, List.of(1L) + )) + + then: + Assert.that(result == response) + } +}