Projekt

Obecné

Profil

« Předchozí | Další » 

Revize afdd306f

Přidáno uživatelem Jakub Šmíd před asi 2 roky(ů)

Added Super Admin user

re #9790

Zobrazit rozdíly:

backend/src/test/java/cz/zcu/kiv/backendapi/user/UserServiceImplTest.java
177 177
        verify(userRepository, never()).save(any());
178 178
    }
179 179

  
180
    @Test
181
    void testCanNotUpdatePermissionsAdmin() {
182
        // given
183
        String email = "test@test.com";
184
        UserEntity userEntity = new UserEntity("John Doe", email, "", (byte) 0, true);
185
        PermissionDto permissionDto = new PermissionDto();
186
        given(userRepository.findByEmail(email)).willReturn(Optional.of(userEntity));
187

  
188
        // when
189
        assertThatThrownBy(() -> underTest.updatePermissions(email, permissionDto))
190
                .isInstanceOf(ApiRequestException.class)
191
                .hasMessageContaining("Permissions for user with ADMIN rights can not be changed");
192

  
193
        // then
194
        verify(userRepository, never()).delete(any());
195
    }
196

  
180 197
    @Test
181 198
    void testCanResetPassword() {
182 199
        // given
......
216 233
        verify(userRepository, never()).save(any());
217 234
    }
218 235

  
236
    @Test
237
    void testCanNotResetSuperAdminPassword() {
238
        // given
239
        String email = "admin@admin.com";
240
        String newPassword = "password123";
241

  
242
        // when
243
        // then
244
        assertThatThrownBy(() -> underTest.resetPassword(email, newPassword))
245
                .isInstanceOf(ApiRequestException.class)
246
                .hasMessageContaining("Password for SUPER ADMIN can not be changed");
247

  
248
        verify(userRepository, never()).save(any());
249
        verify(userRepository, never()).findByEmail(any());
250
    }
251

  
219 252
    @Test
220 253
    void testCanDeleteUser() {
221 254
        // given
......
267 300
        UserEntity userEntity1 = new UserEntity("first", "first@test.com", "password", (byte) 0, false);
268 301
        UserEntity userEntity2 = new UserEntity("second", "second@test.com", "password2", (byte) 1, false);
269 302
        UserEntity userEntity3 = new UserEntity("third", "third@test.com", "password3", (byte) 7, true);
303
        UserEntity userEntity4 = new UserEntity("SuperAdmin", "admin@admin.com", "password3", (byte) 7, true);
270 304

  
271 305
        UserDto userDto1 = new UserDto("first", "first@test.com", new PermissionDto(), null);
272 306
        UserDto userDto2 = new UserDto("second", "second@test.com", new PermissionDto(true, false, false), null);
273 307
        UserDto userDto3 = new UserDto("third", "third@test.com", new PermissionDto(true, true, true), null);
274 308

  
275
        given(userRepository.findAll()).willReturn(List.of(userEntity1, userEntity2, userEntity3));
309
        given(userRepository.findAll()).willReturn(List.of(userEntity1, userEntity2, userEntity3, userEntity4));
276 310

  
277 311
        // when
278 312
        List<UserDto> allUsers = underTest.getAllUsers();
......
344 378

  
345 379
        verify(userRepository, never()).save(any());
346 380
    }
381

  
382
    @Test
383
    void testCanNotChangePasswordSuperAdmin() {
384
        // given
385
        String email = "admin@admin.com";
386
        String oldPassword = "password";
387
        String newPassword = "password123";
388
        UserEntity userEntity = new UserEntity("John Doe", email, bCryptPasswordEncoder.encode(oldPassword), (byte) 7, true);
389
        SecurityContext securityContext = mock(SecurityContext.class);
390
        Authentication authentication = mock(Authentication.class);
391
        given(securityContext.getAuthentication()).willReturn(authentication);
392
        SecurityContextHolder.setContext(securityContext);
393
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email, null, Collections.emptySet());
394
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
395
        given(SecurityContextHolder.getContext().getAuthentication().getPrincipal()).willReturn(email);
396
        given(userRepository.findByEmail(email)).willReturn(Optional.of(userEntity));
397

  
398
        // when
399
        // then
400
        assertThatThrownBy(() -> underTest.changePassword(oldPassword, newPassword))
401
                .isInstanceOf(ApiRequestException.class)
402
                .hasMessageContaining("Can not change password for SUPER ADMIN");
403

  
404
        verify(userRepository, never()).save(any());
405
    }
347 406
}

Také k dispozici: Unified diff