- commit
- 24df143
- parent
- 220bdce
- author
- Eric Bower
- date
- 2024-10-17 02:44:47 +0000 UTC
chore(pgs): api tests
1 files changed,
+127,
-0
+127,
-0
1@@ -2,16 +2,19 @@ package pgs
2
3 import (
4 "fmt"
5+ "io"
6 "log/slog"
7 "net/http"
8 "net/http/httptest"
9 "strings"
10 "testing"
11+ "time"
12
13 "github.com/picosh/pico/db"
14 "github.com/picosh/pico/db/stub"
15 "github.com/picosh/pico/shared"
16 "github.com/picosh/pico/shared/storage"
17+ sst "github.com/picosh/pobj/storage"
18 )
19
20 var testUserID = "user-1"
21@@ -59,6 +62,20 @@ func (p *PgsDb) FindProjectByName(userID, name string) (*db.Project, error) {
22 }, nil
23 }
24
25+type PgsAnalyticsDb struct {
26+ *PgsDb
27+}
28+
29+func NewPgsAnalticsDb(logger *slog.Logger) *PgsAnalyticsDb {
30+ return &PgsAnalyticsDb{
31+ PgsDb: NewPgsDb(logger),
32+ }
33+}
34+
35+func (p *PgsAnalyticsDb) HasFeatureForUser(userID, feature string) bool {
36+ return true
37+}
38+
39 func mkpath(path string) string {
40 return fmt.Sprintf("https://%s-test.pgs.test%s", testUsername, path)
41 }
42@@ -180,6 +197,20 @@ func TestApiBasic(t *testing.T) {
43 },
44 },
45 },
46+ {
47+ name: "images",
48+ path: "/profile.jpg",
49+ want: "image",
50+ status: http.StatusOK,
51+ contentType: "image/jpeg",
52+
53+ dbpool: NewPgsDb(cfg.Logger),
54+ storage: map[string]map[string]string{
55+ bucketName: {
56+ "test/profile.jpg": "image",
57+ },
58+ },
59+ },
60 }
61
62 for _, tc := range tt {
63@@ -215,3 +246,99 @@ func TestApiBasic(t *testing.T) {
64 })
65 }
66 }
67+
68+func TestAnalytics(t *testing.T) {
69+ bucketName := shared.GetAssetBucketName(testUserID)
70+ cfg := NewConfigSite()
71+ cfg.Domain = "pgs.test"
72+ expectedPath := "/app"
73+ request := httptest.NewRequest("GET", mkpath(expectedPath), strings.NewReader(""))
74+ responseRecorder := httptest.NewRecorder()
75+
76+ sto := map[string]map[string]string{
77+ bucketName: {
78+ "test/app.html": "hello world!",
79+ },
80+ }
81+ st, _ := storage.NewStorageMemory(sto)
82+ ch := make(chan *db.AnalyticsVisits)
83+ dbpool := NewPgsAnalticsDb(cfg.Logger)
84+ apiConfig := &shared.ApiConfig{
85+ Cfg: cfg,
86+ Dbpool: dbpool,
87+ Storage: st,
88+ AnalyticsQueue: ch,
89+ }
90+ handler := shared.CreateServe(mainRoutes, createSubdomainRoutes(publicPerm), apiConfig)
91+ router := http.HandlerFunc(handler)
92+
93+ go func() {
94+ for analytics := range ch {
95+ if analytics.Path != expectedPath {
96+ t.Errorf("Want path '%s', got '%s'", expectedPath, analytics.Path)
97+ }
98+ close(ch)
99+ }
100+ }()
101+
102+ router(responseRecorder, request)
103+
104+ select {
105+ case <-ch:
106+ return
107+ case <-time.After(time.Second * 1):
108+ t.Error("didnt receive analytics event within time limit")
109+ }
110+}
111+
112+type ImageStorageMemory struct {
113+ *storage.StorageMemory
114+ Opts *storage.ImgProcessOpts
115+ Fpath string
116+}
117+
118+func (s *ImageStorageMemory) ServeObject(bucket sst.Bucket, fpath string, opts *storage.ImgProcessOpts) (io.ReadCloser, string, error) {
119+ s.Opts = opts
120+ s.Fpath = fpath
121+ return io.NopCloser(strings.NewReader("hello world!")), "image/jpeg", nil
122+}
123+
124+func TestImageManipulation(t *testing.T) {
125+ bucketName := shared.GetAssetBucketName(testUserID)
126+ cfg := NewConfigSite()
127+ cfg.Domain = "pgs.test"
128+ expectedPath := "/app.jpg/s:500/rt:90"
129+ request := httptest.NewRequest("GET", mkpath(expectedPath), strings.NewReader(""))
130+ responseRecorder := httptest.NewRecorder()
131+
132+ sto := map[string]map[string]string{
133+ bucketName: {
134+ "test/app.jpg": "hello world!",
135+ },
136+ }
137+ memst, _ := storage.NewStorageMemory(sto)
138+ st := &ImageStorageMemory{StorageMemory: memst}
139+ ch := make(chan *db.AnalyticsVisits)
140+ dbpool := NewPgsAnalticsDb(cfg.Logger)
141+ apiConfig := &shared.ApiConfig{
142+ Cfg: cfg,
143+ Dbpool: dbpool,
144+ Storage: st,
145+ AnalyticsQueue: ch,
146+ }
147+ handler := shared.CreateServe(mainRoutes, createSubdomainRoutes(publicPerm), apiConfig)
148+ router := http.HandlerFunc(handler)
149+ router(responseRecorder, request)
150+
151+ if st.Fpath != "test/app.jpg" {
152+ t.Errorf("Want path '%s', got '%s'", "test/app.jpg", st.Fpath)
153+ }
154+
155+ if st.Opts.Ratio.Width != 500 {
156+ t.Errorf("Want ratio width '500', got '%d'", st.Opts.Ratio.Width)
157+ }
158+
159+ if st.Opts.Rotate != 90 {
160+ t.Errorf("Want rotate '90', got '%d'", st.Opts.Rotate)
161+ }
162+}