From b836c344d49a8d3ad8f7d164bf0425b59df51384 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 16 Jan 2023 09:21:49 +0300 Subject: email: cleanup expired auth codes --- .../java/com/juick/server/tests/ServerTests.java | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/test/java/com/juick') diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index de85fbf3..f01f58a7 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -134,6 +134,8 @@ import java.security.spec.InvalidKeySpecException; import java.sql.Timestamp; import java.time.Instant; import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.temporal.ChronoUnit; import java.util.*; import java.util.function.BiFunction; import java.util.stream.Collectors; @@ -2767,6 +2769,29 @@ public class ServerTests { .param("uid", String.valueOf(ugnich.getUid()))) .andExpect(status().isOk()); } + @Test + @Transactional + public void authCodesCleanup() throws Exception { + emailService.addVerificationCode(ugnich.getUid(), "test@example.com", "123456"); + String hash = userService.getHashByUID(ugnich.getUid()); + mockMvc.perform(get("/settings") + .param("hash", hash) + .param("page", "auth-email") + .param("code", "123456")) + .andExpect(status().isOk()); + assertThat(emailService.getEmails(ugnich.getUid(), true).size(), is(1)); + emailService.addVerificationCode(ugnich.getUid(), "test2@example.com", "12345678"); + var count = jdbcTemplate.queryForObject( + "SELECT COUNT(*) FROM auth WHERE user_id=?", Integer.class, ugnich.getUid()); + assertThat(count, is(1)); + var timestamp = Instant.now().minus(3, ChronoUnit.DAYS).atOffset(ZoneOffset.UTC); + jdbcTemplate.update("UPDATE auth SET ts=? WHERE user_id=?", timestamp, ugnich.getUid()); + emailService.cleanupAuthCodes(); + count = jdbcTemplate.queryForObject( + "SELECT COUNT(*) FROM auth WHERE user_id=?", Integer.class, ugnich.getUid()); + assertThat(count, is(0)); + jdbcTemplate.execute("DELETE FROM emails"); + } /* @Test public void tokenAuth() throws Exception { -- cgit v1.2.3