Skip to content

Commit ba7c486

Browse files
committed
wip: add expiration warning
1 parent 38a6d54 commit ba7c486

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

enterprise/coderd/license/license.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ func Entitlements(
7070
// LicenseExpires we must be in grace period.
7171
entitlement = codersdk.EntitlementGracePeriod
7272
}
73+
74+
// add warning if license is expiring soon
75+
if claims.LicenseExpires.Time.Sub(now) < 30*24*time.Hour {
76+
entitlements.Warnings = append(entitlements.Warnings, fmt.Sprintf(
77+
"Your license expires in %d days.", int(claims.LicenseExpires.Time.Sub(now).Hours()/24)))
78+
}
79+
7380
for featureName, featureValue := range claims.Features {
7481
// Can this be negative?
7582
if featureValue <= 0 {

enterprise/coderd/license/license_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,34 @@ func TestEntitlements(t *testing.T) {
102102
fmt.Sprintf("%s is enabled but your license for this feature is expired.", codersdk.FeatureAuditLog.Humanize()),
103103
)
104104
})
105+
t.Run("Expiration warning", func(t *testing.T) {
106+
t.Parallel()
107+
db := dbfake.New()
108+
db.InsertLicense(context.Background(), database.InsertLicenseParams{
109+
JWT: coderdenttest.GenerateLicense(t, coderdenttest.LicenseOptions{
110+
Features: license.Features{
111+
codersdk.FeatureUserLimit: 100,
112+
codersdk.FeatureAuditLog: 1,
113+
},
114+
115+
GraceAt: time.Now().Add(-time.Hour),
116+
ExpiresAt: time.Now().AddDate(0, 0, 2),
117+
}),
118+
Exp: time.Now().Add(time.Hour),
119+
})
120+
121+
entitlements, err := license.Entitlements(context.Background(), db, slog.Logger{}, 1, 1, coderdenttest.Keys, all)
122+
123+
require.NoError(t, err)
124+
require.True(t, entitlements.HasLicense)
125+
require.False(t, entitlements.Trial)
126+
127+
require.Equal(t, codersdk.EntitlementGracePeriod, entitlements.Features[codersdk.FeatureAuditLog].Entitlement)
128+
require.Contains(
129+
t, entitlements.Warnings,
130+
"Your license expires in 2 days.",
131+
)
132+
})
105133
t.Run("SingleLicenseNotEntitled", func(t *testing.T) {
106134
t.Parallel()
107135
db := dbfake.New()

0 commit comments

Comments
 (0)