Skip to content

Commit 31c1baa

Browse files
committed
add tests for flags
1 parent 93a5e1b commit 31c1baa

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

codersdk/workspacequota.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
package codersdk
22

3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
)
9+
310
type WorkspaceQuota struct {
411
UserWorkspaceCount int `json:"user_workspace_count"`
512
UserWorkspaceLimit int `json:"user_workspace_limit"`
613
}
14+
15+
func (c *Client) WorkspaceQuota(ctx context.Context, userID string) (WorkspaceQuota, error) {
16+
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/workspace-quota", userID), nil)
17+
if err != nil {
18+
return WorkspaceQuota{}, err
19+
}
20+
defer res.Body.Close()
21+
if res.StatusCode != http.StatusOK {
22+
return WorkspaceQuota{}, readBodyAsError(res)
23+
}
24+
var quota WorkspaceQuota
25+
return quota, json.NewDecoder(res.Body).Decode(&quota)
26+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package coderd_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/coder/coder/coderd/coderdtest"
8+
"github.com/coder/coder/codersdk"
9+
"github.com/coder/coder/enterprise/coderd/coderdenttest"
10+
"github.com/coder/coder/testutil"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestWorkspaceQuota(t *testing.T) {
15+
t.Parallel()
16+
t.Run("Disabled", func(t *testing.T) {
17+
t.Parallel()
18+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
19+
defer cancel()
20+
client := coderdenttest.New(t, &coderdenttest.Options{})
21+
_ = coderdtest.CreateFirstUser(t, client)
22+
coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
23+
WorkspaceQuota: true,
24+
})
25+
q1, err := client.WorkspaceQuota(ctx, "me")
26+
require.NoError(t, err)
27+
require.EqualValues(t, q1.UserWorkspaceLimit, 0)
28+
29+
})
30+
t.Run("Enabled", func(t *testing.T) {
31+
t.Parallel()
32+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
33+
defer cancel()
34+
max := 3
35+
client := coderdenttest.New(t, &coderdenttest.Options{
36+
UserWorkspaceQuota: max,
37+
})
38+
user := coderdtest.CreateFirstUser(t, client)
39+
coderdenttest.AddLicense(t, client, coderdenttest.LicenseOptions{
40+
WorkspaceQuota: true,
41+
})
42+
q1, err := client.WorkspaceQuota(ctx, "me")
43+
require.NoError(t, err)
44+
require.EqualValues(t, q1.UserWorkspaceLimit, max)
45+
46+
// ensure other user IDs work too
47+
u2, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
48+
Email: "whatever@yo.com",
49+
Username: "haha",
50+
Password: "laskjdnvkaj",
51+
OrganizationID: user.OrganizationID,
52+
})
53+
q2, err := client.WorkspaceQuota(ctx, u2.ID.String())
54+
require.NoError(t, err)
55+
require.EqualValues(t, q1, q2)
56+
})
57+
}

0 commit comments

Comments
 (0)