Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
feat/enterpriseportal: support DevOnly in list subscriptions, treat i…
Browse files Browse the repository at this point in the history
…nternal as prod
  • Loading branch information
bobheadxi committed Jul 23, 2024
1 parent 31b271b commit c236467
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 32 deletions.
31 changes: 26 additions & 5 deletions cmd/enterprise-portal/internal/dotcomdb/dotcomdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ FROM product_subscriptions subscription
}
if opts.DevOnly {
// '&&' operator: overlap (have elements in common)
c := fmt.Sprintf("ARRAY['%s','%s'] && MAX(active_license.license_tags)",
licensing.DevTag, licensing.InternalTag)
c := fmt.Sprintf("ARRAY['%s'] && MAX(active_license.license_tags)",
licensing.DevTag)
if conds.havingClause != "" {
clauses = append(clauses, "AND "+c)
} else {
Expand Down Expand Up @@ -347,8 +347,8 @@ LEFT JOIN product_subscriptions subscriptions
}
if opts.DevOnly {
// '&&' operator: overlap (have elements in common)
c := fmt.Sprintf("ARRAY['%s','%s'] && licenses.license_tags",
licensing.DevTag, licensing.InternalTag)
c := fmt.Sprintf("ARRAY['%s'] && licenses.license_tags",
licensing.DevTag)
if conds.whereClause != "" {
clauses = append(clauses, "AND "+c)
} else {
Expand Down Expand Up @@ -484,7 +484,12 @@ type ListEnterpriseSubscriptionsOptions struct {
//
// If no IDs are given, it returns all subscriptions.
func (r *Reader) ListEnterpriseSubscriptions(ctx context.Context, opts ListEnterpriseSubscriptionsOptions) ([]*SubscriptionAttributes, error) {
query := `SELECT id, created_at, archived_at FROM product_subscriptions WHERE true`
query := `
SELECT
id, created_at, archived_at
FROM
product_subscriptions
WHERE true`
namedArgs := pgx.NamedArgs{}
if len(opts.SubscriptionIDs) > 0 {
query += "\nAND id = ANY(@ids)"
Expand All @@ -495,6 +500,22 @@ func (r *Reader) ListEnterpriseSubscriptions(ctx context.Context, opts ListEnter
} else {
query += "\nAND archived_at IS NULL"
}
var licenseCond string
if r.opts.DevOnly {
licenseCond = fmt.Sprintf("'%s' = ANY(product_licenses.license_tags)", licensing.DevTag)
} else {
licenseCond = fmt.Sprintf("NOT '%s' = ANY(product_licenses.license_tags)", licensing.DevTag)
}
query += fmt.Sprintf(`
AND EXISTS (
SELECT 1
FROM product_licenses
WHERE product_licenses.product_subscription_id = product_subscriptions.id
AND %s
ORDER BY product_licenses.created_at DESC
LIMIT 1
)
`, licenseCond)

rows, err := r.db.Query(ctx, query, namedArgs)
if err != nil {
Expand Down
85 changes: 58 additions & 27 deletions cmd/enterprise-portal/internal/dotcomdb/dotcomdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/sourcegraph/sourcegraph/lib/pointers"
)

func newTestDotcomReader(t *testing.T) (database.DB, *dotcomdb.Reader) {
func newTestDotcomReader(t *testing.T, opts dotcomdb.ReaderOptions) (database.DB, *dotcomdb.Reader) {
ctx := context.Background()

// Set up a Sourcegraph test database.
Expand Down Expand Up @@ -62,9 +62,7 @@ func newTestDotcomReader(t *testing.T) (database.DB, *dotcomdb.Reader) {
require.NoError(t, err)

// Make sure it works!
r := dotcomdb.NewReader(conn, dotcomdb.ReaderOptions{
DevOnly: true,
})
r := dotcomdb.NewReader(conn, opts)
require.NoError(t, r.Ping(ctx))

return database.NewDB(logtest.Scoped(t), sgtestdb), r
Expand Down Expand Up @@ -243,7 +241,9 @@ func TestGetCodyGatewayAccessAttributes(t *testing.T) {
tc := tc
t.Parallel()

dotcomdb, dotcomreader := newTestDotcomReader(t)
dotcomdb, dotcomreader := newTestDotcomReader(t, dotcomdb.ReaderOptions{
DevOnly: true,
})
// First, set up a subscription and license and some other rubbish
// data to ensure we only get the license we want.
mock := setupDBAndInsertMockLicense(t, dotcomdb, tc.info, &tc.cgAccess)
Expand Down Expand Up @@ -323,7 +323,9 @@ func validateAccessAttributes(t *testing.T, dotcomdb database.DB, mock mockedDat

func TestGetAllCodyGatewayAccessAttributes(t *testing.T) {
t.Parallel()
dotcomdb, dotcomreader := newTestDotcomReader(t)
dotcomdb, dotcomreader := newTestDotcomReader(t, dotcomdb.ReaderOptions{
DevOnly: true,
})

info := license.Info{
CreatedAt: time.Now().Add(-30 * time.Minute),
Expand Down Expand Up @@ -352,7 +354,9 @@ func TestGetAllCodyGatewayAccessAttributes(t *testing.T) {
func TestListEnterpriseSubscriptionLicenses(t *testing.T) {
t.Parallel()

db, dotcomreader := newTestDotcomReader(t)
db, dotcomreader := newTestDotcomReader(t, dotcomdb.ReaderOptions{
DevOnly: true,
})
info := license.Info{
ExpiresAt: time.Now().Add(30 * time.Minute),
UserCount: 321,
Expand Down Expand Up @@ -467,26 +471,53 @@ func TestListEnterpriseSubscriptionLicenses(t *testing.T) {
}

func TestListEnterpriseSubscriptions(t *testing.T) {
db, dotcomreader := newTestDotcomReader(t)
info := license.Info{
ExpiresAt: time.Now().Add(30 * time.Minute),
UserCount: 321,
Tags: []string{licensing.PlanEnterprise1.Tag(), licensing.DevTag},
}
mock := setupDBAndInsertMockLicense(t, db, info, nil)
t.Run("devonly", func(t *testing.T) {
t.Parallel()

// Just a simple sanity test
ss, err := dotcomreader.ListEnterpriseSubscriptions(
context.Background(),
dotcomdb.ListEnterpriseSubscriptionsOptions{})
require.NoError(t, err)
assert.Len(t, ss, mock.createdSubscriptions-mock.archivedSubscriptions)
var found bool
for _, s := range ss {
if s.ID == mock.targetSubscriptionID {
found = true
break
db, dotcomreader := newTestDotcomReader(t, dotcomdb.ReaderOptions{
DevOnly: true,
})
info := license.Info{
ExpiresAt: time.Now().Add(30 * time.Minute),
UserCount: 321,
Tags: []string{licensing.PlanEnterprise1.Tag(), licensing.DevTag},
}
}
assert.True(t, found)
mock := setupDBAndInsertMockLicense(t, db, info, nil)

ss, err := dotcomreader.ListEnterpriseSubscriptions(
context.Background(),
dotcomdb.ListEnterpriseSubscriptionsOptions{})
require.NoError(t, err)
// We expect 1 less subscription because one of the subscriptions does not
// have a dev/internal license
assert.Len(t, ss, mock.createdSubscriptions-mock.archivedSubscriptions-1)
var found bool
for _, s := range ss {
if s.ID == mock.targetSubscriptionID {
found = true
break
}
}
assert.True(t, found)
})

t.Run("not devonly", func(t *testing.T) {
t.Parallel()

db, dotcomreader := newTestDotcomReader(t, dotcomdb.ReaderOptions{
DevOnly: false,
})
info := license.Info{
ExpiresAt: time.Now().Add(30 * time.Minute),
UserCount: 321,
Tags: []string{licensing.PlanEnterprise1.Tag(), licensing.DevTag},
}
_ = setupDBAndInsertMockLicense(t, db, info, nil)

ss, err := dotcomreader.ListEnterpriseSubscriptions(
context.Background(),
dotcomdb.ListEnterpriseSubscriptionsOptions{})
require.NoError(t, err)
assert.Len(t, ss, 1) // only 1 created without a dev tag
})
}

0 comments on commit c236467

Please sign in to comment.