Search in sources :

Example 1 with ClientRoles

use of com.aidanwhiteley.books.controller.dtos.ClientRoles in project books by aidanwhiteley.

the class UserRepositoryTest method updateUserRoles.

@Test
void updateUserRoles() {
    // Tidy up before test
    List<User> oldUsers = userRepository.findAllByAuthenticationServiceIdAndAuthProvider(SERVICE_ID, GOOGLE.toString());
    for (User user : oldUsers) {
        userRepository.deleteById(user.getId());
    }
    User user = createTestUser();
    userRepository.insert(user);
    List<User> users = userRepository.findAllByAuthenticationServiceIdAndAuthProvider(SERVICE_ID, GOOGLE.toString());
    assertEquals(0, users.get(0).getRoles().size());
    ClientRoles clientRoles = new ClientRoles(users.get(0).getId(), false, true);
    long updatesCount = userRepository.updateUserRoles(clientRoles);
    assertEquals(1, updatesCount);
    users = userRepository.findAllByAuthenticationServiceIdAndAuthProvider(SERVICE_ID, GOOGLE.toString());
    assertEquals(1, users.get(0).getRoles().size());
    assertEquals(ROLE_EDITOR, users.get(0).getRoles().get(0));
    clientRoles = new ClientRoles(users.get(0).getId(), true, true);
    userRepository.updateUserRoles(clientRoles);
    users = userRepository.findAllByAuthenticationServiceIdAndAuthProvider(SERVICE_ID, GOOGLE.toString());
    assertEquals(2, users.get(0).getRoles().size());
    assertEquals(ROLE_ADMIN, users.get(0).getRoles().get(0));
    assertEquals(ROLE_EDITOR, users.get(0).getRoles().get(1));
    clientRoles = new ClientRoles(users.get(0).getId(), false, false);
    userRepository.updateUserRoles(clientRoles);
    users = userRepository.findAllByAuthenticationServiceIdAndAuthProvider(SERVICE_ID, GOOGLE.toString());
    assertEquals(0, users.get(0).getRoles().size());
}
Also used : User(com.aidanwhiteley.books.domain.User) ClientRoles(com.aidanwhiteley.books.controller.dtos.ClientRoles) Test(org.junit.jupiter.api.Test) IntegrationTest(com.aidanwhiteley.books.util.IntegrationTest)

Aggregations

ClientRoles (com.aidanwhiteley.books.controller.dtos.ClientRoles)1 User (com.aidanwhiteley.books.domain.User)1 IntegrationTest (com.aidanwhiteley.books.util.IntegrationTest)1 Test (org.junit.jupiter.api.Test)1