#24 simple sns: join, login test code
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.example.sns.controller;
|
||||
|
||||
import com.example.sns.service.UserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/users")
|
||||
@RequiredArgsConstructor
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
@PostMapping("/join")
|
||||
public void join() {
|
||||
userService.join(username, password);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.sns.controller.request;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class UserJoinRequest {
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.sns.controller.request;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class UserLoginRequest {
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.example.sns.exception;
|
||||
|
||||
public class SnsApplicationException extends RuntimeException {
|
||||
}
|
||||
7
simple_sns/src/main/java/com/example/sns/model/User.java
Normal file
7
simple_sns/src/main/java/com/example/sns/model/User.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package com.example.sns.model;
|
||||
|
||||
public class User {
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.example.sns.model.entity;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
@Getter @Setter
|
||||
public class UserEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.sns.repository;
|
||||
|
||||
import com.example.sns.model.entity.UserEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface UserEntityRepository extends JpaRepository<UserEntity, Integer> {
|
||||
|
||||
Optional<UserEntity> findByUsername(String username);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.example.sns.service;
|
||||
|
||||
import com.example.sns.exception.SnsApplicationException;
|
||||
import com.example.sns.model.User;
|
||||
import com.example.sns.model.entity.UserEntity;
|
||||
import com.example.sns.repository.UserEntityRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserService {
|
||||
|
||||
private final UserEntityRepository userEntityRepository;
|
||||
|
||||
public User join(String username, String password){
|
||||
|
||||
// username 확인
|
||||
Optional<UserEntity> userEntity = userEntityRepository.findByUsername(username);
|
||||
|
||||
// 회원가입 진행
|
||||
userEntityRepository.save(new UserEntity());
|
||||
|
||||
return new User();
|
||||
}
|
||||
|
||||
public String login(String username, String password) {
|
||||
|
||||
// 회원가입 여부 체크
|
||||
UserEntity userEntity = userEntityRepository.findByUsername(username).orElseThrow(SnsApplicationException::new);
|
||||
|
||||
// 비밀번호 체크
|
||||
if (!userEntity.getPassword().equals(password)) {
|
||||
throw new SnsApplicationException();
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.example.sns.controller;
|
||||
|
||||
import com.example.sns.controller.request.UserJoinRequest;
|
||||
import com.example.sns.controller.request.UserLoginRequest;
|
||||
import com.example.sns.exception.SnsApplicationException;
|
||||
import com.example.sns.model.User;
|
||||
import com.example.sns.service.UserService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
public class UserControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@MockBean
|
||||
private UserService userService;
|
||||
|
||||
@Test
|
||||
public void 회원가입() throws Exception {
|
||||
String username = "username";
|
||||
String password = "password";
|
||||
|
||||
when(userService.join(username, password)).thenReturn(mock(User.class));
|
||||
|
||||
mockMvc.perform(post("/api/v1/users/join")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new UserJoinRequest(username, password)))
|
||||
).andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void 회원가입시_이미_회원가입된_username으로_회원가입을_하는경우_에러반환() throws Exception {
|
||||
String username = "username";
|
||||
String password = "password";
|
||||
|
||||
when(userService.join(username, password)).thenThrow(new SnsApplicationException());
|
||||
|
||||
mockMvc.perform(post("/api/v1/users/join")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new UserJoinRequest(username, password)))
|
||||
).andDo(print())
|
||||
.andExpect(status().isConflict());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void 로그인() throws Exception {
|
||||
String username = "username";
|
||||
String password = "password";
|
||||
|
||||
when(userService.login(username, password)).thenReturn("test_token");
|
||||
|
||||
mockMvc.perform(post("/api/v1/users/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new UserLoginRequest(username, password)))
|
||||
).andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void 로그인시_회원가입이_안된_username을_입력할경우_에러반환() throws Exception {
|
||||
String username = "username";
|
||||
String password = "password";
|
||||
|
||||
when(userService.login(username, password)).thenThrow(new SnsApplicationException());
|
||||
|
||||
mockMvc.perform(post("/api/v1/users/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new UserLoginRequest(username, password)))
|
||||
).andDo(print())
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void 로그인시_틀린_password를_입력할경우_에러반환() throws Exception {
|
||||
String username = "username";
|
||||
String password = "password";
|
||||
|
||||
when(userService.login(username, password)).thenThrow(new SnsApplicationException());
|
||||
|
||||
mockMvc.perform(post("/api/v1/users/login")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsBytes(new UserLoginRequest(username, password)))
|
||||
).andDo(print())
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.sns.fixture;
|
||||
|
||||
import com.example.sns.model.entity.UserEntity;
|
||||
|
||||
public class UserEntityFixture {
|
||||
|
||||
public static UserEntity get(String username, String password) {
|
||||
UserEntity result = new UserEntity();
|
||||
result.setId(1);
|
||||
result.setUsername(username);
|
||||
result.setPassword(password);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.example.sns.service;
|
||||
|
||||
import com.example.sns.exception.SnsApplicationException;
|
||||
import com.example.sns.fixture.UserEntityFixture;
|
||||
import com.example.sns.model.entity.UserEntity;
|
||||
import com.example.sns.repository.UserEntityRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SpringBootTest
|
||||
class UserServiceTest {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@MockBean
|
||||
private UserEntityRepository userEntityRepository;
|
||||
|
||||
@Test
|
||||
void 회원가입이_정상적으로_동작하는_경우() {
|
||||
String username = "username";
|
||||
String password = "password";
|
||||
|
||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.empty());
|
||||
when(userEntityRepository.save(any())).thenReturn(Optional.of(UserEntityFixture.get(username, password)));
|
||||
|
||||
assertDoesNotThrow(() -> userService.join(username, password));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 회원가입시_username으로_회원가입한_유저가_이미_있는경우() {
|
||||
String username = "username";
|
||||
String password = "password";
|
||||
|
||||
UserEntity fixture = UserEntityFixture.get(username, password);
|
||||
|
||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(fixture));
|
||||
when(userEntityRepository.save(any())).thenReturn(Optional.of(fixture));
|
||||
|
||||
assertThrows(
|
||||
SnsApplicationException.class,
|
||||
() -> userService.join(username, password)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 로그인이_정상적으로_동작하는_경우() {
|
||||
String username = "username";
|
||||
String password = "password";
|
||||
|
||||
UserEntity fixture = UserEntityFixture.get(username, password);
|
||||
|
||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(fixture));
|
||||
|
||||
assertDoesNotThrow(() -> userService.login(username, password));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 로그인시_username으로_회원가입한_유저가_없는_경우() {
|
||||
String username = "username";
|
||||
String password = "password";
|
||||
|
||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(
|
||||
SnsApplicationException.class,
|
||||
() -> userService.login(username, password)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 로그인시_패스워드가_틀린_경우() {
|
||||
String username = "username";
|
||||
String password = "password";
|
||||
String wrongPassword = "wrongPassword";
|
||||
|
||||
UserEntity fixture = UserEntityFixture.get(username, password);
|
||||
|
||||
when(userEntityRepository.findByUsername(username)).thenReturn(Optional.of(fixture));
|
||||
|
||||
assertThrows(
|
||||
SnsApplicationException.class,
|
||||
() -> userService.login(username, wrongPassword)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user