mirror of
https://github.com/pocket-id/pocket-id.git
synced 2026-10-20 00:29:04 +02:00
fix: send the API key expiration email again after a renewal (#1847)
Co-authored-by: Elias Schneider <login@eliasschneider.com>
This commit is contained in:
co-authored by
Elias Schneider
parent
09d4da81a9
commit
517ea76a9c
@@ -100,3 +100,47 @@ func TestAPIKeyExpiryCronJobNotifiesAndMarksExpiringKeys(t *testing.T) {
|
||||
require.NoError(t, db.First(&laterKey, "id = ?", laterKey.ID).Error)
|
||||
require.False(t, laterKey.ExpirationEmailSent)
|
||||
}
|
||||
|
||||
func TestAPIKeyExpiryCronJobNotifiesAgainAfterRenewal(t *testing.T) {
|
||||
db := testutils.NewDatabaseForTest(t)
|
||||
userEmail := "renewal@example.com"
|
||||
user := model.User{
|
||||
Username: "renewal-user",
|
||||
Email: &userEmail,
|
||||
FirstName: "Renewal",
|
||||
LastName: "User",
|
||||
DisplayName: "Renewal User",
|
||||
}
|
||||
require.NoError(t, db.Create(&user).Error)
|
||||
|
||||
// The key already expired, and its owner was notified before that
|
||||
renewedKey := ApiKey{
|
||||
Name: "Renewed",
|
||||
Key: "renewed-hash",
|
||||
ExpiresAt: datatype.DateTime(time.Now().Add(-time.Hour)),
|
||||
ExpirationEmailSent: true,
|
||||
UserID: user.ID,
|
||||
}
|
||||
require.NoError(t, db.Create(&renewedKey).Error)
|
||||
|
||||
service, err := newService(t.Context(), db, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
renewed, _, err := service.RenewApiKey(t.Context(), user.ID, renewedKey.ID, time.Now().Add(3*24*time.Hour))
|
||||
require.NoError(t, err)
|
||||
require.False(t, renewed.ExpirationEmailSent)
|
||||
|
||||
sender := &expiryJobTestSender{}
|
||||
job := &expiryJob{
|
||||
service: service,
|
||||
appConfig: expiryJobTestConfig{config: &appconfig.AppConfigModel{
|
||||
EmailApiKeyExpirationEnabled: "true",
|
||||
}},
|
||||
email: sender,
|
||||
}
|
||||
require.NoError(t, job.checkAndNotifyExpiringAPIKeys(t.Context()))
|
||||
require.Equal(t, []string{"Renewed"}, sender.keyNames)
|
||||
|
||||
require.NoError(t, db.First(&renewedKey, "id = ?", renewedKey.ID).Error)
|
||||
require.True(t, renewedKey.ExpirationEmailSent)
|
||||
}
|
||||
|
||||
@@ -123,6 +123,8 @@ func (s *Service) RenewApiKey(ctx context.Context, userID, apiKeyID string, expi
|
||||
|
||||
apiKey.Key = utils.CreateSha256Hash(token)
|
||||
apiKey.ExpiresAt = datatype.DateTime(expiration)
|
||||
// The renewed key expires again, so its owner should be notified again
|
||||
apiKey.ExpirationEmailSent = false
|
||||
|
||||
err = tx.WithContext(ctx).Save(&apiKey).Error
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user