initial
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
package net.moustos.mtgsearch;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.http.MediaType;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
|
||||
/**
|
||||
* Integration tests for authentication API
|
||||
*/
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@TestPropertySource(locations = "classpath:application-test.yml")
|
||||
public class AuthControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
// Clear database before each test
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserRegistration() throws Exception {
|
||||
String registerPayload = """
|
||||
{
|
||||
"username": "testuser",
|
||||
"email": "test@example.com",
|
||||
"password": "SecurePassword123!"
|
||||
}
|
||||
""";
|
||||
|
||||
mockMvc.perform(post("/api/v1/auth/register")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(registerPayload))
|
||||
.andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("$.username").value("testuser"))
|
||||
.andExpect(jsonPath("$.email").value("test@example.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserLogin() throws Exception {
|
||||
// First register
|
||||
String registerPayload = """
|
||||
{
|
||||
"username": "testuser",
|
||||
"email": "test@example.com",
|
||||
"password": "SecurePassword123!"
|
||||
}
|
||||
""";
|
||||
|
||||
mockMvc.perform(post("/api/v1/auth/register")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(registerPayload))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
// Then login
|
||||
String loginPayload = """
|
||||
{
|
||||
"username": "testuser",
|
||||
"password": "SecurePassword123!"
|
||||
}
|
||||
""";
|
||||
|
||||
mockMvc.perform(post("/api/v1/auth/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(loginPayload))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.token").exists())
|
||||
.andExpect(jsonPath("$.username").value("testuser"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoginWithInvalidCredentials() throws Exception {
|
||||
String loginPayload = """
|
||||
{
|
||||
"username": "nonexistent",
|
||||
"password": "WrongPassword123!"
|
||||
}
|
||||
""";
|
||||
|
||||
mockMvc.perform(post("/api/v1/auth/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(loginPayload))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDuplicateUsernameRegistration() throws Exception {
|
||||
String registerPayload = """
|
||||
{
|
||||
"username": "testuser",
|
||||
"email": "test@example.com",
|
||||
"password": "SecurePassword123!"
|
||||
}
|
||||
""";
|
||||
|
||||
// Register first user
|
||||
mockMvc.perform(post("/api/v1/auth/register")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(registerPayload))
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
// Try to register with same username
|
||||
mockMvc.perform(post("/api/v1/auth/register")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(registerPayload))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user