-
Notifications
You must be signed in to change notification settings - Fork 0
Sujit81/PandaAIDemo
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest // This loads only the web layer with Spring Security configuration
public class SecurityConfigTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testPublicEndpointAccess() throws Exception {
// No authentication required
mockMvc.perform(get("/public/hello"))
.andExpect(status().isOk()); // Public endpoint should return 200 OK
}
@Test
public void testSecuredEndpointAccessWithoutAuthentication() throws Exception {
// No authentication provided
mockMvc.perform(get("/secured"))
.andExpect(status().isUnauthorized()); // Should return 401 Unauthorized
}
@Test
@WithMockUser(username = "user", roles = {"USER"})
public void testSecuredEndpointAccessWithAuthentication() throws Exception {
// Authentication provided via @WithMockUser
mockMvc.perform(get("/secured"))
.andExpect(status().isOk()); // Should return 200 OK for authenticated user
}
}
About
No description, website, or topics provided.
Security policy
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published