fix(sync): stop the nightly sync when the Steam ID is gone
Build, Test & Push Core Service / quality-check (push) Successful in 49s
Build, Test & Push Core Service / docker (push) Successful in 1m20s

After an unlink in Auth, or when a verified link took the ID away, the
nightly job kept syncing the old Steam ID for up to 14 days. A
games-count request without a steamId claim now clears user_seen_at.
steam_id stays, so a later link with another ID still soft-deletes the
old games.
This commit is contained in:
lobo committed 2026-09-26 11:20:04 +02:00
1 parent 393cdddf06
commit a389bdd0c7
5 files changed
+83 -1

No files matched your search

@@ -0,0 +1,62 @@
package core_service.modules.library.api;
import core_service.modules.library.repo.UserSteamSyncRepository;
import core_service.shared.test.AbstractIntegrationTest;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.Cookie;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.web.servlet.MockMvc;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* A user who unlinked the Steam ID, or lost it to a verified link, must drop out of the nightly sync.
*/
@AutoConfigureMockMvc
class LibraryControllerSteamIdGoneTest extends AbstractIntegrationTest {
private static final long USER_ID = 77L;
@Autowired
private MockMvc mockMvc;
@Autowired
private JdbcTemplate jdbc;
@Autowired
private UserSteamSyncRepository userSteamSyncRepository;
@BeforeEach
void seedSyncedUser() {
jdbc.update("DELETE FROM user_steam_sync WHERE user_id = ?", USER_ID);
jdbc.update("INSERT INTO user_steam_sync (user_id, steam_id, last_synced_at, last_sync_status, user_seen_at) "
+ "VALUES (?, '76561198000000001', now(), 'OK', now())", USER_ID);
}
private static Cookie loginWithoutSteamId() {
return new Cookie("tbp_access", Jwts.builder()
.subject("unlinked@example.com")
.claim("userId", String.valueOf(USER_ID))
.expiration(new Date(System.currentTimeMillis() + 60_000))
.signWith(TEST_KEYS.getPrivate(), Jwts.SIG.EdDSA)
.compact());
}
@Test
void gamesCount_WithoutSteamIdClaim_StopsTheNightlySyncOfTheOldId() throws Exception {
mockMvc.perform(get("/api/library/games-count").cookie(loginWithoutSteamId())).andExpect(status().isOk());
assertTrue(userSteamSyncRepository.findByUserSeenAtAfter(Instant.now().minus(Duration.ofDays(14))).stream()
.noneMatch(sync -> sync.getUserId() == USER_ID));
// The old ID stays: a later link with another ID must still soft-delete the old games.
assertTrue(userSteamSyncRepository.findById(USER_ID).orElseThrow().getSteamId().equals("76561198000000001"));
}
}