@@ -102,6 +102,123 @@ func TestEntitlements(t *testing.T) {
102
102
fmt .Sprintf ("%s is enabled but your license for this feature is expired." ,codersdk .FeatureAuditLog .Humanize ()),
103
103
)
104
104
})
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 ().AddDate (0 ,0 ,2 ),
116
+ ExpiresAt :time .Now ().AddDate (0 ,0 ,5 ),
117
+ }),
118
+ Exp :time .Now ().AddDate (0 ,0 ,5 ),
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 .EntitlementEntitled ,entitlements .Features [codersdk .FeatureAuditLog ].Entitlement )
128
+ require .Contains (
129
+ t ,entitlements .Warnings ,
130
+ "Your license expires in 2 days." ,
131
+ )
132
+ })
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
+
105
222
t .Run ("SingleLicenseNotEntitled" ,func (t * testing.T ) {
106
223
t .Parallel ()
107
224
db := dbfake .New ()
@@ -164,16 +281,18 @@ func TestEntitlements(t *testing.T) {
164
281
Features : license.Features {
165
282
codersdk .FeatureUserLimit :10 ,
166
283
},
284
+ GraceAt :time .Now ().Add (59 * 24 * time .Hour ),
167
285
}),
168
- Exp :time .Now ().Add (time .Hour ),
286
+ Exp :time .Now ().Add (60 * 24 * time .Hour ),
169
287
})
170
288
db .InsertLicense (context .Background (), database.InsertLicenseParams {
171
289
JWT :coderdenttest .GenerateLicense (t , coderdenttest.LicenseOptions {
172
290
Features : license.Features {
173
291
codersdk .FeatureUserLimit :1 ,
174
292
},
293
+ GraceAt :time .Now ().Add (59 * 24 * time .Hour ),
175
294
}),
176
- Exp :time .Now ().Add (time .Hour ),
295
+ Exp :time .Now ().Add (60 * 24 * time .Hour ),
177
296
})
178
297
entitlements ,err := license .Entitlements (context .Background (),db , slog.Logger {},1 ,1 ,coderdenttest .Keys ,empty )
179
298
require .NoError (t ,err )