Search in sources :

Example 1 with AppUserPrincipal

use of com.baeldung.security.AppUserPrincipal in project tutorials by eugenp.

the class SpringDataWithSecurityTest method givenAppUser_whenLoginSuccessful_shouldReadMyPagedTweets.

@Test
public void givenAppUser_whenLoginSuccessful_shouldReadMyPagedTweets() {
    AppUser appUser = userRepository.findByUsername("lionel@messi.com");
    Authentication auth = new UsernamePasswordAuthenticationToken(new AppUserPrincipal(appUser), null, DummyContentUtil.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(auth);
    Page<Tweet> page = null;
    do {
        page = tweetRepository.getMyTweetsAndTheOnesILiked(new PageRequest(page != null ? page.getNumber() + 1 : 0, 5));
        for (Tweet twt : page.getContent()) {
            isTrue((twt.getOwner() == appUser.getUsername()) || (twt.getLikes().contains(appUser.getUsername())), "I do not have any Tweets");
        }
    } while (page.hasNext());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Authentication(org.springframework.security.core.Authentication) Tweet(com.baeldung.models.Tweet) AppUser(com.baeldung.models.AppUser) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AppUserPrincipal(com.baeldung.security.AppUserPrincipal) Test(org.junit.Test)

Example 2 with AppUserPrincipal

use of com.baeldung.security.AppUserPrincipal in project tutorials by eugenp.

the class SpringDataWithSecurityTest method givenAppUser_whenLoginSuccessful_shouldUpdateLastLogin.

@Test
public void givenAppUser_whenLoginSuccessful_shouldUpdateLastLogin() {
    AppUser appUser = userRepository.findByUsername("lionel@messi.com");
    Authentication auth = new UsernamePasswordAuthenticationToken(new AppUserPrincipal(appUser), null, DummyContentUtil.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(auth);
    userRepository.updateLastLogin(new Date());
}
Also used : Authentication(org.springframework.security.core.Authentication) AppUser(com.baeldung.models.AppUser) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Date(java.util.Date) AppUserPrincipal(com.baeldung.security.AppUserPrincipal) Test(org.junit.Test)

Aggregations

AppUser (com.baeldung.models.AppUser)2 AppUserPrincipal (com.baeldung.security.AppUserPrincipal)2 Test (org.junit.Test)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 Tweet (com.baeldung.models.Tweet)1 Date (java.util.Date)1 PageRequest (org.springframework.data.domain.PageRequest)1