(t *testing.T)
| 18 | ) |
| 19 | |
| 20 | func 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 | }, |
nothing calls this directly
no test coverage detected