-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from kakao-tech-campus-2nd-step3/fix/95-jwt-auth
fix: JWT ์ธ์ฆ ๋ฌธ์ ํด๊ฒฐ ๋ฐ ์ถ๊ฐ ํ ์คํธ ์ฝ๋ ์์ฑ
- Loading branch information
Showing
12 changed files
with
830 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/test/java/com/potatocake/everymoment/controller/NotificationControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.potatocake.everymoment.controller; | ||
|
||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.then; | ||
import static org.mockito.BDDMockito.willDoNothing; | ||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; | ||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.potatocake.everymoment.dto.response.NotificationListResponse; | ||
import com.potatocake.everymoment.entity.Member; | ||
import com.potatocake.everymoment.security.MemberDetails; | ||
import com.potatocake.everymoment.service.NotificationService; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
@WebMvcTest(NotificationController.class) | ||
class NotificationControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private NotificationService notificationService; | ||
|
||
@Test | ||
@DisplayName("์๋ฆผ ๋ชฉ๋ก์ด ์ฑ๊ณต์ ์ผ๋ก ์กฐํ๋๋ค.") | ||
void should_GetNotifications_When_ValidRequest() throws Exception { | ||
// given | ||
Long memberId = 1L; | ||
Member member = Member.builder() | ||
.id(memberId) | ||
.number(1234L) | ||
.nickname("testUser") | ||
.build(); | ||
MemberDetails memberDetails = new MemberDetails(member); | ||
|
||
List<NotificationListResponse> responses = List.of( | ||
NotificationListResponse.builder() | ||
.id(1L) | ||
.content("Notification 1") | ||
.type("TEST1") | ||
.targetId(1L) | ||
.isRead(false) | ||
.createdAt(LocalDateTime.now()) | ||
.build(), | ||
NotificationListResponse.builder() | ||
.id(2L) | ||
.content("Notification 2") | ||
.type("TEST2") | ||
.targetId(2L) | ||
.isRead(true) | ||
.createdAt(LocalDateTime.now()) | ||
.build() | ||
); | ||
|
||
given(notificationService.getNotifications(memberId)).willReturn(responses); | ||
|
||
// when | ||
ResultActions result = mockMvc.perform(get("/api/notifications") | ||
.with(user(memberDetails))); | ||
|
||
// then | ||
result.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value(200)) | ||
.andExpect(jsonPath("$.message").value("success")) | ||
.andExpect(jsonPath("$.info").isArray()) | ||
.andExpect(jsonPath("$.info[0].content").value("Notification 1")) | ||
.andExpect(jsonPath("$.info[0].read").value(false)) | ||
.andExpect(jsonPath("$.info[1].content").value("Notification 2")) | ||
.andExpect(jsonPath("$.info[1].read").value(true)); | ||
|
||
then(notificationService).should().getNotifications(memberId); | ||
} | ||
|
||
@Test | ||
@DisplayName("์๋ฆผ์ด ์ฑ๊ณต์ ์ผ๋ก ์ฝ์ ์ฒ๋ฆฌ๋๋ค.") | ||
void should_UpdateNotification_When_ValidId() throws Exception { | ||
// given | ||
Long memberId = 1L; | ||
Long notificationId = 1L; | ||
Member member = Member.builder() | ||
.id(memberId) | ||
.number(1234L) | ||
.nickname("testUser") | ||
.build(); | ||
MemberDetails memberDetails = new MemberDetails(member); | ||
|
||
willDoNothing().given(notificationService).updateNotification(memberId, notificationId); | ||
|
||
// when | ||
ResultActions result = mockMvc.perform(patch("/api/notifications/{notificationId}", notificationId) | ||
.with(user(memberDetails)) | ||
.with(csrf())); | ||
|
||
// then | ||
result.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value(200)) | ||
.andExpect(jsonPath("$.message").value("success")); | ||
|
||
then(notificationService).should().updateNotification(eq(memberId), eq(notificationId)); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/com/potatocake/everymoment/entity/NotificationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.potatocake.everymoment.entity; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class NotificationTest { | ||
|
||
@Test | ||
@DisplayName("์๋ฆผ์ด ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋๋ค.") | ||
void should_CreateNotification_When_ValidInput() { | ||
// given | ||
Member member = Member.builder() | ||
.id(1L) | ||
.build(); | ||
|
||
// when | ||
Notification notification = Notification.builder() | ||
.member(member) | ||
.content("Test notification") | ||
.type("TEST") | ||
.targetId(1L) | ||
.isRead(false) | ||
.build(); | ||
|
||
// then | ||
assertThat(notification.getMember()).isEqualTo(member); | ||
assertThat(notification.getContent()).isEqualTo("Test notification"); | ||
assertThat(notification.getType()).isEqualTo("TEST"); | ||
assertThat(notification.getTargetId()).isEqualTo(1L); | ||
assertThat(notification.isRead()).isFalse(); | ||
} | ||
|
||
@Test | ||
@DisplayName("์๋ฆผ์ด ์ฑ๊ณต์ ์ผ๋ก ์ฝ์ ์ฒ๋ฆฌ๋๋ค.") | ||
void should_MarkAsRead_When_UpdateIsRead() { | ||
// given | ||
Notification notification = Notification.builder() | ||
.content("Test notification") | ||
.isRead(false) | ||
.build(); | ||
|
||
// when | ||
notification.updateIsRead(); | ||
|
||
// then | ||
assertThat(notification.isRead()).isTrue(); | ||
} | ||
|
||
} |
120 changes: 120 additions & 0 deletions
120
src/test/java/com/potatocake/everymoment/repository/NotificationRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.potatocake.everymoment.repository; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.potatocake.everymoment.entity.Member; | ||
import com.potatocake.everymoment.entity.Notification; | ||
import java.util.List; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
@TestPropertySource(properties = "spring.jpa.hibernate.ddl-auto=create-drop") | ||
@DataJpaTest | ||
class NotificationRepositoryTest { | ||
|
||
@Autowired | ||
private NotificationRepository notificationRepository; | ||
|
||
@Autowired | ||
private MemberRepository memberRepository; | ||
|
||
@Test | ||
@DisplayName("์๋ฆผ์ด ์ฑ๊ณต์ ์ผ๋ก ์ ์ฅ๋๋ค.") | ||
void should_SaveNotification_When_ValidEntity() { | ||
// given | ||
Member member = Member.builder() | ||
.number(1234L) | ||
.nickname("testUser") | ||
.profileImageUrl("https://example.com/profile.jpg") | ||
.build(); | ||
Member savedMember = memberRepository.save(member); | ||
|
||
Notification notification = Notification.builder() | ||
.member(savedMember) | ||
.content("Test notification") | ||
.type("TEST") | ||
.targetId(1L) | ||
.isRead(false) | ||
.build(); | ||
|
||
// when | ||
Notification savedNotification = notificationRepository.save(notification); | ||
|
||
// then | ||
assertThat(savedNotification.getId()).isNotNull(); | ||
assertThat(savedNotification.getContent()).isEqualTo("Test notification"); | ||
assertThat(savedNotification.getMember()).isEqualTo(savedMember); | ||
} | ||
|
||
@Test | ||
@DisplayName("ํ์์ ์๋ฆผ ๋ชฉ๋ก์ด ์ฑ๊ณต์ ์ผ๋ก ์กฐํ๋๋ค.") | ||
void should_FindNotifications_When_FilteringByMemberId() { | ||
// given | ||
Member member = Member.builder() | ||
.number(1234L) | ||
.nickname("testUser") | ||
.profileImageUrl("https://example.com/profile.jpg") | ||
.build(); | ||
Member savedMember = memberRepository.save(member); | ||
|
||
List<Notification> notifications = List.of( | ||
Notification.builder() | ||
.member(savedMember) | ||
.content("Notification 1") | ||
.type("TEST1") | ||
.targetId(1L) | ||
.build(), | ||
Notification.builder() | ||
.member(savedMember) | ||
.content("Notification 2") | ||
.type("TEST2") | ||
.targetId(2L) | ||
.build() | ||
); | ||
|
||
notificationRepository.saveAll(notifications); | ||
|
||
// when | ||
List<Notification> foundNotifications = notificationRepository | ||
.findAllByMemberId(savedMember.getId()); | ||
|
||
// then | ||
assertThat(foundNotifications).hasSize(2); | ||
assertThat(foundNotifications) | ||
.extracting("content") | ||
.containsExactlyInAnyOrder("Notification 1", "Notification 2"); | ||
} | ||
|
||
@Test | ||
@DisplayName("์๋ฆผ์ด ์ฑ๊ณต์ ์ผ๋ก ์ญ์ ๋๋ค.") | ||
void should_DeleteNotification_When_ValidEntity() { | ||
// given | ||
Member member = Member.builder() | ||
.number(1234L) | ||
.nickname("testUser") | ||
.profileImageUrl("https://example.com/profile.jpg") | ||
.build(); | ||
Member savedMember = memberRepository.save(member); | ||
|
||
Notification notification = Notification.builder() | ||
.member(savedMember) | ||
.content("Test notification") | ||
.type("TEST") | ||
.targetId(1L) | ||
.build(); | ||
|
||
Notification savedNotification = notificationRepository.save(notification); | ||
|
||
// when | ||
notificationRepository.delete(savedNotification); | ||
|
||
// then | ||
List<Notification> remainingNotifications = notificationRepository | ||
.findAllByMemberId(savedMember.getId()); | ||
assertThat(remainingNotifications).isEmpty(); | ||
} | ||
|
||
} |
Oops, something went wrong.