| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "crypto/aes" |
| 5 | "crypto/cipher" |
| 6 | "crypto/ed25519" |
| 7 | "crypto/rand" |
| 8 | "crypto/sha256" |
| 9 | "encoding/base64" |
| 10 | "encoding/json" |
| 11 | "fmt" |
| 12 | "net/http/httptest" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 18 | "amadan.net/oficina/equipo/internal/equipo/setup" |
| 19 | ) |
| 20 | |
| 21 | func TestPersonAPIDeviceTrustedProxySource(t *testing.T) { |
| 22 | for _, proxied := range []bool{false, true} { |
| 23 | t.Run(fmt.Sprint(proxied), func(t *testing.T) { |
| 24 | f := newFixture(t) |
| 25 | control := "" |
| 26 | if proxied { |
| 27 | control = "fixture-socket" |
| 28 | } |
| 29 | t.Setenv("CARLOS_CONTROL_SOCKET", control) |
| 30 | pub, _, err := ed25519.GenerateKey(rand.Reader) |
| 31 | must(t, err) |
| 32 | request := func(xff string) *httptest.ResponseRecorder { |
| 33 | r := httptest.NewRequest("POST", "/api/v1/auth/devices", strings.NewReader(fmt.Sprintf(`{"public_key":%q,"label":"phone"}`, base64.RawURLEncoding.EncodeToString(pub)))) |
| 34 | r.RemoteAddr = "127.0.0.1:1234" |
| 35 | r.Header.Set("X-Forwarded-For", xff) |
| 36 | r.Header.Set("Content-Type", "application/json") |
| 37 | r.Header.Set("Idempotency-Key", rand.Text()) |
| 38 | w := httptest.NewRecorder() |
| 39 | f.Handler.ServeHTTP(w, r) |
| 40 | return w |
| 41 | } |
| 42 | for i := 0; i < 10; i++ { |
| 43 | expectHTTP(t, request(fmt.Sprintf("198.51.100.%d, 192.0.2.1", i)), 200) |
| 44 | } |
| 45 | expectHTTP(t, request("198.51.100.99, 192.0.2.1"), 429) |
| 46 | want := 429 |
| 47 | if proxied { |
| 48 | want = 200 |
| 49 | } |
| 50 | expectHTTP(t, request("198.51.100.99, 192.0.2.2"), want) |
| 51 | }) |
| 52 | } |
| 53 | } |
| 54 | func TestPersonAPILegacyCursorFailsClosed(t *testing.T) { |
| 55 | f := newFixture(t) |
| 56 | key := sha256.Sum256([]byte("equipo.person-api.cursor.v1\x00plan3-fixture")) |
| 57 | block, err := aes.NewCipher(key[:]) |
| 58 | must(t, err) |
| 59 | g, err := cipher.NewGCM(block) |
| 60 | must(t, err) |
| 61 | raw, err := json.Marshal(struct { |
| 62 | Position any |
| 63 | ExpiresAt time.Time |
| 64 | }{map[string]any{"ID": "", "Sequence": 0}, time.Now().Add(time.Hour)}) |
| 65 | must(t, err) |
| 66 | nonce := make([]byte, g.NonceSize()) |
| 67 | _, err = rand.Read(nonce) |
| 68 | must(t, err) |
| 69 | token := base64.RawURLEncoding.EncodeToString(g.Seal(nonce, nonce, raw, []byte(owner.Address+"\x00people"))) |
| 70 | expectHTTP(t, apiRequest(t, f, owner, "GET", "/api/v1/people?cursor="+token, "", "", ""), 410) |
| 71 | } |
| 72 | func TestPersonAPIRunnerInstanceProjection(t *testing.T) { |
| 73 | f := newFixture(t) |
| 74 | for _, instance := range []string{f.TeamID, "foreign"} { |
| 75 | must(t, f.DB.G.Create(&setup.RunnerRecord{Runner: setup.Runner{ID: instance, Person: owner.Address, Label: instance, Revision: 1}, InstanceID: instance}).Error) |
| 76 | } |
| 77 | w := apiRequest(t, f, owner, "GET", "/api/v1/runners", "", "", "") |
| 78 | expectHTTP(t, w, 200) |
| 79 | page := apiValue[personapi.Page[map[string]any]](t, w) |
| 80 | if len(page.Items) != 1 || page.Items[0]["id"] != f.TeamID { |
| 81 | t.Fatal("runner list crossed instance boundary") |
| 82 | } |
| 83 | for _, key := range []string{"created_at", "last_seen"} { |
| 84 | if _, ok := page.Items[0][key]; ok { |
| 85 | t.Fatal("unavailable runner time should be omitted", key) |
| 86 | } |
| 87 | } |
| 88 | expectHTTP(t, apiRequest(t, f, owner, "GET", "/api/v1/runners/foreign", "", "", ""), 404) |
| 89 | } |
| 90 | |