Questions and replies can attach one MP4 or WebM (25 MB) alongside up to four images. Videos are stored as-is and omitted from Discord embeds. Reviewed-on: #22 Co-authored-by: codegirl-007 <s.raide@gmail.com>
This commit was merged in pull request #22.
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
@@ -56,6 +57,33 @@ func TestPreparePostImage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreparePostVideo(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
prepared, err := preparePostVideoHeader(t, "clip.mp4", tinyMP4())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if prepared.extension != ".mp4" || prepared.contentType != "video/mp4" || len(prepared.body) == 0 {
|
||||
t.Fatalf("prepared MP4 = %+v", prepared)
|
||||
}
|
||||
prepared, err = preparePostVideoHeader(t, "clip.webm", tinyWebM())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if prepared.extension != ".webm" || prepared.contentType != "video/webm" {
|
||||
t.Fatalf("prepared WebM = %+v", prepared)
|
||||
}
|
||||
if _, err := preparePostVideoHeader(t, "notes.txt", []byte("not a video")); err == nil {
|
||||
t.Fatal("text video upload unexpectedly succeeded")
|
||||
}
|
||||
_, err = preparePostVideoHeader(t, "too-large.mp4", make([]byte, postVideoMaxFileBytes+1))
|
||||
var requestErr *postImageRequestError
|
||||
if !errors.As(err, &requestErr) || requestErr.status != http.StatusRequestEntityTooLarge {
|
||||
t.Fatalf("oversized video error = %v, want 413 request error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOrientPostImage(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -203,6 +231,76 @@ func TestPostImageMultipartLifecycle(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostVideoMultipartLifecycle(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
blobs := &recordingImageBlob{}
|
||||
srv, mem := newTestServer(t, Config{Blob: blobs})
|
||||
handler := srv.Handler()
|
||||
homeowner := seedUser(t, mem, uniq("video"), "hunter22", store.RoleUser)
|
||||
cookies := loginUser(t, handler, homeowner.Username, "hunter22")
|
||||
csrf := csrfForCookies(t, handler, cookies)
|
||||
|
||||
rec := multipartPost(t, handler, "/submit", map[string][]string{
|
||||
"_csrf": {csrf},
|
||||
"title": {"Valve clip"},
|
||||
"body": {"A photo and a video."},
|
||||
"city": {"Oakland"},
|
||||
"image_description": {"Still", "Walkthrough"},
|
||||
}, []multipartTestFile{
|
||||
{name: "still.png", body: solidPNG(t, 40, 20)},
|
||||
{name: "walk.mp4", body: tinyMP4()},
|
||||
}, cookies)
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("root video upload status = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
roots, err := mem.ListRootPosts(context.Background(), pacific.Today(), homeowner.ID)
|
||||
if err != nil || len(roots) != 1 {
|
||||
t.Fatalf("roots = %+v, %v", roots, err)
|
||||
}
|
||||
root, err := mem.GetPost(context.Background(), roots[0].ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(root.Images) != 2 ||
|
||||
root.Images[0].Kind != store.MediaKindImage ||
|
||||
root.Images[1].Kind != store.MediaKindVideo ||
|
||||
root.Images[1].Description != "Walkthrough" ||
|
||||
!strings.HasPrefix(root.Images[1].ObjectKey, "post-videos/") {
|
||||
t.Fatalf("root media = %+v", root.Images)
|
||||
}
|
||||
|
||||
rec = multipartPost(t, handler, "/posts/"+root.ID+"/edit", map[string][]string{
|
||||
"_csrf": {csrf},
|
||||
"body": {"Keep the clip."},
|
||||
"existing_image_id": {root.Images[1].ID},
|
||||
"existing_image_description": {"Kept clip"},
|
||||
}, nil, cookies)
|
||||
if rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("retain video status = %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
edited, err := mem.GetPost(context.Background(), root.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(edited.Images) != 1 || edited.Images[0].Kind != store.MediaKindVideo ||
|
||||
edited.Images[0].Description != "Kept clip" {
|
||||
t.Fatalf("retained video = %+v", edited.Images)
|
||||
}
|
||||
|
||||
rec = multipartPost(t, handler, "/posts", map[string][]string{
|
||||
"_csrf": {csrf},
|
||||
"parent_id": {root.ID},
|
||||
"body": {"Two clips."},
|
||||
}, []multipartTestFile{
|
||||
{name: "a.mp4", body: tinyMP4()},
|
||||
{name: "b.mp4", body: tinyMP4()},
|
||||
}, cookies)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("two-video status = %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostImageUploadCompensation(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -318,6 +416,12 @@ func multipartPost(
|
||||
return rec
|
||||
}
|
||||
|
||||
func preparePostVideoHeader(t *testing.T, name string, body []byte) (preparedPostVideo, error) {
|
||||
t.Helper()
|
||||
header := multipartFileHeader(t, name, body)
|
||||
return preparePostVideo(header)
|
||||
}
|
||||
|
||||
func preparePostImageHeader(t *testing.T, name string, body []byte) (preparedPostImage, error) {
|
||||
t.Helper()
|
||||
var requestBody bytes.Buffer
|
||||
@@ -341,6 +445,41 @@ func preparePostImageHeader(t *testing.T, name string, body []byte) (preparedPos
|
||||
return preparePostImage(req.MultipartForm.File["images"][0])
|
||||
}
|
||||
|
||||
func multipartFileHeader(t *testing.T, name string, body []byte) *multipart.FileHeader {
|
||||
t.Helper()
|
||||
var requestBody bytes.Buffer
|
||||
writer := multipart.NewWriter(&requestBody)
|
||||
part, err := writer.CreateFormFile("images", name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := part.Write(body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodPost, "/posts", &requestBody)
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
if err := req.ParseMultipartForm(postImageMultipartMemory); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = req.MultipartForm.RemoveAll() })
|
||||
return req.MultipartForm.File["images"][0]
|
||||
}
|
||||
|
||||
func tinyMP4() []byte {
|
||||
body := make([]byte, 16)
|
||||
body[3] = 16
|
||||
copy(body[4:], "ftypisom")
|
||||
copy(body[12:], "isom")
|
||||
return body
|
||||
}
|
||||
|
||||
func tinyWebM() []byte {
|
||||
return []byte{0x1a, 0x45, 0xdf, 0xa3, 0x01, 0x00, 0x00, 0x00}
|
||||
}
|
||||
|
||||
func solidPNG(t *testing.T, width, height int) []byte {
|
||||
t.Helper()
|
||||
img := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
|
||||
Reference in New Issue
Block a user