From 79e99cf0b31a5a4e407f8147cea25132345a6d03 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Fri, 6 Oct 2023 02:10:46 +0000 Subject: [PATCH 1/3] feat: only display license warnings to privileged users --- cli/root.go | 12 ++++++++-- enterprise/cli/root_test.go | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/cli/root.go b/cli/root.go index d75187d3f4133..761a35f3adfbe 100644 --- a/cli/root.go +++ b/cli/root.go @@ -835,10 +835,18 @@ func (r *RootCmd) checkWarnings(i *clibase.Invocation, client *codersdk.Client) ctx, cancel := context.WithTimeout(i.Context(), 10*time.Second) defer cancel() + user, err := client.User(ctx, codersdk.Me) + if err != nil { + return xerrors.Errorf("get user me: %w", err) + } + entitlements, err := client.Entitlements(ctx) if err == nil { - for _, w := range entitlements.Warnings { - _, _ = fmt.Fprintln(i.Stderr, pretty.Sprint(cliui.DefaultStyles.Warn, w)) + // Don't show warning to regular users. + if len(user.Roles) > 0 { + for _, w := range entitlements.Warnings { + _, _ = fmt.Fprintln(i.Stderr, pretty.Sprint(cliui.DefaultStyles.Warn, w)) + } } } return nil diff --git a/enterprise/cli/root_test.go b/enterprise/cli/root_test.go index e957c8e09d428..9ef37c3f8dc83 100644 --- a/enterprise/cli/root_test.go +++ b/enterprise/cli/root_test.go @@ -1,14 +1,18 @@ package cli_test import ( + "bytes" "testing" + "time" "github.com/stretchr/testify/require" "github.com/coder/coder/v2/cli/clibase" "github.com/coder/coder/v2/cli/clitest" "github.com/coder/coder/v2/cli/config" + "github.com/coder/coder/v2/coderd/coderdtest" "github.com/coder/coder/v2/enterprise/cli" + "github.com/coder/coder/v2/enterprise/coderd/coderdenttest" ) func newCLI(t *testing.T, args ...string) (*clibase.Invocation, config.Root) { @@ -27,3 +31,45 @@ func TestEnterpriseHandlersOK(t *testing.T) { clitest.HandlersOK(t, cmd) } + +func TestCheckWarnings(t *testing.T) { + t.Run("LicenseWarningForPrivilegedRoles", func(t *testing.T) { + client, _ := coderdenttest.New(t, &coderdenttest.Options{ + LicenseOptions: &coderdenttest.LicenseOptions{ + ExpiresAt: time.Now().Add(time.Hour * 24), + }, + }) + + inv, conf := newCLI(t, "list") + + var buf bytes.Buffer + inv.Stderr = &buf + clitest.SetupConfig(t, client, conf) + + err := inv.Run() + require.NoError(t, err) + + require.Contains(t, buf.String(), "Your license expires in 1 day.") + }) + + t.Run("NoLicenseWarningForRegularUser", func(t *testing.T) { + adminClient, admin := coderdenttest.New(t, &coderdenttest.Options{ + LicenseOptions: &coderdenttest.LicenseOptions{ + ExpiresAt: time.Now().Add(time.Hour * 24), + }, + }) + + client, _ := coderdtest.CreateAnotherUser(t, adminClient, admin.OrganizationID) + + inv, conf := newCLI(t, "list") + + var buf bytes.Buffer + inv.Stderr = &buf + clitest.SetupConfig(t, client, conf) + + err := inv.Run() + require.NoError(t, err) + + require.NotContains(t, buf.String(), "Your license expires") + }) +} From 27fecf26cca324261a0b0b4302d2240386ae9032 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Fri, 6 Oct 2023 02:11:13 +0000 Subject: [PATCH 2/3] parallel --- enterprise/cli/root_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/enterprise/cli/root_test.go b/enterprise/cli/root_test.go index 9ef37c3f8dc83..3017b6cb0b5b3 100644 --- a/enterprise/cli/root_test.go +++ b/enterprise/cli/root_test.go @@ -33,7 +33,10 @@ func TestEnterpriseHandlersOK(t *testing.T) { } func TestCheckWarnings(t *testing.T) { + t.Parallel() + t.Run("LicenseWarningForPrivilegedRoles", func(t *testing.T) { + t.Parallel() client, _ := coderdenttest.New(t, &coderdenttest.Options{ LicenseOptions: &coderdenttest.LicenseOptions{ ExpiresAt: time.Now().Add(time.Hour * 24), @@ -53,6 +56,7 @@ func TestCheckWarnings(t *testing.T) { }) t.Run("NoLicenseWarningForRegularUser", func(t *testing.T) { + t.Parallel() adminClient, admin := coderdenttest.New(t, &coderdenttest.Options{ LicenseOptions: &coderdenttest.LicenseOptions{ ExpiresAt: time.Now().Add(time.Hour * 24), From c531cab457aa90c147917b81a2b5a4af7332c75f Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Fri, 6 Oct 2023 02:20:47 +0000 Subject: [PATCH 3/3] fix test --- enterprise/cli/licenses_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/enterprise/cli/licenses_test.go b/enterprise/cli/licenses_test.go index df21ba6b66b25..178068840dd56 100644 --- a/enterprise/cli/licenses_test.go +++ b/enterprise/cli/licenses_test.go @@ -254,6 +254,7 @@ func newFakeLicenseAPI(t *testing.T) http.Handler { r.Post("/api/v2/licenses", a.postLicense) r.Get("/api/v2/licenses", a.licenses) r.Get("/api/v2/buildinfo", a.noop) + r.Get("/api/v2/users/me", a.noop) r.Delete("/api/v2/licenses/{id}", a.deleteLicense) r.Get("/api/v2/entitlements", a.entitlements) return r