返回 DeepSeek-Reasonix
attachment_upload_limit_test.go
根目录 / desktop / attachment_upload_limit_test.go
1 package main
2
3 import (
4 "encoding/json"
5 "net/http"
6 "net/http/httptest"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "reasonix/internal/config"
13 )
14
15 func TestAttachmentUploadEnforcesUserStreamLimitAndCleansPartialFile(t *testing.T) {
16 for _, tc := range []struct {
17 name, user, project, body string
18 length int64
19 status int
20 }{
21 {"exact", "4", "", "1234", 0, http.StatusOK},
22 {"plus one", "4", "", "12345", 0, http.StatusRequestEntityTooLarge},
23 {"unknown length", "4", "", "12345", -1, http.StatusRequestEntityTooLarge},
24 {"misleading length", "4", "", "12345", 1, http.StatusRequestEntityTooLarge},
25 {"project cannot disable", "4", "0", "12345", -1, http.StatusRequestEntityTooLarge},
26 {"user unlimited", "0", "1", "123456", -1, http.StatusOK},
27 } {
28 t.Run(tc.name, func(t *testing.T) {
29 app, root, token := uploadFixture(t)
30 if err := os.WriteFile(config.UserConfigPath(), []byte("[attachments]\nmax_stream_bytes = "+tc.user+"\n"), 0600); err != nil {
31 t.Fatal(err)
32 }
33 if tc.project != "" {
34 if err := os.WriteFile(filepath.Join(root, "reasonix.toml"), []byte("[attachments]\nmax_stream_bytes = "+tc.project+"\n"), 0600); err != nil {
35 t.Fatal(err)
36 }
37 }
38 req := uploadRequest(token, strings.NewReader(tc.body))
39 if tc.length != 0 {
40 req.ContentLength = tc.length
41 }
42 recorder := httptest.NewRecorder()
43 app.attachmentUploadMiddleware()(http.NotFoundHandler()).ServeHTTP(recorder, req)
44 if recorder.Code != tc.status {
45 t.Fatalf("status=%d want=%d body=%s", recorder.Code, tc.status, recorder.Body)
46 }
47 if tc.status != http.StatusRequestEntityTooLarge {
48 return
49 }
50 var result struct {
51 Code string `json:"code"`
52 Message string `json:"message"`
53 LimitBytes int64 `json:"limit_bytes"`
54 LimitSource string `json:"limit_source"`
55 }
56 if err := json.Unmarshal(recorder.Body.Bytes(), &result); err != nil {
57 t.Fatal(err)
58 }
59 if result.Code != "attachment.too_large" || result.Message == "" || result.LimitBytes != 4 || result.LimitSource != "user" {
60 t.Fatalf("untyped limit error: %+v", result)
61 }
62 entries, err := os.ReadDir(filepath.Join(root, ".reasonix", "attachments"))
63 if err != nil || len(entries) != 0 {
64 t.Fatalf("partial files=%v err=%v", entries, err)
65 }
66 })
67 }
68 }
69
70 func TestAttachmentUploadRejectsUnreadableUserLimitBeforeCreatingFile(t *testing.T) {
71 app, root, token := uploadFixture(t)
72 if err := os.WriteFile(config.UserConfigPath(), []byte("[attachments]\nmax_stream_bytes = [broken"), 0600); err != nil {
73 t.Fatal(err)
74 }
75 recorder := httptest.NewRecorder()
76 app.attachmentUploadMiddleware()(http.NotFoundHandler()).ServeHTTP(recorder, uploadRequest(token, strings.NewReader("body")))
77 if recorder.Code != http.StatusInternalServerError {
78 t.Fatalf("configuration failure accepted upload: %d %s", recorder.Code, recorder.Body)
79 }
80 if _, err := os.Stat(filepath.Join(root, ".reasonix", "attachments")); !os.IsNotExist(err) {
81 t.Fatalf("configuration failure created attachment files: %v", err)
82 }
83 }
84
85 type uploadLimitMutationReader struct {
86 body *strings.Reader
87 mutate func()
88 }
89
90 func (reader *uploadLimitMutationReader) Read(p []byte) (int, error) {
91 if reader.mutate != nil {
92 reader.mutate()
93 reader.mutate = nil
94 }
95 return reader.body.Read(p)
96 }
97
98 func TestAttachmentUploadRetiredOwnerWinsOverStreamLimit(t *testing.T) {
99 app, root, token := uploadFixture(t)
100 if err := os.WriteFile(config.UserConfigPath(), []byte("[attachments]\nmax_stream_bytes = 4\n"), 0600); err != nil {
101 t.Fatal(err)
102 }
103 reader := &uploadLimitMutationReader{body: strings.NewReader("12345"), mutate: func() { app.tabs["a"].SessionGeneration++ }}
104 recorder := httptest.NewRecorder()
105 app.attachmentUploadMiddleware()(http.NotFoundHandler()).ServeHTTP(recorder, uploadRequest(token, reader))
106 if recorder.Code != http.StatusConflict {
107 t.Fatalf("retired owner error=%d body=%s", recorder.Code, recorder.Body)
108 }
109 entries, err := os.ReadDir(filepath.Join(root, ".reasonix", "attachments"))
110 if err != nil || len(entries) != 0 {
111 t.Fatalf("partial files=%v err=%v", entries, err)
112 }
113 }
114
114 lines GO