MCPcopy Index your code
hub / github.com/dnote/dnote / SetReqAuthHeader

Function SetReqAuthHeader

pkg/server/testutils/main.go:135–151  ·  view source on GitHub ↗

SetReqAuthHeader sets the authorization header in the given request for the given user with a specific DB

(t *testing.T, db *gorm.DB, req *http.Request, user database.User)

Source from the content-addressed store, hash-verified

133
134// SetReqAuthHeader sets the authorization header in the given request for the given user with a specific DB
135func SetReqAuthHeader(t *testing.T, db *gorm.DB, req *http.Request, user database.User) {
136 b := make([]byte, 32)
137 if _, err := rand.Read(b); err != nil {
138 t.Fatal(errors.Wrap(err, "reading random bits"))
139 }
140
141 session := database.Session{
142 Key: base64.StdEncoding.EncodeToString(b),
143 UserID: user.ID,
144 ExpiresAt: time.Now().Add(time.Hour * 10 * 24),
145 }
146 if err := db.Save(&session).Error; err != nil {
147 t.Fatal(errors.Wrap(err, "Failed to prepare user"))
148 }
149
150 req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", session.Key))
151}
152
153// HTTPAuthDo makes an HTTP request with an appropriate authorization header for a user with a specific DB
154func HTTPAuthDo(t *testing.T, db *gorm.DB, req *http.Request, user database.User) *http.Response {

Callers 1

HTTPAuthDoFunction · 0.85

Calls 1

NowMethod · 0.65

Tested by

no test coverage detected