Search in sources :

Example 21 with UserEntity

use of org.codenergic.theskeleton.user.UserEntity in project theskeleton by codenergic.

the class GalleryRestControllerTest method init.

@Before
public void init() {
    mockMvc = MockMvcBuilders.standaloneSetup(new GalleryRestController(galleryService)).setCustomArgumentResolvers(new AuthenticationPrincipalArgumentResolver(), new PageableHandlerMethodArgumentResolver()).apply(documentationConfiguration(restDocumentation)).build();
    Authentication authentication = new UsernamePasswordAuthenticationToken(new UserEntity().setId(USER_ID).setUsername(USERNAME), "1234");
    SecurityContextHolder.getContext().setAuthentication(authentication);
}
Also used : PageableHandlerMethodArgumentResolver(org.springframework.data.web.PageableHandlerMethodArgumentResolver) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AuthenticationPrincipalArgumentResolver(org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver) UserEntity(org.codenergic.theskeleton.user.UserEntity) Before(org.junit.Before)

Example 22 with UserEntity

use of org.codenergic.theskeleton.user.UserEntity in project theskeleton by codenergic.

the class PostServiceTest method testSavePost.

@Test
public void testSavePost() {
    when(postRepository.save(DUMMY_POST)).thenReturn(DUMMY_POST2);
    PostEntity savedPost = postService.savePost(new UserEntity(), DUMMY_POST);
    assertThat(savedPost).isEqualTo(DUMMY_POST2);
    verify(postRepository).save(DUMMY_POST);
}
Also used : UserEntity(org.codenergic.theskeleton.user.UserEntity) Test(org.junit.Test)

Example 23 with UserEntity

use of org.codenergic.theskeleton.user.UserEntity in project theskeleton by codenergic.

the class UserFollowerRestControllerTest method init.

@Before
public void init() {
    when(userDetailsService.loadUserByUsername(USERNAME)).thenReturn(new UserEntity().setId(USER_ID).setUsername(USERNAME));
    mockMvc = MockMvcBuilders.standaloneSetup(new UserFollowerRestController(userFollowerService)).setCustomArgumentResolvers(new UserArgumentResolver(userDetailsService), new AuthenticationPrincipalArgumentResolver(), new PageableHandlerMethodArgumentResolver()).apply(documentationConfiguration(restDocumentation)).build();
    Authentication authentication = new UsernamePasswordAuthenticationToken(new UserEntity().setId(USER_ID).setUsername(USERNAME), "1234");
    SecurityContextHolder.getContext().setAuthentication(authentication);
}
Also used : PageableHandlerMethodArgumentResolver(org.springframework.data.web.PageableHandlerMethodArgumentResolver) Authentication(org.springframework.security.core.Authentication) UserArgumentResolver(org.codenergic.theskeleton.core.web.UserArgumentResolver) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AuthenticationPrincipalArgumentResolver(org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver) UserEntity(org.codenergic.theskeleton.user.UserEntity) Before(org.junit.Before)

Example 24 with UserEntity

use of org.codenergic.theskeleton.user.UserEntity in project theskeleton by codenergic.

the class TokenStoreServiceImpl method findAndVerifyToken.

@Override
public TokenStoreRestData findAndVerifyToken(String token) {
    try {
        Jwt jwt = JwtHelper.decode(token);
        TokenStoreRestData data = objectMapper.readValue(jwt.getClaims(), TokenStoreRestData.class);
        UserEntity user = userRepository.findById(data.getUserId()).orElseThrow(() -> new UsernameNotFoundException(data.getUserId()));
        SignerVerifier verifier = createSignerVerifier(user);
        jwt.verifySignature(verifier);
        return ImmutableTokenStoreRestData.builder().from(data).signedToken(jwt.getEncoded()).user(user).build();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SignerVerifier(org.springframework.security.jwt.crypto.sign.SignerVerifier) Jwt(org.springframework.security.jwt.Jwt) IOException(java.io.IOException) UserEntity(org.codenergic.theskeleton.user.UserEntity)

Example 25 with UserEntity

use of org.codenergic.theskeleton.user.UserEntity in project theskeleton by codenergic.

the class RegistrationServiceImpl method registerUser.

@Override
@Transactional
public UserEntity registerUser(RegistrationForm form) {
    userRepository.findByUsernameOrEmailAndEnabled(form.getUsername(), form.getEmail(), true).findFirst().ifPresent(user -> {
        throw new RegistrationException("Username or email already exists");
    });
    Stream<UserEntity> existingUsers = userRepository.findByUsernameOrEmailAndEnabled(form.getUsername(), form.getEmail(), false);
    final UserEntity user = existingUsers.findFirst().orElse(new UserEntity()).setUsername(form.getUsername()).setEmail(form.getEmail()).setPassword(passwordEncoder.encode(form.getPassword())).setExpiredAt(null).setAccountNonLocked(true).setCredentialsNonExpired(true);
    TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

        @Override
        public void afterCommit() {
            tokenStoreService.sendTokenNotification(TokenStoreType.USER_ACTIVATION, user);
        }
    });
    return user.getId() == null ? userRepository.save(user) : user;
}
Also used : UserEntity(org.codenergic.theskeleton.user.UserEntity) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserEntity (org.codenergic.theskeleton.user.UserEntity)48 Test (org.junit.Test)30 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)14 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)10 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)8 Transactional (org.springframework.transaction.annotation.Transactional)7 WithMockUser (org.springframework.security.test.context.support.WithMockUser)6 Before (org.junit.Before)5 Authentication (org.springframework.security.core.Authentication)5 PageableHandlerMethodArgumentResolver (org.springframework.data.web.PageableHandlerMethodArgumentResolver)4 AuthenticationPrincipalArgumentResolver (org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver)4 InputStream (java.io.InputStream)3 Date (java.util.Date)3 UserArgumentResolver (org.codenergic.theskeleton.core.web.UserArgumentResolver)3 PageImpl (org.springframework.data.domain.PageImpl)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)3 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)2 OAuth2ClientEntity (org.codenergic.theskeleton.client.OAuth2ClientEntity)2 EmailServiceTest (org.codenergic.theskeleton.core.mail.EmailServiceTest)2 RegistrationException (org.codenergic.theskeleton.registration.RegistrationException)2