29 lines
599 B
Go
29 lines
599 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var testAccessTokens sync.Map
|
|
|
|
func registerTestAccess(t *testing.T, a *API, sid string) {
|
|
t.Helper()
|
|
if a.authErr != nil {
|
|
t.Fatal(a.authErr)
|
|
}
|
|
u, _, err := a.s.SessionUser(sid)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testAccessTokens.Store(sid, a.accessJWT(sid, u.ID, time.Now().Add(accessLifetime)))
|
|
t.Cleanup(func() { testAccessTokens.Delete(sid) })
|
|
}
|
|
func addTestAccess(r *http.Request, sid string) {
|
|
if token, ok := testAccessTokens.Load(sid); ok {
|
|
r.AddCookie(&http.Cookie{Name: "dsh_access", Value: token.(string)})
|
|
}
|
|
}
|