Skip to content

Commit

Permalink
Changed UserDTO to AuthenticatedUser and renamed package
Browse files Browse the repository at this point in the history
  • Loading branch information
vburmus committed Nov 27, 2023
1 parent fc04264 commit 57b4fdf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.epam.esm.filter;


import com.epam.esm.model.UserDTO;
import com.epam.esm.model.AuthenticatedUser;
import com.epam.esm.utils.openfeign.AuthFeignClient;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
Expand Down Expand Up @@ -40,7 +40,7 @@ protected void doFilterInternal(@NonNull HttpServletRequest request,
return;
}
try {
UserDTO user = authClient.getUserFromJwt(bearerToken).getBody();
AuthenticatedUser user = authClient.getAuthenticatedUserFromJwt(bearerToken).getBody();
UsernamePasswordAuthenticationToken authToken =
new UsernamePasswordAuthenticationToken(user, null,
Collections.singleton(new SimpleGrantedAuthority(user.getRole().name())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class UserDTO {
public class AuthenticatedUser {
private Long id;
private String name;
private String surname;
private String phone;
private String email;
private Provider provider;
private Role role;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.epam.esm.utils.openfeign;

import com.epam.esm.model.UserDTO;
import com.epam.esm.model.AuthenticatedUser;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -9,5 +9,5 @@
@FeignClient(name = "auth-service", configuration = CustomFeignClientConfiguration.class)
public interface AuthFeignClient {
@GetMapping("api/v1/auth/user")
ResponseEntity<UserDTO> getUserFromJwt(@RequestHeader(value = "Authorization") String authorizationHeader);
ResponseEntity<AuthenticatedUser> getAuthenticatedUserFromJwt(@RequestHeader(value = "Authorization") String authorizationHeader);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.epam.esm.filter;

import com.epam.esm.model.Role;
import com.epam.esm.model.UserDTO;
import com.epam.esm.model.AuthenticatedUser;
import com.epam.esm.utils.openfeign.AuthFeignClient;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
Expand Down Expand Up @@ -57,17 +57,17 @@ void testFilterInvalidBearerToken() throws ServletException, IOException {

@Test
void testFilterValidBearerTokenSuccessfulAuthentication() throws ServletException, IOException {
String validToken = "Bearer BearerValidToken"; // Replace with a valid token format
String validToken = "Bearer BearerValidToken";
when(request.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn(validToken);

UserDTO userDTO = new UserDTO();
AuthenticatedUser userDTO = new AuthenticatedUser();
userDTO.setRole(Role.USER);
when(authClient.getUserFromJwt(validToken)).thenReturn(ResponseEntity.ok(userDTO));
when(authClient.getAuthenticatedUserFromJwt(validToken)).thenReturn(ResponseEntity.ok(userDTO));

authenticationFilter.doFilterInternal(request, response, filterChain);

assertEquals(userDTO, SecurityContextHolder.getContext().getAuthentication().getPrincipal());
UserDTO authUser = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
AuthenticatedUser authUser = (AuthenticatedUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
assertEquals(Role.USER, authUser.getRole());
verify(filterChain).doFilter(request, response);
}
Expand All @@ -77,7 +77,7 @@ void testFilterAuthenticationErrorHttpClientErrorException() throws ServletExcep
String validToken = "Bearer BearerValidToken";
when(request.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn(validToken);

when(authClient.getUserFromJwt(validToken))
when(authClient.getAuthenticatedUserFromJwt(validToken))
.thenThrow(new HttpClientErrorException(HttpStatus.UNAUTHORIZED));

PrintWriter writer = mock(PrintWriter.class);
Expand All @@ -92,10 +92,10 @@ void testFilterAuthenticationErrorHttpClientErrorException() throws ServletExcep

@Test
void testFilterAuthenticationErrorHttpServerErrorException() throws ServletException, IOException {
String validToken = "Bearer BearerValidToken"; // Replace with a valid token format
String validToken = "Bearer BearerValidToken";
when(request.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn(validToken);

when(authClient.getUserFromJwt(validToken))
when(authClient.getAuthenticatedUserFromJwt(validToken))
.thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR));

PrintWriter writer = mock(PrintWriter.class);
Expand Down

0 comments on commit 57b4fdf

Please sign in to comment.