[tlinh2110@gmail.com] Remove unused test cases
This commit is contained in:
-41
@@ -1,41 +0,0 @@
|
||||
package org.baeldung.testmethodsecurity;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.baeldung.testmethodsecurity.service.UserRoleService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class TestCustomSecurityContext {
|
||||
|
||||
@Autowired
|
||||
UserRoleService userRoleService;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.testmethodsecurity.*")
|
||||
public static class SpringConfig {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockSysUser(systemUserName="jane")
|
||||
public void whenJane_callGetUserName_thenOK(){
|
||||
String userName = userRoleService.getUserName();
|
||||
assertEquals("jane",userName);
|
||||
}
|
||||
|
||||
@Test(expected=AccessDeniedException.class)
|
||||
@WithMockSysUser(systemUserName="john")
|
||||
public void whenJohn_callGetUserName_thenFail(){
|
||||
userRoleService.getUserName();
|
||||
}
|
||||
|
||||
}
|
||||
+8
-8
@@ -29,28 +29,28 @@ public class TestMethodSecurity{
|
||||
|
||||
@Test
|
||||
@WithMockUser(username="john",roles={"VIEWER"})
|
||||
public void whenRoleViewer_callGetUserName_thenOK(){
|
||||
String userName = userRoleService.getUserName();
|
||||
public void givenRoleViewer_whenCallGetUsername_thenReturnUsername(){
|
||||
String userName = userRoleService.getUsername();
|
||||
assertEquals("john", userName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser(username="john",authorities={"SYS_ADMIN"})
|
||||
public void whenSysAdmin_callGetUserName_thenOK(){
|
||||
String userName = userRoleService.getUserName();
|
||||
public void givenAuthoritySysAdmin_whenCallGetUsername_thenReturnUsername(){
|
||||
String userName = userRoleService.getUsername();
|
||||
assertEquals("john", userName);
|
||||
}
|
||||
|
||||
@Test(expected=AccessDeniedException.class)
|
||||
@WithAnonymousUser
|
||||
public void whenAnomynous_callGetUserName_thenFail(){
|
||||
userRoleService.getUserName();
|
||||
public void givenAnomynousUser_whenCallGetUsername_thenAccessDenied(){
|
||||
userRoleService.getUsername();
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockJohnViewer
|
||||
public void whenJohnViewer_callGetUserName_thenOK(){
|
||||
String userName = userRoleService.getUserName();
|
||||
public void givenMockedJohnViewer_whenCallGetUsername_thenReturnUsername(){
|
||||
String userName = userRoleService.getUsername();
|
||||
assertEquals("john", userName);
|
||||
}
|
||||
|
||||
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package org.baeldung.testmethodsecurity;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
import org.baeldung.testmethodsecurity.service.SystemPropImpl;
|
||||
import org.baeldung.testmethodsecurity.service.SystemPropInterface;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class TestSystemProp{
|
||||
|
||||
@Autowired
|
||||
SystemPropInterface systemProp;
|
||||
|
||||
@Test
|
||||
@WithMockUser(username="test")
|
||||
public void checkSystemPropInstance(){
|
||||
assertFalse(systemProp instanceof SystemPropImpl);
|
||||
assertTrue(systemProp instanceof SystemPropInterface);
|
||||
assertTrue(systemProp instanceof Proxy);
|
||||
|
||||
assertEquals("Method Security", systemProp.getSystemName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotAuthentication_callSayHello_thenOK(){
|
||||
String hello = systemProp.sayHello();
|
||||
assertEquals("Hi", hello);
|
||||
}
|
||||
|
||||
@Test(expected=AuthenticationCredentialsNotFoundException.class)
|
||||
public void whenNotAuthentication_callSayHi_thenFailed(){
|
||||
systemProp.sayHi();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.testmethodsecurity.*")
|
||||
public static class SpringConfig {
|
||||
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -18,8 +18,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
public class TestWithMockUserAtClassLevel {
|
||||
|
||||
@Test
|
||||
public void whenRoleViewerLogged_callGetUserName_thenOK(){
|
||||
String currentUserName = userService.getUserName();
|
||||
public void givenRoleViewer_whenCallGetUsername_thenReturnUsername(){
|
||||
String currentUserName = userService.getUsername();
|
||||
assertEquals("john",currentUserName);
|
||||
}
|
||||
|
||||
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
package org.baeldung.testmethodsecurity;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
import org.springframework.security.test.context.support.WithSecurityContext;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@WithSecurityContext(factory = WithMockSysUserSecurityContextFactory.class)
|
||||
public @interface WithMockSysUser {
|
||||
String systemUserName();
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
package org.baeldung.testmethodsecurity;
|
||||
|
||||
import org.baeldung.testmethodsecurity.entity.CustomUser;
|
||||
import org.baeldung.testmethodsecurity.repository.UserRoleRepository;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
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;
|
||||
|
||||
public class WithMockSysUserSecurityContextFactory
|
||||
implements WithSecurityContextFactory<WithMockSysUser> {
|
||||
|
||||
@Override
|
||||
public SecurityContext createSecurityContext(WithMockSysUser customUser) {
|
||||
SecurityContext context = SecurityContextHolder.createEmptyContext();
|
||||
UserRoleRepository userRoleRepo = new UserRoleRepository();
|
||||
|
||||
CustomUser user = userRoleRepo.loadUserByUserName(customUser.systemUserName());
|
||||
|
||||
Authentication auth =
|
||||
new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
|
||||
|
||||
context.setAuthentication(auth);
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user