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

Function TestInjection

site/site_test.go:120–160  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

118}
119
120func TestInjection(t *testing.T) {
121 t.Parallel()
122
123 siteFS := fstest.MapFS{
124 "index.html": &fstest.MapFile{
125 Data: []byte("{{ .User }}"),
126 },
127 }
128 db, _ := dbtestutil.NewDB(t)
129 handler, err := site.New(&site.Options{
130 Telemetry: telemetry.NewNoop(),
131 Database: db,
132 SiteFS: siteFS,
133 })
134 require.NoError(t, err)
135
136 user := dbgen.User(t, db, database.User{})
137 _, token := dbgen.APIKey(t, db, database.APIKey{
138 UserID: user.ID,
139 ExpiresAt: time.Now().Add(time.Hour),
140 })
141
142 r := httptest.NewRequest("GET", "/", nil)
143 r.Header.Set(codersdk.SessionTokenHeader, token)
144 rw := httptest.NewRecorder()
145
146 handler.ServeHTTP(rw, r)
147 require.Equal(t, http.StatusOK, rw.Code)
148 var got codersdk.User
149 err = json.Unmarshal([]byte(html.UnescapeString(rw.Body.String())), &got)
150 require.NoError(t, err)
151
152 // This will update as part of the request!
153 got.LastSeenAt = user.LastSeenAt
154
155 // json.Unmarshal doesn't parse the timezone correctly
156 got.CreatedAt = got.CreatedAt.In(user.CreatedAt.Location())
157 got.UpdatedAt = got.UpdatedAt.In(user.CreatedAt.Location())
158
159 require.Equal(t, db2sdk.User(user, []uuid.UUID{}), got)
160}
161
162func TestInjectionUserAppearance(t *testing.T) {
163 t.Parallel()

Callers

nothing calls this directly

Calls 13

NewDBFunction · 0.92
NewFunction · 0.92
NewNoopFunction · 0.92
UserFunction · 0.92
APIKeyFunction · 0.92
UserFunction · 0.92
LocationMethod · 0.80
AddMethod · 0.65
SetMethod · 0.65
ServeHTTPMethod · 0.45
EqualMethod · 0.45
UnmarshalMethod · 0.45

Tested by

no test coverage detected