|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.fineract.useradministration.service; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | +import static org.mockito.ArgumentMatchers.any; |
| 24 | +import static org.mockito.ArgumentMatchers.anyString; |
| 25 | +import static org.mockito.ArgumentMatchers.eq; |
| 26 | +import static org.mockito.ArgumentMatchers.nullable; |
| 27 | +import static org.mockito.Mockito.doNothing; |
| 28 | +import static org.mockito.Mockito.mock; |
| 29 | +import static org.mockito.Mockito.never; |
| 30 | +import static org.mockito.Mockito.verify; |
| 31 | +import static org.mockito.Mockito.when; |
| 32 | + |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.Optional; |
| 36 | +import org.apache.fineract.commands.domain.CommandWrapper; |
| 37 | +import org.apache.fineract.infrastructure.configuration.domain.ConfigurationDomainService; |
| 38 | +import org.apache.fineract.infrastructure.core.api.JsonCommand; |
| 39 | +import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; |
| 40 | +import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant; |
| 41 | +import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil; |
| 42 | +import org.apache.fineract.infrastructure.security.service.PlatformPasswordEncoder; |
| 43 | +import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext; |
| 44 | +import org.apache.fineract.organisation.office.domain.Office; |
| 45 | +import org.apache.fineract.organisation.office.domain.OfficeRepositoryWrapper; |
| 46 | +import org.apache.fineract.organisation.staff.domain.StaffRepositoryWrapper; |
| 47 | +import org.apache.fineract.portfolio.client.domain.ClientRepositoryWrapper; |
| 48 | +import org.apache.fineract.useradministration.domain.AppUser; |
| 49 | +import org.apache.fineract.useradministration.domain.AppUserPreviousPassword; |
| 50 | +import org.apache.fineract.useradministration.domain.AppUserPreviousPasswordRepository; |
| 51 | +import org.apache.fineract.useradministration.domain.AppUserRepository; |
| 52 | +import org.apache.fineract.useradministration.domain.RoleRepository; |
| 53 | +import org.apache.fineract.useradministration.domain.UserDomainService; |
| 54 | +import org.apache.fineract.useradministration.exception.PasswordPreviouslyUsedException; |
| 55 | +import org.junit.jupiter.api.AfterEach; |
| 56 | +import org.junit.jupiter.api.BeforeEach; |
| 57 | +import org.junit.jupiter.api.Test; |
| 58 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 59 | +import org.mockito.InjectMocks; |
| 60 | +import org.mockito.Mock; |
| 61 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 62 | +import org.springframework.data.domain.PageRequest; |
| 63 | + |
| 64 | +@ExtendWith(MockitoExtension.class) |
| 65 | +public class AppUserWritePlatformServiceJpaRepositoryImplTest { |
| 66 | + |
| 67 | + private static final Long USER_ID = 1L; |
| 68 | + |
| 69 | + @Mock |
| 70 | + private PlatformSecurityContext context; |
| 71 | + @Mock |
| 72 | + private UserDomainService userDomainService; |
| 73 | + @Mock |
| 74 | + private PlatformPasswordEncoder platformPasswordEncoder; |
| 75 | + @Mock |
| 76 | + private AppUserRepository appUserRepository; |
| 77 | + @Mock |
| 78 | + private OfficeRepositoryWrapper officeRepositoryWrapper; |
| 79 | + @Mock |
| 80 | + private RoleRepository roleRepository; |
| 81 | + @Mock |
| 82 | + private UserDataValidator fromApiJsonDeserializer; |
| 83 | + @Mock |
| 84 | + private AppUserPreviousPasswordRepository appUserPreviewPasswordRepository; |
| 85 | + @Mock |
| 86 | + private StaffRepositoryWrapper staffRepositoryWrapper; |
| 87 | + @Mock |
| 88 | + private ClientRepositoryWrapper clientRepositoryWrapper; |
| 89 | + @Mock |
| 90 | + private ConfigurationDomainService configurationDomainService; |
| 91 | + |
| 92 | + @InjectMocks |
| 93 | + private AppUserWritePlatformServiceJpaRepositoryImpl underTest; |
| 94 | + |
| 95 | + private JsonCommand command; |
| 96 | + private AppUser user; |
| 97 | + private AppUser authenticatedUser; |
| 98 | + |
| 99 | + @BeforeEach |
| 100 | + void setUp() { |
| 101 | + ThreadLocalContextUtil.setTenant(new FineractPlatformTenant(1L, "default", "Default", "Asia/Kolkata", null)); |
| 102 | + command = mock(JsonCommand.class); |
| 103 | + user = mock(AppUser.class); |
| 104 | + authenticatedUser = mock(AppUser.class); |
| 105 | + |
| 106 | + when(command.json()).thenReturn("{}"); |
| 107 | + when(appUserRepository.findById(USER_ID)).thenReturn(Optional.of(user)); |
| 108 | + when(context.authenticatedUser(any(CommandWrapper.class))).thenReturn(authenticatedUser); |
| 109 | + doNothing().when(fromApiJsonDeserializer).validateForChangePassword(anyString(), nullable(AppUser.class)); |
| 110 | + } |
| 111 | + |
| 112 | + @AfterEach |
| 113 | + void tearDown() { |
| 114 | + ThreadLocalContextUtil.reset(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void changeUserPasswordThrowsWhenPasswordPreviouslyUsed() { |
| 119 | + when(user.getId()).thenReturn(USER_ID); |
| 120 | + when(user.getEncodedPassword(command, platformPasswordEncoder)).thenReturn("encoded"); |
| 121 | + when(configurationDomainService.getPasswordReuseRestrictionCount()).thenReturn(2); |
| 122 | + |
| 123 | + AppUserPreviousPassword previousPassword = mock(AppUserPreviousPassword.class); |
| 124 | + when(previousPassword.getPassword()).thenReturn("encoded"); |
| 125 | + when(appUserPreviewPasswordRepository.findByUserId(eq(USER_ID), any(PageRequest.class))).thenReturn(List.of(previousPassword)); |
| 126 | + |
| 127 | + assertThrows(PasswordPreviouslyUsedException.class, () -> underTest.changeUserPassword(USER_ID, command)); |
| 128 | + |
| 129 | + verify(appUserRepository, never()).saveAndFlush(user); |
| 130 | + verify(appUserPreviewPasswordRepository, never()).save(any(AppUserPreviousPassword.class)); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void changeUserPasswordSavesPreviousPasswordWhenAllowed() { |
| 135 | + Office office = mock(Office.class); |
| 136 | + when(office.getId()).thenReturn(7L); |
| 137 | + when(user.getOffice()).thenReturn(office); |
| 138 | + when(user.getId()).thenReturn(USER_ID); |
| 139 | + when(user.getPassword()).thenReturn("currentEncoded"); |
| 140 | + when(user.getEncodedPassword(command, platformPasswordEncoder)).thenReturn("newEncoded"); |
| 141 | + when(user.changePassword(command, platformPasswordEncoder)).thenReturn(Map.of("password", "new")); |
| 142 | + when(configurationDomainService.getPasswordReuseRestrictionCount()).thenReturn(2); |
| 143 | + when(appUserPreviewPasswordRepository.findByUserId(eq(USER_ID), any(PageRequest.class))).thenReturn(List.of()); |
| 144 | + |
| 145 | + CommandProcessingResult result = underTest.changeUserPassword(USER_ID, command); |
| 146 | + |
| 147 | + assertEquals(USER_ID, result.getResourceId()); |
| 148 | + verify(appUserRepository).saveAndFlush(user); |
| 149 | + verify(appUserPreviewPasswordRepository).save(any(AppUserPreviousPassword.class)); |
| 150 | + } |
| 151 | +} |
0 commit comments