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

Commit

Permalink
[#141] improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jenarp committed Jan 31, 2022
1 parent 3c736f9 commit fc04af6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public CustomOAuth2User(@NonNull final String externalId,
this.alias = Objects.requireNonNull(alias, "alias must not be null");
this.attributes = Objects.requireNonNull(attributes, "attributes must not be null");
this.authorities = Objects.requireNonNull(authorities, "authorities must not be null");
this.idProvider = Objects.requireNonNull(idProvider, "idp must not be null");
this.idProvider = Objects.requireNonNull(idProvider, "idProvider must not be null");
this.name = name;
this.avatarUrl = avatarUrl;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ public CustomOAuth2UserService(AuthConfigProperties authConfigProperties, Applic
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2User user = super.loadUser(userRequest);
try {
CustomOAuth2User customOAuth2User = fromGitHubUser(user, authConfigProperties.getGithubAliasAttribute(), authConfigProperties.getGithubNameAttribute());
applicationEventPublisher.publishEvent(new OAuth2LoginEvent(customOAuth2User));
return customOAuth2User;
} catch (NullPointerException e) {
throw new OAuth2AuthenticationException(String.format("Failed to create custom user: %s", e.getMessage()));
}
CustomOAuth2User customOAuth2User = fromGitHubUser(user, authConfigProperties.getGithubAliasAttribute(), authConfigProperties.getGithubNameAttribute());
applicationEventPublisher.publishEvent(new OAuth2LoginEvent(customOAuth2User));
return customOAuth2User;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
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.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
Expand All @@ -12,7 +15,7 @@
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

@DataJpaTest
Expand Down Expand Up @@ -53,17 +56,49 @@ public void setup() {
}

@Test
void onLogin() {
@DisplayName("Test if user is not present before login")
void noExistingUser() {

// given
var user = appUserRepository.findByExternalIdAndIdProvider(customOAuth2User.getExternalId(), customOAuth2User.getIdProvider());

user.ifPresent(user1 -> appUserRepository.delete(user1));

// when
oauth2LoginEventListener.onLogin(oAuth2LoginEvent);
user = appUserRepository.findByExternalIdAndIdProvider(customOAuth2User.getExternalId(), customOAuth2User.getIdProvider());

// then
assertThat(user).isPresent();

}

@Test
@DisplayName("Test if user is present before login")
void existingUser() {

// given
var user = appUserRepository.findByExternalIdAndIdProvider(customOAuth2User.getExternalId(), customOAuth2User.getIdProvider());

if (user.isEmpty()) {
AppUser appUser = new AppUser();
appUser.setExternalId(externalId);
appUser.setIdProvider("github");
appUser.setAlias(login);
appUser.setAppUserRole(AppUserRole.USER);
appUserRepository.save(appUser);
}

// when
oauth2LoginEventListener.onLogin(oAuth2LoginEvent);
user = appUserRepository.findByExternalIdAndIdProvider(customOAuth2User.getExternalId(), customOAuth2User.getIdProvider());

// then
assertThat(appUserRepository.findByExternalIdAndIdProvider(customOAuth2User.getExternalId(), customOAuth2User.getIdProvider()).isPresent());
assertThat(appUserRepository.findByExternalIdAndIdProvider(customOAuth2User.getExternalId(), customOAuth2User.getIdProvider()).equals(customOAuth2User));
assertThat(user).isPresent();

}


@Test
void getSource() {

Expand Down

0 comments on commit fc04af6

Please sign in to comment.