package httpapi import ( "net/http" "strings" "testing" ) func TestDevicesNeedASignedInUser(t *testing.T) { h := newTestAPI(t) for _, c := range [][2]string{{"GET", "/api/v1/me/devices"}, {"POST", "/api/v1/me/devices"}, {"DELETE", "/api/v1/me/devices/1"}} { if r := do(t, h, c[0], c[1], "", map[string]any{}); r.Code != http.StatusUnauthorized { t.Errorf("%s %s without a token: %d", c[0], c[1], r.Code) } } } func TestADeviceIsMadeShownOnceAndTakenBack(t *testing.T) { h := newTestAPI(t) alice := registerAndLogin(t, h) r := do(t, h, "GET", "/api/v1/me/devices", alice, nil) if r.Code != http.StatusOK { t.Fatalf("%d %v", r.Code, r.Body) } if list, ok := r.Body["devices"].([]any); !ok || len(list) != 0 { t.Fatalf("devices at the start: %v", r.Body) } r = do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "Phone"}) if r.Code != http.StatusCreated { t.Fatalf("create: %d %v", r.Code, r.Body) } sameKeys(t, "created device", r.Body, "id", "name", "created_at", "secret") secret, _ := r.Body["secret"].(string) if len(secret) != 48 || r.Body["name"] != "Phone" { t.Fatalf("created = %v", r.Body) } r = do(t, h, "GET", "/api/v1/me/devices", alice, nil) list, _ := r.Body["devices"].([]any) if len(list) != 1 { t.Fatalf("list = %v", r.Body) } one := list[0].(map[string]any) sameKeys(t, "listed device", one, "id", "name", "created_at") if strings.Contains(r.Body["devices"].([]any)[0].(map[string]any)["name"].(string), secret) { t.Fatal("the secret is in the list") } id := int64(one["id"].(float64)) path := "/api/v1/me/devices/" + itoa(id) if r := do(t, h, "DELETE", path, alice, nil); r.Code != http.StatusNoContent { t.Fatalf("delete: %d %v", r.Code, r.Body) } if r := do(t, h, "DELETE", path, alice, nil); r.Code != http.StatusNotFound || errCode(r) != "device_not_found" { t.Fatalf("delete twice: %d %v", r.Code, r.Body) } } func TestADeviceNeedsAName(t *testing.T) { h := newTestAPI(t) alice := registerAndLogin(t, h) for _, body := range []any{map[string]any{"name": " "}, map[string]any{}, map[string]any{"name": strings.Repeat("a", 61)}} { if r := do(t, h, "POST", "/api/v1/me/devices", alice, body); r.Code != http.StatusBadRequest || errCode(r) != "invalid_name" { t.Errorf("%v: %d %v", body, r.Code, r.Body) } } if r := do(t, h, "POST", "/api/v1/me/devices", alice, "{not json"); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { t.Errorf("bad json: %d %v", r.Code, r.Body) } if r := do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "ok", "extra": 1}); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { t.Errorf("unknown name: %d %v", r.Code, r.Body) } } func TestADeviceOfSomeoneElseIsNotFound(t *testing.T) { h := newTestAPI(t) alice := registerAndLogin(t, h) r := do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "Phone"}) id := int64(r.Body["id"].(float64)) _, bob := addUser(t, h, alice, "bob", false) if r := do(t, h, "DELETE", "/api/v1/me/devices/"+itoa(id), bob, nil); r.Code != http.StatusNotFound { t.Fatalf("bob deleted alice's device: %d", r.Code) } if r := do(t, h, "GET", "/api/v1/me/devices", bob, nil); len(r.Body["devices"].([]any)) != 0 { t.Fatalf("bob sees %v", r.Body) } } func TestTooManyDevices(t *testing.T) { h := newTestAPI(t) alice := registerAndLogin(t, h) for i := 0; i < 20; i++ { if r := do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "d"}); r.Code != http.StatusCreated { t.Fatalf("%d: %d", i, r.Code) } } if r := do(t, h, "POST", "/api/v1/me/devices", alice, map[string]any{"name": "d"}); r.Code != http.StatusConflict || errCode(r) != "too_many_devices" { t.Fatalf("over the limit: %d %v", r.Code, r.Body) } }