feat: email notifications for replies and reactions

This commit is contained in:
Tim
2025-07-30 11:37:40 +08:00
parent 69f5745fe8
commit 3464137511
5 changed files with 112 additions and 6 deletions

View File

@@ -18,7 +18,9 @@ class NotificationServiceTest {
void markReadUpdatesOnlyOwnedNotifications() {
NotificationRepository nRepo = mock(NotificationRepository.class);
UserRepository uRepo = mock(UserRepository.class);
NotificationService service = new NotificationService(nRepo, uRepo);
EmailSender email = mock(EmailSender.class);
NotificationService service = new NotificationService(nRepo, uRepo, email);
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
User user = new User();
user.setId(1L);
@@ -44,7 +46,9 @@ class NotificationServiceTest {
void listNotificationsWithoutFilter() {
NotificationRepository nRepo = mock(NotificationRepository.class);
UserRepository uRepo = mock(UserRepository.class);
NotificationService service = new NotificationService(nRepo, uRepo);
EmailSender email = mock(EmailSender.class);
NotificationService service = new NotificationService(nRepo, uRepo, email);
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
User user = new User();
user.setId(2L);
@@ -64,7 +68,9 @@ class NotificationServiceTest {
void countUnreadReturnsRepositoryValue() {
NotificationRepository nRepo = mock(NotificationRepository.class);
UserRepository uRepo = mock(UserRepository.class);
NotificationService service = new NotificationService(nRepo, uRepo);
EmailSender email = mock(EmailSender.class);
NotificationService service = new NotificationService(nRepo, uRepo, email);
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
User user = new User();
user.setId(3L);
@@ -82,7 +88,9 @@ class NotificationServiceTest {
void createRegisterRequestNotificationsDeletesOldOnes() {
NotificationRepository nRepo = mock(NotificationRepository.class);
UserRepository uRepo = mock(UserRepository.class);
NotificationService service = new NotificationService(nRepo, uRepo);
EmailSender email = mock(EmailSender.class);
NotificationService service = new NotificationService(nRepo, uRepo, email);
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
User admin = new User();
admin.setId(10L);
@@ -101,7 +109,9 @@ class NotificationServiceTest {
void createActivityRedeemNotificationsDeletesOldOnes() {
NotificationRepository nRepo = mock(NotificationRepository.class);
UserRepository uRepo = mock(UserRepository.class);
NotificationService service = new NotificationService(nRepo, uRepo);
EmailSender email = mock(EmailSender.class);
NotificationService service = new NotificationService(nRepo, uRepo, email);
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
User admin = new User();
admin.setId(10L);
@@ -115,4 +125,25 @@ class NotificationServiceTest {
verify(nRepo).deleteByTypeAndFromUser(NotificationType.ACTIVITY_REDEEM, user);
verify(nRepo).save(any(Notification.class));
}
@Test
void createNotificationSendsEmailForCommentReply() {
NotificationRepository nRepo = mock(NotificationRepository.class);
UserRepository uRepo = mock(UserRepository.class);
EmailSender email = mock(EmailSender.class);
NotificationService service = new NotificationService(nRepo, uRepo, email);
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
User user = new User();
user.setEmail("a@a.com");
Post post = new Post();
post.setId(1L);
Comment comment = new Comment();
comment.setId(2L);
when(nRepo.save(any(Notification.class))).thenAnswer(i -> i.getArgument(0));
service.createNotification(user, NotificationType.COMMENT_REPLY, post, comment, null, null, null, null);
verify(email).sendEmail("a@a.com", "【OpenIsle】有人回复了你", "https://ex.com/posts/1#comment-2");
}
}

View File

@@ -0,0 +1,43 @@
package com.openisle.service;
import com.openisle.model.*;
import com.openisle.repository.*;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import static org.mockito.Mockito.*;
class ReactionServiceTest {
@Test
void reactToPostSendsEmailEveryFive() {
ReactionRepository reactionRepo = mock(ReactionRepository.class);
UserRepository userRepo = mock(UserRepository.class);
PostRepository postRepo = mock(PostRepository.class);
CommentRepository commentRepo = mock(CommentRepository.class);
NotificationService notif = mock(NotificationService.class);
EmailSender email = mock(EmailSender.class);
ReactionService service = new ReactionService(reactionRepo, userRepo, postRepo, commentRepo, notif, email);
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
User user = new User();
user.setId(1L);
user.setUsername("bob");
User author = new User();
author.setId(2L);
author.setEmail("a@a.com");
Post post = new Post();
post.setId(3L);
post.setAuthor(author);
when(userRepo.findByUsername("bob")).thenReturn(Optional.of(user));
when(postRepo.findById(3L)).thenReturn(Optional.of(post));
when(reactionRepo.findByUserAndPostAndType(user, post, ReactionType.LIKE)).thenReturn(Optional.empty());
when(reactionRepo.save(any(Reaction.class))).thenAnswer(i -> i.getArgument(0));
when(reactionRepo.countReceived(author.getUsername())).thenReturn(5L);
service.reactToPost("bob", 3L, ReactionType.LIKE);
verify(email).sendEmail("a@a.com", "【OpenIsle】你有新的互动", "https://ex.com/messages");
}
}