Skip to content

Commit 4680345

Browse files
committed
Fixes #426 username reservation check
1 parent 5ceaa8c commit 4680345

3 files changed

Lines changed: 51 additions & 10 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.faforever.api.user;
2+
3+
import com.faforever.api.AbstractIntegrationTest;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
7+
import java.util.Optional;
8+
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.hamcrest.Matchers.is;
11+
12+
public class NameRecordRepositoryTest extends AbstractIntegrationTest {
13+
@Autowired
14+
private NameRecordRepository nameRecordRepository;
15+
16+
@Test
17+
void testUsernameReservedOnCurrentUsage() {
18+
Optional<Integer> result = nameRecordRepository.getLastUsernameOwnerWithinMonths("OLD_MODERATOR", 6);
19+
20+
assertThat(result.isPresent(), is(true));
21+
}
22+
23+
@Test
24+
void testUsernameReservedOnUsageBehindThreshold() {
25+
Optional<Integer> result = nameRecordRepository.getLastUsernameOwnerWithinMonths("OLD_ADMIN", 6);
26+
27+
assertThat(result.isPresent(), is(false));
28+
}
29+
30+
@Test
31+
void testUsernameReservedOnNonExisting() {
32+
Optional<Integer> result = nameRecordRepository.getLastUsernameOwnerWithinMonths("NOT_EXISTING", 6);
33+
34+
assertThat(result.isPresent(), is(false));
35+
}
36+
}
37+
38+

src/inttest/resources/sql/prepDefaultData.sql

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,20 @@ VALUES (1, 1),
7575
(2, 22);
7676

7777
INSERT INTO name_history (change_time, user_id, previous_name)
78-
VALUES (NOW(), 2, 'OLD_MODERATOR');
78+
VALUES (NOW() - INTERVAL 10 YEAR, 3, 'OLD_ADMIN'),
79+
(NOW(), 2, 'OLD_MODERATOR');
7980

80-
INSERT INTO game_featuredMods (id, gamemod, name, description, publish, git_url, git_branch, file_extension, allow_override)
81-
VALUES
82-
(1, 'faf', 'FAF', 'Forged Alliance Forever', 1, 'https://github.com/FAForever/fa.git', 'deploy/faf', 'nx2', FALSE),
83-
(6, 'ladder1v1', 'FAF', 'Ladder games', 1, 'https://github.com/FAForever/fa.git', 'deploy/faf', 'nx2', TRUE),
84-
(25, 'coop', 'Coop', 'Multiplayer campaign games', 1, 'https://github.com/FAForever/fa-coop.git', 'master', 'cop', TRUE);
81+
INSERT INTO game_featuredMods (id, gamemod, name, description, publish, git_url, git_branch, file_extension,
82+
allow_override)
83+
VALUES (1, 'faf', 'FAF', 'Forged Alliance Forever', 1, 'https://github.com/FAForever/fa.git', 'deploy/faf', 'nx2',
84+
FALSE),
85+
(6, 'ladder1v1', 'FAF', 'Ladder games', 1, 'https://github.com/FAForever/fa.git', 'deploy/faf', 'nx2', TRUE),
86+
(25, 'coop', 'Coop', 'Multiplayer campaign games', 1, 'https://github.com/FAForever/fa-coop.git', 'master',
87+
'cop', TRUE);
8588

86-
INSERT INTO leaderboard (id, technical_name, name_key, description_key) VALUES
87-
(1, 'global', 'leaderboard.global.name', 'leaderboard.global.description'),
88-
(2, 'ladder_1v1', 'leaderboard.ladder_1v1.name', 'leaderboard.ladder_1v1.description');
89+
INSERT INTO leaderboard (id, technical_name, name_key, description_key)
90+
VALUES (1, 'global', 'leaderboard.global.name', 'leaderboard.global.description'),
91+
(2, 'ladder_1v1', 'leaderboard.ladder_1v1.name', 'leaderboard.ladder_1v1.description');
8992

9093
INSERT INTO matchmaker_queue (id, technical_name, featured_mod_id, leaderboard_id, name_key, team_size, enabled) VALUES
9194
(1, 'ladder1v1', 1, 2, 'matchmaker_queue.ladder1v1', 1, 1);

src/main/java/com/faforever/api/user/NameRecordRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public interface NameRecordRepository extends JpaRepository<NameRecord, Integer>
1212
@Query(value = "SELECT datediff(now(), change_time) FROM name_history WHERE user_id = :userId AND datediff(now(), change_time) <= :maximumDaysAgo ORDER BY change_time DESC LIMIT 1", nativeQuery = true)
1313
Optional<BigInteger> getDaysSinceLastNewRecord(@Param("userId") Integer userId, @Param("maximumDaysAgo") Integer maximumDaysAgo);
1414

15-
@Query(value = "SELECT user_id FROM name_history WHERE previous_name = :name AND now() > date_add(change_time, INTERVAL :months MONTH) ORDER BY change_time DESC LIMIT 1", nativeQuery = true)
15+
@Query(value = "SELECT user_id FROM name_history WHERE previous_name = :name AND (now() - INTERVAL :months MONTH) < change_time ORDER BY change_time DESC LIMIT 1", nativeQuery = true)
1616
Optional<Integer> getLastUsernameOwnerWithinMonths(@Param("name") String name, @Param("months") Integer months);
1717
}

0 commit comments

Comments
 (0)