(t *testing.T)
| 18 | ) |
| 19 | |
| 20 | func TestTemplateVersionParam(t *testing.T) { |
| 21 | t.Parallel() |
| 22 | |
| 23 | setupAuthentication := func(db database.Store) (*http.Request, database.Template) { |
| 24 | dbtestutil.DisableForeignKeysAndTriggers(nil, db) |
| 25 | user := dbgen.User(t, db, database.User{}) |
| 26 | _, token := dbgen.APIKey(t, db, database.APIKey{ |
| 27 | UserID: user.ID, |
| 28 | }) |
| 29 | organization := dbgen.Organization(t, db, database.Organization{}) |
| 30 | _ = dbgen.OrganizationMember(t, db, database.OrganizationMember{ |
| 31 | UserID: user.ID, |
| 32 | OrganizationID: organization.ID, |
| 33 | }) |
| 34 | template := dbgen.Template(t, db, database.Template{ |
| 35 | OrganizationID: organization.ID, |
| 36 | Provisioner: database.ProvisionerTypeEcho, |
| 37 | }) |
| 38 | |
| 39 | r := httptest.NewRequest("GET", "/", nil) |
| 40 | r.Header.Set(codersdk.SessionTokenHeader, token) |
| 41 | |
| 42 | ctx := chi.NewRouteContext() |
| 43 | ctx.URLParams.Add("organization", organization.Name) |
| 44 | ctx.URLParams.Add("template", template.Name) |
| 45 | r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx)) |
| 46 | return r, template |
| 47 | } |
| 48 | |
| 49 | t.Run("None", func(t *testing.T) { |
| 50 | t.Parallel() |
| 51 | db, _ := dbtestutil.NewDB(t) |
| 52 | rtr := chi.NewRouter() |
| 53 | rtr.Use(httpmw.ExtractTemplateVersionParam(db)) |
| 54 | rtr.Get("/", nil) |
| 55 | r, _ := setupAuthentication(db) |
| 56 | rw := httptest.NewRecorder() |
| 57 | rtr.ServeHTTP(rw, r) |
| 58 | |
| 59 | res := rw.Result() |
| 60 | defer res.Body.Close() |
| 61 | require.Equal(t, http.StatusBadRequest, res.StatusCode) |
| 62 | }) |
| 63 | |
| 64 | t.Run("NotFound", func(t *testing.T) { |
| 65 | t.Parallel() |
| 66 | db, _ := dbtestutil.NewDB(t) |
| 67 | rtr := chi.NewRouter() |
| 68 | rtr.Use(httpmw.ExtractTemplateVersionParam(db)) |
| 69 | rtr.Get("/", nil) |
| 70 | |
| 71 | r, _ := setupAuthentication(db) |
| 72 | chi.RouteContext(r.Context()).URLParams.Add("templateversion", uuid.NewString()) |
| 73 | rw := httptest.NewRecorder() |
| 74 | rtr.ServeHTTP(rw, r) |
| 75 | |
| 76 | res := rw.Result() |
| 77 | defer res.Body.Close() |
nothing calls this directly
no test coverage detected