|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + daptinClient "github.com/daptin/daptin-go-client" |
| 11 | +) |
| 12 | + |
| 13 | +func TestFindAllParsesValidListResponse(t *testing.T) { |
| 14 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 15 | + if r.URL.Path != "/api/usergroup" { |
| 16 | + t.Fatalf("unexpected path: %s", r.URL.Path) |
| 17 | + } |
| 18 | + if r.URL.Query().Get("page[size]") != "10" { |
| 19 | + t.Fatalf("expected page[size]=10, got %q", r.URL.Query().Get("page[size]")) |
| 20 | + } |
| 21 | + w.Header().Set("Content-Type", "application/json") |
| 22 | + _, _ = w.Write([]byte(`{"data":[{"id":"1","type":"usergroup","attributes":{"name":"users"}}]}`)) |
| 23 | + })) |
| 24 | + defer server.Close() |
| 25 | + |
| 26 | + c := New(server.URL, "", false) |
| 27 | + rows, err := c.FindAll("usergroup", daptinClient.DaptinQueryParameters{"page[size]": 10}) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("unexpected error: %v", err) |
| 30 | + } |
| 31 | + if len(rows) != 1 { |
| 32 | + t.Fatalf("expected 1 row, got %d", len(rows)) |
| 33 | + } |
| 34 | + if rows[0]["id"] != "1" { |
| 35 | + t.Fatalf("expected row id 1, got %v", rows[0]["id"]) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestFindAllJoinTableNotFoundReturnsHelpfulError(t *testing.T) { |
| 40 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 41 | + http.Error(w, `{"error":"not found"}`, http.StatusNotFound) |
| 42 | + })) |
| 43 | + defer server.Close() |
| 44 | + |
| 45 | + c := New(server.URL, "", false) |
| 46 | + _, err := c.FindAll("oauth_connect_oauth_connect_id_has_usergroup_usergroup_id", nil) |
| 47 | + if err == nil { |
| 48 | + t.Fatal("expected error") |
| 49 | + } |
| 50 | + if !strings.Contains(err.Error(), "generated join table") { |
| 51 | + t.Fatalf("expected join table guidance, got: %v", err) |
| 52 | + } |
| 53 | + if errors.Is(err, ErrNotFound) { |
| 54 | + t.Fatalf("expected contextual join table error, got ErrNotFound: %v", err) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestFindAllJoinTableMalformedResponseReturnsHelpfulError(t *testing.T) { |
| 59 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 60 | + w.Header().Set("Content-Type", "application/json") |
| 61 | + _, _ = w.Write([]byte(`{"data":null}`)) |
| 62 | + })) |
| 63 | + defer server.Close() |
| 64 | + |
| 65 | + c := New(server.URL, "", false) |
| 66 | + _, err := c.FindAll("user_account_user_account_id_has_usergroup_usergroup_id", nil) |
| 67 | + if err == nil { |
| 68 | + t.Fatal("expected error") |
| 69 | + } |
| 70 | + if !strings.Contains(err.Error(), "not exposed as API entities") { |
| 71 | + t.Fatalf("expected API entity guidance, got: %v", err) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestFindAllMalformedResponseReturnsError(t *testing.T) { |
| 76 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 77 | + w.Header().Set("Content-Type", "application/json") |
| 78 | + _, _ = w.Write([]byte(`{"data":null}`)) |
| 79 | + })) |
| 80 | + defer server.Close() |
| 81 | + |
| 82 | + c := New(server.URL, "", false) |
| 83 | + _, err := c.FindAll("document", nil) |
| 84 | + if err == nil { |
| 85 | + t.Fatal("expected error") |
| 86 | + } |
| 87 | + if !strings.Contains(err.Error(), "expected JSON:API data array") { |
| 88 | + t.Fatalf("expected malformed response error, got: %v", err) |
| 89 | + } |
| 90 | +} |
0 commit comments