This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-24 22:27:00 +02:00
parent ff51edc112
commit 4253e65241
2 changed files with 17 additions and 16 deletions

View file

@ -18,9 +18,15 @@ import org.springframework.web.filter.OncePerRequestFilter;
@Component @Component
public class JWTRequestFilter extends OncePerRequestFilter { public class JWTRequestFilter extends OncePerRequestFilter {
@Autowired private JWTUserDetailsService jwtUserDetailsService; private final JWTUserDetailsService jwtUserDetailsService;
private final JWTTokenUtils jwtTokenUtils;
@Autowired private JWTTokenUtils jwtTokenUtils; @Autowired
public JWTRequestFilter(
JWTUserDetailsService jwtUserDetailsService, JWTTokenUtils jwtTokenUtils) {
this.jwtUserDetailsService = jwtUserDetailsService;
this.jwtTokenUtils = jwtTokenUtils;
}
@Override @Override
protected void doFilterInternal( protected void doFilterInternal(

View file

@ -9,16 +9,10 @@ import static org.mockito.Mockito.when;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils;
import java.security.Principal; import java.security.Principal;
import java.util.Optional;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
@ -109,12 +103,13 @@ public class RegularLightControllerTests {
when(deviceService.saveAsGuest(any(RegularLight.class), eq("user"), anyLong())) when(deviceService.saveAsGuest(any(RegularLight.class), eq("user"), anyLong()))
.thenAnswer(i -> i.getArguments()[0]); .thenAnswer(i -> i.getArguments()[0]);
when(Utils.returnIfGuest( User guest = new User();
any(UserRepository.class), User host = new User();
any(RegularLight.class), host.getGuests().add(guest);
anyLong(), guest.getHosts().add(host);
any(Principal.class)))
.thenAnswer(i -> i.getArguments()[1]); when(userRepository.findById(20L)).thenReturn(Optional.of(host));
when(userRepository.findByUsername("user")).thenReturn(guest);
MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));