This commit is contained in:
2026-04-27 22:36:44 +09:30
parent 86b6d6c0b9
commit 2d03f3a7f4
58 changed files with 4376 additions and 62 deletions
@@ -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());
}
}
@@ -0,0 +1,108 @@
package net.moustos.mtgsearch.service;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.TestPropertySource;
import net.moustos.mtgsearch.model.User;
import net.moustos.mtgsearch.repository.UserRepository;
import static org.junit.jupiter.api.Assertions.*;
/**
* Unit tests for AuthService
*/
@SpringBootTest
@TestPropertySource(locations = "classpath:application-test.yml")
@DisplayName("AuthService Tests")
public class AuthServiceTest {
@Autowired
private AuthService authService;
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@BeforeEach
public void setUp() {
userRepository.deleteAll();
}
@Test
@DisplayName("Should register a new user successfully")
public void testRegisterUser() {
User user = authService.register("testuser", "test@example.com", "SecurePassword123!");
assertNotNull(user.getId());
assertEquals("testuser", user.getUsername());
assertEquals("test@example.com", user.getEmail());
assertTrue(user.getActive());
}
@Test
@DisplayName("Should throw exception for duplicate username")
public void testRegisterDuplicateUsername() {
authService.register("testuser", "test@example.com", "SecurePassword123!");
assertThrows(IllegalArgumentException.class, () ->
authService.register("testuser", "another@example.com", "SecurePassword123!")
);
}
@Test
@DisplayName("Should throw exception for duplicate email")
public void testRegisterDuplicateEmail() {
authService.register("testuser", "test@example.com", "SecurePassword123!");
assertThrows(IllegalArgumentException.class, () ->
authService.register("anotheruser", "test@example.com", "SecurePassword123!")
);
}
@Test
@DisplayName("Should authenticate user successfully")
public void testAuthenticateUser() {
authService.register("testuser", "test@example.com", "SecurePassword123!");
String token = authService.authenticate("testuser", "SecurePassword123!");
assertNotNull(token);
assertFalse(token.isEmpty());
}
@Test
@DisplayName("Should throw exception for invalid username")
public void testAuthenticateWithInvalidUsername() {
assertThrows(IllegalArgumentException.class, () ->
authService.authenticate("nonexistent", "SomePassword123!")
);
}
@Test
@DisplayName("Should throw exception for invalid password")
public void testAuthenticateWithInvalidPassword() {
authService.register("testuser", "test@example.com", "SecurePassword123!");
assertThrows(IllegalArgumentException.class, () ->
authService.authenticate("testuser", "WrongPassword123!")
);
}
@Test
@DisplayName("Should retrieve user by ID")
public void testGetUserById() {
User registeredUser = authService.register("testuser", "test@example.com", "SecurePassword123!");
var retrievedUser = authService.getUserById(registeredUser.getId());
assertTrue(retrievedUser.isPresent());
assertEquals(registeredUser.getId(), retrievedUser.get().getId());
}
@Test
@DisplayName("Should retrieve user by username")
public void testGetUserByUsername() {
authService.register("testuser", "test@example.com", "SecurePassword123!");
var retrievedUser = authService.getUserByUsername("testuser");
assertTrue(retrievedUser.isPresent());
assertEquals("testuser", retrievedUser.get().getUsername());
}
}
@@ -0,0 +1,88 @@
package net.moustos.mtgsearch.repository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import net.moustos.mtgsearch.model.User;
import static org.junit.jupiter.api.Assertions.*;
/**
* Tests for UserRepository
*/
@SpringBootTest
@TestPropertySource(locations = "classpath:application-test.yml")
public class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@BeforeEach
public void setUp() {
userRepository.deleteAll();
}
@Test
public void testFindByUsername() {
User user = User.builder()
.username("testuser")
.email("test@example.com")
.password("hashedpassword")
.active(true)
.build();
userRepository.save(user);
var found = userRepository.findByUsername("testuser");
assertTrue(found.isPresent());
assertEquals("testuser", found.get().getUsername());
}
@Test
public void testFindByEmail() {
User user = User.builder()
.username("testuser")
.email("test@example.com")
.password("hashedpassword")
.active(true)
.build();
userRepository.save(user);
var found = userRepository.findByEmail("test@example.com");
assertTrue(found.isPresent());
assertEquals("test@example.com", found.get().getEmail());
}
@Test
public void testExistsByUsername() {
User user = User.builder()
.username("testuser")
.email("test@example.com")
.password("hashedpassword")
.active(true)
.build();
userRepository.save(user);
assertTrue(userRepository.existsByUsername("testuser"));
assertFalse(userRepository.existsByUsername("nonexistent"));
}
@Test
public void testExistsByEmail() {
User user = User.builder()
.username("testuser")
.email("test@example.com")
.password("hashedpassword")
.active(true)
.build();
userRepository.save(user);
assertTrue(userRepository.existsByEmail("test@example.com"));
assertFalse(userRepository.existsByEmail("nonexistent@example.com"));
}
}
@@ -0,0 +1,30 @@
spring:
profiles:
active: test
datasource:
url: jdbc:postgresql://localhost:5432/mtgsearch_test
username: postgres
password: postgres
driver-class-name: org.postgresql.Driver
hikari:
maximum-pool-size: 5
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
hibernate:
ddl-auto: create-drop
show-sql: false
flyway:
enabled: true
baseline-on-migrate: true
app:
jwt:
secret: test-secret-key
expiration: 3600
logging:
level:
root: WARN
net.moustos.mtgsearch: DEBUG