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());
}
Aggregations