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

Function TestTemplateParam

coderd/httpmw/templateparam_test.go:20–125  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18)
19
20func TestTemplateParam(t *testing.T) {
21 t.Parallel()
22
23 setupAuthentication := func(db database.Store) (*http.Request, database.Organization) {
24 var (
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 )
35
36 r := httptest.NewRequest("GET", "/", nil)
37 r.Header.Set(codersdk.SessionTokenHeader, token)
38
39 ctx := chi.NewRouteContext()
40 r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx))
41 return r, organization
42 }
43
44 t.Run("None", func(t *testing.T) {
45 t.Parallel()
46 db, _ := dbtestutil.NewDB(t)
47 rtr := chi.NewRouter()
48 rtr.Use(httpmw.ExtractTemplateParam(db))
49 rtr.Get("/", nil)
50 r, _ := setupAuthentication(db)
51 rw := httptest.NewRecorder()
52 rtr.ServeHTTP(rw, r)
53
54 res := rw.Result()
55 defer res.Body.Close()
56 require.Equal(t, http.StatusBadRequest, res.StatusCode)
57 })
58
59 t.Run("NotFound", func(t *testing.T) {
60 t.Parallel()
61 db, _ := dbtestutil.NewDB(t)
62 rtr := chi.NewRouter()
63 rtr.Use(httpmw.ExtractTemplateParam(db))
64 rtr.Get("/", nil)
65
66 r, _ := setupAuthentication(db)
67 chi.RouteContext(r.Context()).URLParams.Add("template", uuid.NewString())
68 rw := httptest.NewRecorder()
69 rtr.ServeHTTP(rw, r)
70
71 res := rw.Result()
72 defer res.Body.Close()
73 require.Equal(t, http.StatusNotFound, res.StatusCode)
74 })
75
76 t.Run("BadUUID", func(t *testing.T) {
77 t.Parallel()

Callers

nothing calls this directly

Calls 15

UserFunction · 0.92
APIKeyFunction · 0.92
OrganizationFunction · 0.92
OrganizationMemberFunction · 0.92
NewDBFunction · 0.92
ExtractTemplateParamFunction · 0.92
ExtractAPIKeyMWFunction · 0.92
ExtractOrganizationParamFunction · 0.92
TemplateParamFunction · 0.92
TemplateFunction · 0.92
WithContextMethod · 0.80

Tested by

no test coverage detected