Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ public UserService(UserRepository userRepository){

/**
* Gets user by email for future oauth stuff
* This also creates the user on the first sign in
* @param email user email
* @return the user
*/
public UserResponseDTO getUserByEmail(String email){
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new EntityNotFoundException("No user found for the authenticated account"));
.orElseGet(() -> userRepository.save(
new User(email, email, "USER")
));
return new UserResponseDTO(
user.getId(),
user.getEmail(),
Expand Down
19 changes: 17 additions & 2 deletions api/src/test/java/edu/csumb/cst438/api/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,22 @@ void testPatchUserNotFound(){
*/
@Test
void testGetUserByEmailNotFound(){
Mockito.when(userRepository.findByEmail("no@gmail.com")).thenReturn(Optional.empty());
assertThrows(EntityNotFoundException.class, () -> userService.getUserByEmail("no@gmail.com"));
Mockito.when(userRepository.findByEmail("no@gmail.com"))
.thenReturn(Optional.empty());

User savedUser = new User("no@gmail.com", "no@gmail.com", "USER");

Mockito.when(userRepository.save(any(User.class)))
.thenReturn(savedUser);

UserResponseDTO result =
userService.getUserByEmail("no@gmail.com");

assertNotNull(result);
assertEquals("no@gmail.com", result.email());
assertEquals("no@gmail.com", result.displayName());
assertEquals("USER", result.role());

Mockito.verify(userRepository).save(any(User.class));
}
}
Loading