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

Function TestGroupParam

coderd/httpmw/groupparam_test.go:19–81  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

17)
18
19func TestGroupParam(t *testing.T) {
20 t.Parallel()
21
22 t.Run("OK", func(t *testing.T) {
23 t.Parallel()
24
25 var (
26 db, _ = dbtestutil.NewDB(t)
27 r = httptest.NewRequest("GET", "/", nil)
28 w = httptest.NewRecorder()
29 )
30 dbtestutil.DisableForeignKeysAndTriggers(t, db)
31 group := dbgen.Group(t, db, database.Group{})
32
33 router := chi.NewRouter()
34 router.Use(httpmw.ExtractGroupParam(db))
35 router.Get("/", func(w http.ResponseWriter, r *http.Request) {
36 g := httpmw.GroupParam(r)
37 require.Equal(t, group, g)
38 w.WriteHeader(http.StatusOK)
39 })
40
41 rctx := chi.NewRouteContext()
42 rctx.URLParams.Add("group", group.ID.String())
43 r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
44
45 router.ServeHTTP(w, r)
46
47 res := w.Result()
48 defer res.Body.Close()
49 require.Equal(t, http.StatusOK, res.StatusCode)
50 })
51
52 t.Run("NotFound", func(t *testing.T) {
53 t.Parallel()
54
55 var (
56 db, _ = dbtestutil.NewDB(t)
57 r = httptest.NewRequest("GET", "/", nil)
58 w = httptest.NewRecorder()
59 )
60 dbtestutil.DisableForeignKeysAndTriggers(t, db)
61 group := dbgen.Group(t, db, database.Group{})
62
63 router := chi.NewRouter()
64 router.Use(httpmw.ExtractGroupParam(db))
65 router.Get("/", func(w http.ResponseWriter, r *http.Request) {
66 g := httpmw.GroupParam(r)
67 require.Equal(t, group, g)
68 w.WriteHeader(http.StatusOK)
69 })
70
71 rctx := chi.NewRouteContext()
72 rctx.URLParams.Add("group", uuid.NewString())
73 r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
74
75 router.ServeHTTP(w, r)
76

Callers

nothing calls this directly

Calls 15

NewDBFunction · 0.92
GroupFunction · 0.92
ExtractGroupParamFunction · 0.92
GroupParamFunction · 0.92
WithContextMethod · 0.80
RunMethod · 0.65
GetMethod · 0.65
AddMethod · 0.65
ContextMethod · 0.65
CloseMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected