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.sonar.api.utils.log.LogAndArguments;
27 import org.sonar.api.utils.log.LogTester;
28 import org.sonar.api.utils.log.LoggerLevel;
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 when(emailComposer.areEmailSettingsSet()).thenReturn(false);
53 underTest.sendNotifications();
54 assertThat(logTester.getLogs(LoggerLevel.DEBUG))
55 .extracting(LogAndArguments::getFormattedMsg)
56 .containsExactly("Emails for token expiration notification have not been sent because email settings are not configured.");
60 public void send_notification() {
61 var expiringToken = new UserTokenDto().setUserUuid("admin");
62 var expiredToken = new UserTokenDto().setUserUuid("admin");
63 var user = mock(UserDto.class);
64 when(user.getUuid()).thenReturn("admin");
65 when(user.getEmail()).thenReturn("admin@admin.com");
66 var userTokenDao = mock(UserTokenDao.class);
67 var userDao = mock(UserDao.class);
68 when(userDao.selectByUuids(any(), any())).thenReturn(List.of(user));
69 when(userTokenDao.selectTokensExpiredInDays(any(), eq(7L))).thenReturn(List.of(expiringToken));
70 when(userTokenDao.selectTokensExpiredInDays(any(), eq(0L))).thenReturn(List.of(expiredToken));
71 when(dbClient.userTokenDao()).thenReturn(userTokenDao);
72 when(dbClient.userDao()).thenReturn(userDao);
73 when(emailComposer.areEmailSettingsSet()).thenReturn(true);
75 underTest.sendNotifications();
77 var argumentCaptor = ArgumentCaptor.forClass(TokenExpirationEmail.class);
78 verify(emailComposer, times(2)).send(argumentCaptor.capture());
79 List<TokenExpirationEmail> emails = argumentCaptor.getAllValues();
80 assertThat(emails).hasSize(2);
81 assertThat(emails.get(0).getRecipients()).containsOnly("admin@admin.com");
82 assertThat(emails.get(0).getUserToken()).isEqualTo(expiringToken);
83 assertThat(emails.get(1).getRecipients()).containsOnly("admin@admin.com");
84 assertThat(emails.get(1).getUserToken()).isEqualTo(expiredToken);