Added UserRepositoryTest and users/admin users/deanery

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
2020-12-27 13:54:33 +01:00
parent da4e683248
commit 817350e85e
6 changed files with 211 additions and 5 deletions

View File

@ -35,9 +35,12 @@ import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@Query("FROM User WHERE email = ?1")
@Query("FROM User WHERE email = ?1 OR usosId = ?1")
Optional<User> getByAuthority(@Param("authority") String authority);
@Query("FROM User WHERE email = ?1")
Optional<User> getByEmail(@Param("authority") String authority);
@Query("FROM User WHERE refreshToken = ?1")
Optional<User> getByRefreshToken(@Param("refreshToken") String refreshToken);

View File

@ -30,7 +30,7 @@ public class UserService {
public User checkForUser(String email, String usosId, UserRoles roleIfNotExist) {
if (usosId == null) {
Optional<User> user = this.repo.getByAuthority(email.replace("\n", "").trim());
Optional<User> user = this.repo.getByEmail(email.replace("\n", "").trim());
if (user.isPresent()) {
return user.get();
} else {
@ -64,11 +64,15 @@ public class UserService {
}
public User getUserByEmail(String email) throws UserNotFoundException {
return this.repo.getByAuthority(email.replace("\n", "").trim())
return this.repo.getByEmail(email.replace("\n", "").trim())
.orElseThrow(() -> new UserNotFoundException("Cannot find user with given authority"));
}
public Optional<User> getByAuthority(String authority) {
return this.repo.getByAuthority(authority);
}
public Optional<User> getByToken(String token) {
return this.repo.getByToken(token);
}

View File

@ -0,0 +1,52 @@
package com.plannaplan.repositories;
import static org.junit.Assert.assertTrue;
import java.util.Optional;
import com.plannaplan.entities.User;
import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class UserRepositoryTest {
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
@Test
public void shouldReturnByAuthorityWithGivenEmail(){
final String email = "shouldReturnByAuthorityWithGivenEmail@UserRepository.Test";
final String usosId = "45678";
final User user = this.userService.save(new User("shouldReturnByAuthority", "WithGivenEmail", email, usosId, UserRoles.TEST_USER));
final Optional<User> response = this.userRepository.getByAuthority(email);
assertTrue(response.get().getEmail().equals(email));
assertTrue(response.get().getUsosId().equals(usosId));
assertTrue(response.get().getId().equals(user.getId()));
}
@Test
public void shouldReturnByAuthorityWithUsosId(){
final String email = "shouldReturnByAuthorityWithUsosId@UserRepository.Test";
final String usosId = "45678";
final User user = this.userService.save(new User("shouldReturnByAuthority", "WithGivenEmail", email, usosId, UserRoles.TEST_USER));
final Optional<User> response = this.userRepository.getByAuthority(usosId);
assertTrue(response.get().getEmail().equals(email));
assertTrue(response.get().getUsosId().equals(usosId));
assertTrue(response.get().getId().equals(user.getId()));
}
}