Files
coder/enterprise/coderd/appearance_test.go
T
Bartek Gatz 55340de360 feat: add deployment setting to disable the Codernauts game (#27662)
Adds a `codernauts_enabled` runtime setting (default: enabled) that
controls whether the Codernauts game link appears in the user dropdown
menu. A toggle at the bottom of the Deployment > Appearance page
controls it, and the value persists in `site_configs`, so it survives
restarts and upgrades without any CLI flags or environment variables.

The setting works on all licenses: `GET /api/v2/appearance` reports it
from both the enterprise and the default (AGPL) appearance fetchers, and
`PUT /api/v2/appearance` persists it gated only by the deployment-config
RBAC permission, not by an entitlement. On the Appearance page the
toggle renders outside the Premium paywall introduced in #27948; the
other appearance fields keep their existing licensing behavior.

Note: a pure AGPL-only build of coderd does not register the
`/api/v2/appearance` routes at all, so the toggle cannot be saved there.
The standard `coder server` binary is unaffected regardless of license
state.

<details>
<summary>Implementation notes</summary>

- `coderd/database/queries/siteconfig.sql`: new `GetCodernautsEnabled` /
`UpsertCodernautsEnabled` queries backed by a `codernauts_enabled` key
in `site_configs`; defaults to `true` when unset (no migration needed).
- `coderd/database/dbauthz`: read has no authz checks (matching other
appearance reads); write requires `ResourceDeploymentConfig` update.
Coverage added to `dbauthz_test.go`.
- `codersdk`: `codernauts_enabled` added to `AppearanceConfig` and
`UpdateAppearanceConfig`.
- `coderd/appearance`: the default fetcher takes a `database.Store` and
reads the setting from the database so unlicensed deployments serve it
too.
- `enterprise/coderd/appearance.go`: fetches the value in `Fetch` and
persists it in `putAppearance`.
- Frontend: "Codernauts game" toggle section on Deployment > Appearance
saving immediately via the existing appearance mutation;
`codernauts_enabled` threaded from `useDashboard().appearance` through
Navbar to `UserDropdownContent`, which renders the menu item only when
enabled. Storybook stories with `play` functions cover toggling
(entitled and not) and the dropdown hiding the link when disabled.

</details>

---

🤖 This PR was generated by Coder Agents on behalf of @bartekgatzcoder.
2026-09-02 11:29:35 +02:00

340 lines
11 KiB
Go

package coderd_test
import (
"context"
"errors"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbfake"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
"github.com/coder/coder/v2/enterprise/coderd/license"
"github.com/coder/coder/v2/testutil"
"github.com/coder/serpent"
)
func TestCustomLogoAndCompanyName(t *testing.T) {
t.Parallel()
// Prepare enterprise deployment
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
adminClient, adminUser := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true})
coderdenttest.AddLicense(t, adminClient, coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAppearance: 1,
},
})
anotherClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
// Update logo and application name
uac := codersdk.UpdateAppearanceConfig{
ApplicationName: "ACME Ltd",
LogoURL: "http://logo-url/file.png",
}
err := adminClient.UpdateAppearance(ctx, uac)
require.NoError(t, err)
// Verify update
got, err := anotherClient.Appearance(ctx)
require.NoError(t, err)
require.Equal(t, uac.ApplicationName, got.ApplicationName)
require.Equal(t, uac.LogoURL, got.LogoURL)
}
func TestCodernautsEnabled(t *testing.T) {
t.Parallel()
adminClient, adminUser := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true})
basicUserClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
ctx := testutil.Context(t, testutil.WaitLong)
// With no stored setting, as after an upgrade, the game defaults to
// enabled. This deployment has no license, so the default appearance
// fetcher serves the value.
got, err := basicUserClient.Appearance(ctx)
require.NoError(t, err)
require.True(t, got.CodernautsEnabled)
// The setting can be disabled without any license entitlement.
err = adminClient.UpdateAppearance(ctx, codersdk.UpdateAppearanceConfig{
CodernautsEnabled: false,
})
require.NoError(t, err)
got, err = basicUserClient.Appearance(ctx)
require.NoError(t, err)
require.False(t, got.CodernautsEnabled)
// The stored value survives switching to the licensed fetcher.
coderdenttest.AddLicense(t, adminClient, coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAppearance: 1,
},
})
got, err = basicUserClient.Appearance(ctx)
require.NoError(t, err)
require.False(t, got.CodernautsEnabled)
err = adminClient.UpdateAppearance(ctx, codersdk.UpdateAppearanceConfig{
CodernautsEnabled: true,
})
require.NoError(t, err)
got, err = basicUserClient.Appearance(ctx)
require.NoError(t, err)
require.True(t, got.CodernautsEnabled)
}
func TestAnnouncementBanners(t *testing.T) {
t.Parallel()
t.Run("User", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
adminClient, adminUser := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true})
basicUserClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
// Without a license, there should be no banners.
sb, err := basicUserClient.Appearance(ctx)
require.NoError(t, err)
require.Empty(t, sb.AnnouncementBanners)
coderdenttest.AddLicense(t, adminClient, coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAppearance: 1,
},
})
// Default state
sb, err = basicUserClient.Appearance(ctx)
require.NoError(t, err)
require.Empty(t, sb.AnnouncementBanners)
// Regular user should be unable to set the banner
uac := codersdk.UpdateAppearanceConfig{
AnnouncementBanners: []codersdk.BannerConfig{{Enabled: true}},
}
err = basicUserClient.UpdateAppearance(ctx, uac)
require.Error(t, err)
var sdkError *codersdk.Error
require.True(t, errors.As(err, &sdkError))
require.ErrorAs(t, err, &sdkError)
require.Equal(t, http.StatusForbidden, sdkError.StatusCode())
// But an admin can
wantBanner := codersdk.UpdateAppearanceConfig{
AnnouncementBanners: []codersdk.BannerConfig{{
Enabled: true,
Message: "The beep-bop will be boop-beeped on Saturday at 12AM PST.",
BackgroundColor: "#00FF00",
}},
}
err = adminClient.UpdateAppearance(ctx, wantBanner)
require.NoError(t, err)
gotBanner, err := adminClient.Appearance(ctx) //nolint:gocritic // we should assert at least once that the owner can get the banner
require.NoError(t, err)
require.Equal(t, wantBanner.AnnouncementBanners, gotBanner.AnnouncementBanners)
// But even an admin can't give a bad color
wantBanner.AnnouncementBanners[0].BackgroundColor = "#bad color"
err = adminClient.UpdateAppearance(ctx, wantBanner)
require.Error(t, err)
var sdkErr *codersdk.Error
require.ErrorAs(t, err, &sdkErr)
require.Equal(t, http.StatusBadRequest, sdkErr.StatusCode())
require.Contains(t, sdkErr.Message, "Invalid color format")
require.Contains(t, sdkErr.Detail, "expected # prefix and 6 characters")
})
t.Run("Agent", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
store, ps := dbtestutil.NewDB(t)
client, user := coderdenttest.New(t, &coderdenttest.Options{
Options: &coderdtest.Options{
Database: store,
Pubsub: ps,
},
DontAddLicense: true,
})
lic := coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAppearance: 1,
},
})
cfg := codersdk.UpdateAppearanceConfig{
AnnouncementBanners: []codersdk.BannerConfig{{
Enabled: true,
Message: "The beep-bop will be boop-beeped on Saturday at 12AM PST.",
BackgroundColor: "#00FF00",
}},
}
err := client.UpdateAppearance(ctx, cfg)
require.NoError(t, err)
r := dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{
OrganizationID: user.OrganizationID,
OwnerID: user.UserID,
}).WithAgent().Do()
agentClient := agentsdk.New(client.URL, agentsdk.WithFixedToken(r.AgentToken))
banners := requireGetAnnouncementBanners(ctx, t, agentClient)
require.Equal(t, cfg.AnnouncementBanners, banners)
// Create an AGPL Coderd against the same database
agplClient := coderdtest.New(t, &coderdtest.Options{Database: store, Pubsub: ps})
agplAgentClient := agentsdk.New(agplClient.URL, agentsdk.WithFixedToken(r.AgentToken))
banners = requireGetAnnouncementBanners(ctx, t, agplAgentClient)
require.Equal(t, []codersdk.BannerConfig{}, banners)
// No license means no banner.
err = client.DeleteLicense(ctx, lic.ID)
require.NoError(t, err)
banners = requireGetAnnouncementBanners(ctx, t, agentClient)
require.Equal(t, []codersdk.BannerConfig{}, banners)
})
}
func requireGetAnnouncementBanners(ctx context.Context, t *testing.T, client *agentsdk.Client) []codersdk.BannerConfig {
cc, err := client.ConnectRPC(ctx)
require.NoError(t, err)
defer func() {
_ = cc.Close()
}()
aAPI := proto.NewDRPCAgentClient(cc)
bannersProto, err := aAPI.GetAnnouncementBanners(ctx, &proto.GetAnnouncementBannersRequest{})
require.NoError(t, err)
banners := make([]codersdk.BannerConfig, 0, len(bannersProto.AnnouncementBanners))
for _, bannerProto := range bannersProto.AnnouncementBanners {
banners = append(banners, agentsdk.BannerConfigFromProto(bannerProto))
}
return banners
}
func TestCustomSupportLinks(t *testing.T) {
t.Parallel()
supportLinks := []codersdk.LinkConfig{
{
Name: "First link",
Target: "http://first-link-1",
Icon: "chat",
},
{
Name: "Second link",
Target: "http://second-link-2",
Icon: "bug",
},
{
Name: "First button",
Target: "http://first-button-1",
Icon: "bug",
Location: "navbar",
},
{
Name: "Third link",
Target: "http://third-link-3",
Icon: "star",
},
}
cfg := coderdtest.DeploymentValues(t)
cfg.Support.Links = serpent.Struct[[]codersdk.LinkConfig]{
Value: supportLinks,
}
adminClient, adminUser := coderdenttest.New(t, &coderdenttest.Options{
Options: &coderdtest.Options{
DeploymentValues: cfg,
},
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAppearance: 1,
},
},
})
anotherClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
defer cancel()
appr, err := anotherClient.Appearance(ctx)
require.NoError(t, err)
require.Equal(t, supportLinks, appr.SupportLinks)
}
func TestCustomDocsURL(t *testing.T) {
t.Parallel()
testURLRawString := "http://google.com"
testURL, err := url.Parse(testURLRawString)
require.NoError(t, err)
cfg := coderdtest.DeploymentValues(t)
cfg.DocsURL = *serpent.URLOf(testURL)
adminClient, adminUser := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true, Options: &coderdtest.Options{DeploymentValues: cfg}})
anotherClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
defer cancel()
appr, err := anotherClient.Appearance(ctx)
require.NoError(t, err)
require.Equal(t, testURLRawString, appr.DocsURL)
}
func TestDefaultSupportLinksWithCustomDocsUrl(t *testing.T) {
t.Parallel()
// Don't need to set the license, as default links are passed without it.
testURLRawString := "http://google.com"
testURL, err := url.Parse(testURLRawString)
require.NoError(t, err)
cfg := coderdtest.DeploymentValues(t)
cfg.DocsURL = *serpent.URLOf(testURL)
adminClient, adminUser := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true, Options: &coderdtest.Options{DeploymentValues: cfg}})
anotherClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
defer cancel()
appr, err := anotherClient.Appearance(ctx)
require.NoError(t, err)
require.Equal(t, codersdk.DefaultSupportLinks(testURLRawString), appr.SupportLinks)
}
func TestDefaultSupportLinks(t *testing.T) {
t.Parallel()
// Don't need to set the license, as default links are passed without it.
adminClient, adminUser := coderdenttest.New(t, &coderdenttest.Options{DontAddLicense: true})
anotherClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
defer cancel()
appr, err := anotherClient.Appearance(ctx)
require.NoError(t, err)
require.Equal(t, codersdk.DefaultSupportLinks(codersdk.DefaultDocsURL()), appr.SupportLinks)
}