From 24df14332dbda13d6ad9359550896b7f3aca9bfa Mon Sep 17 00:00:00 2001 From: Eric Bower Date: Wed, 16 Oct 2024 22:44:47 -0400 Subject: [PATCH] chore(pgs): api tests --- pgs/api_test.go | 127 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/pgs/api_test.go b/pgs/api_test.go index 880b3c60..1d1daf8e 100644 --- a/pgs/api_test.go +++ b/pgs/api_test.go @@ -2,16 +2,19 @@ package pgs import ( "fmt" + "io" "log/slog" "net/http" "net/http/httptest" "strings" "testing" + "time" "github.com/picosh/pico/db" "github.com/picosh/pico/db/stub" "github.com/picosh/pico/shared" "github.com/picosh/pico/shared/storage" + sst "github.com/picosh/pobj/storage" ) var testUserID = "user-1" @@ -59,6 +62,20 @@ func (p *PgsDb) FindProjectByName(userID, name string) (*db.Project, error) { }, nil } +type PgsAnalyticsDb struct { + *PgsDb +} + +func NewPgsAnalticsDb(logger *slog.Logger) *PgsAnalyticsDb { + return &PgsAnalyticsDb{ + PgsDb: NewPgsDb(logger), + } +} + +func (p *PgsAnalyticsDb) HasFeatureForUser(userID, feature string) bool { + return true +} + func mkpath(path string) string { return fmt.Sprintf("https://%s-test.pgs.test%s", testUsername, path) } @@ -180,6 +197,20 @@ func TestApiBasic(t *testing.T) { }, }, }, + { + name: "images", + path: "/profile.jpg", + want: "image", + status: http.StatusOK, + contentType: "image/jpeg", + + dbpool: NewPgsDb(cfg.Logger), + storage: map[string]map[string]string{ + bucketName: { + "test/profile.jpg": "image", + }, + }, + }, } for _, tc := range tt { @@ -215,3 +246,99 @@ func TestApiBasic(t *testing.T) { }) } } + +func TestAnalytics(t *testing.T) { + bucketName := shared.GetAssetBucketName(testUserID) + cfg := NewConfigSite() + cfg.Domain = "pgs.test" + expectedPath := "/app" + request := httptest.NewRequest("GET", mkpath(expectedPath), strings.NewReader("")) + responseRecorder := httptest.NewRecorder() + + sto := map[string]map[string]string{ + bucketName: { + "test/app.html": "hello world!", + }, + } + st, _ := storage.NewStorageMemory(sto) + ch := make(chan *db.AnalyticsVisits) + dbpool := NewPgsAnalticsDb(cfg.Logger) + apiConfig := &shared.ApiConfig{ + Cfg: cfg, + Dbpool: dbpool, + Storage: st, + AnalyticsQueue: ch, + } + handler := shared.CreateServe(mainRoutes, createSubdomainRoutes(publicPerm), apiConfig) + router := http.HandlerFunc(handler) + + go func() { + for analytics := range ch { + if analytics.Path != expectedPath { + t.Errorf("Want path '%s', got '%s'", expectedPath, analytics.Path) + } + close(ch) + } + }() + + router(responseRecorder, request) + + select { + case <-ch: + return + case <-time.After(time.Second * 1): + t.Error("didnt receive analytics event within time limit") + } +} + +type ImageStorageMemory struct { + *storage.StorageMemory + Opts *storage.ImgProcessOpts + Fpath string +} + +func (s *ImageStorageMemory) ServeObject(bucket sst.Bucket, fpath string, opts *storage.ImgProcessOpts) (io.ReadCloser, string, error) { + s.Opts = opts + s.Fpath = fpath + return io.NopCloser(strings.NewReader("hello world!")), "image/jpeg", nil +} + +func TestImageManipulation(t *testing.T) { + bucketName := shared.GetAssetBucketName(testUserID) + cfg := NewConfigSite() + cfg.Domain = "pgs.test" + expectedPath := "/app.jpg/s:500/rt:90" + request := httptest.NewRequest("GET", mkpath(expectedPath), strings.NewReader("")) + responseRecorder := httptest.NewRecorder() + + sto := map[string]map[string]string{ + bucketName: { + "test/app.jpg": "hello world!", + }, + } + memst, _ := storage.NewStorageMemory(sto) + st := &ImageStorageMemory{StorageMemory: memst} + ch := make(chan *db.AnalyticsVisits) + dbpool := NewPgsAnalticsDb(cfg.Logger) + apiConfig := &shared.ApiConfig{ + Cfg: cfg, + Dbpool: dbpool, + Storage: st, + AnalyticsQueue: ch, + } + handler := shared.CreateServe(mainRoutes, createSubdomainRoutes(publicPerm), apiConfig) + router := http.HandlerFunc(handler) + router(responseRecorder, request) + + if st.Fpath != "test/app.jpg" { + t.Errorf("Want path '%s', got '%s'", "test/app.jpg", st.Fpath) + } + + if st.Opts.Ratio.Width != 500 { + t.Errorf("Want ratio width '500', got '%d'", st.Opts.Ratio.Width) + } + + if st.Opts.Rotate != 90 { + t.Errorf("Want rotate '90', got '%d'", st.Opts.Rotate) + } +}