Skip to content

Commit

Permalink
feat: #185 material services tests finished
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbator committed Dec 10, 2024
1 parent 24f36f0 commit bec20f7
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Page<MaterialPageable> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Long>()
))
materialStore.changeStatus(1L, Material.MaterialStatus.APPROVED)
>> Try.success(new Tuple2<>(1L, Material.MaterialStatus.APPROVED))
principalUtil.getPrincipal() >> Optional.of(new UserPrincipal(
1L, "name", new ArrayList<SimpleGrantedAuthority>(
List.of(new SimpleGrantedAuthority("CAN_APPROVED_MATERIAL"))
)
))

when:
def result = materialStatusService.update(1L, Material.MaterialStatus.APPROVED)

then:
Assert.that(result == Material.MaterialStatus.APPROVED)
}
}
Original file line number Diff line number Diff line change
@@ -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<Long>()
))
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)
}
}

0 comments on commit bec20f7

Please sign in to comment.