From 7d820d7246394769175fb560ff1625d6bfbe7c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8E=E1=85=AC=E1=84=8C=E1=85=AE=E1=86=AB=E1=84=92?= =?UTF-8?q?=E1=85=A9?= Date: Thu, 12 Oct 2023 02:52:50 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EB=B0=8F?= =?UTF-8?q?=20=EB=AC=B8=EC=84=9C=20=EB=B3=B4=EC=99=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/pium/src/docs/asciidoc/index.adoc | 1 + backend/pium/src/docs/asciidoc/member.adoc | 72 +++++++++++++++++++ .../pium/controller/MemberController.java | 2 +- .../java/com/official/pium/domain/Member.java | 3 +- .../official/pium/service/MemberService.java | 2 + .../pium/controller/AuthControllerTest.java | 2 +- .../pium/controller/MemberControllerTest.java | 66 ++++++++++++++++- .../pium/service/MemberServiceTest.java | 57 +++++++++++++++ 8 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 backend/pium/src/docs/asciidoc/member.adoc diff --git a/backend/pium/src/docs/asciidoc/index.adoc b/backend/pium/src/docs/asciidoc/index.adoc index 5c048f038..686fa6376 100644 --- a/backend/pium/src/docs/asciidoc/index.adoc +++ b/backend/pium/src/docs/asciidoc/index.adoc @@ -10,3 +10,4 @@ include::petPlant.adoc[] include::reminder.adoc[] include::history.adoc[] include::garden.adoc[] +include::member.adoc[] diff --git a/backend/pium/src/docs/asciidoc/member.adoc b/backend/pium/src/docs/asciidoc/member.adoc new file mode 100644 index 000000000..1ec63013f --- /dev/null +++ b/backend/pium/src/docs/asciidoc/member.adoc @@ -0,0 +1,72 @@ +== 사용자(Member) + +=== 로그인 + +==== Request + +include::{snippets}/auth/login/http-request.adoc[] + +==== Response + +include::{snippets}/auth/login/http-response.adoc[] + +=== 로그아웃 + +==== Request + +include::{snippets}/auth/logout/http-request.adoc[] + +==== Response + +include::{snippets}/auth/logout/http-response.adoc[] + +=== 세션확인 + +==== Request + +include::{snippets}/member/checkSession/http-request.adoc[] + +==== Response + +include::{snippets}/member/checkSession/http-response.adoc[] + +=== 회원탈퇴 + +==== Request + +include::{snippets}/member/withdraw/http-request.adoc[] + +==== Response + +include::{snippets}/member/withdraw/http-response.adoc[] + +=== 알림구독 + +==== Request + +include::{snippets}/member/subscribeNotification/http-request.adoc[] + +==== Response + +include::{snippets}/member/subscribeNotification/http-response.adoc[] + +=== 알림구독해지 + +==== Request + +include::{snippets}/member/unSubscribeNotification/http-request.adoc[] + +==== Response + +include::{snippets}/member/unSubscribeNotification/http-response.adoc[] + +=== 알림 구독상태 확인 + +==== Request + +include::{snippets}/member/checkNotification/http-request.adoc[] + +==== Response + +include::{snippets}/member/checkNotification/http-response.adoc[] +include::{snippets}/member/checkNotification/response-body.adoc[] diff --git a/backend/pium/src/main/java/com/official/pium/controller/MemberController.java b/backend/pium/src/main/java/com/official/pium/controller/MemberController.java index 1f81b3e2d..20798203e 100644 --- a/backend/pium/src/main/java/com/official/pium/controller/MemberController.java +++ b/backend/pium/src/main/java/com/official/pium/controller/MemberController.java @@ -64,6 +64,6 @@ public ResponseEntity checkNotificationStatus(@Auth Member member, @Reques @DeleteMapping("/notification") public ResponseEntity delete(@Auth Member member) { memberService.unSubscribeNotification(member); - return ResponseEntity.ok().build(); + return ResponseEntity.noContent().build(); } } diff --git a/backend/pium/src/main/java/com/official/pium/domain/Member.java b/backend/pium/src/main/java/com/official/pium/domain/Member.java index 30ce8c173..e1641e2d6 100644 --- a/backend/pium/src/main/java/com/official/pium/domain/Member.java +++ b/backend/pium/src/main/java/com/official/pium/domain/Member.java @@ -30,8 +30,9 @@ public class Member extends BaseEntity { private String deviceToken; @Builder - public Member(Long kakaoId) { + public Member(Long kakaoId, String deviceToken) { this.kakaoId = kakaoId; + this.deviceToken = deviceToken; } public void updateDeviceToken(String deviceToken) { diff --git a/backend/pium/src/main/java/com/official/pium/service/MemberService.java b/backend/pium/src/main/java/com/official/pium/service/MemberService.java index b524641ac..8c4b95cfd 100644 --- a/backend/pium/src/main/java/com/official/pium/service/MemberService.java +++ b/backend/pium/src/main/java/com/official/pium/service/MemberService.java @@ -42,10 +42,12 @@ public NotificationCheckResponse checkNotification(Member member) { .build(); } + @Transactional public void subscribeNotification(Member member, NotificationSubscribeRequest request) { member.updateDeviceToken(request.getDeviceToken()); } + @Transactional public void unSubscribeNotification(Member member) { member.updateDeviceToken(null); } diff --git a/backend/pium/src/test/java/com/official/pium/controller/AuthControllerTest.java b/backend/pium/src/test/java/com/official/pium/controller/AuthControllerTest.java index 3b4a7693d..760bf8ec4 100644 --- a/backend/pium/src/test/java/com/official/pium/controller/AuthControllerTest.java +++ b/backend/pium/src/test/java/com/official/pium/controller/AuthControllerTest.java @@ -53,7 +53,7 @@ class 로그인_ { mockMvc.perform(post("/login") .queryParam("code", "authorization code") .contentType(APPLICATION_JSON_VALUE)) - .andDo(document("auth/login.html/", + .andDo(document("auth/login/", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), queryParameters( diff --git a/backend/pium/src/test/java/com/official/pium/controller/MemberControllerTest.java b/backend/pium/src/test/java/com/official/pium/controller/MemberControllerTest.java index 4c9d20385..14f8c453d 100644 --- a/backend/pium/src/test/java/com/official/pium/controller/MemberControllerTest.java +++ b/backend/pium/src/test/java/com/official/pium/controller/MemberControllerTest.java @@ -4,6 +4,7 @@ import static org.mockito.Mockito.doNothing; import static org.springframework.restdocs.cookies.CookieDocumentation.requestCookies; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse; import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; @@ -13,9 +14,11 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import com.fasterxml.jackson.databind.ObjectMapper; import com.official.pium.UITest; import com.official.pium.domain.Member; import com.official.pium.service.MemberService; +import com.official.pium.service.dto.NotificationSubscribeRequest; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Nested; @@ -38,6 +41,9 @@ class MemberControllerTest extends UITest { @MockBean private MemberService memberService; + @Autowired + private ObjectMapper objectMapper; + @Nested class 회원_탈퇴_ { @@ -48,7 +54,7 @@ class 회원_탈퇴_ { mockMvc.perform(delete("/members/withdraw") .session(session) .contentType(MediaType.APPLICATION_JSON_VALUE)) - .andDo(document("auth/withdraw/", + .andDo(document("member/withdraw/", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), requestCookies() @@ -80,7 +86,7 @@ class 사용자_세션_ { mockMvc.perform(get("/members/me") .session(session) .contentType(MediaType.APPLICATION_JSON_VALUE)) - .andDo(document("auth/checkSession/", + .andDo(document("member/checkSession/", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), requestCookies() @@ -102,4 +108,60 @@ class 사용자_세션_ { .andDo(print()); } } + + @Nested + class 알림_구독_ { + + @Test + void 확인_정상_요청_시_200_반환() throws Exception { + mockMvc.perform(get("/members/notification") + .session(session) + .contentType(MediaType.APPLICATION_JSON_VALUE)) + .andDo(document("member/checkNotification/", + preprocessRequest(prettyPrint()), + preprocessResponse(prettyPrint()), + requestCookies() + ) + ) + .andExpect(status().isOk()) + .andDo(print()); + } + + @Test + void 등록_정상_요청_시_200_반환() throws Exception { + doNothing().when(memberService).subscribeNotification(any(Member.class), any()); + + mockMvc.perform(post("/members/notification") + .session(session) + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content(objectMapper.writeValueAsString(NotificationSubscribeRequest.builder() + .deviceToken("deviceToken") + .build()))) + .andDo(document("member/subscribeNotification/", + preprocessRequest(prettyPrint()), + preprocessResponse(prettyPrint()), + requestCookies() + ) + ) + .andExpect(status().isOk()) + .andDo(print()); + } + + @Test + void 삭제_정상_요청_시_204_반환() throws Exception { + doNothing().when(memberService).unSubscribeNotification(any(Member.class)); + + mockMvc.perform(delete("/members/notification") + .session(session) + .contentType(MediaType.APPLICATION_JSON_VALUE)) + .andDo(document("member/unSubscribeNotification/", + preprocessRequest(prettyPrint()), + preprocessResponse(prettyPrint()), + requestCookies() + ) + ) + .andExpect(status().isNoContent()) + .andDo(print()); + } + } } diff --git a/backend/pium/src/test/java/com/official/pium/service/MemberServiceTest.java b/backend/pium/src/test/java/com/official/pium/service/MemberServiceTest.java index d7ea0c998..432b284c7 100644 --- a/backend/pium/src/test/java/com/official/pium/service/MemberServiceTest.java +++ b/backend/pium/src/test/java/com/official/pium/service/MemberServiceTest.java @@ -3,20 +3,25 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.SoftAssertions.assertSoftly; +import com.official.pium.config.DatabaseClearExtension; import com.official.pium.domain.Member; import com.official.pium.repository.MemberRepository; import com.official.pium.repository.PetPlantRepository; +import com.official.pium.service.dto.NotificationCheckResponse; +import com.official.pium.service.dto.NotificationSubscribeRequest; import com.official.pium.support.PetPlantSupport; import java.util.Optional; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") +@ExtendWith(DatabaseClearExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT) class MemberServiceTest { @@ -65,4 +70,56 @@ class MemberServiceTest { softly.assertThat(petPlantRepository.findById(petPlant3)).isEmpty(); }); } + + @Test + void 알림_구독_시_사용자의_디바이스_토큰에_값이_존재한다() { + Member member = Member.builder() + .kakaoId(123451L) + .build(); + Member saveMember = memberRepository.save(member); + + memberService.subscribeNotification(saveMember, NotificationSubscribeRequest.builder() + .deviceToken("deviceToken") + .build()); + + assertThat(saveMember.getDeviceToken()).isEqualTo("deviceToken"); + } + + @Test + void 알림_해지_시_사용자의_디바이스_토큰에_값이_존재한다() { + Member member = Member.builder() + .kakaoId(123451L) + .deviceToken("deviceToken") + .build(); + Member saveMember = memberRepository.save(member); + + memberService.unSubscribeNotification(saveMember); + + assertThat(saveMember.getDeviceToken()).isNull(); + } + + @Test + void 사용자가_알림_구독중이면_True() { + Member member = Member.builder() + .kakaoId(123451L) + .deviceToken("deviceToken") + .build(); + Member saveMember = memberRepository.save(member); + + NotificationCheckResponse notificationCheckResponse = memberService.checkNotification(saveMember); + + assertThat(notificationCheckResponse.isSubscribe()).isTrue(); + } + + @Test + void 사용자가_알림_구독중이_아니면_False() { + Member member = Member.builder() + .kakaoId(123451L) + .build(); + Member saveMember = memberRepository.save(member); + + NotificationCheckResponse notificationCheckResponse = memberService.checkNotification(saveMember); + + assertThat(notificationCheckResponse.isSubscribe()).isFalse(); + } }