From 556e1db667e84725da49bb3e4abe2e019fcb487e Mon Sep 17 00:00:00 2001 From: Oleg Kopysov Date: Wed, 25 Oct 2023 00:09:05 +0300 Subject: [PATCH] fix: Auth module refactoring and unit tests extension Signed-off-by: Oleg Kopysov --- .../auth => config}/SecurityConfig.java | 5 +- .../{auth => enums}/OAuthAttributes.java | 5 +- .../auth => service}/OAuthService.java | 4 +- .../lpvs/entity/auth/MemberProfileTest.java | 2 + .../lpvs/entity/auth/OAuthAttributesTest.java | 40 ----------- .../entity/enums/OAuthAttributesTest.java | 69 +++++++++++++++++++ .../auth => service}/OAuthServiceTest.java | 3 +- 7 files changed, 83 insertions(+), 45 deletions(-) rename src/main/java/com/lpvs/{entity/auth => config}/SecurityConfig.java (96%) rename src/main/java/com/lpvs/entity/{auth => enums}/OAuthAttributes.java (97%) rename src/main/java/com/lpvs/{entity/auth => service}/OAuthService.java (96%) delete mode 100644 src/test/java/com/lpvs/entity/auth/OAuthAttributesTest.java create mode 100644 src/test/java/com/lpvs/entity/enums/OAuthAttributesTest.java rename src/test/java/com/lpvs/{entity/auth => service}/OAuthServiceTest.java (97%) diff --git a/src/main/java/com/lpvs/entity/auth/SecurityConfig.java b/src/main/java/com/lpvs/config/SecurityConfig.java similarity index 96% rename from src/main/java/com/lpvs/entity/auth/SecurityConfig.java rename to src/main/java/com/lpvs/config/SecurityConfig.java index b43c2a48..6b9fcd6e 100644 --- a/src/main/java/com/lpvs/entity/auth/SecurityConfig.java +++ b/src/main/java/com/lpvs/config/SecurityConfig.java @@ -5,8 +5,9 @@ * found in the LICENSE file. */ -package com.lpvs.entity.auth; +package com.lpvs.config; +import com.lpvs.service.OAuthService; import lombok.RequiredArgsConstructor; import java.io.IOException; @@ -18,6 +19,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.security.core.Authentication; import org.springframework.security.oauth2.core.user.OAuth2User; import org.springframework.security.config.annotation.web.builders.HttpSecurity; @@ -30,6 +32,7 @@ import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.util.UriComponentsBuilder; +@Configuration @EnableWebSecurity @RequiredArgsConstructor public class SecurityConfig { diff --git a/src/main/java/com/lpvs/entity/auth/OAuthAttributes.java b/src/main/java/com/lpvs/entity/enums/OAuthAttributes.java similarity index 97% rename from src/main/java/com/lpvs/entity/auth/OAuthAttributes.java rename to src/main/java/com/lpvs/entity/enums/OAuthAttributes.java index 01aff4b6..fc26e1f6 100644 --- a/src/main/java/com/lpvs/entity/auth/OAuthAttributes.java +++ b/src/main/java/com/lpvs/entity/enums/OAuthAttributes.java @@ -5,7 +5,9 @@ * found in the LICENSE file. */ -package com.lpvs.entity.auth; +package com.lpvs.entity.enums; + +import com.lpvs.entity.auth.MemberProfile; import java.util.Arrays; import java.util.Map; @@ -22,7 +24,6 @@ public enum OAuthAttributes { NAVER("naver", (attributes) -> { Map response = (Map) attributes.get("response"); - System.out.println(response); MemberProfile memberProfile = new MemberProfile(); memberProfile.setName((String) response.get("name")); memberProfile.setEmail(((String) response.get("email"))); diff --git a/src/main/java/com/lpvs/entity/auth/OAuthService.java b/src/main/java/com/lpvs/service/OAuthService.java similarity index 96% rename from src/main/java/com/lpvs/entity/auth/OAuthService.java rename to src/main/java/com/lpvs/service/OAuthService.java index 38fb8b0b..8ae5c88e 100644 --- a/src/main/java/com/lpvs/entity/auth/OAuthService.java +++ b/src/main/java/com/lpvs/service/OAuthService.java @@ -5,9 +5,11 @@ * found in the LICENSE file. */ -package com.lpvs.entity.auth; +package com.lpvs.service; import com.lpvs.entity.LPVSMember; +import com.lpvs.entity.auth.MemberProfile; +import com.lpvs.entity.enums.OAuthAttributes; import com.lpvs.repository.LPVSMemberRepository; import lombok.RequiredArgsConstructor; diff --git a/src/test/java/com/lpvs/entity/auth/MemberProfileTest.java b/src/test/java/com/lpvs/entity/auth/MemberProfileTest.java index fc2550b5..a85a5ff5 100644 --- a/src/test/java/com/lpvs/entity/auth/MemberProfileTest.java +++ b/src/test/java/com/lpvs/entity/auth/MemberProfileTest.java @@ -32,10 +32,12 @@ public void testToMember() { profile.setName("John"); profile.setEmail("john@example.com"); profile.setProvider("OAuth2"); + profile.setNickname("Johnny"); LPVSMember member = profile.toMember(); assertEquals("John", member.getName()); assertEquals("john@example.com", member.getEmail()); assertEquals("OAuth2", member.getProvider()); + assertEquals("Johnny", profile.getNickname()); } } diff --git a/src/test/java/com/lpvs/entity/auth/OAuthAttributesTest.java b/src/test/java/com/lpvs/entity/auth/OAuthAttributesTest.java deleted file mode 100644 index b0e928e1..00000000 --- a/src/test/java/com/lpvs/entity/auth/OAuthAttributesTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. - *

- * Use of this source code is governed by a MIT license that can be - * found in the LICENSE file. - */ - -package com.lpvs.entity.auth; - -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.util.HashMap; -import java.util.Map; - -public class OAuthAttributesTest { - - @Test - public void testExtractOAuthAttributes() { - Map attributes = new HashMap() {{ - put("name", "testName"); - put("email", "testEmail"); - }};; - MemberProfile profile = OAuthAttributes.extract("google", attributes); - assertEquals("testName", profile.getName()); - assertEquals("testEmail", profile.getEmail()); - } - - @Test - public void testExtractOAuthAttributesUnknownProvider() { - Map attributes = new HashMap() {{ - put("name", "testName"); - put("email", "testEmail"); - }};; - assertThrows(IllegalArgumentException.class, () -> { - OAuthAttributes.extract("unknown", attributes); - }); - } -} diff --git a/src/test/java/com/lpvs/entity/enums/OAuthAttributesTest.java b/src/test/java/com/lpvs/entity/enums/OAuthAttributesTest.java new file mode 100644 index 00000000..6ea35060 --- /dev/null +++ b/src/test/java/com/lpvs/entity/enums/OAuthAttributesTest.java @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. + *

+ * Use of this source code is governed by a MIT license that can be + * found in the LICENSE file. + */ + +package com.lpvs.entity.enums; + +import com.lpvs.entity.auth.MemberProfile; +import com.lpvs.entity.enums.OAuthAttributes; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.HashMap; +import java.util.Map; + +public class OAuthAttributesTest { + + @Test + public void testExtractOAuthAttributes() { + Map attributesSub = new HashMap() {{ + put("name", "testName"); + put("nickname", "testName"); + put("email", "testEmail"); + }}; + + Map attributesKakao = new HashMap() {{ + put("email", "testEmail"); + put("profile", attributesSub); + }}; + + Map attributes = new HashMap() {{ + put("name", "testName"); + put("email", "testEmail"); + put("login", "testEmail"); + put("response", attributesSub); + put("kakao_account", attributesKakao); + }}; + + MemberProfile profileGoogle = OAuthAttributes.extract("google", attributes); + assertEquals("testName", profileGoogle.getName()); + assertEquals("testEmail", profileGoogle.getEmail()); + + MemberProfile profileNaver = OAuthAttributes.extract("naver", attributes); + assertEquals("testName", profileNaver.getName()); + assertEquals("testEmail", profileNaver.getEmail()); + + MemberProfile profileKakao = OAuthAttributes.extract("kakao", attributes); + assertEquals("testName", profileKakao.getName()); + assertEquals("testEmail", profileKakao.getEmail()); + + MemberProfile profileGithub = OAuthAttributes.extract("github", attributes); + assertEquals("testName", profileGithub.getName()); + assertEquals("testEmail", profileGithub.getEmail()); + } + + @Test + public void testExtractOAuthAttributesUnknownProvider() { + Map attributes = new HashMap() {{ + put("name", "testName"); + put("email", "testEmail"); + }};; + assertThrows(IllegalArgumentException.class, () -> { + OAuthAttributes.extract("unknown", attributes); + }); + } +} diff --git a/src/test/java/com/lpvs/entity/auth/OAuthServiceTest.java b/src/test/java/com/lpvs/service/OAuthServiceTest.java similarity index 97% rename from src/test/java/com/lpvs/entity/auth/OAuthServiceTest.java rename to src/test/java/com/lpvs/service/OAuthServiceTest.java index e5542b2e..6ee0dd23 100644 --- a/src/test/java/com/lpvs/entity/auth/OAuthServiceTest.java +++ b/src/test/java/com/lpvs/service/OAuthServiceTest.java @@ -5,10 +5,11 @@ * found in the LICENSE file. */ -package com.lpvs.entity.auth; +package com.lpvs.service; import com.lpvs.repository.LPVSMemberRepository; +import com.lpvs.service.OAuthService; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.security.core.authority.SimpleGrantedAuthority;