|
7 | 7 | "net/http/httptest"
|
8 | 8 | "testing"
|
9 | 9 |
|
| 10 | + "github.com/go-chi/chi/v5" |
10 | 11 | "github.com/google/uuid"
|
11 | 12 | "github.com/stretchr/testify/require"
|
12 | 13 |
|
@@ -160,4 +161,106 @@ func TestExtractWorkspaceProxy(t *testing.T) {
|
160 | 161 | defer res.Body.Close()
|
161 | 162 | require.Equal(t, http.StatusOK, res.StatusCode)
|
162 | 163 | })
|
| 164 | + |
| 165 | + t.Run("Deleted", func(t *testing.T) { |
| 166 | + t.Parallel() |
| 167 | + var ( |
| 168 | + db = dbfake.New() |
| 169 | + r = httptest.NewRequest("GET", "/", nil) |
| 170 | + rw = httptest.NewRecorder() |
| 171 | + |
| 172 | + proxy, secret = dbgen.WorkspaceProxy(t, db, database.WorkspaceProxy{}) |
| 173 | + ) |
| 174 | + err := db.UpdateWorkspaceProxyDeleted(context.Background(), database.UpdateWorkspaceProxyDeletedParams{ |
| 175 | + ID: proxy.ID, |
| 176 | + Deleted: true, |
| 177 | + }) |
| 178 | + require.NoError(t, err, "failed to delete workspace proxy") |
| 179 | + |
| 180 | + r.Header.Set(httpmw.WorkspaceProxyAuthTokenHeader, fmt.Sprintf("%s:%s", proxy.ID.String(), secret)) |
| 181 | + |
| 182 | + httpmw.ExtractWorkspaceProxy(httpmw.ExtractWorkspaceProxyConfig{ |
| 183 | + DB: db, |
| 184 | + })(successHandler).ServeHTTP(rw, r) |
| 185 | + res := rw.Result() |
| 186 | + defer res.Body.Close() |
| 187 | + require.Equal(t, http.StatusUnauthorized, res.StatusCode) |
| 188 | + }) |
| 189 | +} |
| 190 | + |
| 191 | +func TestExtractWorkspaceProxyParam(t *testing.T) { |
| 192 | + t.Parallel() |
| 193 | + |
| 194 | + successHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 195 | + // Only called if the API key passes through the handler. |
| 196 | + httpapi.Write(context.Background(), rw, http.StatusOK, codersdk.Response{ |
| 197 | + Message: "It worked!", |
| 198 | + }) |
| 199 | + }) |
| 200 | + |
| 201 | + t.Run("OKName", func(t *testing.T) { |
| 202 | + t.Parallel() |
| 203 | + var ( |
| 204 | + db = dbfake.New() |
| 205 | + r = httptest.NewRequest("GET", "/", nil) |
| 206 | + rw = httptest.NewRecorder() |
| 207 | + |
| 208 | + proxy, _ = dbgen.WorkspaceProxy(t, db, database.WorkspaceProxy{}) |
| 209 | + ) |
| 210 | + |
| 211 | + routeContext := chi.NewRouteContext() |
| 212 | + routeContext.URLParams.Add("workspaceproxy", proxy.Name) |
| 213 | + r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, routeContext)) |
| 214 | + |
| 215 | + httpmw.ExtractWorkspaceProxyParam(db)(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 216 | + // Checks that it exists on the context! |
| 217 | + _ = httpmw.WorkspaceProxyParam(request) |
| 218 | + successHandler.ServeHTTP(writer, request) |
| 219 | + })).ServeHTTP(rw, r) |
| 220 | + res := rw.Result() |
| 221 | + defer res.Body.Close() |
| 222 | + require.Equal(t, http.StatusOK, res.StatusCode) |
| 223 | + }) |
| 224 | + |
| 225 | + t.Run("OKID", func(t *testing.T) { |
| 226 | + t.Parallel() |
| 227 | + var ( |
| 228 | + db = dbfake.New() |
| 229 | + r = httptest.NewRequest("GET", "/", nil) |
| 230 | + rw = httptest.NewRecorder() |
| 231 | + |
| 232 | + proxy, _ = dbgen.WorkspaceProxy(t, db, database.WorkspaceProxy{}) |
| 233 | + ) |
| 234 | + |
| 235 | + routeContext := chi.NewRouteContext() |
| 236 | + routeContext.URLParams.Add("workspaceproxy", proxy.ID.String()) |
| 237 | + r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, routeContext)) |
| 238 | + |
| 239 | + httpmw.ExtractWorkspaceProxyParam(db)(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 240 | + // Checks that it exists on the context! |
| 241 | + _ = httpmw.WorkspaceProxyParam(request) |
| 242 | + successHandler.ServeHTTP(writer, request) |
| 243 | + })).ServeHTTP(rw, r) |
| 244 | + res := rw.Result() |
| 245 | + defer res.Body.Close() |
| 246 | + require.Equal(t, http.StatusOK, res.StatusCode) |
| 247 | + }) |
| 248 | + |
| 249 | + t.Run("NotFound", func(t *testing.T) { |
| 250 | + t.Parallel() |
| 251 | + var ( |
| 252 | + db = dbfake.New() |
| 253 | + r = httptest.NewRequest("GET", "/", nil) |
| 254 | + rw = httptest.NewRecorder() |
| 255 | + ) |
| 256 | + |
| 257 | + routeContext := chi.NewRouteContext() |
| 258 | + routeContext.URLParams.Add("workspaceproxy", uuid.NewString()) |
| 259 | + r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, routeContext)) |
| 260 | + |
| 261 | + httpmw.ExtractWorkspaceProxyParam(db)(successHandler).ServeHTTP(rw, r) |
| 262 | + res := rw.Result() |
| 263 | + defer res.Body.Close() |
| 264 | + require.Equal(t, http.StatusNotFound, res.StatusCode) |
| 265 | + }) |
163 | 266 | }
|
0 commit comments