Always include user and device ID in API request headers, so that they're available in all server-side handlers

This commit is contained in:
David Dworken
2023-10-14 10:52:35 -07:00
parent 54c3429bca
commit fca2b1441f
12 changed files with 87 additions and 62 deletions

View File

@@ -2,10 +2,12 @@ package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
@@ -351,7 +353,7 @@ func captureTerminalOutputComplex(t testing.TB, captureConfig TmuxCaptureConfig)
}
func assertNoLeakedConnections(t testing.TB) {
resp, err := lib.ApiGet("/api/v1/get-num-connections")
resp, err := lib.ApiGet(makeTestOnlyContextWithFakeConfig(), "/api/v1/get-num-connections")
require.NoError(t, err)
numConnections, err := strconv.Atoi(string(resp))
require.NoError(t, err)
@@ -368,6 +370,21 @@ func getPidofCommand() string {
return "pidof"
}
func makeTestOnlyContextWithFakeConfig() context.Context {
fakeConfig := hctx.ClientConfig{
UserSecret: "FAKE_TEST_DEVICE",
DeviceId: "FAKE_TEST_DEVICE",
}
ctx := context.Background()
ctx = context.WithValue(ctx, hctx.ConfigCtxKey, &fakeConfig)
// Note: We don't create a DB here
homedir, err := os.UserHomeDir()
if err != nil {
panic(fmt.Errorf("failed to get homedir: %w", err))
}
return context.WithValue(ctx, hctx.HomedirCtxKey, homedir)
}
type deviceSet struct {
deviceMap *map[device]deviceOp
currentDevice *device