Management API: POST/DELETE /materials/{id}/files (attach.Store, 50 MB, 20 files), DELETE /materials/{id} removes files; download keeps the file name
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
@@ -12,6 +13,8 @@ import (
|
||||
|
||||
userclasses "git.golem15.com/golem15/sm-user-plugin/classes"
|
||||
"git.golem15.com/golem15/summercms/modules/surf"
|
||||
"gocloud.dev/blob"
|
||||
_ "gocloud.dev/blob/memblob"
|
||||
)
|
||||
|
||||
func TestManagementAPIUsesSharedTokenAndCurrentUserPermissions(t *testing.T) {
|
||||
@@ -203,3 +206,143 @@ RETURNING id`).Scan(&groupID).Error; err != nil {
|
||||
t.Errorf("show after delete = %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestManagementMaterialFiles uploads files to a material through the
|
||||
// management API, refuses a disallowed type, detaches one file and deletes
|
||||
// the material with the other, checking rows and blobs.
|
||||
func TestManagementMaterialFiles(t *testing.T) {
|
||||
app, plugins, db := migrated(t)
|
||||
bucket, err := blob.OpenBucket(t.Context(), "mem://")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { bucket.Close() })
|
||||
if err := app.Publish(bucket); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var userID uint
|
||||
if err := db.Raw(`
|
||||
INSERT INTO users (email, password, must_change_password)
|
||||
VALUES ('bm-files-token@example.test', '', false)
|
||||
RETURNING id`).Scan(&userID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var groupID uint
|
||||
if err := db.Raw(`
|
||||
INSERT INTO user_groups (name, code, permissions, created_at, updated_at)
|
||||
VALUES ('BM files', 'bm-files', '{"jz.bm.access_materials":1}', NOW(), NOW())
|
||||
RETURNING id`).Scan(&groupID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Exec(`INSERT INTO users_groups (user_id, user_group_id) VALUES (?, ?)`, userID, groupID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secret, token, err := userclasses.MintAPIToken("usr_", userID, "BM files", []string{"read", "write"}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(token).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
handler, err := surf.Assemble(app, plugins)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
do := func(req *http.Request) (*httptest.ResponseRecorder, map[string]any) {
|
||||
t.Helper()
|
||||
req.Header.Set("Authorization", "Bearer "+secret)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
var out map[string]any
|
||||
if rec.Body.Len() > 0 {
|
||||
_ = json.Unmarshal(rec.Body.Bytes(), &out)
|
||||
}
|
||||
return rec, out
|
||||
}
|
||||
upload := func(path string, files map[string][]byte) (*httptest.ResponseRecorder, map[string]any) {
|
||||
t.Helper()
|
||||
var buf bytes.Buffer
|
||||
mw := multipart.NewWriter(&buf)
|
||||
for name, content := range files {
|
||||
fw, err := mw.CreateFormFile("file", name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fw.Write(content)
|
||||
}
|
||||
mw.Close()
|
||||
req := httptest.NewRequest(http.MethodPost, path, &buf)
|
||||
req.Header.Set("Content-Type", mw.FormDataContentType())
|
||||
return do(req)
|
||||
}
|
||||
countFiles := func() int64 {
|
||||
var n int64
|
||||
if err := db.Table("system_files").Where("field = 'files'").Count(&n).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
raw, _ := json.Marshal(map[string]any{"title": "Podręcznik", "type": "pdf"})
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/bm/materials", bytes.NewReader(raw))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rec, out := do(req)
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("create = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
id := strconv.Itoa(int(out["data"].(map[string]any)["id"].(float64)))
|
||||
base := "/api/v1/bm/materials/" + id
|
||||
|
||||
pdf := []byte("%PDF-1.4\n1 0 obj << >> endobj\ntrailer << >>\n%%EOF\n")
|
||||
rec, out = upload(base+"/files", map[string][]byte{"wojna.pdf": pdf})
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("upload = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
rec, out = upload(base+"/files", map[string][]byte{"plan.txt": []byte("plan zajęć\n")})
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("second upload = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
files, _ := out["data"].(map[string]any)["files"].([]any)
|
||||
if len(files) != 2 {
|
||||
t.Fatalf("files after two uploads = %v", out["data"])
|
||||
}
|
||||
first := files[0].(map[string]any)
|
||||
if first["name"] != "wojna.pdf" || first["mime"] != "application/pdf" || !strings.HasSuffix(first["url"].(string), ".pdf") {
|
||||
t.Errorf("first file = %v", first)
|
||||
}
|
||||
|
||||
if rec, _ := upload(base+"/files", map[string][]byte{"x.exe": []byte("MZ")}); rec.Code != http.StatusUnprocessableEntity {
|
||||
t.Errorf("disallowed type = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec, _ := upload(base+"/files", nil); rec.Code != http.StatusUnprocessableEntity {
|
||||
t.Errorf("no file = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec, _ := upload("/api/v1/bm/materials/999999/files", map[string][]byte{"a.pdf": pdf}); rec.Code != http.StatusNotFound {
|
||||
t.Errorf("unknown material = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if n := countFiles(); n != 2 {
|
||||
t.Fatalf("system_files rows = %d after refusals, want 2", n)
|
||||
}
|
||||
|
||||
fileID := strconv.Itoa(int(first["id"].(float64)))
|
||||
if rec, _ := do(httptest.NewRequest(http.MethodDelete, "/api/v1/bm/materials/999999/files/"+fileID, nil)); rec.Code != http.StatusNotFound {
|
||||
t.Errorf("detach through another material = %d", rec.Code)
|
||||
}
|
||||
if rec, _ := do(httptest.NewRequest(http.MethodDelete, base+"/files/"+fileID, nil)); rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("detach = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if n := countFiles(); n != 1 {
|
||||
t.Errorf("system_files rows = %d after detach, want 1", n)
|
||||
}
|
||||
if rec, _ := do(httptest.NewRequest(http.MethodDelete, base, nil)); rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("delete material = %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if n := countFiles(); n != 0 {
|
||||
t.Errorf("system_files rows = %d after deleting the material, want 0", n)
|
||||
}
|
||||
iter := bucket.List(nil)
|
||||
if obj, err := iter.Next(t.Context()); err != io.EOF {
|
||||
t.Errorf("a blob is left in the bucket: %v %v", obj, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user