From 870cc2366543972aed9f441f503def5cadab3ba2 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Sat, 29 Mar 2025 23:27:38 +0000 Subject: [PATCH] Add some api tests --- api/api.go | 7 ++- api/api_test.go | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 api/api_test.go diff --git a/api/api.go b/api/api.go index 913d9a5..823cc0a 100644 --- a/api/api.go +++ b/api/api.go @@ -6,7 +6,6 @@ import ( "encoding/hex" "encoding/json" "github.com/1f349/bluebell/database" - "github.com/1f349/bluebell/upload" "github.com/1f349/bluebell/validation" "github.com/1f349/mjwt" "github.com/1f349/mjwt/auth" @@ -22,7 +21,11 @@ type apiDB interface { SetBranchEnabled(ctx context.Context, arg database.SetBranchEnabledParams) error } -func New(upload *upload.Handler, keyStore *mjwt.KeyStore, db apiDB) *httprouter.Router { +type uploadInterface interface { + Handle(rw http.ResponseWriter, req *http.Request, params httprouter.Params) +} + +func New(upload uploadInterface, keyStore *mjwt.KeyStore, db apiDB) *httprouter.Router { router := httprouter.New() router.GET("/", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) { diff --git a/api/api_test.go b/api/api_test.go new file mode 100644 index 0000000..d5eb67b --- /dev/null +++ b/api/api_test.go @@ -0,0 +1,113 @@ +package api + +import ( + "context" + "github.com/1f349/bluebell/database" + "github.com/1f349/mjwt" + "github.com/1f349/mjwt/auth" + "github.com/golang-jwt/jwt/v4" + "github.com/julienschmidt/httprouter" + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "testing" + "time" +) + +func TestApi(t *testing.T) { + keystore := mjwt.NewKeyStoreWithDir(afero.NewMemMapFs()) + issuer, err := mjwt.NewIssuerWithKeyStore("test", "1", jwt.SigningMethodRS512, keystore) + if err != nil { + panic(err) + } + + psValid := auth.NewPermStorage() + psValid.Set("domain:owns=example.com") + + psInvalid := auth.NewPermStorage() + psInvalid.Set("domain:owns=example.org") + + validToken, err := issuer.GenerateJwt("1", "2", jwt.ClaimStrings{"3"}, time.Hour, auth.AccessTokenClaims{Perms: psValid}) + if err != nil { + panic(err) + } + invalidToken, err := issuer.GenerateJwt("1", "2", jwt.ClaimStrings{"3"}, time.Hour, auth.AccessTokenClaims{Perms: psInvalid}) + if err != nil { + panic(err) + } + + mux := New(&fakeUpload{}, keystore, &fakeDB{}) + + t.Run("GET /", func(t *testing.T) { + rec := httptest.NewRecorder() + mux.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil)) + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, "Bluebell API Endpoint\n", rec.Body.String()) + }) + + t.Run("POST /u/example.com/main", func(t *testing.T) { + rec := httptest.NewRecorder() + mux.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/u/example.com/main", nil)) + assert.Equal(t, 788, rec.Code) + assert.Equal(t, "fakeUpload.Handle called\n", rec.Body.String()) + }) + t.Run("POST /u", func(t *testing.T) { + rec := httptest.NewRecorder() + mux.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/u", nil)) + assert.Equal(t, 788, rec.Code) + assert.Equal(t, "fakeUpload.Handle called\n", rec.Body.String()) + }) + + t.Run("GET /api/v1/sites/example.com", func(t *testing.T) { + t.Run("Invalid", func(t *testing.T) { + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/api/v1/sites/example.com", nil) + req.Header.Set("Authorization", "Bearer "+invalidToken) + mux.ServeHTTP(rec, req) + assert.Equal(t, http.StatusForbidden, rec.Code) + assert.Equal(t, "Forbidden\n", rec.Body.String()) + }) + t.Run("Valid", func(t *testing.T) { + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/api/v1/sites/example.com", nil) + req.Header.Set("Authorization", "Bearer "+validToken) + mux.ServeHTTP(rec, req) + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, "[{\"domain\":\"example.com\",\"branches\":[{\"domain\":\"example.com\",\"branch\":\"test\",\"last_update\":\"2000-02-08T01:02:03Z\",\"enable\":true}]}]\n", rec.Body.String()) + }) + }) +} + +type fakeUpload struct{} + +func (f *fakeUpload) Handle(rw http.ResponseWriter, req *http.Request, params httprouter.Params) { + rw.WriteHeader(788) + rw.Write([]byte("fakeUpload.Handle called\n")) +} + +type fakeDB struct{} + +func (f *fakeDB) GetBranchesByHost(ctx context.Context, arg database.GetBranchesByHostParams) ([]database.Branch, error) { + if arg.Domain == "example.com" && arg.DomainWildcard == "%.example.com" { + return []database.Branch{ + {Domain: "example.com", Branch: "test", LastUpdate: time.Date(2000, time.February, 8, 1, 2, 3, 0, time.UTC), Enable: true}, + }, nil + } + return []database.Branch{}, nil +} + +func (f *fakeDB) AddSite(ctx context.Context, arg database.AddSiteParams) error { + //TODO implement me + panic("implement me") +} + +func (f *fakeDB) UpdateSiteToken(ctx context.Context, arg database.UpdateSiteTokenParams) error { + //TODO implement me + panic("implement me") +} + +func (f *fakeDB) SetBranchEnabled(ctx context.Context, arg database.SetBranchEnabledParams) error { + //TODO implement me + panic("implement me") +}