Skip to content

Commit

Permalink
Test: fix to save jobgroup authentication fail
Browse files Browse the repository at this point in the history
  • Loading branch information
emost22 committed Jul 14, 2024
1 parent 1cdc87d commit 5329063
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/test/java/com/sirius/spurt/BaseMvcTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sirius.spurt;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;

import capital.scalable.restdocs.AutoDocumentation;
Expand All @@ -9,6 +11,7 @@
import com.sirius.spurt.common.auth.PrincipalDetails;
import com.sirius.spurt.common.jwt.JwtUtils;
import com.sirius.spurt.common.jwt.MockSpringSecurityFilter;
import com.sirius.spurt.common.oauth.user.OAuthUser;
import com.sirius.spurt.custom.CustomRequestFieldSnippet;
import com.sirius.spurt.custom.CustomResponseFieldSnippet;
import com.sirius.spurt.test.UserTest;
Expand Down Expand Up @@ -43,6 +46,8 @@ public class BaseMvcTest implements UserTest {

@BeforeEach
public void setUp(RestDocumentationContextProvider restDocumentation) throws Exception {
OAuthUser oAuthUser = OAuthUser.builder().userId(TEST_USER_ID).email(TEST_EMAIL).build();
when(jwtUtils.getOAuthUser(anyString())).thenReturn(oAuthUser);
setUpMockUser();
var mockMvcRequestBuilders =
MockMvcRequestBuilders.get("https://api.spurtapp.com")
Expand Down
10 changes: 7 additions & 3 deletions src/test/java/com/sirius/spurt/JobGroupControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sirius.spurt;

import static com.sirius.spurt.common.jwt.JwtUtils.ACCESS_TOKEN_NAME;
import static com.sirius.spurt.common.jwt.JwtUtils.TOKEN_TYPE;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand All @@ -11,13 +13,15 @@
import com.sirius.spurt.service.business.jobgroup.SaveJobGroupBusiness;
import com.sirius.spurt.service.business.jobgroup.UpdateJobGroupBusiness;
import com.sirius.spurt.service.controller.jobgroup.JobGroupController;
import com.sirius.spurt.test.TokenTest;
import jakarta.servlet.http.Cookie;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;

@WebMvcTest(controllers = {JobGroupController.class})
public class JobGroupControllerTest extends BaseMvcTest {
public class JobGroupControllerTest extends BaseMvcTest implements TokenTest {
@MockBean private SaveJobGroupBusiness saveJobGroupBusiness;
@MockBean private UpdateJobGroupBusiness updateJobGroupBusiness;

Expand All @@ -29,10 +33,10 @@ public class JobGroupControllerTest extends BaseMvcTest {
this.mockMvc
.perform(
post("/v1/jobgroup")
.cookie(new Cookie(ACCESS_TOKEN_NAME, TOKEN_TYPE + TEST_TOKEN_VALUE))
.requestAttr("userId", "admin")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(dto))
.principal(this.mockPrincipal))
.content(objectMapper.writeValueAsString(dto)))
.andExpect(status().isOk())
.andDo(print());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.sirius.spurt.store.provider.auth.impl;

import com.sirius.spurt.store.repository.redis.auth.AuthRepository;
import com.sirius.spurt.test.RefreshTokenTest;
import com.sirius.spurt.test.TokenTest;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class AuthProviderImplTest implements RefreshTokenTest {
class AuthProviderImplTest implements TokenTest {
@InjectMocks private AuthProviderImpl authProvider;

@Mock private AuthRepository authRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.sirius.spurt.test.RefreshTokenTest;
import com.sirius.spurt.test.TokenTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -19,7 +19,7 @@
import org.springframework.data.redis.core.ValueOperations;

@ExtendWith(MockitoExtension.class)
class AuthRepositoryImplTest implements RefreshTokenTest {
class AuthRepositoryImplTest implements TokenTest {
@InjectMocks private AuthRepositoryImpl tokenRepository;

@Mock private RedisTemplate<String, String> redisTemplate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sirius.spurt.test;

public interface RefreshTokenTest {
public interface TokenTest {
String TEST_TOKEN_KEY = "jwt:key";
String TEST_TOKEN_VALUE = "value";
long TEST_TOKEN_EXPIRE_TIME = 1000L;
Expand Down

0 comments on commit 5329063

Please sign in to comment.