mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
Test backend
This commit is contained in:
@@ -822,6 +822,33 @@ func TestOffsetLimit(t *testing.T) {
|
||||
require.Len(t, ws, 0)
|
||||
}
|
||||
|
||||
func TestWorkspaceCount(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
defer cancel()
|
||||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
|
||||
user := coderdtest.CreateFirstUser(t, client)
|
||||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
|
||||
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
|
||||
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
|
||||
template2 := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
|
||||
_ = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
|
||||
_ = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template2.ID)
|
||||
_ = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template2.ID)
|
||||
|
||||
response, err := client.WorkspaceCount(ctx, codersdk.WorkspaceCountRequest{})
|
||||
require.NoError(t, err, "fetch workspace count")
|
||||
// counts all
|
||||
require.Equal(t, int(response.Count), 3)
|
||||
|
||||
response2, err2 := client.WorkspaceCount(ctx, codersdk.WorkspaceCountRequest{
|
||||
SearchQuery: fmt.Sprintf("template:%s", template.Name),
|
||||
})
|
||||
require.NoError(t, err2, "fetch workspace count")
|
||||
// counts only those that pass filter
|
||||
require.Equal(t, int(response2.Count), 1)
|
||||
}
|
||||
|
||||
func TestPostWorkspaceBuild(t *testing.T) {
|
||||
t.Parallel()
|
||||
t.Run("NoTemplateVersion", func(t *testing.T) {
|
||||
|
||||
@@ -324,6 +324,34 @@ func (c *Client) Workspaces(ctx context.Context, filter WorkspaceFilter) ([]Work
|
||||
return workspaces, json.NewDecoder(res.Body).Decode(&workspaces)
|
||||
}
|
||||
|
||||
func (c *Client) WorkspaceCount(ctx context.Context, req WorkspaceCountRequest) (WorkspaceCountResponse, error) {
|
||||
res, err := c.Request(ctx, http.MethodGet, "/api/v2/workspaces/count", nil, func(r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
var params []string
|
||||
if req.SearchQuery != "" {
|
||||
params = append(params, req.SearchQuery)
|
||||
}
|
||||
q.Set("q", strings.Join(params, " "))
|
||||
r.URL.RawQuery = q.Encode()
|
||||
})
|
||||
if err != nil {
|
||||
return WorkspaceCountResponse{}, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return WorkspaceCountResponse{}, readBodyAsError(res)
|
||||
}
|
||||
|
||||
var countRes WorkspaceCountResponse
|
||||
err = json.NewDecoder(res.Body).Decode(&countRes)
|
||||
if err != nil {
|
||||
return WorkspaceCountResponse{}, err
|
||||
}
|
||||
|
||||
return countRes, nil
|
||||
}
|
||||
|
||||
// WorkspaceByOwnerAndName returns a workspace by the owner's UUID and the workspace's name.
|
||||
func (c *Client) WorkspaceByOwnerAndName(ctx context.Context, owner string, name string, params WorkspaceOptions) (Workspace, error) {
|
||||
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/workspace/%s", owner, name), nil, func(r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user