Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
taratatach committed Sep 15, 2023
1 parent 96f4e5e commit 732e98c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
9 changes: 9 additions & 0 deletions model/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,15 @@ func (i *Instance) MovedError() *jsonapi.Error {
return &jerr
}

func (i *Instance) HasPremiumLinksEnabled() bool {
if ctxSettings, ok := i.SettingsContext(); ok {
if enabled, ok := ctxSettings["enable_premium_links"].(bool); ok {
return enabled
}
}
return false
}

// ensure Instance implements couchdb.Doc
var (
_ couchdb.Doc = &Instance{}
Expand Down
6 changes: 3 additions & 3 deletions model/notification/center/notification_center.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ func init() {
_ = PushStack(domain, NotificationDiskQuota, n)
})

oauth.RegisterClientsLimitAlertCallback(func(i *instance.Instance, clientName string, clientsLimit int, enablePremiumLinks bool) {
oauth.RegisterClientsLimitAlertCallback(func(i *instance.Instance, clientName string, clientsLimit int) {
devicesLink := i.SubDomain(consts.SettingsSlug)
devicesLink.Fragment = "/connectedDevices"

var offersLink string
if enablePremiumLinks {
if i.HasPremiumLinksEnabled() {
var err error
offersLink, err = i.ManagerURL(instance.ManagerPremiumURL)
if err != nil {
return
i.Logger().Errorf("Could not get instance Premium Manager URL: %s", err.Error())
}
}

Expand Down
12 changes: 6 additions & 6 deletions model/oauth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,14 @@ func (c *Client) Create(i *instance.Instance, opts ...CreateOptions) *ClientRegi
Errorf("Failed to get the OAuth clients limit: %s", err)
return nil
}

limit := -1
if clientsLimit, ok := flags.M["cozy.oauthclients.max"].(float64); ok && clientsLimit >= 0 {
limit = int(clientsLimit)
}
_, exceeded := CheckOAuthClientsLimitReached(i, limit)
if exceeded {
enablePremiumLinks, _ := flags.M["enable_premium_links"].(bool)
PushClientsLimitAlert(i, c.ClientName, limit, enablePremiumLinks)
PushClientsLimitAlert(i, c.ClientName, limit)
}
return nil
}
Expand Down Expand Up @@ -851,19 +851,19 @@ func CheckOAuthClientsLimitReached(i *instance.Instance, limit int) (reached, ex
return
}

var cbClientsLimitAlert func(i *instance.Instance, clientName string, clientsLimit int, enablePremiumLinks bool)
var cbClientsLimitAlert func(i *instance.Instance, clientName string, clientsLimit int)

// RegisterClientsLimitAlertCallback allows to register a callback function
// called when the connected OAuth clients limit (if present) is exceeded.
func RegisterClientsLimitAlertCallback(cb func(i *instance.Instance, clientName string, clientsLimit int, enablePremiumLinks bool)) {
func RegisterClientsLimitAlertCallback(cb func(i *instance.Instance, clientName string, clientsLimit int)) {
cbClientsLimitAlert = cb
}

// PushClientsLimitAlert can be used to notify when the connected OAuth clients
// limit (if present) is exceeded.
func PushClientsLimitAlert(i *instance.Instance, clientName string, clientsLimit int, enablePremiumLinks bool) {
func PushClientsLimitAlert(i *instance.Instance, clientName string, clientsLimit int) {
if cbClientsLimitAlert != nil {
cbClientsLimitAlert(i, clientName, clientsLimit, enablePremiumLinks)
cbClientsLimitAlert(i, clientName, clientsLimit)
}
}

Expand Down
27 changes: 16 additions & 11 deletions tests/testutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"flag"
"fmt"
"io"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -421,14 +422,16 @@ func WithManager(t *testing.T, inst *instance.Instance) (shouldRemoveUUID bool)
require.True(t, ok, "Could not enable test instance manager: manager_url config is required")
require.NotEmpty(t, managerURL, "Could not enable test instance manager: manager_url config is required")

if inst.FeatureFlags == nil {
inst.FeatureFlags = map[string]interface{}{}
}
inst.FeatureFlags["enable_premium_links"] = true
was := config["enable_premium_links"]
config["enable_premium_links"] = true

t.Cleanup(func() {
err := DisableManager(inst, shouldRemoveUUID)
require.NoError(t, err)
config["enable_premium_links"] = was

if shouldRemoveUUID {
inst.UUID = ""
require.NoError(t, instance.Update(inst))
}
})

err := instance.Update(inst)
Expand All @@ -438,16 +441,18 @@ func WithManager(t *testing.T, inst *instance.Instance) (shouldRemoveUUID bool)
}

func DisableManager(inst *instance.Instance, shouldRemoveUUID bool) error {
if inst.FeatureFlags == nil {
inst.FeatureFlags = map[string]interface{}{}
config, ok := inst.SettingsContext()
if !ok {
return fmt.Errorf("Could not disable test instance manager: could not fetch test instance settings context")
}
inst.FeatureFlags["enable_premium_links"] = false

config["enable_premium_links"] = false

if shouldRemoveUUID {
inst.UUID = ""
return instance.Update(inst)
}

return instance.Update(inst)
return nil
}

func WithOAuthClientsLimit(t *testing.T, inst *instance.Instance, limit float64) {
Expand Down
4 changes: 2 additions & 2 deletions web/auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ func (a *AuthorizeHTTPHandler) authorizeForm(c echo.Context) error {
connectedDevicesURL.Fragment = "/connectedDevices"
manageDevicesURL = connectedDevicesURL.String()

if enablePremiumLinks, ok := flags.M["enable_premium_links"].(bool); ok && enablePremiumLinks {
if inst.HasPremiumLinksEnabled() {
if premiumURL, err = inst.ManagerURL(instance.ManagerPremiumURL); err != nil {
return fmt.Errorf("Could not get Premium Manager URL for instance %s: %w", inst.DomainName(), err)
inst.Logger().Errorf("Could not get instance Premium Manager URL: %s", err.Error())
}
}

Expand Down
4 changes: 2 additions & 2 deletions web/settings/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ func (h *HTTPHandler) limitExceeded(c echo.Context) error {
connectedDevicesURL.Fragment = "/connectedDevices"

var premiumURL string
if enablePremiumLinks, ok := flags.M["enable_premium_links"].(bool); ok && enablePremiumLinks {
if inst.HasPremiumLinksEnabled() {
isFlagship, _ := strconv.ParseBool(c.QueryParam("isFlagship"))
iapEnabled, _ := flags.M["flagship.iap.enabled"].(bool)
if !isFlagship || iapEnabled {
var err error
if premiumURL, err = inst.ManagerURL(instance.ManagerPremiumURL); err != nil {
return fmt.Errorf("Could not get Premium Manager URL for instance %s: %w", inst.DomainName(), err)
inst.Logger().Errorf("Could not get instance Premium Manager URL: %s", err.Error())
}
}
}
Expand Down

0 comments on commit 732e98c

Please sign in to comment.