|
| 1 | +package com.syncapi.controller.auth; |
| 2 | + |
| 3 | +import com.syncapi.AbstractIntegrationTest; |
| 4 | +import com.syncapi.dto.auth.LoginRequest; |
| 5 | +import com.syncapi.dto.auth.RegisterRequest; |
| 6 | +import com.syncapi.entity.User; |
| 7 | +import com.syncapi.repository.UserRepository; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.params.ParameterizedTest; |
| 11 | +import org.junit.jupiter.params.provider.MethodSource; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.test.web.servlet.MockMvc; |
| 16 | +import org.springframework.test.web.servlet.ResultActions; |
| 17 | + |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +import static com.syncapi.TestUtil.*; |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 23 | + |
| 24 | +@AutoConfigureMockMvc |
| 25 | +class AuthControllerTest extends AbstractIntegrationTest { |
| 26 | + private static final String AUTH_URL = "/api/auth"; |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private MockMvc mockMvc; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private UserRepository userRepository; |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + void setUp() { |
| 36 | + userRepository.deleteAll(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void shouldRegisterNewUser() throws Exception { |
| 41 | + // given |
| 42 | + String name = generateRandomName(); |
| 43 | + String email = generateRandomEmail(); |
| 44 | + RegisterRequest request = new RegisterRequest(name, email, generateRandomPassword()); |
| 45 | + |
| 46 | + // when |
| 47 | + ResultActions res = mockMvc.perform(postJson(AUTH_URL + "/register", request)); |
| 48 | + |
| 49 | + // then |
| 50 | + res.andExpect(status().isOk()) |
| 51 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
| 52 | + .andExpect(jsonPath("$.token").isString()) |
| 53 | + .andExpect(jsonPath("$.token").isNotEmpty()) |
| 54 | + .andExpect(jsonPath("$.email").value(email)) |
| 55 | + .andExpect(jsonPath("$.name").value(name)); |
| 56 | + |
| 57 | + User user = userRepository.findByEmail(email).orElseThrow(); |
| 58 | + assertThat(user.getName()).isEqualTo(name); |
| 59 | + assertThat(user.getPasswordHash()).isNotBlank(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void shouldFailWhenEmailAlreadyExists() throws Exception { |
| 64 | + // given |
| 65 | + String email = generateRandomEmail(); |
| 66 | + RegisterRequest first = new RegisterRequest(generateRandomName(), email, generateRandomPassword()); |
| 67 | + RegisterRequest second = new RegisterRequest(generateRandomName(), email, generateRandomPassword()); |
| 68 | + |
| 69 | + // when / then |
| 70 | + mockMvc.perform(postJson(AUTH_URL + "/register", first)) |
| 71 | + .andExpect(status().isOk()); |
| 72 | + mockMvc.perform(postJson(AUTH_URL + "/register", second)) |
| 73 | + .andExpect(status().isBadRequest()); |
| 74 | + } |
| 75 | + |
| 76 | + @ParameterizedTest(name = "[{index}] invalid register request -> 400") |
| 77 | + @MethodSource("invalidRegisterRequests") |
| 78 | + void shouldFailValidationForInvalidRegisterRequest(RegisterRequest request) throws Exception { |
| 79 | + // when / then |
| 80 | + mockMvc.perform(postJson(AUTH_URL + "/register", request)) |
| 81 | + .andExpect(status().isBadRequest()); |
| 82 | + } |
| 83 | + |
| 84 | + private static Stream<RegisterRequest> invalidRegisterRequests() { |
| 85 | + return Stream.of( |
| 86 | + new RegisterRequest("", generateRandomEmail(), generateRandomPassword()), // blank name |
| 87 | + new RegisterRequest(null, generateRandomEmail(), generateRandomPassword()), // null name |
| 88 | + new RegisterRequest("John", "not-an-email", generateRandomPassword()), // invalid email |
| 89 | + new RegisterRequest("John", "", generateRandomPassword()), // blank email |
| 90 | + new RegisterRequest("John", generateRandomEmail(), "123") // short password |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void shouldLoginSuccessfully() throws Exception { |
| 96 | + // given |
| 97 | + String email = generateRandomEmail(); |
| 98 | + String password = generateRandomPassword(); |
| 99 | + RegisterRequest register = new RegisterRequest("Login User", email, password); |
| 100 | + |
| 101 | + mockMvc.perform(postJson(AUTH_URL + "/register", register)) |
| 102 | + .andExpect(status().isOk()); |
| 103 | + |
| 104 | + LoginRequest login = new LoginRequest(email, password); |
| 105 | + |
| 106 | + // when |
| 107 | + ResultActions res = mockMvc.perform(postJson(AUTH_URL + "/login", login)); |
| 108 | + |
| 109 | + // then |
| 110 | + res.andExpect(status().isOk()) |
| 111 | + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) |
| 112 | + .andExpect(jsonPath("$.token").isString()) |
| 113 | + .andExpect(jsonPath("$.token").isNotEmpty()) |
| 114 | + .andExpect(jsonPath("$.email").value(email)); |
| 115 | + } |
| 116 | + |
| 117 | + @ParameterizedTest(name = "[{index}] invalid login -> 400") |
| 118 | + @MethodSource("invalidLoginRequests") |
| 119 | + void shouldFailLoginForInvalidCredentials(LoginRequest request) throws Exception { |
| 120 | + // when / then |
| 121 | + mockMvc.perform(postJson(AUTH_URL + "/login", request)) |
| 122 | + .andExpect(status().isBadRequest()); |
| 123 | + } |
| 124 | + |
| 125 | + private static Stream<LoginRequest> invalidLoginRequests() { |
| 126 | + return Stream.of( |
| 127 | + new LoginRequest("not-an-email", "password"), // invalid email |
| 128 | + new LoginRequest("", "password"), // blank email |
| 129 | + new LoginRequest(generateRandomEmail(), ""), // blank password |
| 130 | + new LoginRequest(generateRandomEmail(), "wrong-password") // wrong password |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
0 commit comments