Skip to content

Commit

Permalink
test(jacoco): user rest controller test case add
Browse files Browse the repository at this point in the history
  • Loading branch information
gmoon92 committed Jun 10, 2024
1 parent 35b4112 commit 05f9d0b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.gmoon.jacoco.global;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.core.annotation.AliasFor;
import org.springframework.security.test.context.support.WithSecurityContext;

import com.gmoon.jacoco.users.domain.Role;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@WithSecurityContext(factory = CustomWithMockUserSecurityContextFactory.class)
public @interface CustomWithMockUser {

@AliasFor(attribute = "userId")
String value() default "";

String userId() default "";

Role role() default Role.USER;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.gmoon.jacoco.global;

import java.util.List;

import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.support.WithSecurityContextFactory;

import com.gmoon.jacoco.users.domain.Role;
import com.gmoon.jacoco.users.domain.User;

public class CustomWithMockUserSecurityContextFactory implements WithSecurityContextFactory<CustomWithMockUser> {

@Override
public SecurityContext createSecurityContext(CustomWithMockUser annotation) {
Authentication authentication = createAuthentication(annotation);

SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authentication);
return context;
}

private Authentication createAuthentication(CustomWithMockUser annotation) {
String username = annotation.userId();
Role role = annotation.role();

User principal = User.builder()
.id(username)
.username(username)
.role(role)
.build();

Authentication authentication = new TestingAuthenticationToken(principal, null, List.of(role));
authentication.setAuthenticated(true);
return authentication;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.gmoon.jacoco.users.api;

import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -12,6 +15,9 @@
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.gmoon.jacoco.global.CustomWithMockUser;
import com.gmoon.jacoco.users.domain.Role;

@SpringBootTest
class UserRestControllerTest {

Expand All @@ -20,17 +26,34 @@ class UserRestControllerTest {
@BeforeEach
void setUp(@Autowired WebApplicationContext context) {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(springSecurity())
.build();
}

@Test
@WithMockUser
void updatePassword() throws Exception {
ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/users/password")
.param("password", "password")
.param("newPassword", "newPassord")
);
@DisplayName("/users/password")
@Nested
class UpdatePasswordTest {

@CustomWithMockUser(value = "admin", role = Role.ADMIN)
@Test
void ok() throws Exception {
ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/users/password")
.param("password", "password")
.param("newPassword", "newPassword")
);

result.andExpect(status().isNoContent());
}

@CustomWithMockUser(value = "user", role = Role.USER)
@Test
void deny() throws Exception {
ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/users/password")
.param("password", "password")
.param("newPassword", "newPassword")
);

result.andExpect(status().isNoContent());
result.andExpect(status().isForbidden());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.gmoon.jacoco.users.application;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class UserServiceTest {

@Test
void updatePassword() {
UserService service = Mockito.mock(UserService.class);

service.updatePassword("admin", "password", "newPassword");
}
}

0 comments on commit 05f9d0b

Please sign in to comment.