Skip to content

Commit

Permalink
test: Add helper to set an OAuth clients limit
Browse files Browse the repository at this point in the history
  With a cleanup callback to set it back to its previous value.
  • Loading branch information
taratatach committed Sep 14, 2023
1 parent f95d8f3 commit 8497454
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
20 changes: 20 additions & 0 deletions tests/testutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,23 @@ func DisableManager(inst *instance.Instance, shouldRemoveUUID bool) error {

return instance.Update(inst)
}

func WithOAuthClientsLimit(t *testing.T, inst *instance.Instance, limit float64) {
flags := inst.FeatureFlags
if flags == nil {
flags = map[string]interface{}{}
}

was := flags["cozy.oauthclients.max"]

flags["cozy.oauthclients.max"] = limit
inst.FeatureFlags = flags
err := instance.Update(inst)
require.NoError(t, err, "Could not set OAuth clients limit")

t.Cleanup(func() {
flags["cozy.oauthclients.max"] = was
inst.FeatureFlags = flags
require.NoError(t, instance.Update(inst))
})
}
4 changes: 1 addition & 3 deletions web/apps/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ func TestApps(t *testing.T) {
}
require.Nil(t, flagship.Create(testInstance, oauth.NotPending))

var limit float64
testInstance.FeatureFlags = map[string]interface{}{"cozy.oauthclients.max": limit}
require.NoError(t, instance.Update(testInstance))
testutils.WithOAuthClientsLimit(t, testInstance, 0)

e = e.Builder(func(r *httpexpect.Request) {
r.WithCookie("cozysessid", cozysessID)
Expand Down
14 changes: 4 additions & 10 deletions web/settings/clients_usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package settings_test
import (
"testing"

"github.com/cozy/cozy-stack/model/instance"
"github.com/cozy/cozy-stack/model/instance/lifecycle"
"github.com/cozy/cozy-stack/model/oauth"
csettings "github.com/cozy/cozy-stack/model/settings"
Expand All @@ -14,11 +13,6 @@ import (
"github.com/stretchr/testify/require"
)

func setClientsLimit(t *testing.T, inst *instance.Instance, limit float64) {
inst.FeatureFlags = map[string]interface{}{"cozy.oauthclients.max": limit}
require.NoError(t, instance.Update(inst))
}

func TestClientsUsage(t *testing.T) {
config.UseTestFile(t)
testutils.NeedCouchdb(t)
Expand All @@ -45,7 +39,7 @@ func TestClientsUsage(t *testing.T) {
require.Nil(t, flagship.Create(testInstance, oauth.NotPending))

t.Run("WithoutLimit", func(t *testing.T) {
setClientsLimit(t, testInstance, -1)
testutils.WithOAuthClientsLimit(t, testInstance, -1)

e := testutils.CreateTestClient(t, ts.URL)
obj := e.GET("/settings/clients-usage").
Expand All @@ -66,7 +60,7 @@ func TestClientsUsage(t *testing.T) {
})

t.Run("WithLimitNotReached", func(t *testing.T) {
setClientsLimit(t, testInstance, 2)
testutils.WithOAuthClientsLimit(t, testInstance, 2)

e := testutils.CreateTestClient(t, ts.URL)
obj := e.GET("/settings/clients-usage").
Expand All @@ -87,7 +81,7 @@ func TestClientsUsage(t *testing.T) {
})

t.Run("WithLimitReached", func(t *testing.T) {
setClientsLimit(t, testInstance, 1)
testutils.WithOAuthClientsLimit(t, testInstance, 1)

e := testutils.CreateTestClient(t, ts.URL)
obj := e.GET("/settings/clients-usage").
Expand All @@ -108,7 +102,7 @@ func TestClientsUsage(t *testing.T) {
})

t.Run("WithLimitExceeded", func(t *testing.T) {
setClientsLimit(t, testInstance, 0)
testutils.WithOAuthClientsLimit(t, testInstance, 0)

e := testutils.CreateTestClient(t, ts.URL)
obj := e.GET("/settings/clients-usage").
Expand Down
8 changes: 2 additions & 6 deletions web/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,9 +906,7 @@ func TestSettings(t *testing.T) {
t.Run("ClientsLimitExceededWithLimitExceeded", func(t *testing.T) {
e := testutils.CreateTestClient(t, tsURL)

var limit float64
testInstance.FeatureFlags = map[string]interface{}{"cozy.oauthclients.max": limit}
require.NoError(t, instance.Update(testInstance))
testutils.WithOAuthClientsLimit(t, testInstance, 0)

// Create the OAuth client for the flagship app
flagship := oauth.Client{
Expand Down Expand Up @@ -964,9 +962,7 @@ func TestSettings(t *testing.T) {
clients, _, err := oauth.GetConnectedUserClients(testInstance, 100, "")
require.NoError(t, err)

limit := float64(len(clients))
testInstance.FeatureFlags = map[string]interface{}{"cozy.oauthclients.max": limit}
require.NoError(t, instance.Update(testInstance))
testutils.WithOAuthClientsLimit(t, testInstance, float64(len(clients)))

e.GET("/settings/clients/limit-exceeded").
WithHeader("Authorization", "Bearer "+token).
Expand Down

0 comments on commit 8497454

Please sign in to comment.