From bd07c8df14df9e011f35eb0add88c5ac4a4dd8be Mon Sep 17 00:00:00 2001 From: haerong22 Date: Wed, 25 Jan 2023 03:26:53 +0900 Subject: [PATCH] #16 board: kakao oauth - response dto, test --- .../dto/security/KakaoOAuth2Response.java | 58 ++++++++++++++++++ .../dto/security/KakaoOAuth2ResponseTest.java | 60 +++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 board/src/main/java/com/example/board/dto/security/KakaoOAuth2Response.java create mode 100644 board/src/test/java/com/example/board/dto/security/KakaoOAuth2ResponseTest.java diff --git a/board/src/main/java/com/example/board/dto/security/KakaoOAuth2Response.java b/board/src/main/java/com/example/board/dto/security/KakaoOAuth2Response.java new file mode 100644 index 00000000..b84c0c69 --- /dev/null +++ b/board/src/main/java/com/example/board/dto/security/KakaoOAuth2Response.java @@ -0,0 +1,58 @@ +package com.example.board.dto.security; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.Map; +@SuppressWarnings("unchecked") +public record KakaoOAuth2Response( + Long id, + LocalDateTime connectedAt, + Map properties, + KakaoAccount kakaoAccount +) { + public record KakaoAccount( + Boolean profileNicknameNeedsAgreement, + Profile profile, + Boolean hasEmail, + Boolean emailNeedsAgreement, + Boolean isEmailValid, + Boolean isEmailVerified, + String email + ) { + public record Profile(String nickname) { + public static Profile from(Map attributes) { + return new Profile(String.valueOf(attributes.get("nickname"))); + } + } + + public static KakaoAccount from(Map attributes) { + return new KakaoAccount( + Boolean.valueOf(String.valueOf(attributes.get("profile_nickname_needs_agreement"))), + Profile.from((Map) attributes.get("profile")), + Boolean.valueOf(String.valueOf(attributes.get("has_email"))), + Boolean.valueOf(String.valueOf(attributes.get("email_needs_agreement"))), + Boolean.valueOf(String.valueOf(attributes.get("is_email_valid"))), + Boolean.valueOf(String.valueOf(attributes.get("is_email_verified"))), + String.valueOf(attributes.get("email")) + ); + } + + public String nickname() { return this.profile().nickname(); } + } + + public static KakaoOAuth2Response from(Map attributes) { + return new KakaoOAuth2Response( + Long.valueOf(String.valueOf(attributes.get("id"))), + LocalDateTime.parse( + String.valueOf(attributes.get("connected_at")), + DateTimeFormatter.ISO_INSTANT.withZone(ZoneId.systemDefault()) + ), + (Map) attributes.get("properties"), + KakaoAccount.from((Map) attributes.get("kakao_account")) + ); + } + + public String email() { return this.kakaoAccount().email(); } + public String nickname() { return this.kakaoAccount().nickname(); } +} \ No newline at end of file diff --git a/board/src/test/java/com/example/board/dto/security/KakaoOAuth2ResponseTest.java b/board/src/test/java/com/example/board/dto/security/KakaoOAuth2ResponseTest.java new file mode 100644 index 00000000..0fbace0b --- /dev/null +++ b/board/src/test/java/com/example/board/dto/security/KakaoOAuth2ResponseTest.java @@ -0,0 +1,60 @@ +package com.example.board.dto.security; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("DTO - Kakao OAuth 2.0 인증 응답 데이터 테스트") +class KakaoOAuth2ResponseTest { + + private final ObjectMapper mapper = new ObjectMapper(); + + @DisplayName("인증 결과를 Map(deserialized json)으로 받으면, 카카오 인증 응답 객체로 변환한다.") + @Test + void givenMapFromJson_whenInstantiating_thenReturnsKakaoResponseObject() throws Exception { + // Given + String serializedResponse = """ + { + "id": 1234567890, + "connected_at": "2022-01-02T00:12:34Z", + "properties": { + "nickname": "홍길동" + }, + "kakao_account": { + "profile_nickname_needs_agreement": false, + "profile": { + "nickname": "홍길동" + }, + "has_email": true, + "email_needs_agreement": false, + "is_email_valid": true, + "is_email_verified": true, + "email": "test@gmail.com" + } + } + """; + Map attributes = mapper.readValue(serializedResponse, new TypeReference<>() {}); + + // When + KakaoOAuth2Response result = KakaoOAuth2Response.from(attributes); + + // Then + assertThat(result) + .hasFieldOrPropertyWithValue("id", 1234567890L) + .hasFieldOrPropertyWithValue("connectedAt", ZonedDateTime.of(2022, 1, 2, 0, 12, 34, 0, ZoneOffset.UTC) + .withZoneSameInstant(ZoneId.systemDefault()) + .toLocalDateTime() + ) + .hasFieldOrPropertyWithValue("kakaoAccount.profile.nickname", "홍길동") + .hasFieldOrPropertyWithValue("kakaoAccount.email", "test@gmail.com"); + } + +} \ No newline at end of file