Skip to content

Commit

Permalink
Task 52 : Revise AbstractRestControllerTest in product service
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapter1990 committed Jul 19, 2024
1 parent e5a0575 commit cee6ca9
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.springbootmicroservices.productservice.builder.UserEntityBuilder;
import com.springbootmicroservices.productservice.client.UserServiceClient;
import com.springbootmicroservices.productservice.config.TokenConfigurationParameter;
import com.springbootmicroservices.productservice.model.auth.Token;
import com.springbootmicroservices.productservice.model.auth.enums.TokenClaims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.shaded.org.apache.commons.lang3.time.DateUtils;
Expand All @@ -37,6 +42,9 @@ public class AbstractRestControllerTest extends AbstractTestContainerConfigurati
@Mock
private TokenConfigurationParameter tokenConfiguration;

@MockBean
private UserServiceClient userServiceClient;


@BeforeEach
public void initializeAuth() {
Expand All @@ -45,6 +53,15 @@ public void initializeAuth() {
this.mockAdminToken = this.generate(new UserEntityBuilder().withValidAdminFields().build().getClaims());
this.mockUserToken = this.generate(new UserEntityBuilder().withValidUserFields().build().getClaims());

Mockito.doNothing().when(userServiceClient).validateToken(mockAdminToken.getAccessToken());
Mockito.doNothing().when(userServiceClient).validateToken(mockAdminToken.getAccessToken());

Mockito.when(userServiceClient.getAuthentication(mockAdminToken.getAccessToken()))
.thenReturn(new UsernamePasswordAuthenticationToken("admin", null, AuthorityUtils.createAuthorityList("ADMIN")));

Mockito.when(userServiceClient.getAuthentication(mockUserToken.getAccessToken()))
.thenReturn(new UsernamePasswordAuthenticationToken("user", null, AuthorityUtils.createAuthorityList("USER")));

}

private Token generate(Map<String, Object> claims) {
Expand Down

0 comments on commit cee6ca9

Please sign in to comment.