-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_view_test.go
More file actions
129 lines (112 loc) · 4.18 KB
/
main_view_test.go
File metadata and controls
129 lines (112 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/johnwmail/nclip/config"
"github.com/johnwmail/nclip/handlers/retrieval"
"github.com/johnwmail/nclip/internal/services"
"github.com/johnwmail/nclip/models"
)
// Test that small content is rendered fully in HTML view
func TestViewSmallRendersFull(t *testing.T) {
gin.SetMode(gin.TestMode)
cfg := &config.Config{MaxRenderSize: 1024}
store := NewMockStore(cfg.DataDir)
defer cleanupTestData(store.dataDir)
svc := services.NewPasteService(store, cfg)
rh := retrieval.NewHandler(svc, store, cfg)
// Create small paste
paste := &models.Paste{ID: "SMALLA", CreatedAt: time.Now(), Size: 11, ContentType: "text/plain", Content: []byte("hello world"), BurnAfterRead: false}
if err := store.Store(paste); err != nil {
t.Fatalf("failed to store paste: %v", err)
}
// Call handler directly using a test context to avoid router matching issues
w := httptest.NewRecorder()
c, engine := gin.CreateTestContext(w)
engine.LoadHTMLGlob("static/*.html")
c.Request = httptest.NewRequest("GET", "/SMALLA", nil)
c.Params = gin.Params{{Key: "slug", Value: "SMALLA"}}
rh.View(c)
if w.Code != http.StatusOK {
t.Fatalf("expected 200 OK, got %d", w.Code)
}
body := w.Body.String()
if !strings.Contains(body, "hello world") {
t.Fatalf("expected full content in HTML view, got body: %s", body)
}
}
// Test that large content shows a preview (not full content) in HTML view
func TestViewLargeShowsPreview(t *testing.T) {
gin.SetMode(gin.TestMode)
cfg := &config.Config{MaxRenderSize: 10}
store := NewMockStore(cfg.DataDir)
defer cleanupTestData(store.dataDir)
svc := services.NewPasteService(store, cfg)
rh := retrieval.NewHandler(svc, store, cfg)
// Create large paste
long := strings.Repeat("A", 50)
paste := &models.Paste{ID: "LARGEA", CreatedAt: time.Now(), Size: int64(len(long)), ContentType: "text/plain", Content: []byte(long), BurnAfterRead: false}
if err := store.Store(paste); err != nil {
t.Fatalf("failed to store paste: %v", err)
}
w := httptest.NewRecorder()
c, engine := gin.CreateTestContext(w)
engine.LoadHTMLGlob("static/*.html")
c.Request = httptest.NewRequest("GET", "/LARGEA", nil)
c.Params = gin.Params{{Key: "slug", Value: "LARGEA"}}
rh.View(c)
if w.Code != http.StatusOK {
t.Fatalf("expected 200 OK, got %d", w.Code)
}
body := w.Body.String()
if strings.Contains(body, long) {
t.Fatalf("expected preview, not full content")
}
// preview should contain MaxRenderSize bytes of 'A'
if !strings.Contains(body, strings.Repeat("A", int(cfg.MaxRenderSize))) {
t.Fatalf("expected preview substring in body")
}
}
// Integration-style test: burn-after-read preview reads prefix from temp file and deletes it
func TestBurnAfterReadPreviewDeletesTemp(t *testing.T) {
gin.SetMode(gin.TestMode)
cfg := &config.Config{MaxRenderSize: 10}
store := NewMockStore(cfg.DataDir)
defer cleanupTestData(store.dataDir)
svc := services.NewPasteService(store, cfg)
rh := retrieval.NewHandler(svc, store, cfg)
// Create burn paste
content := strings.Repeat("B", 32)
paste := &models.Paste{ID: "BURN2", CreatedAt: time.Now(), Size: int64(len(content)), ContentType: "text/plain", Content: []byte(content), BurnAfterRead: true}
if err := store.Store(paste); err != nil {
t.Fatalf("failed to store paste: %v", err)
}
// ensure data dir exists and the file is present (MockStore writes file)
dataDir := cfg.DataDir
if dataDir == "" {
dataDir = "./data"
}
// sanity check file exists
if _, err := os.Stat(filepath.Join(dataDir, "BURN2")); err != nil {
t.Fatalf("expected content file on disk before request: %v", err)
}
w := httptest.NewRecorder()
c, engine := gin.CreateTestContext(w)
engine.LoadHTMLGlob("static/*.html")
c.Request = httptest.NewRequest("GET", "/BURN2", nil)
c.Params = gin.Params{{Key: "slug", Value: "BURN2"}}
rh.View(c)
if w.Code != http.StatusOK {
t.Fatalf("expected 200 OK, got %d", w.Code)
}
// after request, original file should be gone (moved then deleted by handler)
if _, err := os.Stat(filepath.Join(dataDir, "BURN2")); !os.IsNotExist(err) {
t.Fatalf("expected original content removed after burn, stat err: %v", err)
}
}