|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Graylog, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.graylog2.migrations; |
| 19 | + |
| 20 | +import org.graylog2.database.NotFoundException; |
| 21 | +import org.graylog2.plugin.cluster.ClusterConfigService; |
| 22 | +import org.graylog2.plugin.database.ValidationException; |
| 23 | +import org.graylog2.plugin.database.users.User; |
| 24 | +import org.graylog2.shared.users.Role; |
| 25 | +import org.graylog2.shared.users.UserService; |
| 26 | +import org.graylog2.users.RoleService; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 30 | +import org.mockito.Mock; |
| 31 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 32 | + |
| 33 | +import java.util.HashSet; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Set; |
| 36 | +import java.util.UUID; |
| 37 | + |
| 38 | +import static org.mockito.ArgumentMatchers.any; |
| 39 | +import static org.mockito.Mockito.ignoreStubs; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | +import static org.mockito.Mockito.times; |
| 42 | +import static org.mockito.Mockito.verify; |
| 43 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 44 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 45 | +import static org.mockito.Mockito.when; |
| 46 | + |
| 47 | +@ExtendWith(MockitoExtension.class) |
| 48 | +public class V20250721090000_AddClusterConfigurationPermissionTest { |
| 49 | + |
| 50 | + @Mock |
| 51 | + ClusterConfigService clusterConfigService; |
| 52 | + @Mock |
| 53 | + RoleService roleService; |
| 54 | + @Mock |
| 55 | + UserService userService; |
| 56 | + |
| 57 | + V20250721090000_AddClusterConfigurationPermission migration; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + public void setUp() { |
| 61 | + when(clusterConfigService.get(V20250721090000_AddClusterConfigurationPermission.MigrationCompleted.class)) |
| 62 | + .thenReturn(null); |
| 63 | + migration = new V20250721090000_AddClusterConfigurationPermission(clusterConfigService, roleService, userService); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void nothingMigratedIfReaderRoleNotFound() throws NotFoundException { |
| 68 | + String readerId = UUID.randomUUID().toString(); |
| 69 | + when(roleService.getReaderRoleObjectId()).thenReturn(readerId); |
| 70 | + when(roleService.loadById(readerId)).thenThrow(NotFoundException.class); |
| 71 | + migration.upgrade(); |
| 72 | + verifyNoInteractions(userService); |
| 73 | + verify(clusterConfigService, times(1)).write(any()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void nothingMigratedIfClusterConfigurationReaderRoleNotFound() throws NotFoundException { |
| 78 | + String readerId = UUID.randomUUID().toString(); |
| 79 | + when(roleService.getReaderRoleObjectId()).thenReturn(readerId); |
| 80 | + when(roleService.loadById(readerId)).thenReturn(mock(Role.class)); |
| 81 | + when(roleService.load(V20250721090000_AddClusterConfigurationPermission.CLUSTER_CONFIGURATION_READER_ROLE)) |
| 82 | + .thenThrow(NotFoundException.class); |
| 83 | + migration.upgrade(); |
| 84 | + verifyNoInteractions(userService); |
| 85 | + verify(clusterConfigService, times(1)).write(any()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void roleAddedToAllUsersWithReaderRole() throws NotFoundException, ValidationException { |
| 90 | + String readerId = UUID.randomUUID().toString(); |
| 91 | + String clusterConfigurationReaderRoleId = UUID.randomUUID().toString(); |
| 92 | + when(roleService.getReaderRoleObjectId()).thenReturn(readerId); |
| 93 | + Role readerRole = mock(Role.class); |
| 94 | + when(roleService.loadById(readerId)).thenReturn(readerRole); |
| 95 | + Role clusterConfigurationReaderRole = mock(Role.class); |
| 96 | + when(clusterConfigurationReaderRole.getId()).thenReturn(clusterConfigurationReaderRoleId); |
| 97 | + when(roleService.load(V20250721090000_AddClusterConfigurationPermission.CLUSTER_CONFIGURATION_READER_ROLE)) |
| 98 | + .thenReturn(clusterConfigurationReaderRole); |
| 99 | + |
| 100 | + User user1 = mock(User.class); |
| 101 | + when(user1.getRoleIds()).thenReturn(new HashSet<>(List.of(readerId))); |
| 102 | + User user2 = mock(User.class); |
| 103 | + when(user2.getRoleIds()).thenReturn(new HashSet<>(List.of(readerId))); |
| 104 | + |
| 105 | + when(userService.loadAllForRole(readerRole)).thenReturn(List.of(user1, user2)); |
| 106 | + |
| 107 | + migration.upgrade(); |
| 108 | + |
| 109 | + verify(user1, times(1)).setRoleIds(Set.of(readerId, clusterConfigurationReaderRoleId)); |
| 110 | + verify(user2, times(1)).setRoleIds(Set.of(readerId, clusterConfigurationReaderRoleId)); |
| 111 | + verify(userService, times(2)).save(any()); |
| 112 | + verify(clusterConfigService, times(1)).write(any()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void migrationRunsOnlyOnce() { |
| 117 | + when(clusterConfigService.get(V20250721090000_AddClusterConfigurationPermission.MigrationCompleted.class)) |
| 118 | + .thenReturn(new V20250721090000_AddClusterConfigurationPermission.MigrationCompleted()); |
| 119 | + migration.upgrade(); |
| 120 | + verifyNoMoreInteractions(ignoreStubs(clusterConfigService)); |
| 121 | + verifyNoInteractions(roleService, userService); |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments