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)
| 133 | |
| 134 | // SetReqAuthHeader sets the authorization header in the given request for the given user with a specific DB |
| 135 | func 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 |
| 154 | func HTTPAuthDo(t *testing.T, db *gorm.DB, req *http.Request, user database.User) *http.Response { |