feat: company-scoped emails and audit log, company on admin password emails

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-11-30 20:03:19 +08:00
1 parent b7d9da8ec0
commit 00ce47bc49
13 files changed
+435 -25

No files matched your search

@@ -0,0 +1,106 @@
package com.solvway.services.system.controller;
import com.solvway.core.enums.UserEnum.UserType;
import com.solvway.core.model.Email;
import com.solvway.services.system.model.Member;
import com.solvway.services.system.model.User;
import com.solvway.services.system.service.EmailService;
import com.solvway.services.system.service.MemberService;
import com.solvway.services.system.service.UserService;
import com.solvway.core.utils.ValidateUtil;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class UserControllerEmailCompanyTest {
@Mock
private UserService userService;
@Mock
private MemberService memberService;
@Mock
private EmailService emailService;
@Mock
private ValidateUtil validateUtil;
@Mock
private BCryptPasswordEncoder bCryptPasswordEncoder;
@InjectMocks
private UserController controller;
@Test
void adminPasswordEmailCarriesTheAdminsCompany() {
User user = new User();
user.setId(5L);
user.setUsername("sales1@ins");
user.setEmail("sales1@example.com");
user.setType(UserType.A);
user.setEnabled(true);
user.setCompanyId(7L);
when(userService.findByUsername("sales1@ins")).thenReturn(user);
when(validateUtil.getRandomPasswordString(8)).thenReturn("Xy7kPq2m");
when(bCryptPasswordEncoder.encode(anyString())).thenReturn("hash");
when(userService.resetPassword(anyLong(), anyString(), any())).thenReturn(1);
controller.resetAdminPassword(new HashMap<>(Map.of("username", "sales1@ins", "email", "sales1@example.com")));
ArgumentCaptor<Email> sent = ArgumentCaptor.forClass(Email.class);
verify(emailService).send(sent.capture());
assertThat(sent.getValue().getCompanyId()).isEqualTo(7L);
}
@Test
void superAdminPasswordEmailHasNoCompany() {
User user = new User();
user.setId(1L);
user.setUsername("boss");
user.setEmail("boss@example.com");
user.setType(UserType.A);
user.setEnabled(true);
when(userService.findByUsername("boss")).thenReturn(user);
when(validateUtil.getRandomPasswordString(8)).thenReturn("Xy7kPq2m");
when(bCryptPasswordEncoder.encode(anyString())).thenReturn("hash");
when(userService.resetPassword(anyLong(), anyString(), any())).thenReturn(1);
controller.resetAdminPassword(new HashMap<>(Map.of("username", "boss", "email", "boss@example.com")));
ArgumentCaptor<Email> sent = ArgumentCaptor.forClass(Email.class);
verify(emailService).send(sent.capture());
assertThat(sent.getValue().getCompanyId()).isNull();
}
@Test
void memberPasswordEmailHasNoCompany() {
User user = new User();
user.setId(9L);
user.setUsername("m1");
user.setCompanyId(7L);
Member member = new Member();
member.setPhoneNo("0123456789");
when(userService.findByUsername("m1")).thenReturn(user);
when(memberService.findByUsername("m1")).thenReturn(Optional.of(member));
when(bCryptPasswordEncoder.encode(anyString())).thenReturn("hash");
when(userService.resetPasswordSecondary(anyLong(), anyString())).thenReturn(1);
controller.resetMemberPasswordSecondary(new HashMap<>(Map.of("username", "m1", "email", "m1@example.com")), null);
ArgumentCaptor<Email> sent = ArgumentCaptor.forClass(Email.class);
verify(emailService).send(sent.capture());
assertThat(sent.getValue().getCompanyId()).isNull();
}
}