loginAndJar logs in with the given token and returns a cookiejar that subsequent calls can reuse.
(t *testing.T, server *httptest.Server, token string)
| 42 | // loginAndJar logs in with the given token and returns a cookiejar |
| 43 | // that subsequent calls can reuse. |
| 44 | func loginAndJar(t *testing.T, server *httptest.Server, token string) http.CookieJar { |
| 45 | t.Helper() |
| 46 | jar, err := cookiejar.New(nil) |
| 47 | if err != nil { |
| 48 | t.Fatalf("cookiejar.New: %v", err) |
| 49 | } |
| 50 | client := &http.Client{Jar: jar} |
| 51 | body, _ := json.Marshal(map[string]string{"token": token}) |
| 52 | resp, err := client.Post(server.URL+"/api/session/login", "application/json", bytes.NewReader(body)) |
| 53 | if err != nil { |
| 54 | t.Fatalf("login: %v", err) |
| 55 | } |
| 56 | defer resp.Body.Close() |
| 57 | if resp.StatusCode != http.StatusOK { |
| 58 | t.Fatalf("login status: %d", resp.StatusCode) |
| 59 | } |
| 60 | return jar |
| 61 | } |
| 62 | |
| 63 | func TestSessionLoginProtectsVaultRoutes(t *testing.T) { |
| 64 | root := t.TempDir() |
no test coverage detected