MCPcopy Index your code
hub / github.com/coder/coder / TestResolveWorkspace

Function TestResolveWorkspace

codersdk/workspaces_test.go:20–310  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18)
19
20func TestResolveWorkspace(t *testing.T) {
21 t.Parallel()
22
23 // writeJSON is a small helper that writes a JSON-encoded value
24 // with the given status code.
25 writeJSON := func(w http.ResponseWriter, status int, v any) {
26 w.Header().Set("Content-Type", "application/json")
27 w.WriteHeader(status)
28 _ = json.NewEncoder(w).Encode(v)
29 }
30
31 // errResponse builds a codersdk.Response suitable for error
32 // replies.
33 errResponse := func(msg string) codersdk.Response {
34 return codersdk.Response{Message: msg}
35 }
36
37 // newWorkspace returns a Workspace with the given ID and name.
38 newWorkspace := func(id uuid.UUID, name string) codersdk.Workspace {
39 return codersdk.Workspace{ID: id, Name: name}
40 }
41
42 // Each table case configures a mock server with separate UUID
43 // and name endpoint behaviors, then calls ResolveWorkspace with
44 // the given identifier.
45 type endpointResponse struct {
46 status int
47 workspace codersdk.Workspace
48 errMsg string
49 }
50 tests := []struct {
51 name string
52 identifier string
53 // uuidEndpoint configures GET /api/v2/workspaces/{workspace}.
54 // nil means the endpoint is not registered (404 from chi).
55 uuidEndpoint *endpointResponse
56 // nameEndpoint configures GET /api/v2/users/{user}/workspace/{workspace}.
57 // nil means the endpoint is not registered.
58 nameEndpoint *endpointResponse
59 // expectedOwner and expectedName are checked via assert inside
60 // the name endpoint handler (when non-empty).
61 expectedOwner string
62 expectedName string
63 // Expected outcomes.
64 wantErr bool
65 wantStatusCode int
66 wantUUIDHits int64
67 wantNameHits int64
68 }{
69 {
70 name: "ByUUID",
71 identifier: "", // filled dynamically below
72 uuidEndpoint: &endpointResponse{
73 status: http.StatusOK,
74 },
75 wantUUIDHits: 1,
76 wantNameHits: 0,
77 },

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
StatusCodeMethod · 0.95
NewFunction · 0.92
errResponseFunction · 0.85
EncodeMethod · 0.80
ResolveWorkspaceMethod · 0.80
AsMethod · 0.80
SetMethod · 0.65
RunMethod · 0.65
NewMethod · 0.65
GetMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected