2022-10-07 00:28:22 +00:00
package license_test
import (
"context"
"fmt"
"testing"
"time"
2023-08-02 14:31:25 +00:00
"github.com/google/uuid"
2022-10-07 00:28:22 +00:00
"github.com/stretchr/testify/require"
"cdr.dev/slog"
2023-08-18 18:55:43 +00:00
"github.com/coder/coder/v2/coderd/database"
2023-10-30 17:42:20 +00:00
"github.com/coder/coder/v2/coderd/database/dbmem"
2023-09-01 16:50:12 +00:00
"github.com/coder/coder/v2/coderd/database/dbtime"
2023-08-18 18:55:43 +00:00
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
"github.com/coder/coder/v2/enterprise/coderd/license"
2022-10-07 00:28:22 +00:00
)
func TestEntitlements ( t * testing . T ) {
t . Parallel ( )
2023-01-17 23:04:29 +00:00
all := make ( map [ codersdk . FeatureName ] bool )
for _ , n := range codersdk . FeatureNames {
all [ n ] = true
2022-10-07 00:28:22 +00:00
}
2023-01-17 23:04:29 +00:00
empty := map [ codersdk . FeatureName ] bool { }
2022-10-07 00:28:22 +00:00
t . Run ( "Defaults" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-25 00:46:24 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , all )
2022-10-07 00:28:22 +00:00
require . NoError ( t , err )
require . False ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
for _ , featureName := range codersdk . FeatureNames {
require . False ( t , entitlements . Features [ featureName ] . Enabled )
require . Equal ( t , codersdk . EntitlementNotEntitled , entitlements . Features [ featureName ] . Entitlement )
}
} )
2023-04-26 20:47:46 +00:00
t . Run ( "Always return the current user count" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2023-04-26 20:47:46 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , all )
require . NoError ( t , err )
require . False ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
require . Equal ( t , * entitlements . Features [ codersdk . FeatureUserLimit ] . Actual , int64 ( 0 ) )
} )
2022-10-07 00:28:22 +00:00
t . Run ( "SingleLicenseNothing" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-07 00:28:22 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions { } ) ,
Exp : time . Now ( ) . Add ( time . Hour ) ,
} )
2023-01-17 23:04:29 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , empty )
2022-10-07 00:28:22 +00:00
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
for _ , featureName := range codersdk . FeatureNames {
require . False ( t , entitlements . Features [ featureName ] . Enabled )
require . Equal ( t , codersdk . EntitlementNotEntitled , entitlements . Features [ featureName ] . Entitlement )
}
} )
t . Run ( "SingleLicenseAll" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-07 00:28:22 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
2023-01-17 23:04:29 +00:00
Features : func ( ) license . Features {
f := make ( license . Features )
for _ , name := range codersdk . FeatureNames {
f [ name ] = 1
}
return f
} ( ) ,
2022-10-07 00:28:22 +00:00
} ) ,
Exp : time . Now ( ) . Add ( time . Hour ) ,
} )
2023-01-17 23:04:29 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , empty )
2022-10-07 00:28:22 +00:00
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
for _ , featureName := range codersdk . FeatureNames {
2022-12-06 18:38:38 +00:00
require . Equal ( t , codersdk . EntitlementEntitled , entitlements . Features [ featureName ] . Entitlement , featureName )
2022-10-07 00:28:22 +00:00
}
} )
t . Run ( "SingleLicenseGrace" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-07 00:28:22 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
2023-01-17 23:04:29 +00:00
Features : license . Features {
codersdk . FeatureUserLimit : 100 ,
codersdk . FeatureAuditLog : 1 ,
} ,
GraceAt : time . Now ( ) . Add ( - time . Hour ) ,
ExpiresAt : time . Now ( ) . Add ( time . Hour ) ,
2022-10-07 00:28:22 +00:00
} ) ,
Exp : time . Now ( ) . Add ( time . Hour ) ,
} )
2022-10-25 00:46:24 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , all )
2022-10-07 00:28:22 +00:00
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
2023-01-17 23:04:29 +00:00
require . Equal ( t , codersdk . EntitlementGracePeriod , entitlements . Features [ codersdk . FeatureAuditLog ] . Entitlement )
require . Contains (
t , entitlements . Warnings ,
fmt . Sprintf ( "%s is enabled but your license for this feature is expired." , codersdk . FeatureAuditLog . Humanize ( ) ) ,
)
2022-10-07 00:28:22 +00:00
} )
2023-04-26 19:39:39 +00:00
t . Run ( "Expiration warning" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2023-04-26 19:39:39 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
Features : license . Features {
codersdk . FeatureUserLimit : 100 ,
codersdk . FeatureAuditLog : 1 ,
} ,
GraceAt : time . Now ( ) . AddDate ( 0 , 0 , 2 ) ,
ExpiresAt : time . Now ( ) . AddDate ( 0 , 0 , 5 ) ,
} ) ,
Exp : time . Now ( ) . AddDate ( 0 , 0 , 5 ) ,
} )
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , all )
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
require . Equal ( t , codersdk . EntitlementEntitled , entitlements . Features [ codersdk . FeatureAuditLog ] . Entitlement )
require . Contains (
t , entitlements . Warnings ,
"Your license expires in 2 days." ,
)
} )
t . Run ( "Expiration warning for license expiring in 1 day" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2023-04-26 19:39:39 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
Features : license . Features {
codersdk . FeatureUserLimit : 100 ,
codersdk . FeatureAuditLog : 1 ,
} ,
GraceAt : time . Now ( ) . AddDate ( 0 , 0 , 1 ) ,
ExpiresAt : time . Now ( ) . AddDate ( 0 , 0 , 5 ) ,
} ) ,
Exp : time . Now ( ) . AddDate ( 0 , 0 , 5 ) ,
} )
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , all )
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
require . Equal ( t , codersdk . EntitlementEntitled , entitlements . Features [ codersdk . FeatureAuditLog ] . Entitlement )
require . Contains (
t , entitlements . Warnings ,
"Your license expires in 1 day." ,
)
} )
t . Run ( "Expiration warning for trials" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2023-04-26 19:39:39 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
Features : license . Features {
codersdk . FeatureUserLimit : 100 ,
codersdk . FeatureAuditLog : 1 ,
} ,
Trial : true ,
GraceAt : time . Now ( ) . AddDate ( 0 , 0 , 8 ) ,
ExpiresAt : time . Now ( ) . AddDate ( 0 , 0 , 5 ) ,
} ) ,
Exp : time . Now ( ) . AddDate ( 0 , 0 , 5 ) ,
} )
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , all )
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . True ( t , entitlements . Trial )
require . Equal ( t , codersdk . EntitlementEntitled , entitlements . Features [ codersdk . FeatureAuditLog ] . Entitlement )
require . NotContains ( // it should not contain a warning since it is a trial license
t , entitlements . Warnings ,
"Your license expires in 8 days." ,
)
} )
t . Run ( "Expiration warning for non trials" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2023-04-26 19:39:39 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
Features : license . Features {
codersdk . FeatureUserLimit : 100 ,
codersdk . FeatureAuditLog : 1 ,
} ,
GraceAt : time . Now ( ) . AddDate ( 0 , 0 , 30 ) ,
ExpiresAt : time . Now ( ) . AddDate ( 0 , 0 , 5 ) ,
} ) ,
Exp : time . Now ( ) . AddDate ( 0 , 0 , 5 ) ,
} )
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , all )
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
require . Equal ( t , codersdk . EntitlementEntitled , entitlements . Features [ codersdk . FeatureAuditLog ] . Entitlement )
require . NotContains ( // it should not contain a warning since it is a trial license
t , entitlements . Warnings ,
"Your license expires in 30 days." ,
)
} )
2022-10-07 00:28:22 +00:00
t . Run ( "SingleLicenseNotEntitled" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-07 00:28:22 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions { } ) ,
Exp : time . Now ( ) . Add ( time . Hour ) ,
} )
2022-10-25 00:46:24 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , all )
2022-10-07 00:28:22 +00:00
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
for _ , featureName := range codersdk . FeatureNames {
if featureName == codersdk . FeatureUserLimit {
continue
}
2022-10-17 13:43:30 +00:00
if featureName == codersdk . FeatureHighAvailability {
continue
}
2023-10-03 15:27:02 +00:00
if featureName == codersdk . FeatureMultipleExternalAuth {
2022-10-25 00:46:24 +00:00
continue
}
2023-01-17 23:04:29 +00:00
niceName := featureName . Humanize ( )
2022-10-07 00:28:22 +00:00
// Ensures features that are not entitled are properly disabled.
require . False ( t , entitlements . Features [ featureName ] . Enabled )
require . Equal ( t , codersdk . EntitlementNotEntitled , entitlements . Features [ featureName ] . Entitlement )
require . Contains ( t , entitlements . Warnings , fmt . Sprintf ( "%s is enabled but your license is not entitled to this feature." , niceName ) )
}
} )
t . Run ( "TooManyUsers" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2023-08-02 14:31:25 +00:00
activeUser1 , err := db . InsertUser ( context . Background ( ) , database . InsertUserParams {
ID : uuid . New ( ) ,
2023-01-23 11:14:47 +00:00
Username : "test1" ,
LoginType : database . LoginTypePassword ,
2022-10-18 03:07:11 +00:00
} )
2023-08-02 14:31:25 +00:00
require . NoError ( t , err )
_ , err = db . UpdateUserStatus ( context . Background ( ) , database . UpdateUserStatusParams {
ID : activeUser1 . ID ,
Status : database . UserStatusActive ,
2023-09-01 16:50:12 +00:00
UpdatedAt : dbtime . Now ( ) ,
2023-08-02 14:31:25 +00:00
} )
require . NoError ( t , err )
activeUser2 , err := db . InsertUser ( context . Background ( ) , database . InsertUserParams {
ID : uuid . New ( ) ,
2023-01-23 11:14:47 +00:00
Username : "test2" ,
LoginType : database . LoginTypePassword ,
2022-10-18 03:07:11 +00:00
} )
2023-08-02 14:31:25 +00:00
require . NoError ( t , err )
_ , err = db . UpdateUserStatus ( context . Background ( ) , database . UpdateUserStatusParams {
ID : activeUser2 . ID ,
Status : database . UserStatusActive ,
2023-09-01 16:50:12 +00:00
UpdatedAt : dbtime . Now ( ) ,
2023-08-02 14:31:25 +00:00
} )
require . NoError ( t , err )
_ , err = db . InsertUser ( context . Background ( ) , database . InsertUserParams {
ID : uuid . New ( ) ,
Username : "dormant-user" ,
LoginType : database . LoginTypePassword ,
} )
require . NoError ( t , err )
2022-10-07 00:28:22 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
2023-01-17 23:04:29 +00:00
Features : license . Features {
codersdk . FeatureUserLimit : 1 ,
} ,
2022-10-07 00:28:22 +00:00
} ) ,
Exp : time . Now ( ) . Add ( time . Hour ) ,
} )
2023-01-17 23:04:29 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , empty )
2022-10-07 00:28:22 +00:00
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . Contains ( t , entitlements . Warnings , "Your deployment has 2 active users but is only licensed for 1." )
} )
2022-10-07 01:59:25 +00:00
t . Run ( "MaximizeUserLimit" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-07 01:59:25 +00:00
db . InsertUser ( context . Background ( ) , database . InsertUserParams { } )
db . InsertUser ( context . Background ( ) , database . InsertUserParams { } )
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
2023-01-17 23:04:29 +00:00
Features : license . Features {
codersdk . FeatureUserLimit : 10 ,
} ,
2023-04-26 19:39:39 +00:00
GraceAt : time . Now ( ) . Add ( 59 * 24 * time . Hour ) ,
2022-10-07 01:59:25 +00:00
} ) ,
2023-04-26 19:39:39 +00:00
Exp : time . Now ( ) . Add ( 60 * 24 * time . Hour ) ,
2022-10-07 01:59:25 +00:00
} )
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
2023-01-17 23:04:29 +00:00
Features : license . Features {
codersdk . FeatureUserLimit : 1 ,
} ,
2023-04-26 19:39:39 +00:00
GraceAt : time . Now ( ) . Add ( 59 * 24 * time . Hour ) ,
2022-10-07 01:59:25 +00:00
} ) ,
2023-04-26 19:39:39 +00:00
Exp : time . Now ( ) . Add ( 60 * 24 * time . Hour ) ,
2022-10-07 01:59:25 +00:00
} )
2023-01-17 23:04:29 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , empty )
2022-10-07 01:59:25 +00:00
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . Empty ( t , entitlements . Warnings )
} )
2022-10-07 00:28:22 +00:00
t . Run ( "MultipleLicenseEnabled" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-07 00:28:22 +00:00
// One trial
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
Exp : time . Now ( ) . Add ( time . Hour ) ,
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
Trial : true ,
} ) ,
} )
// One not
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
Exp : time . Now ( ) . Add ( time . Hour ) ,
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
Trial : false ,
} ) ,
} )
2023-01-17 23:04:29 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , empty )
2022-10-07 00:28:22 +00:00
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
} )
t . Run ( "AllFeatures" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-07 00:28:22 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
Exp : time . Now ( ) . Add ( time . Hour ) ,
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
AllFeatures : true ,
} ) ,
} )
2022-10-25 00:46:24 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , all )
2022-10-07 00:28:22 +00:00
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
for _ , featureName := range codersdk . FeatureNames {
if featureName == codersdk . FeatureUserLimit {
continue
}
require . True ( t , entitlements . Features [ featureName ] . Enabled )
require . Equal ( t , codersdk . EntitlementEntitled , entitlements . Features [ featureName ] . Entitlement )
}
} )
2022-10-17 13:43:30 +00:00
2023-09-22 05:02:28 +00:00
t . Run ( "AllFeaturesAlwaysEnable" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2023-09-22 05:02:28 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
Exp : dbtime . Now ( ) . Add ( time . Hour ) ,
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
AllFeatures : true ,
} ) ,
} )
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , empty )
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
for _ , featureName := range codersdk . FeatureNames {
if featureName == codersdk . FeatureUserLimit {
continue
}
feature := entitlements . Features [ featureName ]
require . Equal ( t , featureName . AlwaysEnable ( ) , feature . Enabled )
require . Equal ( t , codersdk . EntitlementEntitled , feature . Entitlement )
}
} )
t . Run ( "AllFeaturesGrace" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2023-09-22 05:02:28 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
Exp : dbtime . Now ( ) . Add ( time . Hour ) ,
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
AllFeatures : true ,
GraceAt : dbtime . Now ( ) . Add ( - time . Hour ) ,
ExpiresAt : dbtime . Now ( ) . Add ( time . Hour ) ,
} ) ,
} )
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 1 , coderdenttest . Keys , all )
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . False ( t , entitlements . Trial )
for _ , featureName := range codersdk . FeatureNames {
if featureName == codersdk . FeatureUserLimit {
continue
}
require . True ( t , entitlements . Features [ featureName ] . Enabled )
require . Equal ( t , codersdk . EntitlementGracePeriod , entitlements . Features [ featureName ] . Entitlement )
}
} )
2022-10-17 13:43:30 +00:00
t . Run ( "MultipleReplicasNoLicense" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-25 00:46:24 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 2 , 1 , coderdenttest . Keys , all )
2022-10-17 13:43:30 +00:00
require . NoError ( t , err )
require . False ( t , entitlements . HasLicense )
require . Len ( t , entitlements . Errors , 1 )
require . Equal ( t , "You have multiple replicas but high availability is an Enterprise feature. You will be unable to connect to workspaces." , entitlements . Errors [ 0 ] )
} )
t . Run ( "MultipleReplicasNotEntitled" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-17 13:43:30 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
Exp : time . Now ( ) . Add ( time . Hour ) ,
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
2023-01-17 23:04:29 +00:00
Features : license . Features {
codersdk . FeatureAuditLog : 1 ,
} ,
2022-10-17 13:43:30 +00:00
} ) ,
} )
2023-01-17 23:04:29 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 2 , 1 , coderdenttest . Keys , map [ codersdk . FeatureName ] bool {
2022-10-17 13:43:30 +00:00
codersdk . FeatureHighAvailability : true ,
} )
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . Len ( t , entitlements . Errors , 1 )
require . Equal ( t , "You have multiple replicas but your license is not entitled to high availability. You will be unable to connect to workspaces." , entitlements . Errors [ 0 ] )
} )
t . Run ( "MultipleReplicasGrace" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-17 13:43:30 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
2023-01-17 23:04:29 +00:00
Features : license . Features {
codersdk . FeatureHighAvailability : 1 ,
} ,
GraceAt : time . Now ( ) . Add ( - time . Hour ) ,
ExpiresAt : time . Now ( ) . Add ( time . Hour ) ,
2022-10-17 13:43:30 +00:00
} ) ,
Exp : time . Now ( ) . Add ( time . Hour ) ,
} )
2023-01-17 23:04:29 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 2 , 1 , coderdenttest . Keys , map [ codersdk . FeatureName ] bool {
2022-10-17 13:43:30 +00:00
codersdk . FeatureHighAvailability : true ,
} )
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . Len ( t , entitlements . Warnings , 1 )
require . Equal ( t , "You have multiple replicas but your license for high availability is expired. Reduce to one replica or workspace connections will stop working." , entitlements . Warnings [ 0 ] )
} )
2022-10-25 00:46:24 +00:00
t . Run ( "MultipleGitAuthNoLicense" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-25 00:46:24 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 2 , coderdenttest . Keys , all )
require . NoError ( t , err )
require . False ( t , entitlements . HasLicense )
require . Len ( t , entitlements . Errors , 1 )
2023-10-03 15:27:02 +00:00
require . Equal ( t , "You have multiple External Auth Providers configured but this is an Enterprise feature. Reduce to one." , entitlements . Errors [ 0 ] )
2022-10-25 00:46:24 +00:00
} )
t . Run ( "MultipleGitAuthNotEntitled" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-25 00:46:24 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
Exp : time . Now ( ) . Add ( time . Hour ) ,
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
2023-01-17 23:04:29 +00:00
Features : license . Features {
codersdk . FeatureAuditLog : 1 ,
} ,
2022-10-25 00:46:24 +00:00
} ) ,
} )
2023-01-17 23:04:29 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 2 , coderdenttest . Keys , map [ codersdk . FeatureName ] bool {
2023-10-03 15:27:02 +00:00
codersdk . FeatureMultipleExternalAuth : true ,
2022-10-25 00:46:24 +00:00
} )
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . Len ( t , entitlements . Errors , 1 )
2023-10-03 15:27:02 +00:00
require . Equal ( t , "You have multiple External Auth Providers configured but your license is limited at one." , entitlements . Errors [ 0 ] )
2022-10-25 00:46:24 +00:00
} )
t . Run ( "MultipleGitAuthGrace" , func ( t * testing . T ) {
t . Parallel ( )
2023-10-30 17:42:20 +00:00
db := dbmem . New ( )
2022-10-25 00:46:24 +00:00
db . InsertLicense ( context . Background ( ) , database . InsertLicenseParams {
JWT : coderdenttest . GenerateLicense ( t , coderdenttest . LicenseOptions {
2023-01-17 23:04:29 +00:00
GraceAt : time . Now ( ) . Add ( - time . Hour ) ,
ExpiresAt : time . Now ( ) . Add ( time . Hour ) ,
Features : license . Features {
2023-10-03 15:27:02 +00:00
codersdk . FeatureMultipleExternalAuth : 1 ,
2023-01-17 23:04:29 +00:00
} ,
2022-10-25 00:46:24 +00:00
} ) ,
Exp : time . Now ( ) . Add ( time . Hour ) ,
} )
2023-01-17 23:04:29 +00:00
entitlements , err := license . Entitlements ( context . Background ( ) , db , slog . Logger { } , 1 , 2 , coderdenttest . Keys , map [ codersdk . FeatureName ] bool {
2023-10-03 15:27:02 +00:00
codersdk . FeatureMultipleExternalAuth : true ,
2022-10-25 00:46:24 +00:00
} )
require . NoError ( t , err )
require . True ( t , entitlements . HasLicense )
require . Len ( t , entitlements . Warnings , 1 )
2023-10-03 15:27:02 +00:00
require . Equal ( t , "You have multiple External Auth Providers configured but your license is expired. Reduce to one." , entitlements . Warnings [ 0 ] )
2022-10-25 00:46:24 +00:00
} )
2022-10-07 00:28:22 +00:00
}