Commit ac976ed1 authored by Matija Obreza's avatar Matija Obreza
Browse files

Added BasicUserService test

parent bfa8d2b5
......@@ -45,10 +45,6 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
//@Cacheable
//@Entity
//@Table(name = "user")
//@Audited
@MappedSuperclass
public abstract class BasicUser<R extends GrantedAuthority> extends AuditedVersionedModel implements UserDetails {
......
......@@ -72,7 +72,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
}
return user;
}
@Override
@Transactional
public T updateUser(T user, String email, String fullName) {
......@@ -108,8 +108,12 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
@Override
@Transactional
public T changePassword(final T user, final String password) throws PasswordPolicyException {
setPassword(user, password);
return userRepository.save(user);
if (user.getAccountType() == AccountType.LOCAL) {
setPassword(user, password);
return userRepository.save(user);
} else {
throw new PasswordPolicyException("Password can be set only for LOCAL account types");
}
}
protected void setPassword(final T user, final String password) throws PasswordPolicyException {
......
......@@ -18,6 +18,15 @@ package org.genesys.blocks.security.config;
import java.util.Arrays;
import org.genesys.blocks.oauth.service.OAuthServiceImpl;
import org.genesys.blocks.security.NotUniqueUserException;
import org.genesys.blocks.security.model.BasicUser.AccountType;
import org.genesys.blocks.security.model.TestUser;
import org.genesys.blocks.security.model.UserRole;
import org.genesys.blocks.security.persistence.TestUserPersistence;
import org.genesys.blocks.security.service.BasicUserService;
import org.genesys.blocks.security.service.PasswordPolicy.PasswordPolicyException;
import org.genesys.blocks.security.service.impl.BasicUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
......@@ -28,6 +37,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Maxym Borodenko
......@@ -60,4 +71,29 @@ public class ApplicationConfig {
public OAuthServiceImpl oauthService() {
return new OAuthServiceImpl();
}
@Bean
public BasicUserService<UserRole, TestUser> testUserService() {
return new BasicUserServiceImpl<UserRole, TestUser>() {
@Autowired
private TestUserPersistence testUserRepository;
@Override
public TestUser getUserByEmail(String email) throws UsernameNotFoundException {
return testUserRepository.findByEmail(email);
}
@Override
@Transactional
public TestUser createUser(String email, String fullName, String password, AccountType accountType) throws NotUniqueUserException, PasswordPolicyException {
TestUser user = new TestUser();
user.setEmail(email);
user.setFullName(fullName);
user.setAccountType(accountType);
setPassword(user, password);
return testUserRepository.save(user);
}
};
}
}
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.security.model;
import javax.persistence.Entity;
@Entity
public class TestUser extends BasicUser<UserRole> {
private static final long serialVersionUID = 1L;
}
\ No newline at end of file
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.security.model;
import org.springframework.security.core.GrantedAuthority;
public enum UserRole implements GrantedAuthority {
USER("User"), ADMINISTRATOR("Administrator");
String label;
UserRole(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public static UserRole getByLabel(String value) {
for (final UserRole userRole : values()) {
if (userRole.label.equals(value)) {
return userRole;
}
}
throw new IllegalArgumentException(value);
}
public String getName() {
return name();
}
@Override
public String getAuthority() {
return "ROLE_" + getName();
}
}
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.security.persistence;
import org.genesys.blocks.security.model.TestUser;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TestUserPersistence extends JpaRepository<TestUser, Long> {
TestUser findByEmail(String email);
}
......@@ -15,12 +15,16 @@
*/
package org.genesys.blocks.security.tests;
import org.genesys.blocks.security.config.ApplicationConfig;
import org.genesys.blocks.security.config.AuthorizationServerConfig;
import org.genesys.blocks.security.config.SecurityConfig;
import org.genesys.blocks.oauth.persistence.AccessTokenRepository;
import org.genesys.blocks.oauth.persistence.OAuthClientRepository;
import org.genesys.blocks.oauth.persistence.RefreshTokenRepository;
import org.genesys.blocks.security.config.ApplicationConfig;
import org.genesys.blocks.security.config.AuthorizationServerConfig;
import org.genesys.blocks.security.config.SecurityConfig;
import org.genesys.blocks.security.model.TestUser;
import org.genesys.blocks.security.model.UserRole;
import org.genesys.blocks.security.persistence.TestUserPersistence;
import org.genesys.blocks.security.service.BasicUserService;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -45,6 +49,9 @@ import org.springframework.transaction.annotation.Transactional;
public abstract class BaseTest {
protected final Logger LOG = LoggerFactory.getLogger(getClass());
@Autowired
protected BasicUserService<UserRole, TestUser> testUserService;
@Autowired
protected OAuthClientRepository oAuthClientRepository;
......@@ -53,6 +60,9 @@ public abstract class BaseTest {
@Autowired
protected RefreshTokenRepository refreshTokenRepository;
@Autowired
protected TestUserPersistence testUserRepository;
@AfterTransaction
@Transactional
......@@ -63,5 +73,7 @@ public abstract class BaseTest {
accessTokenRepository.deleteAll();
LOG.trace("Deleting all from refresh token repository");
refreshTokenRepository.deleteAll();
LOG.trace("Deleting all from test user repository");
testUserRepository.deleteAll();
}
}
\ No newline at end of file
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.security.tests;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.genesys.blocks.security.NotUniqueUserException;
import org.genesys.blocks.security.model.BasicUser.AccountType;
import org.genesys.blocks.security.model.TestUser;
import org.genesys.blocks.security.service.PasswordPolicy.PasswordPolicyException;
import org.junit.Test;
/**
* @author Matija Obreza
*/
public class BasicUserServiceTest extends ServiceTest {
private static final String USER_EMAIL = "user@example.com";
private static final String USER_FULLNAME = "Full Name";
@Test
public void testChangePassword() throws NotUniqueUserException, PasswordPolicyException {
TestUser user = testUserService.createUser(USER_EMAIL, USER_FULLNAME, "password", AccountType.LOCAL);
assertThat(user.getId(), is(notNullValue()));
assertThat(user.getUuid(), is(notNullValue()));
assertThat(user.getAccountType(), is(AccountType.LOCAL));
testUserService.changePassword(user, "newPassword");
}
@Test(expected = PasswordPolicyException.class)
public void testChangePasswordFail() throws NotUniqueUserException, PasswordPolicyException {
TestUser user = testUserService.createUser(USER_EMAIL, USER_FULLNAME, "password", AccountType.SYSTEM);
assertThat(user.getAccountType(), is(AccountType.SYSTEM));
assertThat(user.getPassword(), is("THIS-IS-NOT-A-PASSWORD"));
testUserService.changePassword(user, "newPassword");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment