Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
[#141] add tests for loginEvent und listener
Browse files Browse the repository at this point in the history
  • Loading branch information
jenarp committed Jan 26, 2022
1 parent 9fef348 commit 58caef4
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 42 deletions.
15 changes: 15 additions & 0 deletions src/main/java/de/bonndan/nivio/security/OAuth2LoginEvent.java
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;
}

}
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);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,46 +77,4 @@ void fromGitHubUserWithMissingNameFallsBackToLogin() {
assertThat(customOAuth2User.getName()).isEqualTo(login);
}

@Test
void loadUser() {

// given
AuthConfigProperties authConfigProperties = new AuthConfigProperties();
OAuth2UserRequest userRequest;
userRequest = mock(OAuth2UserRequest.class);
applicationEventPublisher = mock(ApplicationEventPublisher.class);
CustomOAuth2UserService customOAuth2UserService = new CustomOAuth2UserService(authConfigProperties, applicationEventPublisher);

// when
customOAuth2UserService.loadUser(userRequest);


// then


// customOAuth2User = CustomOAuth2UserService.fromGitHubUser(oAuth2User, "login", "name");

// AppUser appUser = new AppUser();

// appUser.setName(customOAuth2User.getName());
// appUser.setAlias(customOAuth2User.getAlias());
// appUser.setAvatarUrl(customOAuth2User.getAvatarUrl());
// appUser.setAppUserRole(AppUserRole.USER);
// appUser.setLocked(false);
// appUser.setEnabled(true);
// appUser.setExternalId(customOAuth2User.getExternalId());
// appUser.setIdp(customOAuth2User.getIdp());

// then
// assertThat(appUser.getName()).isEqualTo(name);
// assertThat(appUser.getAlias()).isEqualTo(login);
// assertThat(appUser.getAvatarUrl()).isEqualTo(avatarUrl);
// assertThat(appUser.getAppUserRole()).isEqualTo(AppUserRole.USER);
// assertThat(appUser.getLocked()).isFalse();
// assertThat(appUser.getEnabled()).isTrue();
// assertThat(appUser.getExternalId()).isEqualTo(externalId);
// assertThat(appUser.getIdp()).isEqualTo(idp);

}

}
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);

}
}

0 comments on commit 58caef4

Please sign in to comment.