286 lines
7.7 KiB
Go
286 lines
7.7 KiB
Go
package httpapi_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"watch-party-backend/internal/core/episode"
|
|
httpapi "watch-party-backend/internal/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ---- fake service implementing episode.UseCases ----
|
|
|
|
type fakeSvc struct {
|
|
cur episode.Episode
|
|
getErr error
|
|
setCur episode.Episode
|
|
setErr error
|
|
moveRes episode.MoveResult
|
|
moveErr error
|
|
listRes []episode.Episode
|
|
listErr error
|
|
deleteErr error
|
|
lastSetID int64
|
|
lastTime string
|
|
lastMove []int64
|
|
lastDelID int64
|
|
}
|
|
|
|
func (f *fakeSvc) GetCurrent(ctx context.Context) (episode.Episode, error) {
|
|
return f.cur, f.getErr
|
|
}
|
|
func (f *fakeSvc) SetCurrent(ctx context.Context, id int64, start string) (episode.Episode, error) {
|
|
f.lastSetID, f.lastTime = id, start
|
|
return f.setCur, f.setErr
|
|
}
|
|
func (f *fakeSvc) MoveToArchive(ctx context.Context, ids []int64) (episode.MoveResult, error) {
|
|
f.lastMove = append([]int64(nil), ids...)
|
|
return f.moveRes, f.moveErr
|
|
}
|
|
func (f *fakeSvc) ListAll(ctx context.Context) ([]episode.Episode, error) {
|
|
if f.listRes == nil && f.listErr == nil {
|
|
return []episode.Episode{{Id: 10, EpTitle: "X"}}, nil
|
|
}
|
|
return f.listRes, f.listErr
|
|
}
|
|
func (f *fakeSvc) Delete(ctx context.Context, id int64) error {
|
|
f.lastDelID = id
|
|
return f.deleteErr
|
|
}
|
|
|
|
// ---- helpers ----
|
|
|
|
func newRouterWithSvc(svc episode.UseCases) *gin.Engine {
|
|
gin.SetMode(gin.TestMode)
|
|
return httpapi.NewRouter(svc)
|
|
}
|
|
|
|
func doJSON(t *testing.T, r *gin.Engine, method, path string, body any) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
if body != nil {
|
|
if err := json.NewEncoder(&buf).Encode(body); err != nil {
|
|
t.Fatalf("encode body: %v", err)
|
|
}
|
|
}
|
|
req := httptest.NewRequest(method, path, &buf)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
return w
|
|
}
|
|
|
|
// ---- tests ----
|
|
|
|
func TestHealthz_OK(t *testing.T) {
|
|
r := newRouterWithSvc(&fakeSvc{})
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestGetCurrent_V1_OK(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
svc := &fakeSvc{
|
|
cur: episode.Episode{
|
|
EpNum: 7,
|
|
EpTitle: "Lucky",
|
|
SeasonName: "S2",
|
|
StartTime: "18:30:00",
|
|
PlaybackLength: "00:24:00",
|
|
DateCreated: now,
|
|
},
|
|
}
|
|
r := newRouterWithSvc(svc)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/current", nil)
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("got %d", w.Code)
|
|
}
|
|
var got episode.Episode
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("json: %v", err)
|
|
}
|
|
if got.EpNum != 7 || got.EpTitle != "Lucky" {
|
|
t.Fatalf("unexpected %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestPostCurrent_BadPayload(t *testing.T) {
|
|
r := newRouterWithSvc(&fakeSvc{})
|
|
// missing fields
|
|
w := doJSON(t, r, http.MethodPost, "/api/v1/current", map[string]any{"id": 1})
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestPostCurrent_BadTime(t *testing.T) {
|
|
svc := &fakeSvc{setErr: episode.ErrInvalidStartTime}
|
|
r := newRouterWithSvc(svc)
|
|
w := doJSON(t, r, http.MethodPost, "/api/v1/current", map[string]any{
|
|
"id": 42, "start_time": "7:0:0",
|
|
})
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d; body=%s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestPostCurrent_OK(t *testing.T) {
|
|
want := episode.Episode{EpNum: 42, EpTitle: "Answer", StartTime: "21:07:05"}
|
|
svc := &fakeSvc{setCur: want}
|
|
r := newRouterWithSvc(svc)
|
|
w := doJSON(t, r, http.MethodPost, "/api/v1/current", map[string]any{
|
|
"id": 42, "start_time": "21:07:05",
|
|
})
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d; body=%s", w.Code, w.Body.String())
|
|
}
|
|
var got episode.Episode
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("json: %v", err)
|
|
}
|
|
if got.EpNum != want.EpNum || got.StartTime != want.StartTime {
|
|
t.Fatalf("unexpected %+v", got)
|
|
}
|
|
if svc.lastSetID != 42 || svc.lastTime != "21:07:05" {
|
|
t.Fatalf("service not called as expected: id=%d time=%s", svc.lastSetID, svc.lastTime)
|
|
}
|
|
}
|
|
|
|
func TestPostArchive_Empty(t *testing.T) {
|
|
svc := &fakeSvc{moveErr: episode.ErrEmptyIDs}
|
|
r := newRouterWithSvc(svc)
|
|
w := doJSON(t, r, http.MethodPost, "/api/v1/archive", map[string]any{})
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d; body=%s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestPostArchive_SingleAndMultiple_OK(t *testing.T) {
|
|
svc := &fakeSvc{
|
|
moveRes: episode.MoveResult{
|
|
MovedIDs: []int64{1, 2},
|
|
DeletedIDs: []int64{1, 2},
|
|
SkippedIDs: []int64{999},
|
|
},
|
|
}
|
|
r := newRouterWithSvc(svc)
|
|
|
|
// single
|
|
w1 := doJSON(t, r, http.MethodPost, "/api/v1/archive", map[string]any{"id": 1})
|
|
if w1.Code != http.StatusOK {
|
|
t.Fatalf("got %d: %s", w1.Code, w1.Body.String())
|
|
}
|
|
// multiple
|
|
w2 := doJSON(t, r, http.MethodPost, "/api/v1/archive", map[string]any{"ids": []int64{1, 2, 1}})
|
|
if w2.Code != http.StatusOK {
|
|
t.Fatalf("got %d: %s", w2.Code, w2.Body.String())
|
|
}
|
|
// check the lastMove captured by fake
|
|
if len(svc.lastMove) != 3 || svc.lastMove[0] != 1 || svc.lastMove[1] != 2 {
|
|
t.Fatalf("unexpected service ids: %+v", svc.lastMove)
|
|
}
|
|
// basic JSON fields present
|
|
var body map[string]any
|
|
if err := json.Unmarshal(w2.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("json: %v", err)
|
|
}
|
|
if body["inserted"] == nil || body["deleted"] == nil {
|
|
t.Fatalf("missing counters in response: %+v", body)
|
|
}
|
|
}
|
|
|
|
func TestListShows_OK(t *testing.T) {
|
|
svc := &fakeSvc{
|
|
listRes: []episode.Episode{
|
|
{Id: 1, EpTitle: "Pilot"},
|
|
{Id: 2, EpTitle: "Next"},
|
|
},
|
|
}
|
|
r := newRouterWithSvc(svc)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/shows", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
var got []episode.Episode
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("json: %v", err)
|
|
}
|
|
if len(got) != 2 || got[0].EpTitle != "Pilot" {
|
|
t.Fatalf("unexpected list: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestListShows_Error(t *testing.T) {
|
|
svc := &fakeSvc{listErr: errors.New("query failed")}
|
|
r := newRouterWithSvc(svc)
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/shows", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Fatalf("expected 500, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDeleteShows_InvalidID(t *testing.T) {
|
|
r := newRouterWithSvc(&fakeSvc{})
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/shows?id=abc", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDeleteShows_NotFound(t *testing.T) {
|
|
svc := &fakeSvc{deleteErr: episode.ErrNotFound}
|
|
r := newRouterWithSvc(svc)
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/shows?id=99", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("expected 404, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDeleteShows_OtherError(t *testing.T) {
|
|
svc := &fakeSvc{deleteErr: errors.New("db down")}
|
|
r := newRouterWithSvc(svc)
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/shows?id=77", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Fatalf("expected 500, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDeleteShows_OK(t *testing.T) {
|
|
svc := &fakeSvc{}
|
|
r := newRouterWithSvc(svc)
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/v1/shows?id=11", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusNoContent {
|
|
t.Fatalf("expected 204, got %d", w.Code)
|
|
}
|
|
if svc.lastDelID != 11 {
|
|
t.Fatalf("expected delete id 11, got %d", svc.lastDelID)
|
|
}
|
|
}
|