Skip to content

Commit

Permalink
chore(pgs): api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Oct 17, 2024
1 parent 220bdce commit 24df143
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions pgs/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 24df143

Please sign in to comment.