app/api_test.go

v1.5.0
skunky-art/app/api_test.go history · blame · raw

110 lines · 3652 bytes

  1package app
  2
  3import (
  4	"net/http/httptest"
  5	"testing"
  6
  7	"github.com/krazywarez/devianter"
  8)
  9
 10// fullviewDeviation returns a deviation whose media assembles into a non-empty
 11// wixmp URL, i.e. one that sendMedia is meant to serve.
 12func fullviewDeviation() *devianter.Deviation {
 13	d := &devianter.Deviation{}
 14	d.Media.BaseUri = "https://images-wixmp-abc.wixmp.com/f/u/x.png"
 15	d.Media.Name = "x"
 16	d.Media.Types = append(d.Media.Types, struct {
 17		T    string
 18		H, W int
 19	}{T: "fullview", H: 1920, W: 1280})
 20	return d
 21}
 22
 23// TestSendMediaServesRealMedia is the regression test for the inverted guard: a
 24// deviation that has media must be sent, not dropped. In non-proxy mode that is
 25// a 302 to the wixmp URL; the pre-fix guard returned before writing anything.
 26func TestSendMediaServesRealMedia(t *testing.T) {
 27	proxy := CFG.Proxy
 28	CFG.Proxy = false
 29	defer func() { CFG.Proxy = proxy }()
 30
 31	w := httptest.NewRecorder()
 32	API{main: &skunkyart{Writer: w}}.sendMedia(fullviewDeviation())
 33
 34	if w.Code != 302 {
 35		t.Errorf("status is %d, want a 302 redirect to the media", w.Code)
 36	}
 37	if w.Header().Get("Location") == "" {
 38		t.Error("no Location header set — the media was dropped")
 39	}
 40}
 41
 42// TestSendMediaIgnoresEmptyMedia pins the other half of the bug: a deviation
 43// with no media must be a no-op. With proxy on, the pre-fix code fell through to
 44// mediaURL[21:] on an empty string and panicked.
 45func TestSendMediaIgnoresEmptyMedia(t *testing.T) {
 46	proxy := CFG.Proxy
 47	CFG.Proxy = true
 48	defer func() { CFG.Proxy = proxy }()
 49
 50	w := httptest.NewRecorder()
 51	API{main: &skunkyart{Writer: w}}.sendMedia(&devianter.Deviation{})
 52
 53	if w.Code != 200 {
 54		t.Errorf("status is %d, want nothing written (recorder default 200)", w.Code)
 55	}
 56	if loc := w.Header().Get("Location"); loc != "" {
 57		t.Errorf("Location %q set for a media-less deviation, want none", loc)
 58	}
 59}
 60
 61// withDailyDeviations scripts the daily deviations fetch with the given
 62// entries and counts the calls.
 63func withDailyDeviations(t *testing.T, devs ...devianter.Deviation) *int {
 64	t.Helper()
 65	orig := fetchDailyDeviations
 66	calls := 0
 67	fetchDailyDeviations = func(int) (devianter.DailyDeviations, devianter.Error) {
 68		calls++
 69		return devianter.DailyDeviations{Deviations: devs}, devianter.Error{}
 70	}
 71	t.Cleanup(func() { fetchDailyDeviations = orig })
 72	return &calls
 73}
 74
 75// TestRandomPicksFromTheDailyDeviations pins the new source: one fetch of the
 76// daily page, and the pick is served as media.
 77func TestRandomPicksFromTheDailyDeviations(t *testing.T) {
 78	proxy, nsfw := CFG.Proxy, CFG.Nsfw
 79	CFG.Proxy, CFG.Nsfw = false, true
 80	defer func() { CFG.Proxy, CFG.Nsfw = proxy, nsfw }()
 81	calls := withDailyDeviations(t, *fullviewDeviation())
 82
 83	w := httptest.NewRecorder()
 84	API{main: &skunkyart{Writer: w}}.Random()
 85
 86	if *calls != 1 {
 87		t.Errorf("daily deviations fetched %d times, want 1", *calls)
 88	}
 89	if w.Code != 302 || w.Header().Get("Location") == "" {
 90		t.Errorf("status %d, Location %q; want a 302 to the pick's media", w.Code, w.Header().Get("Location"))
 91	}
 92}
 93
 94// TestRandomHonoursNSFW pins that a pick is drawn only from what the instance
 95// may show: with nsfw off and only mature entries there is nothing to serve.
 96func TestRandomHonoursNSFW(t *testing.T) {
 97	proxy, nsfw := CFG.Proxy, CFG.Nsfw
 98	CFG.Proxy, CFG.Nsfw = false, false
 99	defer func() { CFG.Proxy, CFG.Nsfw = proxy, nsfw }()
100	mature := *fullviewDeviation()
101	mature.NSFW = true
102	withDailyDeviations(t, mature)
103
104	w := httptest.NewRecorder()
105	API{main: &skunkyart{Writer: w}}.Random()
106
107	if w.Code != 404 || w.Header().Get("Location") != "" {
108		t.Errorf("status %d, Location %q; want 404 and no media", w.Code, w.Header().Get("Location"))
109	}
110}