- ์ํ๋ฆฌํฐ๋ฅผ ์ด์ฉํ JSON ๋ฐ์ดํฐ๋ก ๋ก๊ทธ์ธ (์๋ฃ)
- JWT๋ฅผ ์ด์ฉํ ์ธ์ฆ (์๋ฃ)
- ๋๋ฉ์ธ, ํ ์ด๋ธ ์ค๊ณ, ์ํฐํฐ ์์ฑ (์๋ฃ)
- ๋๊ธ ์ญ์ ๋ก์ง ๊ตฌํ (์๋ฃ)
- ํ์๊ฐ์ + ์ ๋ณด์์ ๋ฑ ํ์ ์๋น์ค ๊ตฌํ (์งํ ์ค)
- ๊ฒ์ํ ์๋น์ค ๊ตฌํ
- ๋๊ธ ์๋น์ค ๊ตฌํ (1๋๊ธ -> *(๋ฌดํ) ๋๋๊ธ ๊ตฌ์กฐ)
- ์์ธ ์ฒ๋ฆฌ
- ์์ธ ๋ฉ์ธ์ง ๊ตญ์ ํ
- ์นดํ ๊ณ ๋ฆฌ๋ณ ๊ฒ์ํ ๋ถ๋ฅ
- ๊ฒ์๊ธ ํ์ด์ง
- ๋์ ์ธ ๊ฒ์ ์กฐ๊ฑด์ ์ฌ์ฉํ ๊ฒ์
- ์ฌ์ฉ์ ๊ฐ ์ชฝ์ง ๊ธฐ๋ฅ
- ๋ฌดํ ์ชฝ์ง ์คํฌ๋กค
- ๊ฒ์๋ฌผ & ๋๊ธ์ ๋ํ ์๋
- ์ชฝ์ง์ ๋ํ ์๋
- ์ ์ํ ์ฌ์ฉ์ ๊ฐ ์ค์๊ฐ ์ฑํ
- ํ์๊ฐ์ ์ ๊ฒ์ฆ(์: XX๋ํ๊ต XX๊ณผ๊ฐ ์๋๋ฉด ๊ฐ์ ํ ์ ์๊ฒ)
- Swagger๋ฅผ ์ฌ์ฉํ API ๋ฌธ์ ๋ง๋ค๊ธฐ
- ์ ๊ณ & ๋ธ๋๋ฆฌ์คํธ ๊ธฐ๋ฅ
- AOP๋ฅผ ํตํ ๋ก๊ทธ
- ์ด๋๋ฏผ ํ์ด์ง
- ์บ์
- ๋ฐฐํฌ (+ ๋ฌด์ค๋จ ๋ฐฐํฌ)
- ๋ฐฐํฌ ์๋ํ
- ํฌํธ์ ์ด๋ํฐ ์ค๊ณ๋ฅผ ๋ฐ๋ฅด๋ ํจํค์ง ๊ตฌ์กฐ ์ค๊ณํ๊ธฐ
- ...
ํ์ ์๋น์ค ๊ฐ๋ฐํ๊ธฐ
์ฐ์ ๋ก๊ทธ์ธ์ ์ด์ ์ ๊ฐ๋ฐํด ๋์๊ธฐ ๋๋ฌธ์, ํ์๊ฐ์ ๊ณผ ์ ๋ณด์์ , ํ์ํํด, ์ ๋ณด์กฐํ๋ฅผ ๊ตฌํํ๋๋ก ํ๊ฒ ์ต๋๋ค.
ํ์ฌ Member ํด๋์ค์ ์ํ์ ๋๋ค.
@Table(name = "MEMBER")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@AllArgsConstructor
@Builder
public class Member extends BaseTimeEntity {
@Id @GeneratedValue(strategy = IDENTITY)
@Column(name = "member_id")
private Long id; //primary Key
@Column(nullable = false, length = 30, unique = true)
private String username;//์์ด๋
private String password;//๋น๋ฐ๋ฒํธ
@Column(nullable = false, length = 30)
private String name;//์ด๋ฆ(์ค๋ช
)
@Column(nullable = false, length = 30)
private String nickName;//๋ณ๋ช
@Column(nullable = false, length = 30)
private Integer age;//๋์ด
@Enumerated(STRING)
@Column(nullable = false, length = 30)
private Role role;//๊ถํ -> USER, ADMIN
@Column(length = 1000)
private String refreshToken;//RefreshToken
//== ํ์ํํด -> ์์ฑํ ๊ฒ์๋ฌผ, ๋๊ธ ๋ชจ๋ ์ญ์ ==//
@OneToMany(mappedBy = "writer", cascade = ALL, orphanRemoval = true)
private List<Post> postList = new ArrayList<>();
@OneToMany(mappedBy = "writer", cascade = ALL, orphanRemoval = true)
private List<Comment> commentList = new ArrayList<>();
//== ์ฐ๊ด๊ด๊ณ ๋ฉ์๋ ==//
public void addPost(Post post){
//post์ writer ์ค์ ์ post์์ ํจ
postList.add(post);
}
public void addComment(Comment comment){
//comment์ writer ์ค์ ์ comment์์ ํจ
commentList.add(comment);
}
//== ์ ๋ณด ์์ ==//
public void updatePassword(PasswordEncoder passwordEncoder, String password){
this.password = passwordEncoder.encode(password);
}
public void updateName(String name){
this.name = name;
}
public void updateNickName(String nickName){
this.nickName = nickName;
}
public void updateAge(int age){
this.age = age;
}
public void updateRefreshToken(String refreshToken){
this.refreshToken = refreshToken;
}
public void destroyRefreshToken(){
this.refreshToken = null;
}
//== ํจ์ค์๋ ์ํธํ ==//
public void encodePassword(PasswordEncoder passwordEncoder){
this.password = passwordEncoder.encode(password);
}
}
๋ค์ ์ฝ๋๋ฅผ ์ถ๊ฐํด์ฃผ๊ฒ ์ต๋๋ค.
//๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ, ํ์ ํํด ์, ๋น๋ฐ๋ฒํธ๋ฅผ ํ์ธํ๋ฉฐ, ์ด๋ ๋น๋ฐ๋ฒํธ์ ์ผ์น์ฌ๋ถ๋ฅผ ํ๋จํ๋ ๋ฉ์๋์
๋๋ค.
public boolean matchPassword(PasswordEncoder passwordEncoder, String checkPassword){
return passwordEncoder.matches(checkPassword, getPassword());
}
//ํ์๊ฐ์
์, USER์ ๊ถํ์ ๋ถ์ฌํ๋ ๋ฉ์๋์
๋๋ค.
public void addUserAuthority() {
this.role = Role.USER;
}
๊ทธ๋ฆฌ๊ณ ํ์ฌ MemberRepository์ ์ํ์ ๋๋ค.
public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findByUsername(String username);
boolean existsByUsername(String username);
Optional<Member> findByRefreshToken(String refreshToken);
}
์ด์ MemberService๋ฅผ ๊ฐ๋ฐํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
MemberService
์์น๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
public interface MemberService {
/**
* ํ์๊ฐ์
* ์ ๋ณด์์
* ํ์ํํด
* ์ ๋ณด์กฐํ
*/
void signUp(MemberSignUpDto memberSignUpDto) throws Exception;
void update(MemberUpdateDto memberUpdateDto) throws Exception;
void updatePassword(String checkPassword, String toBePassword) throws Exception;
void withdraw(String checkPassword) throws Exception;
MemberInfoDto getInfo(Long id) throws Exception;
MemberInfoDto getMyInfo() throws Exception;
}
DTO
์ฌ์ฉํ DTO๋ค์ ๋ค์ ์์น์ ์์ฑํ์ต๋๋ค.
MemberInfoDto
@Data
public class MemberInfoDto {
private final String name;
private final String nickName;
private final String username;
private final Integer age;
@Builder
public MemberInfoDto(Member member) {
this.name = member.getName();
this.nickName = member.getNickName();
this.username = member.getUsername();
this.age = member.getAge();
}
}
MemberSignUpDto
public record MemberSignUpDto(String username, String password, String name,
String nickName, Integer age) {
public Member toEntity() {
return Member.builder().username(username).password(password).name(name).nickName(nickName).age(age).build();
}
}
MemberUpdateDto
public record MemberUpdateDto(Optional<String> name, Optional<String> nickName,
Optional<Integer> age) {
}
SecurityUtil
SecurityContextHolder์์ username์ ๊บผ๋ด์ค๋ ์ฝ๋๋ฅผ, ํด๋น ํด๋์ค๋ฅผ ํตํด์ ์์ฑํ ํ, ํธํ๊ฒ ์ฌ์ฉํ๋๋ก ํ๊ฒ ์ต๋๋ค.
public class SecurityUtil {
public static String getLoginUsername(){
UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return user.getUsername();
}
}
MemberServiceImpl
@Service
@RequiredArgsConstructor
@Transactional
public class MemberServiceImpl implements MemberService{
private final MemberRepository memberRepository;
private final PasswordEncoder passwordEncoder;
@Override
public void signUp(MemberSignUpDto memberSignUpDto) throws Exception {
Member member = memberSignUpDto.toEntity();
member.addUserAuthority();
member.encodePassword(passwordEncoder);
if(memberRepository.findByUsername(memberSignUpDto.username()).isPresent()){
throw new Exception("์ด๋ฏธ ์กด์ฌํ๋ ์์ด๋์
๋๋ค.");
}
memberRepository.save(member);
}
@Override
public void update(MemberUpdateDto memberUpdateDto) throws Exception {
Member member = memberRepository.findByUsername(SecurityUtil.getLoginUsername()).orElseThrow(() -> new Exception("ํ์์ด ์กด์ฌํ์ง ์์ต๋๋ค"));
memberUpdateDto.age().ifPresent(member::updateAge);
memberUpdateDto.name().ifPresent(member::updateName);
memberUpdateDto.nickName().ifPresent(member::updateNickName);
}
@Override
public void updatePassword(String checkPassword, String toBePassword) throws Exception {
Member member = memberRepository.findByUsername(SecurityUtil.getLoginUsername()).orElseThrow(() -> new Exception("ํ์์ด ์กด์ฌํ์ง ์์ต๋๋ค"));
if(!member.matchPassword(passwordEncoder, checkPassword) ) {
throw new Exception("๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ์ง ์์ต๋๋ค.");
}
member.updatePassword(passwordEncoder, toBePassword);
}
@Override
public void withdraw(String checkPassword) throws Exception {
Member member = memberRepository.findByUsername(SecurityUtil.getLoginUsername()).orElseThrow(() -> new Exception("ํ์์ด ์กด์ฌํ์ง ์์ต๋๋ค"));
if(!member.matchPassword(passwordEncoder, checkPassword) ) {
throw new Exception("๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ์ง ์์ต๋๋ค.");
}
memberRepository.delete(member);
}
@Override
public MemberInfoDto getInfo(Long id) throws Exception {
Member findMember = memberRepository.findById(id).orElseThrow(() -> new Exception("ํ์์ด ์์ต๋๋ค"));
return new MemberInfoDto(findMember);
}
@Override
public MemberInfoDto getMyInfo() throws Exception {
Member findMember = memberRepository.findByUsername(SecurityUtil.getLoginUsername()).orElseThrow(() -> new Exception("ํ์์ด ์์ต๋๋ค"));
return new MemberInfoDto(findMember);
}
}
์ค๋ช
1. signUp()
ํ์๊ฐ์ ์ ์งํํ๋ ๋ฉ์๋์ ๋๋ค.
ํ์๊ฐ์ ์ ์ปจํธ๋กค๋ฌ ๋จ์์ ์ํฐํฐ๋ก ๋ณํํ์ฌ ๋ฐ์์ค๋ ๊ฒ์ด ์๋๋ผ, ์๋น์ค ๋จ์์ DTO๋ฅผ ์ํฐํฐ๋ก ๋ณํํ์์ต๋๋ค.
๋ณํ ํ USER๋ผ๋ ๊ถํ์ ์ค์ ํ์๊ณ , ์ดํ ์ค๋ณต๋ ์์ด๋๊ฐ ์๋์ง ์ฒดํฌํฉ๋๋ค.
์๋ค๋ฉด ํ์๊ฐ์ ์ ์งํํฉ๋๋ค.
2. update()
ํ์์ ๋ณด๋ฅผ ์์ ํฉ๋๋ค.
MemberUpdateDto๋ Optional ํ๋๋ค์ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ,
ifPresent๋ฅผ ํตํด ํ๋๊ฐ ์กด์ฌํ๋ ๊ฒฝ์ฐ์๋ง ์ ๋ฐ์ดํธ๋ฅผ ์งํํ๋๋ก ์์ฑํ์์ต๋๋ค.
3. updatePassword()
๋น๋ฐ๋ฒํธ๋ฅผ ๋ณ๊ฒฝํ๋ ๋ฉ์๋์ ๋๋ค.
๋น๋ฐ๋ฒํธ๋ ๋ค๋ฅธ ํ์์ ๋ณด๋ค๊ณผ ๋ค๋ฅด๊ฒ ๋ฌด์กฐ๊ฑด ๋ฐ๋ก ์ ๋ฐ์ดํธ ํด์ผ ํ๋ฉฐ,
๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ์์๋ ํ์ฌ ๋น๋ฐ๋ฒํธ๋ฅผ ์ ๋ ฅ๋ฐ์ ๋ณด์์ ๊ฐํํฉ๋๋ค.
Member ํด๋์ค์ matchPassword()๋ผ๋ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด ๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ๋์ง ํ์ธํ๋ ๋ฉ์๋๋ฅผ ์์ฑํ์๊ณ , ์ผ์นํ๋ค๋ฉด, ๋ณ๊ฒฝํ๊ณ ์ ํ๋ ๋น๋ฐ๋ฒํธ (toBePassword)๋ก ๋ณ๊ฒฝํฉ๋๋ค
4. withdraw()
ํ์ํํด๋ฅผ ์งํํ๋ ๋ฉ์๋์ ๋๋ค.
๋น๋ฐ๋ฒํธ๋ฅผ ์ฌ์ ๋ ฅ๋ฐ์ ๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํด์ผ๋ง ํ์ํํด๋ฅผ ์งํํฉ๋๋ค.
5. getInfo()
id๋ฅผ ๋ฐ์์์ ํด๋น ํ์์ ์ ๋ณด๋ฅผ ์กฐํํ๋ ๋ฉ์๋์ ๋๋ค.
MemberInfoDto์ ํํ๋ก ๊ฐ์ธ์ ๋ฐํํ๋ฉฐ ์ด๋ ์ดํ์ ๋น๊ณต๊ฐ ๊ณ์ ๊ณผ ๊ณต๊ฐ ๊ณ์ ์ ๋๋์ด ๋น๊ณต๊ฐ ๊ณ์ ์ผ ๊ฒฝ์ฐ ์ ๋ณด์กฐํ๋ฅผ ํ ์ ์๋๋ก ๋ง๋ค๊ณ ์ถ์ต๋๋ค.
6. getMyInfo()
๋์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์๋์ ๋๋ค.
ํ์ฌ ๋์ ์ ๋ณด๋ ๋ก๊ทธ์ธ ํ ๊ฒฝ์ฐ SecurityContextHolder์ ๋ค์ด์๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก ์ ๋ ฅ๋ฐ์ง ์์๋ ์ธ์ฆ๋ง ๋์ด์๋ค๋ฉด ์ ๋ณด ์กฐํ๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
ํ ์คํธ์ฝ๋
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@SpringBootTest
@Transactional
class MemberServiceTest {
@Autowired EntityManager em;
@Autowired
MemberRepository memberRepository;
@Autowired MemberService memberService;
@Autowired
PasswordEncoder passwordEncoder;
String PASSWORD = "password";
private void clear(){
em.flush();
em.clear();
}
private MemberSignUpDto makeMemberSignUpDto() {
return new MemberSignUpDto("username",PASSWORD,"name","nickNAme",22);
}
private MemberSignUpDto setMember() throws Exception {
MemberSignUpDto memberSignUpDto = makeMemberSignUpDto();
memberService.signUp(memberSignUpDto);
clear();
SecurityContext emptyContext = SecurityContextHolder.createEmptyContext();
emptyContext.setAuthentication(new UsernamePasswordAuthenticationToken(User.builder()
.username(memberSignUpDto.username())
.password(memberSignUpDto.password())
.roles(Role.USER.name())
.build(),
null, null));
SecurityContextHolder.setContext(emptyContext);
return memberSignUpDto;
}
@AfterEach
public void removeMember(){
SecurityContextHolder.createEmptyContext().setAuthentication(null);
}
/**
* ํ์๊ฐ์
* ํ์๊ฐ์
์ ์์ด๋, ๋น๋ฐ๋ฒํธ, ์ด๋ฆ, ๋ณ๋ช
, ๋์ด๋ฅผ ์
๋ ฅํ์ง ์์ผ๋ฉด ์ค๋ฅ
* ์ด๋ฏธ ์กด์ฌํ๋ ์์ด๋๊ฐ ์์ผ๋ฉด ์ค๋ฅ
* ํ์๊ฐ์
ํ ํ์์ ROLE ์ USER
*
*
*/
@Test
public void ํ์๊ฐ์
_์ฑ๊ณต() throws Exception {
//given
MemberSignUpDto memberSignUpDto = makeMemberSignUpDto();
//when
memberService.signUp(memberSignUpDto);
clear();
//then TODO : ์ฌ๊ธฐ MEMBEREXCEPTION์ผ๋ก ๊ณ ์น๊ธฐ
Member member = memberRepository.findByUsername(memberSignUpDto.username()).orElseThrow(() -> new Exception("ํ์์ด ์์ต๋๋ค"));
assertThat(member.getId()).isNotNull();
assertThat(member.getUsername()).isEqualTo(memberSignUpDto.username());
assertThat(member.getName()).isEqualTo(memberSignUpDto.name());
assertThat(member.getNickName()).isEqualTo(memberSignUpDto.nickName());
assertThat(member.getAge()).isEqualTo(memberSignUpDto.age());
assertThat(member.getRole()).isSameAs(Role.USER);
}
@Test
public void ํ์๊ฐ์
_์คํจ_์์ธ_์์ด๋์ค๋ณต() throws Exception {
//given
MemberSignUpDto memberSignUpDto = makeMemberSignUpDto();
memberService.signUp(memberSignUpDto);
clear();
//when, then TODO : MemberException์ผ๋ก ๊ณ ์ณ์ผ ํจ
assertThat(assertThrows(Exception.class, () -> memberService.signUp(memberSignUpDto)).getMessage()).isEqualTo("์ด๋ฏธ ์กด์ฌํ๋ ์์ด๋์
๋๋ค.");
}
@Test
public void ํ์๊ฐ์
_์คํจ_์
๋ ฅํ์ง์์_ํ๋๊ฐ์์ผ๋ฉด_์ค๋ฅ() throws Exception {
//given
MemberSignUpDto memberSignUpDto1 = new MemberSignUpDto(null,passwordEncoder.encode(PASSWORD),"name","nickNAme",22);
MemberSignUpDto memberSignUpDto2 = new MemberSignUpDto("username",null,"name","nickNAme",22);
MemberSignUpDto memberSignUpDto3 = new MemberSignUpDto("username",passwordEncoder.encode(PASSWORD),null,"nickNAme",22);
MemberSignUpDto memberSignUpDto4 = new MemberSignUpDto("username",passwordEncoder.encode(PASSWORD),"name",null,22);
MemberSignUpDto memberSignUpDto5 = new MemberSignUpDto("username",passwordEncoder.encode(PASSWORD),"name","nickNAme",null);
//when, then
assertThrows(Exception.class, () -> memberService.signUp(memberSignUpDto1));
assertThrows(Exception.class, () -> memberService.signUp(memberSignUpDto2));
assertThrows(Exception.class, () -> memberService.signUp(memberSignUpDto3));
assertThrows(Exception.class, () -> memberService.signUp(memberSignUpDto4));
assertThrows(Exception.class, () -> memberService.signUp(memberSignUpDto5));
}
/**
* ํ์์ ๋ณด์์
* ํ์๊ฐ์
์ ํ์ง ์์ ์ฌ๋์ด ์ ๋ณด์์ ์ ์ค๋ฅ -> ์ํ๋ฆฌํฐ ํํฐ๊ฐ ์์์ ๋ง์์ค๊ฑฐ์
* ์์ด๋๋ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ
* ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ์์๋, ํ์ฌ ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅ๋ฐ์์, ์ผ์นํ ๊ฒฝ์ฐ์๋ง ๋ฐ๊ฟ ์ ์์
* ๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ์์๋ ์ค์ง ๋น๋ฐ๋ฒํธ๋ง ๋ฐ๊ฟ ์ ์์
*
* ๋น๋ฐ๋ฒํธ๊ฐ ์๋ ์ด๋ฆ,๋ณ๋ช
,๋์ด ๋ณ๊ฒฝ ์์๋, 3๊ฐ๋ฅผ ํ๊บผ๋ฒ์ ๋ฐ๊ฟ ์๋ ์๊ณ , ํ,๋๊ฐ๋ง ์ ํํด์ ๋ฐ๊ฟ์๋ ์์
* ์๋ฌด๊ฒ๋ ๋ฐ๋๋๊ฒ ์๋๋ฐ ๋ณ๊ฒฝ์์ฒญ์ ๋ณด๋ด๋ฉด ์ค๋ฅ
*
*/
@Test
public void ํ์์์ _๋น๋ฐ๋ฒํธ์์ _์ฑ๊ณต() throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
//when
String toBePassword = "1234567890!@#!@#";
memberService.updatePassword(PASSWORD, toBePassword);
clear();
//then
Member findMember = memberRepository.findByUsername(memberSignUpDto.username()).orElseThrow(() -> new Exception());
assertThat(findMember.matchPassword(passwordEncoder, toBePassword)).isTrue();
}
@Test
public void ํ์์์ _์ด๋ฆ๋ง์์ () throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
//when
String updateName = "๋ณ๊ฒฝํ ๋์ฉ";
memberService.update(new MemberUpdateDto(Optional.of(updateName),Optional.empty(), Optional.empty()));
clear();
//then
memberRepository.findByUsername(memberSignUpDto.username()).ifPresent((member -> {
assertThat(member.getName()).isEqualTo(updateName);
assertThat(member.getAge()).isEqualTo(memberSignUpDto.age());
assertThat(member.getNickName()).isEqualTo(memberSignUpDto.nickName());
}));
}
@Test
public void ํ์์์ _๋ณ๋ช
๋ง์์ () throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
//when
String updateNickName = "๋ณ๊ฒฝํ ๋์ฉ";
memberService.update(new MemberUpdateDto(Optional.empty(), Optional.of(updateNickName), Optional.empty()));
clear();
//then
memberRepository.findByUsername(memberSignUpDto.username()).ifPresent((member -> {
assertThat(member.getNickName()).isEqualTo(updateNickName);
assertThat(member.getAge()).isEqualTo(memberSignUpDto.age());
assertThat(member.getName()).isEqualTo(memberSignUpDto.name());
}));
}
@Test
public void ํ์์์ _๋์ด๋ง์์ () throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
//when
Integer updateAge = 33;
memberService.update(new MemberUpdateDto(Optional.empty(), Optional.empty(), Optional.of(updateAge)));
clear();
//then
memberRepository.findByUsername(memberSignUpDto.username()).ifPresent((member -> {
assertThat(member.getAge()).isEqualTo(updateAge);
assertThat(member.getNickName()).isEqualTo(memberSignUpDto.nickName());
assertThat(member.getName()).isEqualTo(memberSignUpDto.name());
}));
}
@Test
public void ํ์์์ _์ด๋ฆ๋ณ๋ช
์์ () throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
//when
String updateNickName = "๋ณ๊ฒฝํ ๋์์น";
String updateName = "๋ณ๊ฒฝํ ๋์ฉ";
memberService.update(new MemberUpdateDto(Optional.of(updateName),Optional.of(updateNickName),Optional.empty()));
clear();
//then
memberRepository.findByUsername(memberSignUpDto.username()).ifPresent((member -> {
assertThat(member.getNickName()).isEqualTo(updateNickName);
assertThat(member.getName()).isEqualTo(updateName);
assertThat(member.getAge()).isEqualTo(memberSignUpDto.age());
}));
}
@Test
public void ํ์์์ _์ด๋ฆ๋์ด์์ () throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
//when
Integer updateAge = 33;
String updateName = "๋ณ๊ฒฝํ ๋์ฉ";
memberService.update(new MemberUpdateDto(Optional.of(updateName),Optional.empty(),Optional.of(updateAge)));
clear();
//then
memberRepository.findByUsername(memberSignUpDto.username()).ifPresent((member -> {
assertThat(member.getAge()).isEqualTo(updateAge);
assertThat(member.getName()).isEqualTo(updateName);
assertThat(member.getNickName()).isEqualTo(memberSignUpDto.nickName());
}));
}
@Test
public void ํ์์์ _๋ณ๋ช
๋์ด์์ () throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
//when
Integer updateAge = 33;
String updateNickname = "๋ณ๊ฒฝํ ๋์ฉ";
memberService.update(new MemberUpdateDto(Optional.empty(),Optional.of(updateNickname),Optional.of(updateAge)));
clear();
//then
memberRepository.findByUsername(memberSignUpDto.username()).ifPresent((member -> {
assertThat(member.getAge()).isEqualTo(updateAge);
assertThat(member.getNickName()).isEqualTo(updateNickname);
assertThat(member.getName()).isEqualTo(memberSignUpDto.name());
}));
}
@Test
public void ํ์์์ _์ด๋ฆ๋ณ๋ช
๋์ด์์ () throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
//when
Integer updateAge = 33;
String updateNickname = "๋ณ๊ฒฝํ ๋์ฉ";
String updateName = "๋ณ๊ฒฝํ ๋์ฉ";
memberService.update(new MemberUpdateDto(Optional.of(updateName),Optional.of(updateNickname),Optional.of(updateAge)));
clear();
//then
memberRepository.findByUsername(memberSignUpDto.username()).ifPresent((member -> {
assertThat(member.getAge()).isEqualTo(updateAge);
assertThat(member.getNickName()).isEqualTo(updateNickname);
assertThat(member.getName()).isEqualTo(updateName);
}));
}
/**
* ํ์ํํด
* ๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅ๋ฐ์์ ์ผ์นํ๋ฉด ํํด ๊ฐ๋ฅ
*/
@Test
public void ํ์ํํด() throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
//when
memberService.withdraw(PASSWORD);
//then
assertThat(assertThrows(Exception.class, ()-> memberRepository.findByUsername(memberSignUpDto.username()).orElseThrow(() -> new Exception("ํ์์ด ์์ต๋๋ค"))).getMessage()).isEqualTo("ํ์์ด ์์ต๋๋ค");
}
@Test
public void ํ์ํํด_์คํจ_๋น๋ฐ๋ฒํธ๊ฐ_์ผ์นํ์ง์์() throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
//when, then TODO : MemberException์ผ๋ก ๊ณ ์ณ์ผ ํจ
assertThat(assertThrows(Exception.class ,() -> memberService.withdraw(PASSWORD+"1")).getMessage()).isEqualTo("๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ์ง ์์ต๋๋ค.");
}
@Test
public void ํ์์ ๋ณด์กฐํ() throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
Member member = memberRepository.findByUsername(memberSignUpDto.username()).orElseThrow(() -> new Exception());
clear();
//when
MemberInfoDto info = memberService.getInfo(member.getId());
//then
assertThat(info.getUsername()).isEqualTo(memberSignUpDto.username());
assertThat(info.getName()).isEqualTo(memberSignUpDto.name());
assertThat(info.getAge()).isEqualTo(memberSignUpDto.age());
assertThat(info.getNickName()).isEqualTo(memberSignUpDto.nickName());
}
@Test
public void ๋ด์ ๋ณด์กฐํ() throws Exception {
//given
MemberSignUpDto memberSignUpDto = setMember();
//when
MemberInfoDto myInfo = memberService.getMyInfo();
//then
assertThat(myInfo.getUsername()).isEqualTo(memberSignUpDto.username());
assertThat(myInfo.getName()).isEqualTo(memberSignUpDto.name());
assertThat(myInfo.getAge()).isEqualTo(memberSignUpDto.age());
assertThat(myInfo.getNickName()).isEqualTo(memberSignUpDto.nickName());
}
}
3๊ฐ์ ๋ฉ์๋๋ง ์ดํด๋ณด๊ฒ ์ต๋๋ค.
private MemberSignUpDto makeMemberSignUpDto() {
return new MemberSignUpDto("username",PASSWORD,"name","nickNAme",22);
}
MemberSignUpDto๋ฅผ ๋ฐํํ๋ ๋ฉ์๋์ ๋๋ค. Http์์ฒญ์ ๋ณด๋ด๋ ๊ฒ์ด ์๋๊ธฐ๋๋ฌธ์ ์์๋ก ์์ฑํด ์ฃผ์์ต๋๋ค.
private MemberSignUpDto setMember() throws Exception {
MemberSignUpDto memberSignUpDto1 = makeMemberSignUpDto();
memberService.signUp(memberSignUpDto1);
clear();
SecurityContext emptyContext = SecurityContextHolder.createEmptyContext();
emptyContext.setAuthentication(new UsernamePasswordAuthenticationToken(User.builder()
.username(memberSignUpDto1.username())
.password(memberSignUpDto1.password())
.roles(Role.USER.name())
.build(),
null, null));
SecurityContextHolder.setContext(emptyContext);
return memberSignUpDto1;
}
ํ์๊ฐ์ ์ ์งํํ ํ SecurityContextHolder์ ์ธ์ฆ๋ ํ์์ ๋ณด๋ฅผ ์ ์ฅํ๋ ๋ฉ์๋์ ๋๋ค.
๋ฐํ์ ํ์๊ฐ์ ์ ์ฌ์ฉํ๋ MemberSignUpDto๋ฅผ ๋ฐํํฉ๋๋ค.
@AfterEach
public void removeMember(){
SecurityContextHolder.createEmptyContext().setAuthentication(null);
}
ํ ์คํธ๊ฐ ๋๋ ๋ ๋ง๋ค, SecurityContextHolder์ Authentication(์ธ์ฆ)์ ๋ณด๋ฅผ ๋น์์ค๋๋ค.
ํ์ ์๋น์ค๋ ๊ตฌํ์ด ๋๋ฌ์ต๋๋ค. ์ด์ Controller๋ฅผ ๋ง๋ค๊ณ , ํ ์คํธ๋ฅผ ์งํํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์ ์ฒด ์ฝ๋๋ ๊นํ๋ธ์์ ํ์ธํ์ค ์ ์์ต๋๋ค.
https://github.com/ShinDongHun1/SpringBoot-Board-API