Search in sources :

Example 31 with UserEntity

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

the class ProfileServiceTest method testFindOAuth2ClientApprovalByUsername.

@Test
public void testFindOAuth2ClientApprovalByUsername() throws Exception {
    final UserOAuth2ClientApprovalEntity result = new UserOAuth2ClientApprovalEntity().setId("123").setUser(new UserEntity().setUsername("user")).setClient(new OAuth2ClientEntity().setId("123")).setApprovalStatus(Approval.ApprovalStatus.APPROVED).setScope("scope1");
    when(approvalRepository.findByUserUsername("user")).thenReturn(Collections.singletonList(result));
    List<UserOAuth2ClientApprovalEntity> approvals = profileService.findOAuth2ClientApprovalByUsername("user");
    assertThat(approvals).isNotEmpty();
    assertThat(approvals).hasSize(1);
    assertThat(approvals.get(0)).isEqualTo(result);
    verify(approvalRepository).findByUserUsername("user");
}
Also used : OAuth2ClientEntity(org.codenergic.theskeleton.client.OAuth2ClientEntity) UserOAuth2ClientApprovalEntity(org.codenergic.theskeleton.user.UserOAuth2ClientApprovalEntity) UserEntity(org.codenergic.theskeleton.user.UserEntity) Test(org.junit.Test)

Example 32 with UserEntity

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

the class SocialUserServiceTest method testLoadUserByUserId.

@Test
public void testLoadUserByUserId() {
    when(userRepository.findOne("123")).thenReturn(new UserEntity());
    socialUserService.loadUserByUserId("123");
    verify(userRepository).findOne("123");
}
Also used : UserEntity(org.codenergic.theskeleton.user.UserEntity) Test(org.junit.Test)

Example 33 with UserEntity

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

the class ProfileRestController method findProfileActiveSessions.

@GetMapping("/sessions")
public List<SessionInformation> findProfileActiveSessions(Authentication authentication) {
    if (authentication == null || authentication.getPrincipal() == null)
        return Collections.emptyList();
    List<SessionInformation> sessions = new ArrayList<>();
    for (Object principal : sessionRegistry.getAllPrincipals()) {
        UserEntity user = (UserEntity) principal;
        if (!user.getUsername().equals(authentication.getName()))
            continue;
        sessions.addAll(sessionRegistry.getAllSessions(user, true));
    }
    return sessions.stream().map(i -> new SessionInformation(authentication.getName(), i.getSessionId(), i.getLastRequest())).collect(Collectors.toList());
}
Also used : Valid(javax.validation.Valid) HttpServletRequest(javax.servlet.http.HttpServletRequest) java.util(java.util) UserOAuth2ClientApprovalRestData(org.codenergic.theskeleton.user.UserOAuth2ClientApprovalRestData) UserEntity(org.codenergic.theskeleton.user.UserEntity) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) SessionInformation(org.springframework.security.core.session.SessionInformation) Authentication(org.springframework.security.core.Authentication) SessionRegistry(org.springframework.security.core.session.SessionRegistry) Collectors(java.util.stream.Collectors) InputStream(java.io.InputStream) SessionInformation(org.springframework.security.core.session.SessionInformation) UserEntity(org.codenergic.theskeleton.user.UserEntity)

Example 34 with UserEntity

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

the class RegistrationControllerTest method testRegisterUser.

@Test
public void testRegisterUser() throws Exception {
    UserEntity user = new UserEntity().setId("123");
    when(registrationService.registerUser(any())).thenReturn(user);
    when(tokenStoreService.sendTokenNotification(any(), any())).thenReturn(any());
    MockHttpServletRequestBuilder request = post("/registration").param("username", "testuser").param("password", "securepassword").param("email", "theskeleton-test@codenergic.org");
    MockHttpServletResponse response = mockMvc.perform(request).andReturn().getResponse();
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsString()).contains("We have sent");
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) UserEntity(org.codenergic.theskeleton.user.UserEntity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 35 with UserEntity

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

the class RegistrationServiceTest method testChangePassword.

@Test
public void testChangePassword() {
    UserEntity user = new UserEntity().setPassword("1234");
    when(tokenStoreService.findAndVerifyToken("TOKEN1234")).thenReturn(ImmutableTokenStoreRestData.builder().from(defaultTokenStoreData).expiryDate(Date.from(defaultTokenStoreData.getExpiryDate().toInstant().plus(Duration.ofHours(1)))).user(user).build());
    registrationService.changePassword("TOKEN1234", "notsecurepassword");
    assertThat(user.getPassword()).isEqualTo("notsecurepassword");
    verify(tokenStoreService).findAndVerifyToken("TOKEN1234");
    when(tokenStoreService.findAndVerifyToken("TOKEN1235")).thenReturn(ImmutableTokenStoreRestData.builder().from(defaultTokenStoreData).expiryDate(Date.from(defaultTokenStoreData.getExpiryDate().toInstant().minus(Duration.ofHours(1)))).user(user).build());
    // expired token
    assertThatThrownBy(() -> registrationService.changePassword("TOKEN1235", "notsecurepassword")).hasMessage("Key is Expired").isInstanceOf(RegistrationException.class);
}
Also used : UserEntity(org.codenergic.theskeleton.user.UserEntity) Test(org.junit.Test)

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