Skip to content

Commit 2c7c2ee

Browse files
committed
show expiration warning for trial accounts
1 parent 7070c14 commit 2c7c2ee

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

enterprise/coderd/license/license.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ func Entitlements(
7474

7575
// Add warning if license is expiring soon
7676
daysToExpire := int(math.Ceil(claims.LicenseExpires.Sub(now).Hours() / 24))
77-
if daysToExpire > 0 && daysToExpire < 30 {
77+
isTrial := entitlements.Trial
78+
showWarningDays := 30
79+
if isTrial {
80+
showWarningDays = 7
81+
}
82+
isExpiringSoon := daysToExpire > 0 && daysToExpire < showWarningDays
83+
if isExpiringSoon {
7884
day := "day"
7985
if daysToExpire > 1 {
8086
day = "days"

enterprise/coderd/license/license_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,95 @@ func TestEntitlements(t *testing.T) {
130130
"Your license expires in 2 days.",
131131
)
132132
})
133+
134+
t.Run("Expiration warning for license expiring in 1 day", func(t *testing.T) {
135+
t.Parallel()
136+
db := dbfake.New()
137+
db.InsertLicense(context.Background(), database.InsertLicenseParams{
138+
JWT: coderdenttest.GenerateLicense(t, coderdenttest.LicenseOptions{
139+
Features: license.Features{
140+
codersdk.FeatureUserLimit: 100,
141+
codersdk.FeatureAuditLog: 1,
142+
},
143+
144+
GraceAt: time.Now().AddDate(0, 0, 1),
145+
ExpiresAt: time.Now().AddDate(0, 0, 5),
146+
}),
147+
Exp: time.Now().AddDate(0, 0, 5),
148+
})
149+
150+
entitlements, err := license.Entitlements(context.Background(), db, slog.Logger{}, 1, 1, coderdenttest.Keys, all)
151+
152+
require.NoError(t, err)
153+
require.True(t, entitlements.HasLicense)
154+
require.False(t, entitlements.Trial)
155+
156+
require.Equal(t, codersdk.EntitlementEntitled, entitlements.Features[codersdk.FeatureAuditLog].Entitlement)
157+
require.Contains(
158+
t, entitlements.Warnings,
159+
"Your license expires in 1 day.",
160+
)
161+
})
162+
163+
t.Run("Expiration warning for trials", func(t *testing.T) {
164+
t.Parallel()
165+
db := dbfake.New()
166+
db.InsertLicense(context.Background(), database.InsertLicenseParams{
167+
JWT: coderdenttest.GenerateLicense(t, coderdenttest.LicenseOptions{
168+
Features: license.Features{
169+
codersdk.FeatureUserLimit: 100,
170+
codersdk.FeatureAuditLog: 1,
171+
},
172+
173+
Trial: true,
174+
GraceAt: time.Now().AddDate(0, 0, 8),
175+
ExpiresAt: time.Now().AddDate(0, 0, 5),
176+
}),
177+
Exp: time.Now().AddDate(0, 0, 5),
178+
})
179+
180+
entitlements, err := license.Entitlements(context.Background(), db, slog.Logger{}, 1, 1, coderdenttest.Keys, all)
181+
182+
require.NoError(t, err)
183+
require.True(t, entitlements.HasLicense)
184+
require.True(t, entitlements.Trial)
185+
186+
require.Equal(t, codersdk.EntitlementEntitled, entitlements.Features[codersdk.FeatureAuditLog].Entitlement)
187+
require.NotContains( // it should not contain a warning since it is a trial license
188+
t, entitlements.Warnings,
189+
"Your license expires in 8 days.",
190+
)
191+
})
192+
193+
t.Run("Expiration warning for non trials", func(t *testing.T) {
194+
t.Parallel()
195+
db := dbfake.New()
196+
db.InsertLicense(context.Background(), database.InsertLicenseParams{
197+
JWT: coderdenttest.GenerateLicense(t, coderdenttest.LicenseOptions{
198+
Features: license.Features{
199+
codersdk.FeatureUserLimit: 100,
200+
codersdk.FeatureAuditLog: 1,
201+
},
202+
203+
GraceAt: time.Now().AddDate(0, 0, 30),
204+
ExpiresAt: time.Now().AddDate(0, 0, 5),
205+
}),
206+
Exp: time.Now().AddDate(0, 0, 5),
207+
})
208+
209+
entitlements, err := license.Entitlements(context.Background(), db, slog.Logger{}, 1, 1, coderdenttest.Keys, all)
210+
211+
require.NoError(t, err)
212+
require.True(t, entitlements.HasLicense)
213+
require.False(t, entitlements.Trial)
214+
215+
require.Equal(t, codersdk.EntitlementEntitled, entitlements.Features[codersdk.FeatureAuditLog].Entitlement)
216+
require.NotContains( // it should not contain a warning since it is a trial license
217+
t, entitlements.Warnings,
218+
"Your license expires in 30 days.",
219+
)
220+
})
221+
133222
t.Run("SingleLicenseNotEntitled", func(t *testing.T) {
134223
t.Parallel()
135224
db := dbfake.New()

0 commit comments

Comments
 (0)