Files
thamanyah/tests/steps_test.go
T
2026-08-27 18:42:35 +03:00

355 lines
10 KiB
Go

package tests
import (
"bytes"
"slices"
"strings"
"github.com/go-bdd/gobdd"
)
// uploadPayload stands in for the video file. Nothing in the upload flow reads
// it — the CMS only checks that an object exists under the key, and
// MediaConvert does not open the input until the job runs — so a fixed blob
// keeps the scenarios fast and deterministic.
var uploadPayload = bytes.Repeat([]byte("thamanyah-test-video-"), 64)
type presignRequest struct {
FileName string `json:"fileName"`
ContentType string `json:"contentType"`
}
type registerRequest struct {
Title string `json:"title"`
Description string `json:"description"`
CategoryIDs []int16 `json:"categoryIds"`
Tags string `json:"tags"`
FileName string `json:"fileName"`
Key string `json:"key"`
}
const (
sentDescription = "A behind-the-scenes look at the evening bulletin."
sentTags = "media, press, riyadh"
)
// --- Given -----------------------------------------------------------------
func theAPIIsAvailable(t gobdd.StepTest, ctx gobdd.Context) {
w := worldOf(t, ctx)
result, err := w.client.get("/health")
if err != nil {
t.Fatalf("the CMS at %s is not answering: %s", w.client.base, err)
return
}
if result.status != 200 {
t.Fatalf("the CMS at %s is not healthy: %s", w.client.base, result.summary())
}
}
// --- When ------------------------------------------------------------------
func askForCategories(t gobdd.StepTest, ctx gobdd.Context) {
w := worldOf(t, ctx)
result, err := w.client.get("/api/categories")
if err != nil {
t.Fatalf("could not ask for the categories: %s", err)
return
}
w.last = result
}
func requestUploadSlot(t gobdd.StepTest, ctx gobdd.Context, fileName, contentType string) {
w := worldOf(t, ctx)
result, err := w.client.postJSON("/api/videos/presign", presignRequest{
FileName: fileName,
ContentType: contentType,
})
if err != nil {
t.Fatalf("could not ask for an upload slot: %s", err)
return
}
w.last = result
w.slot = presignBody{}
w.slotType = contentType
w.slotFile = fileName
w.uploaded = false
// A rejected request has no slot to remember; the scenario asserting the
// rejection does not need one.
if result.status == 200 {
if err := result.json(&w.slot); err != nil {
t.Fatalf("could not decode the upload slot: %s", err)
}
}
}
func uploadTheFile(t gobdd.StepTest, ctx gobdd.Context) {
w := worldOf(t, ctx)
if w.slot.UploadURL == "" {
t.Fatalf("there is no upload URL to PUT to — the upload slot step did not succeed")
return
}
// The content type is signed into the URL, so it has to be sent back
// verbatim or storage rejects the PUT as a signature mismatch.
result, err := w.client.put(w.slot.UploadURL, w.slotType, uploadPayload)
if err != nil {
t.Fatalf("could not upload to the presigned URL: %s", err)
return
}
if result.status < 200 || result.status > 299 {
t.Fatalf("storage refused the upload: %s", result.summary())
return
}
w.uploaded = true
}
func sendMalformedJSON(t gobdd.StepTest, ctx gobdd.Context) {
w := worldOf(t, ctx)
result, err := w.client.postRaw("/api/videos/presign", "application/json", []byte(`{"fileName": `))
if err != nil {
t.Fatalf("could not send the malformed body: %s", err)
return
}
w.last = result
}
func registerUploadedVideo(t gobdd.StepTest, ctx gobdd.Context, title, categories string) {
w := worldOf(t, ctx)
register(t, w, registerRequest{
Title: title,
Description: sentDescription,
CategoryIDs: w.categoryIDs(t, categories),
Tags: sentTags,
FileName: w.slotFile,
Key: w.slot.Key,
})
}
func registerUnderCategoryID(t gobdd.StepTest, ctx gobdd.Context, title string, categoryID int) {
w := worldOf(t, ctx)
register(t, w, registerRequest{
Title: title,
Description: sentDescription,
CategoryIDs: []int16{int16(categoryID)},
Tags: sentTags,
FileName: w.slotFile,
Key: w.slot.Key,
})
}
func registerWithKey(t gobdd.StepTest, ctx gobdd.Context, title, key, categories string) {
w := worldOf(t, ctx)
register(t, w, registerRequest{
Title: title,
Description: sentDescription,
CategoryIDs: w.categoryIDs(t, categories),
Tags: sentTags,
FileName: "smuggled.mp4",
Key: key,
})
}
func register(t gobdd.StepTest, w *world, body registerRequest) {
result, err := w.client.postJSON("/api/videos", body)
if err != nil {
t.Fatalf("could not register the video: %s", err)
return
}
w.last = result
w.sent = body
w.video = videoBody{}
if result.status == 201 {
if err := result.json(&w.video); err != nil {
t.Fatalf("could not decode the registered video: %s", err)
}
}
}
// --- Then ------------------------------------------------------------------
func theRequestSucceedsWith(t gobdd.StepTest, ctx gobdd.Context, status int) {
w := worldOf(t, ctx)
if w.last.status != status {
t.Fatalf("expected the request to succeed with %d, got %s", status, w.last.summary())
return
}
if !strings.HasPrefix(w.last.contentType, "application/json") {
t.Errorf("expected a application/json response, got %q", w.last.contentType)
}
}
func theRequestIsRejectedWith(t gobdd.StepTest, ctx gobdd.Context, status int) {
w := worldOf(t, ctx)
if w.last.status != status {
t.Fatalf("expected the request to be rejected with %d, got %s", status, w.last.summary())
return
}
// Errors are RFC 9457 Problem Details, and the media type is part of that
// contract — clients branch on it to know the body is a problem.
if !strings.HasPrefix(w.last.contentType, "application/problem+json") {
t.Errorf("expected a application/problem+json response, got %q", w.last.contentType)
}
}
func theProblemTitleIs(t gobdd.StepTest, ctx gobdd.Context, title string) {
w := worldOf(t, ctx)
var p problem
if err := w.last.json(&p); err != nil {
t.Fatalf("could not decode the problem details: %s", err)
return
}
if p.Title != title {
t.Errorf("expected the problem title %q, got %q (detail: %s)", title, p.Title, p.Detail)
}
if p.Status != w.last.status {
t.Errorf("the problem body says status %d but the response was %d", p.Status, w.last.status)
}
if p.Type != "about:blank" {
t.Errorf("expected the problem type %q, got %q", "about:blank", p.Type)
}
}
func theCategoryListIsNotEmpty(t gobdd.StepTest, ctx gobdd.Context) {
if len(decodeCategories(t, ctx)) == 0 {
t.Errorf("the CMS offered no categories, so no video could ever be filed")
}
}
func everyCategoryHasAnIDAndAName(t gobdd.StepTest, ctx gobdd.Context) {
for _, c := range decodeCategories(t, ctx) {
if c.ID <= 0 {
t.Errorf("the category %q has id %d, which is not a usable id", c.Name, c.ID)
}
if strings.TrimSpace(c.Name) == "" {
t.Errorf("the category with id %d has no name", c.ID)
}
}
}
func decodeCategories(t gobdd.StepTest, ctx gobdd.Context) []category {
w := worldOf(t, ctx)
var body categoriesBody
if err := w.last.json(&body); err != nil {
t.Fatalf("could not decode the category list: %s", err)
return nil
}
return body.Categories
}
func iAmGivenAnUploadURL(t gobdd.StepTest, ctx gobdd.Context) {
w := worldOf(t, ctx)
if w.slot.UploadURL == "" {
t.Fatalf("no upload URL was issued: %s", w.last.summary())
return
}
if !strings.HasPrefix(w.slot.UploadURL, "http://") && !strings.HasPrefix(w.slot.UploadURL, "https://") {
t.Errorf("the upload URL %q is not an absolute HTTP URL", w.slot.UploadURL)
}
// It is presigned, not a bare object URL — the signature is what lets an
// unauthenticated client PUT to a private bucket.
if !strings.Contains(w.slot.UploadURL, "X-Amz-Signature=") {
t.Errorf("the upload URL carries no X-Amz-Signature, so it is not presigned: %s", w.slot.UploadURL)
}
}
func theStorageKeyIs(t gobdd.StepTest, ctx gobdd.Context, prefix, suffix string) {
w := worldOf(t, ctx)
if !strings.HasPrefix(w.slot.Key, prefix) {
t.Errorf("expected the storage key to start with %q, got %q", prefix, w.slot.Key)
}
if !strings.HasSuffix(w.slot.Key, suffix) {
t.Errorf("expected the storage key to end with %q, got %q", suffix, w.slot.Key)
}
// The key is generated, never the client's file name — two people
// uploading "interview.mp4" must not collide.
if strings.Contains(w.slot.Key, w.slotFile) {
t.Errorf("the storage key %q echoes the submitted file name %q", w.slot.Key, w.slotFile)
}
}
func theVideoIsRegisteredWithStatus(t gobdd.StepTest, ctx gobdd.Context, status string) {
w := worldOf(t, ctx)
if w.video.Status != status {
t.Errorf("expected the video to be registered with status %q, got %q", status, w.video.Status)
}
}
func theVideoHasAnID(t gobdd.StepTest, ctx gobdd.Context) {
w := worldOf(t, ctx)
if strings.TrimSpace(w.video.ID) == "" {
t.Errorf("the registered video came back without an id: %s", w.last.summary())
}
}
func theVideoIsStoredUnderTheSlotKey(t gobdd.StepTest, ctx gobdd.Context) {
w := worldOf(t, ctx)
if !w.uploaded {
t.Fatalf("nothing was uploaded, so there is no stored file to check against")
return
}
if w.video.StorageKey != w.slot.Key {
t.Errorf("the video was filed under %q but the file was uploaded to %q", w.video.StorageKey, w.slot.Key)
}
}
func theVideoHasATranscodingJobID(t gobdd.StepTest, ctx gobdd.Context) {
w := worldOf(t, ctx)
if strings.TrimSpace(w.video.MediaConvertJobID) == "" {
t.Errorf("the video has no transcoding job id, so nothing was queued for it: %s", w.last.summary())
}
}
func theVideoIsFiledUnder(t gobdd.StepTest, ctx gobdd.Context, categories string) {
w := worldOf(t, ctx)
want := w.categoryIDs(t, categories)
got := slices.Clone(w.video.CategoryIDs)
slices.Sort(want)
slices.Sort(got)
if !slices.Equal(want, got) {
t.Errorf("expected the video to be filed under %s (ids %v), got ids %v",
categories, want, got)
}
}
func theVideoKeepsTheMetadataISent(t gobdd.StepTest, ctx gobdd.Context) {
w := worldOf(t, ctx)
if w.video.Title != strings.TrimSpace(w.sent.Title) {
t.Errorf("expected the title %q, got %q", strings.TrimSpace(w.sent.Title), w.video.Title)
}
if w.video.Description != w.sent.Description {
t.Errorf("expected the description %q, got %q", w.sent.Description, w.video.Description)
}
if w.video.Tags != w.sent.Tags {
t.Errorf("expected the tags %q, got %q", w.sent.Tags, w.video.Tags)
}
if w.video.FileName != w.sent.FileName {
t.Errorf("expected the file name %q, got %q", w.sent.FileName, w.video.FileName)
}
}