|
| 1 | +package com.javachinna.controller; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.any; |
| 4 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 5 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 6 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.TestInstance; |
| 11 | +import org.junit.jupiter.api.TestInstance.Lifecycle; |
| 12 | +import org.mockito.Mockito; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 15 | +import org.springframework.boot.test.context.SpringBootTest; |
| 16 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 17 | +import org.springframework.http.MediaType; |
| 18 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 19 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 20 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 21 | +import org.springframework.test.web.servlet.MockMvc; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import com.javachinna.config.MockUserUtils; |
| 25 | +import com.javachinna.dto.LocalUser; |
| 26 | +import com.javachinna.dto.LoginRequest; |
| 27 | +import com.javachinna.dto.SignUpRequest; |
| 28 | +import com.javachinna.dto.SocialProvider; |
| 29 | +import com.javachinna.exception.UserAlreadyExistAuthenticationException; |
| 30 | +import com.javachinna.model.User; |
| 31 | +import com.javachinna.service.UserService; |
| 32 | + |
| 33 | +import dev.samstevens.totp.code.CodeVerifier; |
| 34 | + |
| 35 | +@SpringBootTest |
| 36 | +@TestInstance(Lifecycle.PER_CLASS) |
| 37 | +@AutoConfigureMockMvc |
| 38 | +class AuthControllerTest2 { |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private MockMvc mockMvc; |
| 42 | + |
| 43 | + @MockBean |
| 44 | + private UserService userService; |
| 45 | + |
| 46 | + @MockBean |
| 47 | + private CodeVerifier verifier; |
| 48 | + |
| 49 | + @MockBean |
| 50 | + private PasswordEncoder passwordEncoder; |
| 51 | + |
| 52 | + private static User user = MockUserUtils.getMockUser("JavaChinna"); |
| 53 | + |
| 54 | + private static ObjectMapper mapper = new ObjectMapper(); |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + public void init() { |
| 58 | + LocalUser localUser = LocalUser.create(user, null, null, null); |
| 59 | + SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(localUser, "secret")); |
| 60 | + Mockito.when(userService.findUserByEmail(Mockito.anyString())).thenReturn(user); |
| 61 | + Mockito.when(passwordEncoder.matches(Mockito.anyString(), Mockito.anyString())).thenReturn(true); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testAuthenticateUser() throws Exception { |
| 66 | + LoginRequest loginRequest = new LoginRequest(user.getEmail(), user.getPassword()); |
| 67 | + String json = mapper.writeValueAsString(loginRequest); |
| 68 | + mockMvc.perform(post("/api/auth/signin").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8").content(json).accept(MediaType.APPLICATION_JSON)) |
| 69 | + .andExpect(status().isOk()).andExpect(jsonPath("$.authenticated").value("true")).andExpect(jsonPath("$.accessToken").isNotEmpty()); |
| 70 | + |
| 71 | + // Test when user 2fa is enabled |
| 72 | + user.setUsing2FA(true); |
| 73 | + mockMvc.perform(post("/api/auth/signin").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8").content(json).accept(MediaType.APPLICATION_JSON)) |
| 74 | + .andExpect(status().isOk()).andExpect(jsonPath("$.authenticated").value("false")).andExpect(jsonPath("$.user").doesNotExist()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testRegisterUser() throws Exception { |
| 79 | + SignUpRequest signUpRequest = new SignUpRequest("1234", "JavaChinna", user.getEmail(), user.getPassword(), user.getPassword(), SocialProvider.FACEBOOK); |
| 80 | + // Test when user provided email already exists in the database |
| 81 | + Mockito.when(userService.registerNewUser(any(SignUpRequest.class))).thenReturn(user); |
| 82 | + String json = mapper.writeValueAsString(signUpRequest); |
| 83 | + mockMvc.perform(post("/api/auth/signup").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8").content(json).accept(MediaType.APPLICATION_JSON)) |
| 84 | + .andExpect(status().isOk()).andExpect(jsonPath("$.success").value("true")).andExpect(jsonPath("$.message").value("User registered successfully")); |
| 85 | + |
| 86 | + // Test when user provided email already exists in the database |
| 87 | + Mockito.when(userService.registerNewUser(any(SignUpRequest.class))).thenThrow(new UserAlreadyExistAuthenticationException("exists")); |
| 88 | + json = mapper.writeValueAsString(signUpRequest); |
| 89 | + mockMvc.perform(post("/api/auth/signup").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8").content(json).accept(MediaType.APPLICATION_JSON)) |
| 90 | + .andExpect(status().isBadRequest()).andExpect(jsonPath("$.success").value("false")).andExpect(jsonPath("$.message").value("Email Address already in use!")); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testVerifyCodeWhenCodeIsNotValid() throws Exception { |
| 95 | + Mockito.when(verifier.isValidCode(Mockito.anyString(), Mockito.anyString())).thenReturn(false); |
| 96 | + String json = mapper.writeValueAsString("443322"); |
| 97 | + mockMvc.perform(post("/api/auth/verify").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8").content(json).accept(MediaType.APPLICATION_JSON)) |
| 98 | + .andExpect(status().isBadRequest()).andExpect(jsonPath("$.success").value("false")).andExpect(jsonPath("$.message").value("Invalid Code!")); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testVerifyCodeWhenCodeIsValid() throws Exception { |
| 103 | + Mockito.when(verifier.isValidCode(Mockito.anyString(), Mockito.anyString())).thenReturn(true); |
| 104 | + String json = mapper.writeValueAsString("443322"); |
| 105 | + mockMvc.perform(post("/api/auth/verify").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8").content(json).accept(MediaType.APPLICATION_JSON)) |
| 106 | + .andExpect(status().isOk()).andExpect(jsonPath("$.authenticated").value("true")).andExpect(jsonPath("$.accessToken").isNotEmpty()) |
| 107 | + .andExpect(jsonPath("$.user").exists()); |
| 108 | + } |
| 109 | +} |
0 commit comments