Skip to content

Commit f8ce104

Browse files
committed
test(enterprise): add tests for embeddable dashboard endpoint
- Test no license scenario (returns 404) - Test license without PublishUsageData (returns 404) - Test unauthorized user access (returns 403) - Focus on authorization and license validation logic
1 parent 45fd5ac commit f8ce104

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

enterprise/coderd/licenses_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,82 @@ import (
1111
"github.com/stretchr/testify/require"
1212
"golang.org/x/xerrors"
1313

14+
"github.com/coder/coder/v2/coderd/coderdtest"
1415
"github.com/coder/coder/v2/codersdk"
1516
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
1617
"github.com/coder/coder/v2/enterprise/coderd/license"
18+
"github.com/coder/coder/v2/enterprise/coderd/usage/tallymansdk"
1719
"github.com/coder/coder/v2/testutil"
1820
)
1921

22+
func TestPostUsageEmbeddableDashboard(t *testing.T) {
23+
t.Parallel()
24+
25+
// Note: Tests that require a working Tallyman server are integration tests
26+
// and should be run against a real or properly mocked Tallyman instance.
27+
// The tests below focus on authorization and license validation logic.
28+
29+
t.Run("NoLicense", func(t *testing.T) {
30+
t.Parallel()
31+
32+
client, _ := coderdenttest.New(t, &coderdenttest.Options{
33+
DontAddLicense: true,
34+
})
35+
36+
ctx := testutil.Context(t, testutil.WaitShort)
37+
_, err := client.GetUsageEmbeddableDashboard(ctx, codersdk.GetUsageEmbeddableDashboardRequest{
38+
Dashboard: string(tallymansdk.DashboardTypeUsage),
39+
})
40+
41+
var apiErr *codersdk.Error
42+
require.ErrorAs(t, err, &apiErr)
43+
assert.Equal(t, http.StatusNotFound, apiErr.StatusCode())
44+
assert.Contains(t, apiErr.Message, "No license supports usage publishing")
45+
})
46+
47+
t.Run("LicenseWithoutPublishing", func(t *testing.T) {
48+
t.Parallel()
49+
50+
client, _ := coderdenttest.New(t, &coderdenttest.Options{})
51+
// Default license has PublishUsageData: false
52+
53+
ctx := testutil.Context(t, testutil.WaitShort)
54+
_, err := client.GetUsageEmbeddableDashboard(ctx, codersdk.GetUsageEmbeddableDashboardRequest{
55+
Dashboard: string(tallymansdk.DashboardTypeUsage),
56+
})
57+
58+
var apiErr *codersdk.Error
59+
require.ErrorAs(t, err, &apiErr)
60+
assert.Equal(t, http.StatusNotFound, apiErr.StatusCode())
61+
assert.Contains(t, apiErr.Message, "No license supports usage publishing")
62+
})
63+
64+
65+
66+
t.Run("UnauthorizedUser", func(t *testing.T) {
67+
t.Parallel()
68+
69+
adminClient, adminUser := coderdenttest.New(t, &coderdenttest.Options{})
70+
coderdenttest.AddLicense(t, adminClient, coderdenttest.LicenseOptions{
71+
AccountType: license.AccountTypeSalesforce,
72+
AccountID: "test-account",
73+
PublishUsageData: true,
74+
})
75+
76+
// Create a regular user (non-admin)
77+
memberClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
78+
79+
ctx := testutil.Context(t, testutil.WaitShort)
80+
_, err := memberClient.GetUsageEmbeddableDashboard(ctx, codersdk.GetUsageEmbeddableDashboardRequest{
81+
Dashboard: string(tallymansdk.DashboardTypeUsage),
82+
})
83+
84+
var apiErr *codersdk.Error
85+
require.ErrorAs(t, err, &apiErr)
86+
assert.Equal(t, http.StatusForbidden, apiErr.StatusCode())
87+
})
88+
}
89+
2090
func TestPostLicense(t *testing.T) {
2191
t.Parallel()
2292

0 commit comments

Comments
 (0)