3 * Copyright (C) 2009-2021 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.authentication;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.utils.System2;
25 import org.sonar.api.impl.utils.TestSystem2;
26 import org.sonar.db.DbTester;
27 import org.sonar.db.user.UserDto;
28 import org.sonar.db.user.UserTokenDto;
30 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
32 public class UserLastConnectionDatesUpdaterImplTest {
34 private static final long NOW = 10_000_000_000L;
35 private static final long ONE_MINUTE = 60_000L;
36 private static final long ONE_HOUR = ONE_MINUTE * 60L;
37 private static final long TWO_HOUR = ONE_HOUR * 2L;
40 public DbTester db = DbTester.create();
42 private System2 system2 = new TestSystem2().setNow(NOW);
44 private UserLastConnectionDatesUpdaterImpl underTest = new UserLastConnectionDatesUpdaterImpl(db.getDbClient(), system2);
47 public void update_last_connection_date_from_user_when_last_connection_was_more_than_one_hour() {
48 UserDto user = db.users().insertUser();
49 db.users().updateLastConnectionDate(user, NOW - TWO_HOUR);
51 underTest.updateLastConnectionDateIfNeeded(user);
53 UserDto userReloaded = db.getDbClient().userDao().selectByUuid(db.getSession(), user.getUuid());
54 assertThat(userReloaded.getLastConnectionDate()).isEqualTo(NOW);
58 public void update_last_connection_date_from_user_when_no_last_connection_date() {
59 UserDto user = db.users().insertUser();
61 underTest.updateLastConnectionDateIfNeeded(user);
63 UserDto userReloaded = db.getDbClient().userDao().selectByUuid(db.getSession(), user.getUuid());
64 assertThat(userReloaded.getLastConnectionDate()).isEqualTo(NOW);
68 public void do_not_update_when_last_connection_from_user_was_less_than_one_hour() {
69 UserDto user = db.users().insertUser();
70 db.users().updateLastConnectionDate(user, NOW - ONE_MINUTE);
72 underTest.updateLastConnectionDateIfNeeded(user);
74 UserDto userReloaded = db.getDbClient().userDao().selectByUuid(db.getSession(), user.getUuid());
75 assertThat(userReloaded.getLastConnectionDate()).isEqualTo(NOW - ONE_MINUTE);
79 public void update_last_connection_date_from_user_token_when_last_connection_was_more_than_one_hour() {
80 UserDto user = db.users().insertUser();
81 UserTokenDto userToken = db.users().insertToken(user);
82 db.getDbClient().userTokenDao().update(db.getSession(), userToken.setLastConnectionDate(NOW - TWO_HOUR), false, null);
85 underTest.updateLastConnectionDateIfNeeded(userToken);
87 UserTokenDto userTokenReloaded = db.getDbClient().userTokenDao().selectByTokenHash(db.getSession(), userToken.getTokenHash());
88 assertThat(userTokenReloaded.getLastConnectionDate()).isEqualTo(NOW);
92 public void update_last_connection_date_from_user_token_when_no_last_connection_date() {
93 UserDto user = db.users().insertUser();
94 UserTokenDto userToken = db.users().insertToken(user);
96 underTest.updateLastConnectionDateIfNeeded(userToken);
98 UserTokenDto userTokenReloaded = db.getDbClient().userTokenDao().selectByTokenHash(db.getSession(), userToken.getTokenHash());
99 assertThat(userTokenReloaded.getLastConnectionDate()).isEqualTo(NOW);
103 public void do_not_update_when_last_connection_from_user_token_was_less_than_one_hour() {
104 UserDto user = db.users().insertUser();
105 UserTokenDto userToken = db.users().insertToken(user);
106 db.getDbClient().userTokenDao().update(db.getSession(), userToken.setLastConnectionDate(NOW - ONE_MINUTE), false, null);
109 underTest.updateLastConnectionDateIfNeeded(userToken);
111 UserTokenDto userTokenReloaded = db.getDbClient().userTokenDao().selectByTokenHash(db.getSession(), userToken.getTokenHash());
112 assertThat(userTokenReloaded.getLastConnectionDate()).isEqualTo(NOW - ONE_MINUTE);