Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Standardize test structure for codersdk
  • Loading branch information
kylecarbs committed Feb 5, 2022
commit e202b2d3a5927fb7376936797fec96de0ff20704
1 change: 0 additions & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func New(options *Options) http.Handler {
r.Get("/", api.workspaces)
r.Route("/{user}", func(r chi.Router) {
r.Use(httpmw.ExtractUserParam(options.Database))
r.Get("/", api.workspaces)
r.Post("/", api.postWorkspaceByUser)
r.Route("/{workspace}", func(r chi.Router) {
r.Use(httpmw.ExtractWorkspaceParam(options.Database))
Expand Down
5 changes: 3 additions & 2 deletions coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ func (api *api) provisionerDaemons(rw http.ResponseWriter, r *http.Request) {
daemons, err := api.Database.GetProvisionerDaemons(r.Context())
if errors.Is(err, sql.ErrNoRows) {
err = nil
daemons = []database.ProvisionerDaemon{}
}
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("get provisioner daemons: %s", err),
})
return
}

if daemons == nil {
daemons = []database.ProvisionerDaemon{}
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, daemons)
}
Expand Down
22 changes: 12 additions & 10 deletions coderd/provisionerdaemons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (
)

func TestProvisionerDaemons(t *testing.T) {
// Tests for properly processing specific job
// types should be placed in their respective
// resource location.
//
// eg. project import is a project-related job
t.Parallel()

t.Run("Register", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
_ = coderdtest.NewProvisionerDaemon(t, client)
require.Eventually(t, func() bool {
daemons, err := client.ProvisionerDaemons(context.Background())
require.NoError(t, err)
return len(daemons) > 0
}, time.Second, 10*time.Millisecond)
})
client := coderdtest.New(t)
_ = coderdtest.NewProvisionerDaemon(t, client)
require.Eventually(t, func() bool {
daemons, err := client.ProvisionerDaemons(context.Background())
require.NoError(t, err)
return len(daemons) > 0
}, time.Second, 25*time.Millisecond)
}
2 changes: 1 addition & 1 deletion coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (api *api) postWorkspaceByUser(rw http.ResponseWriter, r *http.Request) {
render.JSON(rw, r, convertWorkspace(workspace))
}

// Returns a single singleWorkspace.
// Returns a single single workspace.
func (*api) workspaceByUser(rw http.ResponseWriter, r *http.Request) {
workspace := httpmw.WorkspaceParam(r)

Expand Down
155 changes: 74 additions & 81 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package coderd_test

import (
"context"
"net/http"
"testing"

"github.com/google/uuid"
Expand All @@ -10,143 +11,135 @@ import (
"github.com/coder/coder/coderd"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/database"
)

func TestWorkspaces(t *testing.T) {
t.Parallel()

t.Run("ListNone", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
_ = coderdtest.CreateInitialUser(t, client)
workspaces, err := client.WorkspacesByUser(context.Background(), "")
workspaces, err := client.Workspaces(context.Background(), "")
require.NoError(t, err)
require.NotNil(t, workspaces)
require.Len(t, workspaces, 0)
})

setupProjectAndWorkspace := func(t *testing.T, client *codersdk.Client, user coderd.CreateInitialUserRequest) (coderd.Project, coderd.Workspace) {
project, err := client.CreateProject(context.Background(), user.Organization, coderd.CreateProjectRequest{
Name: "banana",
Provisioner: database.ProvisionerTypeEcho,
})
require.NoError(t, err)
workspace, err := client.CreateWorkspace(context.Background(), "", coderd.CreateWorkspaceRequest{
Name: "example",
ProjectID: project.ID,
})
require.NoError(t, err)
return project, workspace
}

t.Run("List", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.CreateInitialUser(t, client)
_, _ = setupProjectAndWorkspace(t, client, user)
workspaces, err := client.WorkspacesByUser(context.Background(), "")
project := coderdtest.CreateProject(t, client, user.Organization)
_ = coderdtest.CreateWorkspace(t, client, "", project.ID)
workspaces, err := client.Workspaces(context.Background(), "")
require.NoError(t, err)
require.Len(t, workspaces, 1)
})
}

t.Run("ListNoneForProject", func(t *testing.T) {
func TestPostWorkspaceByUser(t *testing.T) {
t.Parallel()
t.Run("InvalidProject", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.CreateInitialUser(t, client)
project, err := client.CreateProject(context.Background(), user.Organization, coderd.CreateProjectRequest{
Name: "banana",
Provisioner: database.ProvisionerTypeEcho,
_ = coderdtest.CreateInitialUser(t, client)
_, err := client.CreateWorkspace(context.Background(), "", coderd.CreateWorkspaceRequest{
ProjectID: uuid.New(),
Name: "workspace",
})
require.NoError(t, err)
workspaces, err := client.WorkspacesByProject(context.Background(), user.Organization, project.Name)
require.NoError(t, err)
require.Len(t, workspaces, 0)
require.Error(t, err)
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusBadRequest, apiErr.StatusCode())
})

t.Run("ListForProject", func(t *testing.T) {
t.Run("NoProjectAccess", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.CreateInitialUser(t, client)
project, _ := setupProjectAndWorkspace(t, client, user)
workspaces, err := client.WorkspacesByProject(context.Background(), user.Organization, project.Name)
require.NoError(t, err)
require.Len(t, workspaces, 1)
})
project := coderdtest.CreateProject(t, client, user.Organization)

t.Run("CreateInvalidInput", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.CreateInitialUser(t, client)
project, err := client.CreateProject(context.Background(), user.Organization, coderd.CreateProjectRequest{
Name: "banana",
Provisioner: database.ProvisionerTypeEcho,
anotherUser := coderd.CreateUserRequest{
Email: "another@user.org",
Username: "someuser",
Password: "somepass",
}
_, err := client.CreateUser(context.Background(), anotherUser)
require.NoError(t, err)
token, err := client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{
Email: anotherUser.Email,
Password: anotherUser.Password,
})
require.NoError(t, err)
err = client.SetSessionToken(token.SessionToken)
require.NoError(t, err)

_, err = client.CreateWorkspace(context.Background(), "", coderd.CreateWorkspaceRequest{
ProjectID: project.ID,
Name: "$$$",
Name: "workspace",
})
require.Error(t, err)
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode())
})

t.Run("CreateInvalidProject", func(t *testing.T) {
t.Run("AlreadyExists", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
_ = coderdtest.CreateInitialUser(t, client)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
workspace := coderdtest.CreateWorkspace(t, client, "", project.ID)
_, err := client.CreateWorkspace(context.Background(), "", coderd.CreateWorkspaceRequest{
ProjectID: uuid.New(),
Name: "moo",
ProjectID: project.ID,
Name: workspace.Name,
})
require.Error(t, err)
var apiErr *codersdk.Error
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusConflict, apiErr.StatusCode())
})

t.Run("CreateNotInProjectOrganization", func(t *testing.T) {
t.Run("Create", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
initial := coderdtest.CreateInitialUser(t, client)
project, err := client.CreateProject(context.Background(), initial.Organization, coderd.CreateProjectRequest{
Name: "banana",
Provisioner: database.ProvisionerTypeEcho,
})
require.NoError(t, err)
_, err = client.CreateUser(context.Background(), coderd.CreateUserRequest{
Email: "hello@ok.io",
Username: "example",
Password: "password",
})
require.NoError(t, err)
token, err := client.LoginWithPassword(context.Background(), coderd.LoginWithPasswordRequest{
Email: "hello@ok.io",
Password: "password",
})
require.NoError(t, err)
err = client.SetSessionToken(token.SessionToken)
require.NoError(t, err)
_, err = client.CreateWorkspace(context.Background(), "", coderd.CreateWorkspaceRequest{
ProjectID: project.ID,
Name: "moo",
})
require.Error(t, err)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
_ = coderdtest.CreateWorkspace(t, client, "", project.ID)
})
}

func TestWorkspaceByUser(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.CreateInitialUser(t, client)
project := coderdtest.CreateProject(t, client, user.Organization)
workspace := coderdtest.CreateWorkspace(t, client, "", project.ID)
_, err := client.Workspace(context.Background(), "", workspace.Name)
require.NoError(t, err)
}

t.Run("CreateAlreadyExists", func(t *testing.T) {
func TestWorkspacesByProject(t *testing.T) {
t.Parallel()
t.Run("ListEmpty", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.CreateInitialUser(t, client)
project, workspace := setupProjectAndWorkspace(t, client, user)
_, err := client.CreateWorkspace(context.Background(), "", coderd.CreateWorkspaceRequest{
Name: workspace.Name,
ProjectID: project.ID,
})
require.Error(t, err)
project := coderdtest.CreateProject(t, client, user.Organization)
workspaces, err := client.WorkspacesByProject(context.Background(), user.Organization, project.Name)
require.NoError(t, err)
require.NotNil(t, workspaces)
})

t.Run("Single", func(t *testing.T) {
t.Run("List", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t)
user := coderdtest.CreateInitialUser(t, client)
_, workspace := setupProjectAndWorkspace(t, client, user)
_, err := client.Workspace(context.Background(), "", workspace.Name)
project := coderdtest.CreateProject(t, client, user.Organization)
_ = coderdtest.CreateWorkspace(t, client, "", project.ID)
workspaces, err := client.WorkspacesByProject(context.Background(), user.Organization, project.Name)
require.NoError(t, err)
require.NotNil(t, workspaces)
require.Len(t, workspaces, 1)
})
}
Loading