Search in sources :

Example 26 with UserEntity

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

the class RegistrationServiceImpl method changePassword.

@Override
@Transactional
public void changePassword(String activationToken, String password) {
    try {
        TokenStoreRestData token = tokenStoreService.findAndVerifyToken(activationToken);
        if (token.isExpired()) {
            throw new RegistrationException("Key is Expired");
        }
        UserEntity user = (UserEntity) token.getUser();
        user.setPassword(passwordEncoder.encode(password));
    } catch (InvalidSignatureException e) {
        throw new RegistrationException("Invalid Token");
    }
}
Also used : InvalidSignatureException(org.springframework.security.jwt.crypto.sign.InvalidSignatureException) TokenStoreRestData(org.codenergic.theskeleton.tokenstore.TokenStoreRestData) UserEntity(org.codenergic.theskeleton.user.UserEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 27 with UserEntity

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

the class UserAccessTokenAuthenticationConverter method convertUserAuthentication.

@Override
public Map<String, ?> convertUserAuthentication(Authentication authentication) {
    LinkedHashMap<String, Object> response = new LinkedHashMap<>(super.convertUserAuthentication(authentication));
    if (authentication.getPrincipal() instanceof UserEntity) {
        UserEntity user = (UserEntity) authentication.getPrincipal();
        response.put(EMAIL, user.getEmail());
        response.put(USER_ID, user.getId());
    }
    return response;
}
Also used : UserEntity(org.codenergic.theskeleton.user.UserEntity) LinkedHashMap(java.util.LinkedHashMap)

Example 28 with UserEntity

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

the class ProfileRestControllerTest method testFindUserByUsername.

@Test
@WithMockUser("user123")
public void testFindUserByUsername() throws Exception {
    final UserEntity user = new UserEntity().setId("user123").setEmail("user@server");
    when(profileService.findProfileByUsername("user123")).thenReturn(user);
    MockHttpServletRequestBuilder request = get("/api/profile").contentType(MediaType.APPLICATION_JSON);
    MockHttpServletResponse response = mockMvc.perform(request).andDo(document("user-profile-view")).andReturn().getResponse();
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(ProfileRestData.builder().fromUserEntity(user).build()));
    verify(profileService).findProfileByUsername("user123");
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) UserEntity(org.codenergic.theskeleton.user.UserEntity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 29 with UserEntity

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

the class ProfileRestControllerTest method testUpdateProfile.

@Test
@WithMockUser("user123")
public void testUpdateProfile() throws Exception {
    final UserEntity user = new UserEntity().setId("user125").setUsername("user").setEmail("user@server");
    when(profileService.updateProfile(eq("user123"), any())).thenReturn(user);
    MockHttpServletRequestBuilder request = put("/api/profile").content("{\"username\": \"user1234\"}").contentType(MediaType.APPLICATION_JSON);
    MockHttpServletResponse response = mockMvc.perform(request).andDo(document("user-profile-update")).andReturn().getResponse();
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(ProfileRestData.builder().fromUserEntity(user).build()));
    verify(profileService).updateProfile(eq("user123"), any());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) UserEntity(org.codenergic.theskeleton.user.UserEntity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 30 with UserEntity

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

the class ProfileRestControllerTest method testFindProfileActiveSessions.

@Test
@WithMockUser("user123")
public void testFindProfileActiveSessions() throws Exception {
    final UserEntity user = new UserEntity().setUsername("user123");
    when(sessionRegistry.getAllPrincipals()).thenReturn(Collections.singletonList(user));
    final SessionInformation sessionInformation = new SessionInformation("1", "1", new Date());
    when(sessionRegistry.getAllSessions(user, true)).thenReturn(Collections.singletonList(sessionInformation));
    MockHttpServletRequestBuilder request = get("/api/profile/sessions").contentType(MediaType.APPLICATION_JSON);
    MockHttpServletResponse response = mockMvc.perform(request).andDo(document("user-profile-sessions-list")).andReturn().getResponse();
    assertThat(response.getStatus()).isEqualTo(200);
    List<SessionInformation> expectedValue = Collections.singletonList(new SessionInformation("user123", "1", sessionInformation.getLastRequest()));
    assertThat(response.getContentAsByteArray()).isEqualTo(objectMapper.writeValueAsBytes(expectedValue));
    verify(sessionRegistry).getAllPrincipals();
    verify(sessionRegistry).getAllSessions(user, true);
}
Also used : SessionInformation(org.springframework.security.core.session.SessionInformation) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) UserEntity(org.codenergic.theskeleton.user.UserEntity) Date(java.util.Date) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

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