Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105package client
import ( "context" "encoding/json" "net/http/httptest" "strings" "testing"
"github.com/isabelroses/quoteit/internal/api" "github.com/isabelroses/quoteit/internal/store")
func newTestServer(t *testing.T) (*httptest.Server, *store.Store) { t.Helper() st, err := store.Open(":memory:") if err != nil { t.Fatal(err) } ts := httptest.NewServer(api.New(st).Routes()) t.Cleanup(ts.Close) return ts, st}
func TestAdd_Get_RoundTrip(t *testing.T) { ts, _ := newTestServer(t) cl := New(ts.URL, "")
raw, err := cl.Add(context.Background(), "hello there", "Obi-Wan", "") if err != nil { t.Fatalf("add: %v", err) } var added store.Quote if err := json.Unmarshal(raw, &added); err != nil { t.Fatal(err) } if added.ID == 0 || added.Quote != "hello there" { t.Errorf("bad add response: %+v", added) }
raw, err = cl.ByID(context.Background(), added.ID) if err != nil { t.Fatalf("by id: %v", err) } var got store.Quote if err := json.Unmarshal(raw, &got); err != nil { t.Fatal(err) } if got.ID != added.ID { t.Errorf("got id=%d, want %d", got.ID, added.ID) }}
func TestModeration_RequiresToken(t *testing.T) { ts, _ := newTestServer(t) cl := New(ts.URL, "") _, err := cl.Pending(context.Background()) if err == nil || !strings.Contains(err.Error(), "401") { t.Errorf("expected 401 error without token, got %v", err) }}
func TestModeration_ApproveFlow(t *testing.T) { ts, st := newTestServer(t) _, token, _ := st.CreateAdmin(context.Background(), "alice") cl := New(ts.URL, token)
if err := st.Create(context.Background(), &store.Quote{ Quote: "blocked", Status: store.StatusPending, }); err != nil { t.Fatal(err) }
raw, err := cl.Pending(context.Background()) if err != nil { t.Fatal(err) } var resp struct { Quotes []store.Quote `json:"quotes"` } if err := json.Unmarshal(raw, &resp); err != nil { t.Fatal(err) } if len(resp.Quotes) != 1 { t.Fatalf("got %d pending, want 1", len(resp.Quotes)) }
if _, err := cl.Approve(context.Background(), resp.Quotes[0].ID); err != nil { t.Errorf("approve: %v", err) }}
func TestClient_SurfacesServerError(t *testing.T) { ts, _ := newTestServer(t) cl := New(ts.URL, "") _, err := cl.ByID(context.Background(), 999) if err == nil { t.Fatal("expected error for unknown id") } if !strings.Contains(err.Error(), "404") || !strings.Contains(err.Error(), "not found") { t.Errorf("error should include status + server message; got %v", err) }}