RezinaLMS-BackEnd/internal/web_server/handlers/course_video_access_test.go

63 lines
1.8 KiB
Go

package handlers
import (
"Rezina-Backend/internal/domain"
"Rezina-Backend/internal/repository"
"Rezina-Backend/internal/services/examprep"
"context"
"github.com/gofiber/fiber/v2"
"net/http/httptest"
"testing"
)
type courseVideoAccessStore struct {
*repository.Store
tier domain.ContentAccessTier
owned bool
}
func (s *courseVideoAccessStore) CourseVideoLessonIDs(context.Context, string) ([]int64, error) {
return []int64{7}, nil
}
func (s *courseVideoAccessStore) CourseContentAccess(context.Context, string, int64) (domain.CourseContentAccess, error) {
return domain.CourseContentAccess{CourseID: 12, Tier: s.tier, Published: true}, nil
}
func (s *courseVideoAccessStore) HasCoursePurchase(context.Context, int64, int64) (bool, error) {
return s.owned, nil
}
func TestVideoURLRefreshRequiresCoursePurchase(t *testing.T) {
for _, tt := range []struct {
name string
tier domain.ContentAccessTier
owned bool
status int
}{
{"premium key blocked", domain.ContentAccessPremium, false, 403},
{"purchased key allowed", domain.ContentAccessPremium, true, 204},
{"preview key allowed", domain.ContentAccessFree, false, 204},
} {
t.Run(tt.name, func(t *testing.T) {
store := &courseVideoAccessStore{tier: tt.tier, owned: tt.owned}
h := &Handler{examPrepSvc: examprep.NewService(store)}
app := fiber.New()
app.Get("/refresh", func(c *fiber.Ctx) error {
c.Locals("role", domain.RoleStudent)
c.Locals("user_id", int64(42))
if err := h.authorizeCourseVideoReference(c, "lessons/video.mp4"); err != nil {
return err
}
return c.SendStatus(204)
})
resp, err := app.Test(httptest.NewRequest("GET", "/refresh", nil))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != tt.status {
t.Fatalf("got %d want %d", resp.StatusCode, tt.status)
}
})
}
}