mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-22 15:25:49 +00:00
add CredentialServiceTest
This commit is contained in:
parent
f37360e8ab
commit
37f8f05c01
19 changed files with 262 additions and 45 deletions
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.samples.petclinic.configuration;
|
package org.springframework.samples.petclinic.configuration;
|
||||||
|
|
||||||
|
import javax.cache.CacheManager;
|
||||||
import javax.cache.configuration.MutableConfiguration;
|
import javax.cache.configuration.MutableConfiguration;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
|
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
|
||||||
|
@ -34,7 +35,11 @@ class CacheConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer() {
|
public JCacheManagerCustomizer petclinicCacheConfigurationCustomizer() {
|
||||||
return cm -> cm.createCache("vets", cacheConfiguration());
|
return (cm) -> {
|
||||||
|
if (cm.getCache("vets") == null) {
|
||||||
|
cm.createCache("vets", cacheConfiguration());
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,4 +55,13 @@ class CacheConfiguration {
|
||||||
return new MutableConfiguration<>().setStatisticsEnabled(true);
|
return new MutableConfiguration<>().setStatisticsEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MyCashe implements JCacheManagerCustomizer {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void customize(CacheManager cacheManager) {
|
||||||
|
if (cacheManager.getCache("vets") == null) {
|
||||||
|
cacheManager.createCache("vets", cacheConfiguration());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.ResolvableType;
|
import org.springframework.core.ResolvableType;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
import org.springframework.samples.petclinic.common.*;
|
import org.springframework.samples.petclinic.common.*;
|
||||||
import org.springframework.samples.petclinic.controller.common.WebSocketSender;
|
|
||||||
import org.springframework.samples.petclinic.dto.common.CredentialDTO;
|
import org.springframework.samples.petclinic.dto.common.CredentialDTO;
|
||||||
import org.springframework.samples.petclinic.dto.common.MessageDTO;
|
import org.springframework.samples.petclinic.dto.common.MessageDTO;
|
||||||
import org.springframework.samples.petclinic.dto.common.UserDTO;
|
import org.springframework.samples.petclinic.dto.common.UserDTO;
|
||||||
|
@ -38,6 +37,9 @@ import java.util.Map;
|
||||||
@Controller
|
@Controller
|
||||||
public class UserController extends WebSocketSender {
|
public class UserController extends WebSocketSender {
|
||||||
|
|
||||||
|
// set true if you whant confirmation email for first provider connection
|
||||||
|
private static final boolean ASK_OAUTH2_CONFIRMATION = false;
|
||||||
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
|
||||||
private final CredentialService credentialService;
|
private final CredentialService credentialService;
|
||||||
|
@ -179,15 +181,23 @@ public class UserController extends WebSocketSender {
|
||||||
user = userService.save(user);
|
user = userService.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
// send confirmation mail
|
if (ASK_OAUTH2_CONFIRMATION) {
|
||||||
MessageDTO message = new MessageDTO(firstName, lastName, "admin@petclinic.com", credential.getEmail(),
|
// prepare message
|
||||||
"New connexion from " + credential.getProvider(),
|
MessageDTO message = new MessageDTO(firstName, lastName, "admin@petclinic.com", email,
|
||||||
"Your attempt to connect from " + credential.getProvider()
|
"New connexion from " + credential.getProvider(),
|
||||||
+ " To confirm this connection, please click the link below : ",
|
"Your attempt to connect from " + credential.getProvider()
|
||||||
"http://localhost:8080/confirm-account?token=" + credential.getToken());
|
+ " To confirm this connection, please click the link below : ",
|
||||||
|
"http://localhost:8080/confirm-account?token=" + credential.getToken());
|
||||||
|
|
||||||
log.info(message.toString());
|
// send confirmation mail
|
||||||
emailService.sendMailAsynch(message, Locale.getDefault());
|
emailService.sendMailAsynch(message, Locale.getDefault());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
credential.setExpiration(null);
|
||||||
|
credential.setToken("");
|
||||||
|
credential.setVerified(true);
|
||||||
|
credentialService.save(credential);
|
||||||
|
}
|
||||||
|
|
||||||
// disconnect
|
// disconnect
|
||||||
authentication.eraseCredentials();
|
authentication.eraseCredentials();
|
||||||
|
|
|
@ -38,7 +38,7 @@ public class CredentialDTO extends BaseDTO {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX, message = CommonError.FORMAT_BETWEEN
|
@Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX, message = CommonError.FORMAT_BETWEEN
|
||||||
+ CommonParameter.PASSWORD_MIN + " AND " + CommonParameter.PASSWORD_MAX + " !")
|
+ CommonParameter.PASSWORD_MIN + " AND " + CommonParameter.PASSWORD_MAX + " !")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@ -57,9 +57,15 @@ public class CredentialDTO extends BaseDTO {
|
||||||
this.setExpiration();
|
this.setExpiration();
|
||||||
}
|
}
|
||||||
|
|
||||||
public CredentialDTO(@NotNull String provider, @NotNull @Size(min = CommonParameter.EMAIL_MIN, max = CommonParameter.EMAIL_MAX, message = CommonError.FORMAT_BETWEEN
|
public CredentialDTO(@NotNull String provider,
|
||||||
+ CommonParameter.EMAIL_MIN + " AND " + CommonParameter.EMAIL_MAX + " !") @Pattern(regexp = CommonParameter.EMAIL_REGEXP, message = CommonError.EMAIL_FORMAT) String email, @NotNull @Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX, message = CommonError.FORMAT_BETWEEN
|
@NotNull @Size(min = CommonParameter.EMAIL_MIN, max = CommonParameter.EMAIL_MAX,
|
||||||
+ CommonParameter.PASSWORD_MIN + " AND " + CommonParameter.PASSWORD_MAX + " !") String password, @NotNull Boolean verified) {
|
message = CommonError.FORMAT_BETWEEN + CommonParameter.EMAIL_MIN + " AND "
|
||||||
|
+ CommonParameter.EMAIL_MAX + " !") @Pattern(regexp = CommonParameter.EMAIL_REGEXP,
|
||||||
|
message = CommonError.EMAIL_FORMAT) String email,
|
||||||
|
@NotNull @Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX,
|
||||||
|
message = CommonError.FORMAT_BETWEEN + CommonParameter.PASSWORD_MIN + " AND "
|
||||||
|
+ CommonParameter.PASSWORD_MAX + " !") String password,
|
||||||
|
@NotNull Boolean verified) {
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
|
|
|
@ -23,4 +23,5 @@ public class AuthProvider extends NamedEntity {
|
||||||
this.setId(id);
|
this.setId(id);
|
||||||
this.setName(name);
|
this.setName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,9 +58,19 @@ public class Credential extends BaseEntity {
|
||||||
@Column(name = "expiration")
|
@Column(name = "expiration")
|
||||||
private Date expiration;
|
private Date expiration;
|
||||||
|
|
||||||
public Credential(@NotNull Integer providerId, @NotNull @Size(min = CommonParameter.EMAIL_MIN, max = CommonParameter.EMAIL_MAX, message = CommonError.FORMAT_BETWEEN
|
public int getTokenExpiration() {
|
||||||
+ CommonParameter.EMAIL_MIN + " AND " + CommonParameter.EMAIL_MAX + " !") @Pattern(regexp = CommonParameter.EMAIL_REGEXP, message = CommonError.EMAIL_FORMAT) String email, @NotNull @Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX, message = CommonError.FORMAT_BETWEEN
|
return TOKEN_EXPIRATION;
|
||||||
+ CommonParameter.PASSWORD_MIN + " AND " + CommonParameter.PASSWORD_MAX + " !") String password, @NotNull Boolean verified) {
|
}
|
||||||
|
|
||||||
|
public Credential(@NotNull Integer providerId,
|
||||||
|
@NotNull @Size(min = CommonParameter.EMAIL_MIN, max = CommonParameter.EMAIL_MAX,
|
||||||
|
message = CommonError.FORMAT_BETWEEN + CommonParameter.EMAIL_MIN + " AND "
|
||||||
|
+ CommonParameter.EMAIL_MAX + " !") @Pattern(regexp = CommonParameter.EMAIL_REGEXP,
|
||||||
|
message = CommonError.EMAIL_FORMAT) String email,
|
||||||
|
@NotNull @Size(min = CommonParameter.PASSWORD_MIN, max = CommonParameter.PASSWORD_MAX,
|
||||||
|
message = CommonError.FORMAT_BETWEEN + CommonParameter.PASSWORD_MIN + " AND "
|
||||||
|
+ CommonParameter.PASSWORD_MAX + " !") String password,
|
||||||
|
@NotNull Boolean verified) {
|
||||||
this.providerId = providerId;
|
this.providerId = providerId;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
|
|
|
@ -4,6 +4,7 @@ import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.Repository;
|
import org.springframework.data.repository.Repository;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
import org.springframework.samples.petclinic.model.common.Credential;
|
import org.springframework.samples.petclinic.model.common.Credential;
|
||||||
|
import org.springframework.samples.petclinic.model.common.User;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -43,9 +44,17 @@ public interface CredentialRepository extends Repository<Credential, Integer> {
|
||||||
List<Credential> findAll();
|
List<Credential> findAll();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a {@link Credential} to the data store, either inserting or updating it.
|
* Save an {@link Credential} to the data store, either inserting or updating it.
|
||||||
* @param credential the {@link Credential} to save
|
* @param credential the {@link Credential} to save
|
||||||
|
* @return the deleted {@link Credential}
|
||||||
*/
|
*/
|
||||||
Credential save(Credential credential);
|
Credential save(Credential credential);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete an {@link Credential} to the data store.
|
||||||
|
* @param credential the {@link Credential} to delete
|
||||||
|
* @return the deleted {@link Credential}
|
||||||
|
*/
|
||||||
|
Credential delete(Credential credential);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ public interface UserRepository extends Repository<User, Integer> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save an {@link User} to the data store, either inserting or updating it.
|
* Save an {@link User} to the data store, either inserting or updating it.
|
||||||
* @param user the {@link User} to delete
|
* @param user the {@link User} to save
|
||||||
* @return the deleted {@link User}
|
* @return the deleted {@link User}
|
||||||
*/
|
*/
|
||||||
User save(User user);
|
User save(User user);
|
||||||
|
|
|
@ -126,4 +126,10 @@ public class CredentialService {
|
||||||
return entityToDTO(credential);
|
return entityToDTO(credential);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CredentialDTO delete(CredentialDTO dto) {
|
||||||
|
Credential credential = dtoToEntity(dto);
|
||||||
|
credential = credentialRepository.delete(credential);
|
||||||
|
return entityToDTO(credential);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package org.springframework.samples.petclinic.service.common;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.samples.petclinic.dto.common.UserDTO;
|
import org.springframework.samples.petclinic.dto.common.UserDTO;
|
||||||
import org.springframework.samples.petclinic.model.common.Role;
|
|
||||||
import org.springframework.samples.petclinic.model.common.User;
|
import org.springframework.samples.petclinic.model.common.User;
|
||||||
import org.springframework.samples.petclinic.repository.RoleRepository;
|
import org.springframework.samples.petclinic.repository.RoleRepository;
|
||||||
import org.springframework.samples.petclinic.repository.UserRepository;
|
import org.springframework.samples.petclinic.repository.UserRepository;
|
||||||
|
|
|
@ -128,7 +128,7 @@ CREATE TABLE credentials (
|
||||||
password VARCHAR(255) NOT NULL,
|
password VARCHAR(255) NOT NULL,
|
||||||
verified BOOLEAN NOT NULL,
|
verified BOOLEAN NOT NULL,
|
||||||
token VARCHAR(255) DEFAULT NULL,
|
token VARCHAR(255) DEFAULT NULL,
|
||||||
expiration DATE DEFAULT NULL
|
expiration TIMESTAMP DEFAULT NULL
|
||||||
);
|
);
|
||||||
ALTER TABLE credentials ADD CONSTRAINT fk_credentials_provider_id FOREIGN KEY (provider_id) REFERENCES auth_providers (id);
|
ALTER TABLE credentials ADD CONSTRAINT fk_credentials_provider_id FOREIGN KEY (provider_id) REFERENCES auth_providers (id);
|
||||||
CREATE INDEX credentials_email ON credentials (email);
|
CREATE INDEX credentials_email ON credentials (email);
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.samples.petclinic;
|
package org.springframework.samples.petclinic;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
|
@ -0,0 +1,106 @@
|
||||||
|
package org.springframework.samples.petclinic.service.common;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.samples.petclinic.dto.common.CredentialDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.common.UserDTO;
|
||||||
|
import org.springframework.samples.petclinic.model.common.AuthProvider;
|
||||||
|
import org.springframework.samples.petclinic.model.common.Credential;
|
||||||
|
import org.springframework.samples.petclinic.repository.AuthProviderRepository;
|
||||||
|
import org.springframework.samples.petclinic.repository.CredentialRepository;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@AutoConfigureTestDatabase
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
class CredentialServiceIntegrationTest {
|
||||||
|
|
||||||
|
private static String PROVIDER_TEST_NAME = "Provider Test";
|
||||||
|
|
||||||
|
private static Integer PROVIDER_TEST_ID = 11;
|
||||||
|
|
||||||
|
private static final String EMAIL_TEST = "eduardo.rodriguez@petclinic.com";
|
||||||
|
|
||||||
|
private static final String PASSWORD_TEST = "$2a$10$8KypNYtPopFo8Sk5jbKJ4.lCKeBhdApsrkmFfhwjB8nCls8qpzjZG";
|
||||||
|
|
||||||
|
private static final String TOKEN_TEST = UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BCryptPasswordEncoder bCryptPasswordEncoder;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AuthProviderRepository authProviderRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CredentialRepository credentialRepository;
|
||||||
|
|
||||||
|
private CredentialService credentialService;
|
||||||
|
|
||||||
|
private Credential credential;
|
||||||
|
|
||||||
|
private CredentialDTO credentialDTO;
|
||||||
|
|
||||||
|
private AuthProvider authProvider;
|
||||||
|
|
||||||
|
private List<Credential> allCredentials;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void beforeEach() {
|
||||||
|
allCredentials = credentialRepository.findAll();
|
||||||
|
|
||||||
|
credentialService = new CredentialService(credentialRepository, bCryptPasswordEncoder, authProviderRepository);
|
||||||
|
authProvider = new AuthProvider(PROVIDER_TEST_ID, PROVIDER_TEST_NAME);
|
||||||
|
credential = new Credential(PROVIDER_TEST_ID, EMAIL_TEST, PASSWORD_TEST, true);
|
||||||
|
credential.setToken(TOKEN_TEST);
|
||||||
|
credentialDTO = new CredentialDTO(PROVIDER_TEST_NAME, EMAIL_TEST, PASSWORD_TEST, true);
|
||||||
|
credentialDTO.setToken(TOKEN_TEST);
|
||||||
|
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTime(new Timestamp(cal.getTime().getTime()));
|
||||||
|
cal.add(Calendar.MINUTE, credential.getTokenExpiration());
|
||||||
|
credential.setExpiration(cal.getTime());
|
||||||
|
credentialDTO.setExpiration(cal.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("findByEmailAndProvider")
|
||||||
|
@DisplayName("Verify that we call right method to get Credential by Email and Provider")
|
||||||
|
void findByEmailAndProvider() {
|
||||||
|
|
||||||
|
for(Credential credential: allCredentials) {
|
||||||
|
String email = credential.getEmail();
|
||||||
|
String provider = authProviderRepository.findById(credential.getProviderId()).getName();
|
||||||
|
CredentialDTO found = credentialService.findByEmailAndProvider(email,provider);
|
||||||
|
assertThat(found).isEqualToComparingFieldByField(credentialService.entityToDTO(credential));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("findByToken")
|
||||||
|
@DisplayName("Verify that we call right method to get Credential by Token")
|
||||||
|
@Disabled
|
||||||
|
void findByToken() {
|
||||||
|
credentialDTO = credentialService.save(credentialDTO);
|
||||||
|
|
||||||
|
CredentialDTO found = credentialService.findByToken(TOKEN_TEST);
|
||||||
|
|
||||||
|
assertThat(found).isEqualToComparingFieldByField(credentialDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabas
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.samples.petclinic.dto.common.CredentialDTO;
|
import org.springframework.samples.petclinic.dto.common.CredentialDTO;
|
||||||
|
import org.springframework.samples.petclinic.dto.common.UserDTO;
|
||||||
import org.springframework.samples.petclinic.model.common.AuthProvider;
|
import org.springframework.samples.petclinic.model.common.AuthProvider;
|
||||||
import org.springframework.samples.petclinic.model.common.Credential;
|
import org.springframework.samples.petclinic.model.common.Credential;
|
||||||
import org.springframework.samples.petclinic.repository.AuthProviderRepository;
|
import org.springframework.samples.petclinic.repository.AuthProviderRepository;
|
||||||
|
@ -17,23 +18,28 @@ import org.springframework.samples.petclinic.repository.CredentialRepository;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.sql.Timestamp;
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
|
|
||||||
@AutoConfigureTestDatabase
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
@AutoConfigureTestDatabase
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
class CredentialServiceTest {
|
class CredentialServiceTest {
|
||||||
|
|
||||||
private static String PROVIDER_TEST_NAME = "Provider Test";
|
private static String PROVIDER_TEST_NAME = "Provider Test";
|
||||||
|
|
||||||
private static Integer PROVIDER_TEST_ID = 11;
|
private static Integer PROVIDER_TEST_ID = 11;
|
||||||
|
|
||||||
private static final String EMAIL_TEST = "eduardo.rodriguez@petclinic.com";
|
private static final String EMAIL_TEST = "eduardo.rodriguez@petclinic.com";
|
||||||
|
|
||||||
private static final String PASSWORD_TEST = "$2a$10$8KypNYtPopFo8Sk5jbKJ4.lCKeBhdApsrkmFfhwjB8nCls8qpzjZG";
|
private static final String PASSWORD_TEST = "$2a$10$8KypNYtPopFo8Sk5jbKJ4.lCKeBhdApsrkmFfhwjB8nCls8qpzjZG";
|
||||||
|
|
||||||
|
private static final String TOKEN_TEST = UUID.randomUUID().toString();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private BCryptPasswordEncoder bCryptPasswordEncoder;
|
private BCryptPasswordEncoder bCryptPasswordEncoder;
|
||||||
|
|
||||||
|
@ -54,20 +60,26 @@ class CredentialServiceTest {
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void beforeEach() {
|
void beforeEach() {
|
||||||
credentialService = new CredentialService(credentialRepository, bCryptPasswordEncoder, authProviderRepository);
|
credentialService = new CredentialService(credentialRepository, bCryptPasswordEncoder, authProviderRepository);
|
||||||
authProvider = new AuthProvider(PROVIDER_TEST_ID,PROVIDER_TEST_NAME);
|
authProvider = new AuthProvider(PROVIDER_TEST_ID, PROVIDER_TEST_NAME);
|
||||||
credential = new Credential(PROVIDER_TEST_ID, EMAIL_TEST, PASSWORD_TEST,true);
|
credential = new Credential(PROVIDER_TEST_ID, EMAIL_TEST, PASSWORD_TEST, true);
|
||||||
credentialDTO = new CredentialDTO(PROVIDER_TEST_NAME, EMAIL_TEST, PASSWORD_TEST,true);
|
credential.setToken(TOKEN_TEST);
|
||||||
|
credentialDTO = new CredentialDTO(PROVIDER_TEST_NAME, EMAIL_TEST, PASSWORD_TEST, true);
|
||||||
|
credentialDTO.setToken(TOKEN_TEST);
|
||||||
|
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.setTime(new Timestamp(cal.getTime().getTime()));
|
||||||
|
cal.add(Calendar.MINUTE, credential.getTokenExpiration());
|
||||||
|
credential.setExpiration(cal.getTime());
|
||||||
|
credentialDTO.setExpiration(cal.getTime());
|
||||||
|
|
||||||
|
given(authProviderRepository.findByName(anyString())).willReturn(authProvider);
|
||||||
|
given(authProviderRepository.findById(anyInt())).willReturn(authProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("dtoToEntity")
|
@Tag("dtoToEntity")
|
||||||
@DisplayName("Verify the convertion from DTO to Entity")
|
@DisplayName("Verify the convertion from DTO to Entity")
|
||||||
void dtoToEntity() {
|
void dtoToEntity() {
|
||||||
given(authProviderRepository.findByName(PROVIDER_TEST_NAME)).willReturn(authProvider);
|
|
||||||
Credential found = credentialService.dtoToEntity(credentialDTO);
|
Credential found = credentialService.dtoToEntity(credentialDTO);
|
||||||
|
|
||||||
assertThat(found).isEqualToComparingFieldByField(credential);
|
assertThat(found).isEqualToComparingFieldByField(credential);
|
||||||
|
@ -77,24 +89,67 @@ class CredentialServiceTest {
|
||||||
@Tag("entityToDTO")
|
@Tag("entityToDTO")
|
||||||
@DisplayName("Verify the convertion from Entity to DTO")
|
@DisplayName("Verify the convertion from Entity to DTO")
|
||||||
void entityToDTO() {
|
void entityToDTO() {
|
||||||
given(authProviderRepository.findById(PROVIDER_TEST_ID)).willReturn(authProvider);
|
|
||||||
|
|
||||||
CredentialDTO found = credentialService.entityToDTO(credential);
|
CredentialDTO found = credentialService.entityToDTO(credential);
|
||||||
|
|
||||||
assertThat(found).isEqualToComparingFieldByField(credentialDTO);
|
assertThat(found).isEqualToComparingFieldByField(credentialDTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Tag("findByEmailAndProvider")
|
@Tag("findByEmailAndProvider")
|
||||||
@DisplayName("Verify that we call right method to get Credential by Email and Provider")
|
@DisplayName("Verify that we call right method to get Credential by Email and Provider")
|
||||||
void findByEmailAndProvider() {
|
void findByEmailAndProvider() {
|
||||||
given(credentialRepository.findByEmailAndProvider(EMAIL_TEST,PROVIDER_TEST_ID)).willReturn(credential);
|
given(credentialRepository.findByEmailAndProvider(EMAIL_TEST, PROVIDER_TEST_ID)).willReturn(credential);
|
||||||
given(authProviderRepository.findByName(PROVIDER_TEST_NAME)).willReturn(authProvider);
|
|
||||||
given(authProviderRepository.findById(PROVIDER_TEST_ID)).willReturn(authProvider);
|
|
||||||
|
|
||||||
CredentialDTO found = credentialService.findByEmailAndProvider(EMAIL_TEST,PROVIDER_TEST_NAME);
|
CredentialDTO found = credentialService.findByEmailAndProvider(EMAIL_TEST, PROVIDER_TEST_NAME);
|
||||||
|
|
||||||
|
assertThat(found).isEqualToComparingFieldByField(credentialDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("findByToken")
|
||||||
|
@DisplayName("Verify that we call right method to get Credential by Token")
|
||||||
|
void findByToken() {
|
||||||
|
given(credentialRepository.findByToken(TOKEN_TEST)).willReturn(credential);
|
||||||
|
|
||||||
|
CredentialDTO found = credentialService.findByToken(TOKEN_TEST);
|
||||||
|
|
||||||
|
assertThat(found).isEqualToComparingFieldByField(credentialDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("save")
|
||||||
|
@DisplayName("Verify that we call right method to save Credential")
|
||||||
|
void save() {
|
||||||
|
given(credentialRepository.save(any(Credential.class))).willReturn(credential);
|
||||||
|
|
||||||
|
CredentialDTO found = credentialService.save(credentialDTO);
|
||||||
|
|
||||||
|
assertThat(found).isEqualToComparingFieldByField(credentialDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("saveNew")
|
||||||
|
@DisplayName("Verify that we call right method to save Credential from User")
|
||||||
|
void saveNew() {
|
||||||
|
UserDTO user = new UserDTO();
|
||||||
|
user.setEmail(EMAIL_TEST);
|
||||||
|
user.setPassword(PASSWORD_TEST);
|
||||||
|
user.setMatchingPassword(PASSWORD_TEST);
|
||||||
|
|
||||||
|
given(credentialRepository.save(any(Credential.class))).willReturn(credential);
|
||||||
|
|
||||||
|
CredentialDTO found = credentialService.saveNew(user);
|
||||||
|
|
||||||
|
assertThat(found).isEqualToComparingFieldByField(credentialDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Tag("delete")
|
||||||
|
@DisplayName("Verify that we call right method to delete Credential")
|
||||||
|
void delete() {
|
||||||
|
given(credentialRepository.delete(any(Credential.class))).willReturn(credential);
|
||||||
|
|
||||||
|
CredentialDTO found = credentialService.delete(credentialDTO);
|
||||||
|
|
||||||
assertThat(found).isEqualToComparingFieldByField(credentialDTO);
|
assertThat(found).isEqualToComparingFieldByField(credentialDTO);
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,8 +23,8 @@ import java.util.List;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@AutoConfigureTestDatabase
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
@AutoConfigureTestDatabase
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
class PrivilegeServiceIntegrationTest {
|
class PrivilegeServiceIntegrationTest {
|
||||||
|
|
||||||
|
|
|
@ -25,8 +25,8 @@ import java.util.List;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@AutoConfigureTestDatabase
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
@AutoConfigureTestDatabase
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
class PrivilegeServiceTest {
|
class PrivilegeServiceTest {
|
||||||
|
|
||||||
|
|
|
@ -21,8 +21,8 @@ import java.util.*;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@AutoConfigureTestDatabase
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
@AutoConfigureTestDatabase
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
class RoleServiceIntegrationTest {
|
class RoleServiceIntegrationTest {
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,8 @@ import java.util.*;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@AutoConfigureTestDatabase
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
@AutoConfigureTestDatabase
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
class RoleServiceTest {
|
class RoleServiceTest {
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,8 @@ import java.util.*;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@AutoConfigureTestDatabase
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
@AutoConfigureTestDatabase
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
class UserServiceIntegrationTest {
|
class UserServiceIntegrationTest {
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,8 @@ import java.util.*;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@AutoConfigureTestDatabase
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
@AutoConfigureTestDatabase
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
class UserServiceTest {
|
class UserServiceTest {
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue