Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: add test for subscription (#3843)

ecmadao106ba7ef3b8a
diff --git a/tests/subscription_test.go b/tests/subscription_test.go
new file mode 100644
index 0000000000..4af6e5432d
--- /dev/null
+++ b/tests/subscription_test.go
@@ -0,0 +1,82 @@
+package tests
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ "go.uber.org/zap/zapcore"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common/log"
+ "github.com/bytebase/bytebase/tests/fake"
+)
+
+type trial struct {
+ instanceCount int
+ expectInstanceCount int
+ plan api.PlanType
+ expectPlan api.PlanType
+ Days int
+}
+
+func TestSubscription(t *testing.T) {
+ a := require.New(t)
+ log.SetLevel(zapcore.DebugLevel)
+ ctx := context.Background()
+ ctl := &controller{}
+ dataDir := t.TempDir()
+ err := ctl.StartServerWithExternalPg(ctx, &config{
+ dataDir: dataDir,
+ vcsProviderCreator: fake.NewGitLab,
+ })
+ a.NoError(err)
+ defer ctl.Close(ctx)
+ err = ctl.Login()
+ a.NoError(err)
+
+ subscription, err := ctl.getSubscription()
+ a.NoError(err)
+ a.Equal(api.FREE, subscription.Plan)
+
+ trialList := []trial{
+ {
+ // Test trial the TEAM plan.
+ instanceCount: 20,
+ expectInstanceCount: 20,
+ plan: api.TEAM,
+ expectPlan: api.TEAM,
+ Days: 7,
+ },
+ {
+ // Test trial the ENTERPRISE plan.
+ instanceCount: 10,
+ expectInstanceCount: 10,
+ plan: api.ENTERPRISE,
+ expectPlan: api.ENTERPRISE,
+ Days: 7,
+ },
+ {
+ // Downgrade should be ignored.
+ instanceCount: 99,
+ expectInstanceCount: 10,
+ plan: api.TEAM,
+ expectPlan: api.ENTERPRISE,
+ Days: 7,
+ },
+ }
+
+ for _, trial := range trialList {
+ err = ctl.trialPlan(&api.TrialPlanCreate{
+ InstanceCount: trial.instanceCount,
+ Type: trial.plan,
+ Days: trial.Days,
+ })
+ a.NoError(err)
+
+ subscription, err = ctl.getSubscription()
+ a.NoError(err)
+ a.Equal(trial.expectPlan, subscription.Plan)
+ a.Equal(trial.expectInstanceCount, subscription.InstanceCount)
+ }
+}
diff --git a/tests/tests.go b/tests/tests.go
index 05529881a7..7a4e0e015d 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -853,6 +853,29 @@ func (ctl *controller) removeLicense() error {
return nil
}
+func (ctl *controller) getSubscription() (*enterpriseAPI.Subscription, error) {
+ body, err := ctl.get("/subscription", nil)
+ if err != nil {
+ return nil, err
+ }
+
+ subscription := new(enterpriseAPI.Subscription)
+ if err = jsonapi.UnmarshalPayload(body, subscription); err != nil {
+ return nil, errors.Wrap(err, "fail to unmarshal get subscription response")
+ }
+ return subscription, nil
+}
+
+func (ctl *controller) trialPlan(trial *api.TrialPlanCreate) error {
+ buf := new(bytes.Buffer)
+ if err := jsonapi.MarshalPayload(buf, trial); err != nil {
+ return errors.Wrap(err, "failed to marshal subscription patch")
+ }
+
+ _, err := ctl.post("/subscription/trial", buf)
+ return err
+}
+
func (ctl *controller) switchPlan(patch *enterpriseAPI.SubscriptionPatch) error {
buf := new(bytes.Buffer)
if err := jsonapi.MarshalPayload(buf, patch); err != nil {