3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.usertoken.notification;
22 import java.util.List;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.slf4j.event.Level;
27 import org.sonar.api.testfixtures.log.LogAndArguments;
28 import org.sonar.api.testfixtures.log.LogTester;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.user.UserDao;
31 import org.sonar.db.user.UserDto;
32 import org.sonar.db.user.UserTokenDao;
33 import org.sonar.db.user.UserTokenDto;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.ArgumentMatchers.any;
37 import static org.mockito.ArgumentMatchers.eq;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.times;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.when;
43 public class TokenExpirationNotificationSenderTest {
45 public LogTester logTester = new LogTester();
46 private final DbClient dbClient = mock(DbClient.class);
47 private final TokenExpirationEmailComposer emailComposer = mock(TokenExpirationEmailComposer.class);
48 private final TokenExpirationNotificationSender underTest = new TokenExpirationNotificationSender(dbClient, emailComposer);
51 public void no_notification_when_email_setting_is_not_set() {
52 logTester.setLevel(Level.DEBUG);
53 when(emailComposer.areEmailSettingsSet()).thenReturn(false);
54 underTest.sendNotifications();
55 assertThat(logTester.getLogs(Level.DEBUG))
56 .extracting(LogAndArguments::getFormattedMsg)
57 .containsExactly("Emails for token expiration notification have not been sent because email settings are not configured.");
61 public void send_notification() {
62 var expiringToken = new UserTokenDto().setUserUuid("admin");
63 var expiredToken = new UserTokenDto().setUserUuid("admin");
64 var user = mock(UserDto.class);
65 when(user.getUuid()).thenReturn("admin");
66 when(user.getEmail()).thenReturn("admin@admin.com");
67 var userTokenDao = mock(UserTokenDao.class);
68 var userDao = mock(UserDao.class);
69 when(userDao.selectByUuids(any(), any())).thenReturn(List.of(user));
70 when(userTokenDao.selectTokensExpiredInDays(any(), eq(7L))).thenReturn(List.of(expiringToken));
71 when(userTokenDao.selectTokensExpiredInDays(any(), eq(0L))).thenReturn(List.of(expiredToken));
72 when(dbClient.userTokenDao()).thenReturn(userTokenDao);
73 when(dbClient.userDao()).thenReturn(userDao);
74 when(emailComposer.areEmailSettingsSet()).thenReturn(true);
76 underTest.sendNotifications();
78 var argumentCaptor = ArgumentCaptor.forClass(TokenExpirationEmail.class);
79 verify(emailComposer, times(2)).send(argumentCaptor.capture());
80 List<TokenExpirationEmail> emails = argumentCaptor.getAllValues();
81 assertThat(emails).hasSize(2);
82 assertThat(emails.get(0).getRecipients()).containsOnly("admin@admin.com");
83 assertThat(emails.get(0).getUserToken()).isEqualTo(expiringToken);
84 assertThat(emails.get(1).getRecipients()).containsOnly("admin@admin.com");
85 assertThat(emails.get(1).getUserToken()).isEqualTo(expiredToken);