MCPcopy Index your code
hub / github.com/coder/coder / TestDeleteExpiredAPIKeys

Function TestDeleteExpiredAPIKeys

coderd/database/dbpurge/dbpurge_test.go:1788–1909  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1786}
1787
1788func TestDeleteExpiredAPIKeys(t *testing.T) {
1789 t.Parallel()
1790
1791 now := time.Date(2025, 1, 15, 7, 30, 0, 0, time.UTC)
1792
1793 testCases := []struct {
1794 name string
1795 retentionConfig codersdk.RetentionConfig
1796 oldExpiredTime time.Time
1797 recentExpiredTime *time.Time // nil means no recent expired key created
1798 activeTime *time.Time // nil means no active key created
1799 expectOldExpiredDeleted bool
1800 expectedKeysRemaining int
1801 }{
1802 {
1803 name: "RetentionEnabled",
1804 retentionConfig: codersdk.RetentionConfig{
1805 APIKeys: serpent.Duration(7 * 24 * time.Hour), // 7 days
1806 },
1807 oldExpiredTime: now.Add(-8 * 24 * time.Hour), // Expired 8 days ago
1808 recentExpiredTime: ptr(now.Add(-6 * 24 * time.Hour)), // Expired 6 days ago
1809 activeTime: ptr(now.Add(24 * time.Hour)), // Expires tomorrow
1810 expectOldExpiredDeleted: true,
1811 expectedKeysRemaining: 2, // recent expired + active
1812 },
1813 {
1814 name: "RetentionDisabled",
1815 retentionConfig: codersdk.RetentionConfig{
1816 APIKeys: serpent.Duration(0),
1817 },
1818 oldExpiredTime: now.Add(-365 * 24 * time.Hour), // Expired 1 year ago
1819 recentExpiredTime: nil,
1820 activeTime: nil,
1821 expectOldExpiredDeleted: false,
1822 expectedKeysRemaining: 1, // old expired is kept
1823 },
1824
1825 {
1826 name: "CustomRetention30Days",
1827 retentionConfig: codersdk.RetentionConfig{
1828 APIKeys: serpent.Duration(30 * 24 * time.Hour), // 30 days
1829 },
1830 oldExpiredTime: now.Add(-31 * 24 * time.Hour), // Expired 31 days ago
1831 recentExpiredTime: ptr(now.Add(-29 * 24 * time.Hour)), // Expired 29 days ago
1832 activeTime: nil,
1833 expectOldExpiredDeleted: true,
1834 expectedKeysRemaining: 1, // only recent expired remains
1835 },
1836 }
1837
1838 for _, tc := range testCases {
1839 t.Run(tc.name, func(t *testing.T) {
1840 t.Parallel()
1841
1842 ctx := testutil.Context(t, testutil.WaitShort)
1843 clk := quartz.NewMock(t)
1844 clk.Set(now).MustWait(ctx)
1845

Callers

nothing calls this directly

Calls 15

ContextFunction · 0.92
NewDBFunction · 0.92
WithDumpOnFailureFunction · 0.92
UserFunction · 0.92
APIKeyFunction · 0.92
NewFunction · 0.92
WithClockFunction · 0.92
TryReceiveFunction · 0.92
awaitDoTickFunction · 0.85
nopAuditorPtrFunction · 0.85
DurationMethod · 0.80
ptrFunction · 0.70

Tested by

no test coverage detected