- Implemented FetchEpisode function to retrieve episode metadata from dアニメストア. - Added /api/v1/danime GET endpoint to fetch episode details by partId. - Updated Swagger documentation for the new endpoint and response structure. - Created DanimeEpisodeResponse type for API responses. - Added tests for the new functionality and handlers.
458 lines
13 KiB
Go
458 lines
13 KiB
Go
package httpapi_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"watch-party-backend/internal/core/episode"
|
|
"watch-party-backend/internal/danime"
|
|
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
|
|
createRes episode.Episode
|
|
createErr error
|
|
lastSetID int64
|
|
lastTime string
|
|
lastMove []int64
|
|
lastDelID int64
|
|
lastCreate episode.NewShowInput
|
|
}
|
|
|
|
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
|
|
}
|
|
func (f *fakeSvc) Create(ctx context.Context, in episode.NewShowInput) (episode.Episode, error) {
|
|
f.lastCreate = in
|
|
return f.createRes, f.createErr
|
|
}
|
|
|
|
// ---- 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 TestPostShows_BadPayload(t *testing.T) {
|
|
r := newRouterWithSvc(&fakeSvc{})
|
|
w := doJSON(t, r, http.MethodPost, "/api/v1/shows", map[string]any{"ep_title": "Pilot"})
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestPostShows_ValidationErrors(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
err error
|
|
code int
|
|
payload any
|
|
}{
|
|
{
|
|
name: "bad start",
|
|
err: episode.ErrInvalidStartTime,
|
|
code: http.StatusBadRequest,
|
|
payload: map[string]any{
|
|
"ep_num": 1, "ep_title": "Pilot", "season_name": "S1", "start_time": "1:0:0", "playback_length": "00:24:00",
|
|
},
|
|
},
|
|
{
|
|
name: "bad playback",
|
|
err: episode.ErrInvalidPlayback,
|
|
code: http.StatusBadRequest,
|
|
payload: map[string]any{
|
|
"ep_num": 1, "ep_title": "Pilot", "season_name": "S1", "start_time": "10:00:00", "playback_length": "0:0:0",
|
|
},
|
|
},
|
|
{
|
|
name: "service error",
|
|
err: errors.New("db down"),
|
|
code: http.StatusInternalServerError,
|
|
payload: map[string]any{
|
|
"ep_num": 1, "ep_title": "Pilot", "season_name": "S1", "start_time": "10:00:00", "playback_length": "00:24:00",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
svc := &fakeSvc{createErr: tt.err}
|
|
r := newRouterWithSvc(svc)
|
|
w := doJSON(t, r, http.MethodPost, "/api/v1/shows", tt.payload)
|
|
if w.Code != tt.code {
|
|
t.Fatalf("expected %d, got %d body=%s", tt.code, w.Code, w.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPostShows_OK(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
want := episode.Episode{
|
|
Id: 9,
|
|
EpNum: 1,
|
|
EpTitle: "Pilot",
|
|
SeasonName: "S1",
|
|
StartTime: "10:00:00",
|
|
PlaybackLength: "00:24:00",
|
|
CurrentEp: false,
|
|
DateCreated: now,
|
|
}
|
|
svc := &fakeSvc{createRes: want}
|
|
r := newRouterWithSvc(svc)
|
|
w := doJSON(t, r, http.MethodPost, "/api/v1/shows", map[string]any{
|
|
"ep_num": 1, "ep_title": "Pilot", "season_name": "S1", "start_time": "10:00:00", "playback_length": "00:24:00",
|
|
})
|
|
if w.Code != http.StatusCreated {
|
|
t.Fatalf("expected 201, 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 got.Id != want.Id || got.CurrentEp {
|
|
t.Fatalf("unexpected body: %+v", got)
|
|
}
|
|
if svc.lastCreate.EpTitle != "Pilot" || svc.lastCreate.StartTime != "10:00:00" {
|
|
t.Fatalf("service called with wrong input: %+v", svc.lastCreate)
|
|
}
|
|
}
|
|
|
|
func TestPostShows_DefaultStartTime(t *testing.T) {
|
|
now := time.Now().UTC()
|
|
svc := &fakeSvc{
|
|
createRes: episode.Episode{
|
|
Id: 11,
|
|
EpNum: 2,
|
|
EpTitle: "No Start",
|
|
SeasonName: "S2",
|
|
StartTime: "22:00:00",
|
|
PlaybackLength: "00:24:00",
|
|
DateCreated: now,
|
|
},
|
|
}
|
|
r := newRouterWithSvc(svc)
|
|
w := doJSON(t, r, http.MethodPost, "/api/v1/shows", map[string]any{
|
|
"ep_num": 2, "ep_title": "No Start", "season_name": "S2", "playback_length": "00:24:00",
|
|
})
|
|
if w.Code != http.StatusCreated {
|
|
t.Fatalf("expected 201, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
if svc.lastCreate.StartTime != "22:00:00" {
|
|
t.Fatalf("expected default start time, got %s", svc.lastCreate.StartTime)
|
|
}
|
|
}
|
|
|
|
func TestGetDanime_MissingParam(t *testing.T) {
|
|
r := newRouterWithSvc(&fakeSvc{})
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/danime", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("expected 400, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestGetDanime_Error(t *testing.T) {
|
|
prev := httpapi.FetchDanimeEpisode
|
|
httpapi.FetchDanimeEpisode = func(ctx context.Context, partID string) (*danime.Episode, error) {
|
|
return nil, errors.New("boom")
|
|
}
|
|
defer func() { httpapi.FetchDanimeEpisode = prev }()
|
|
|
|
r := newRouterWithSvc(&fakeSvc{})
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/danime?part_id=123", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusBadGateway {
|
|
t.Fatalf("expected 502, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestGetDanime_OK(t *testing.T) {
|
|
prev := httpapi.FetchDanimeEpisode
|
|
httpapi.FetchDanimeEpisode = func(ctx context.Context, partID string) (*danime.Episode, error) {
|
|
return &danime.Episode{
|
|
EpNum: 21,
|
|
EpTitle: "Title",
|
|
SeasonName: "Season",
|
|
PlaybackLength: "00:23:50",
|
|
}, nil
|
|
}
|
|
defer func() { httpapi.FetchDanimeEpisode = prev }()
|
|
|
|
r := newRouterWithSvc(&fakeSvc{})
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/danime?part_id=23863021", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
var got danime.Episode
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("json: %v", err)
|
|
}
|
|
if got.EpNum != 21 || got.PlaybackLength != "00:23:50" {
|
|
t.Fatalf("unexpected body: %+v", got)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|