Skip to content

Commit

Permalink
🔥 : remove creation of user in UserControllerAdvice (done in SuccessH…
Browse files Browse the repository at this point in the history
…andler)
  • Loading branch information
cdubuisson committed Oct 16, 2019
1 parent 09e61de commit e0a30bc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,29 @@
@ControllerAdvice
public class UserControllerAdvice {

@Autowired
private UserRepository userRepository;

@Autowired
public UserControllerAdvice(UserRepository userRepository) {
this.userRepository = userRepository;
}

@ModelAttribute
public User user(Authentication authentication){
public User user(Authentication authentication) {
// in case of anonymous access (like healthcheck)
if (authentication == null){
if (authentication == null) {
return null;
}
if (! userRepository.existsById(authentication.getName())){
// creating user
var user = new User(authentication.getName(), null);
userRepository.save(user);
return user;
}
return userRepository.findById(authentication.getName()).orElseThrow();
}

@ModelAttribute
public Team userTeam(Authentication authentication, @ModelAttribute User user){
public Team userTeam(Authentication authentication, @ModelAttribute User user) {
// in case of anonymous access (like healthcheck)
if (authentication == null){
if (authentication == null) {
return null;
}
return user.getTeam();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class TeamsRestControllerIT {

@Container
private static MongoContainer mongoContainer = new MongoContainer()
.withScript("src/test/resources/db/00_team.js");
.withScript("src/test/resources/db/00_team.js")
.withScript("src/test/resources/db/10_user.js");

@Autowired
private TeamsRestController teamsRestController;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.codeka.gaia.teams.controller;

import io.codeka.gaia.teams.Team;
import io.codeka.gaia.teams.User;
import io.codeka.gaia.teams.repository.UserRepository;
import org.junit.jupiter.api.Test;
Expand All @@ -12,6 +13,7 @@
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
Expand All @@ -27,38 +29,41 @@ class UserControllerAdviceTest {
private UserControllerAdvice userControllerAdvice;

@Test
void user_shouldBeLoadedFromRepository(){
void user_shouldBeLoadedFromRepository() {
// given
var bob = new User("bob", null);
when(userRepository.findById("bob")).thenReturn(Optional.of(bob));
when(userRepository.existsById("bob")).thenReturn(true);

when(authentication.getName()).thenReturn("bob");

// when
when(userRepository.findById("bob")).thenReturn(Optional.of(bob));
when(authentication.getName()).thenReturn("bob");
var result = userControllerAdvice.user(authentication);

// then
assertThat(result).isEqualTo(bob);
verify(userRepository).existsById("bob");
verify(userRepository).findById("bob");
verifyNoMoreInteractions(userRepository);
}

@Test
void newUsers_shouldBeAddedToTheRepository(){
// given
when(userRepository.existsById("bob")).thenReturn(false);
void user_shouldReturnNothing_withoutAuthentication() {
assertNull(userControllerAdvice.user(null));
}

when(authentication.getName()).thenReturn("bob");
@Test
void userTeam_shouldTeamOfUser() {
// given
var the_wailers = new Team("the_wailers");
var marley = new User("marley", the_wailers);

// when
var result = userControllerAdvice.user(authentication);
var result = userControllerAdvice.userTeam(authentication, marley);

// then
verify(userRepository).existsById("bob");
verify(userRepository).save(any(User.class));
verifyNoMoreInteractions(userRepository);
assertThat(result).isEqualTo(the_wailers);
}

@Test
void userTeam_shouldReturnNothing_withoutAuthentication() {
assertNull(userControllerAdvice.userTeam(null, null));
}
}

0 comments on commit e0a30bc

Please sign in to comment.