Skip to content

Commit

Permalink
fix: Auth module refactoring and unit tests extension (#261)
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov authored Oct 25, 2023
1 parent 36b0eae commit bb48d98
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -30,6 +32,7 @@
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.UriComponentsBuilder;

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +24,6 @@ public enum OAuthAttributes {

NAVER("naver", (attributes) -> {
Map<String, Object> response = (Map<String, Object>) attributes.get("response");
System.out.println(response);
MemberProfile memberProfile = new MemberProfile();
memberProfile.setName((String) response.get("name"));
memberProfile.setEmail(((String) response.get("email")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/lpvs/entity/auth/MemberProfileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ public void testToMember() {
profile.setName("John");
profile.setEmail("[email protected]");
profile.setProvider("OAuth2");
profile.setNickname("Johnny");
LPVSMember member = profile.toMember();

assertEquals("John", member.getName());
assertEquals("[email protected]", member.getEmail());
assertEquals("OAuth2", member.getProvider());
assertEquals("Johnny", profile.getNickname());
}
}
40 changes: 0 additions & 40 deletions src/test/java/com/lpvs/entity/auth/OAuthAttributesTest.java

This file was deleted.

69 changes: 69 additions & 0 deletions src/test/java/com/lpvs/entity/enums/OAuthAttributesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved.
* <p>
* 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<String, Object> attributesSub = new HashMap<String, Object>() {{
put("name", "testName");
put("nickname", "testName");
put("email", "testEmail");
}};

Map<String, Object> attributesKakao = new HashMap<String, Object>() {{
put("email", "testEmail");
put("profile", attributesSub);
}};

Map<String, Object> attributes = new HashMap<String, Object>() {{
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<String, Object> attributes = new HashMap<String, Object>() {{
put("name", "testName");
put("email", "testEmail");
}};;
assertThrows(IllegalArgumentException.class, () -> {
OAuthAttributes.extract("unknown", attributes);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bb48d98

Please sign in to comment.