This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#141] add tests for loginEvent und listener
- Loading branch information
Showing
4 changed files
with
141 additions
and
42 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/main/java/de/bonndan/nivio/security/OAuth2LoginEvent.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,15 @@ | ||
package de.bonndan.nivio.security; | ||
|
||
import org.springframework.context.ApplicationEvent; | ||
|
||
public class OAuth2LoginEvent extends ApplicationEvent { | ||
public OAuth2LoginEvent(CustomOAuth2User customOAuth2User) { | ||
super(customOAuth2User); | ||
} | ||
|
||
@Override | ||
public CustomOAuth2User getSource() { | ||
return (CustomOAuth2User) source; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/de/bonndan/nivio/security/Oauth2LoginEventListener.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,45 @@ | ||
package de.bonndan.nivio.security; | ||
|
||
import de.bonndan.nivio.appuser.AppUser; | ||
import de.bonndan.nivio.appuser.AppUserRepository; | ||
import de.bonndan.nivio.appuser.AppUserRole; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Optional; | ||
|
||
import static io.swagger.v3.oas.integration.StringOpenApiConfigurationLoader.LOGGER; | ||
|
||
@Service | ||
public class Oauth2LoginEventListener { | ||
|
||
private final AppUserRepository appUserRepository; | ||
|
||
public Oauth2LoginEventListener(AppUserRepository appUserRepository) { | ||
this.appUserRepository = appUserRepository; | ||
} | ||
|
||
@EventListener(OAuth2LoginEvent.class) | ||
public void onLogin(final OAuth2LoginEvent oAuth2LoginEvent) { | ||
|
||
CustomOAuth2User customOAuth2User = oAuth2LoginEvent.getSource(); | ||
Optional<AppUser> appUser = appUserRepository.findByExternalId(customOAuth2User.getExternalId()); | ||
|
||
if (appUser.isEmpty()) { | ||
LOGGER.info("No user found, generating profile for {}", customOAuth2User.getExternalId()); | ||
AppUser newAppUser = new AppUser(); | ||
newAppUser.setName(customOAuth2User.getName()); | ||
newAppUser.setAlias(customOAuth2User.getAlias()); | ||
newAppUser.setAvatarUrl(customOAuth2User.getAvatarUrl()); | ||
newAppUser.setAppUserRole(AppUserRole.USER); | ||
newAppUser.setLocked(false); | ||
newAppUser.setEnabled(true); | ||
newAppUser.setExternalId(customOAuth2User.getExternalId()); | ||
newAppUser.setIdp(customOAuth2User.getIdp()); | ||
|
||
appUserRepository.save(newAppUser); | ||
} | ||
|
||
} | ||
|
||
} |
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
81 changes: 81 additions & 0 deletions
81
src/test/java/de/bonndan/nivio/security/Oauth2LoginEventAndListenerTest.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,81 @@ | ||
package de.bonndan.nivio.security; | ||
|
||
import de.bonndan.nivio.appuser.AppUserRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
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.context.ApplicationEventPublisher; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@DataJpaTest | ||
class Oauth2LoginEventAndListenerTest { | ||
|
||
private OAuth2User oAuth2User; | ||
|
||
private final String name = "Mary"; | ||
private final String login = "foo"; | ||
private final String avatarUrl = "https://www.avatar.com"; | ||
private final String externalId = "123"; | ||
private final String idp = "github"; | ||
private Collection<OAuth2UserAuthority> authorities; | ||
private CustomOAuth2User customOAuth2User; | ||
private ApplicationEventPublisher applicationEventPublisher; | ||
OAuth2LoginEvent oAuth2LoginEvent; | ||
Oauth2LoginEventListener oauth2LoginEventListener; | ||
|
||
@Autowired | ||
private AppUserRepository appUserRepository; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
oAuth2User = mock(OAuth2User.class); | ||
when(oAuth2User.getAttribute("login")).thenReturn(login); | ||
when(oAuth2User.getAttribute("avatar_url")).thenReturn(avatarUrl); | ||
when(oAuth2User.getAttribute("id")).thenReturn(externalId); | ||
when(oAuth2User.getAttribute("name")).thenReturn(name); | ||
|
||
when(oAuth2User.getAttributes()).thenReturn(Map.of()); | ||
|
||
Map<String, Object> authorityAttributes = Map.of("key", new Object()); | ||
OAuth2UserAuthority grantedAuthority = new OAuth2UserAuthority(authorityAttributes); | ||
authorities = List.of(grantedAuthority); | ||
doReturn(authorities).when(oAuth2User).getAuthorities(); | ||
customOAuth2User = CustomOAuth2UserService.fromGitHubUser(oAuth2User, "login", "name"); | ||
|
||
oAuth2LoginEvent = new OAuth2LoginEvent(customOAuth2User); | ||
oauth2LoginEventListener = new Oauth2LoginEventListener(appUserRepository); | ||
} | ||
|
||
@Test | ||
void onLogin() { | ||
|
||
// when | ||
oauth2LoginEventListener.onLogin(oAuth2LoginEvent); | ||
|
||
// then | ||
assertThat(appUserRepository.findByExternalId(customOAuth2User.getExternalId()).isPresent()); | ||
assertThat(appUserRepository.findByExternalId(customOAuth2User.getExternalId()).equals(customOAuth2User)); | ||
|
||
} | ||
|
||
@Test | ||
void getSource() { | ||
|
||
System.out.println("HUHU: " + oAuth2LoginEvent.getSource()); | ||
|
||
// then | ||
assertThat(oAuth2LoginEvent.getSource()).isNotNull(); | ||
assertThat(oAuth2LoginEvent.getSource()).isEqualTo(customOAuth2User); | ||
|
||
} | ||
} |