Search in sources :

Example 1 with AppUser

use of com.baeldung.models.AppUser in project tutorials by eugenp.

the class DummyContentUtil method generateDummyTweets.

public static final List<Tweet> generateDummyTweets(List<AppUser> users) {
    List<Tweet> tweets = new ArrayList<>();
    Random random = new Random();
    IntStream.range(0, 9).sequential().forEach(i -> {
        Tweet twt = new Tweet(String.format("Tweet %d", i), users.get(random.nextInt(users.size())).getUsername());
        twt.getLikes().addAll(users.subList(0, random.nextInt(users.size())).stream().map(AppUser::getUsername).collect(Collectors.toSet()));
        tweets.add(twt);
    });
    return tweets;
}
Also used : Random(java.util.Random) Tweet(com.baeldung.models.Tweet) ArrayList(java.util.ArrayList) AppUser(com.baeldung.models.AppUser)

Example 2 with AppUser

use of com.baeldung.models.AppUser in project tutorials by eugenp.

the class DummyContentUtil method generateDummyUsers.

public static final List<AppUser> generateDummyUsers() {
    List<AppUser> appUsers = new ArrayList<>();
    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    appUsers.add(new AppUser("Lionel Messi", "lionel@messi.com", passwordEncoder.encode("li1234")));
    appUsers.add(new AppUser("Cristiano Ronaldo", "cristiano@ronaldo.com", passwordEncoder.encode("c1234")));
    appUsers.add(new AppUser("Neymar Dos Santos", "neymar@neymar.com", passwordEncoder.encode("n1234")));
    appUsers.add(new AppUser("Luiz Suarez", "luiz@suarez.com", passwordEncoder.encode("lu1234")));
    appUsers.add(new AppUser("Andres Iniesta", "andres@iniesta.com", passwordEncoder.encode("a1234")));
    appUsers.add(new AppUser("Ivan Rakitic", "ivan@rakitic.com", passwordEncoder.encode("i1234")));
    appUsers.add(new AppUser("Ousman Dembele", "ousman@dembele.com", passwordEncoder.encode("o1234")));
    appUsers.add(new AppUser("Sergio Busquet", "sergio@busquet.com", passwordEncoder.encode("s1234")));
    appUsers.add(new AppUser("Gerard Pique", "gerard@pique.com", passwordEncoder.encode("g1234")));
    appUsers.add(new AppUser("Ter Stergen", "ter@stergen.com", passwordEncoder.encode("t1234")));
    return appUsers;
}
Also used : ArrayList(java.util.ArrayList) AppUser(com.baeldung.models.AppUser) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)

Example 3 with AppUser

use of com.baeldung.models.AppUser 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 4 with AppUser

use of com.baeldung.models.AppUser 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)4 Tweet (com.baeldung.models.Tweet)2 AppUserPrincipal (com.baeldung.security.AppUserPrincipal)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 Date (java.util.Date)1 Random (java.util.Random)1 PageRequest (org.springframework.data.domain.PageRequest)1 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)1