bluebell/upload/upload_test.go

111 lines
2.7 KiB
Go
Raw Normal View History

2023-07-22 00:59:45 +01:00
package upload
import (
"bytes"
"context"
"database/sql"
2023-07-22 00:59:45 +01:00
_ "embed"
"fmt"
"github.com/1f349/bluebell/database"
2023-07-22 00:59:45 +01:00
"github.com/julienschmidt/httprouter"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
)
2023-08-21 00:27:54 +01:00
var (
//go:embed test-archive.tar.gz
testArchiveTarGz []byte
)
2023-07-22 00:59:45 +01:00
func assertUploadedFile(t *testing.T, fs afero.Fs, branch string) {
switch branch {
case "main", "master":
branch = ""
}
2023-07-22 00:59:45 +01:00
// check uploaded file exists
stat, err := fs.Stat("example.com/@" + branch + "/test.txt")
2023-07-22 00:59:45 +01:00
assert.NoError(t, err)
assert.False(t, stat.IsDir())
assert.Equal(t, int64(13), stat.Size())
// check contents
o, err := fs.Open("example.com/@" + branch + "/test.txt")
2023-07-22 00:59:45 +01:00
assert.NoError(t, err)
all, err := io.ReadAll(o)
assert.NoError(t, err)
assert.Equal(t, "Hello world!\n", string(all))
}
type fakeUploadDB struct {
}
func (f *fakeUploadDB) GetSiteByDomain(_ context.Context, domain string) (database.Site, error) {
if domain == "example.com" {
return database.Site{
ID: 1,
Domain: "example.com",
Token: "abcd1234",
}, nil
}
return database.Site{}, sql.ErrNoRows
}
2023-07-22 00:59:45 +01:00
func TestHandler_Handle(t *testing.T) {
fs := afero.NewMemMapFs()
h := New(fs, new(fakeUploadDB))
2023-08-21 00:27:54 +01:00
r := httprouter.New()
r.POST("/u/:site/:branch", h.Handle)
r.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("Not Found")
})
for _, branch := range []string{"main", "test", "dev"} {
t.Run(branch+" branch", func(t *testing.T) {
mpBuf := new(bytes.Buffer)
mp := multipart.NewWriter(mpBuf)
file, err := mp.CreateFormFile("upload", "test-archive.tar.gz")
assert.NoError(t, err)
_, err = file.Write(testArchiveTarGz)
assert.NoError(t, err)
assert.NoError(t, mp.Close())
2023-07-22 00:59:45 +01:00
req := httptest.NewRequest(http.MethodPost, "https://example.com/u/example.com/"+branch, mpBuf)
req.Header.Set("Authorization", "Bearer abcd1234")
req.Header.Set("Content-Type", mp.FormDataContentType())
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
res := rec.Result()
assert.Equal(t, http.StatusAccepted, res.StatusCode)
assert.NotNil(t, res.Body)
all, err := io.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, "", string(all))
fmt.Println(fs)
assertUploadedFile(t, fs, branch)
})
}
2023-07-22 00:59:45 +01:00
}
func TestHandler_extractTarGzUpload(t *testing.T) {
for _, branch := range []string{"main", "test", "dev"} {
t.Run(branch+" branch", func(t *testing.T) {
fs := afero.NewMemMapFs()
2025-01-07 23:51:31 +00:00
h := New(fs, new(fakeUploadDB))
buffer := bytes.NewBuffer(testArchiveTarGz)
assert.NoError(t, h.extractTarGzUpload(buffer, "example.com", branch))
2023-07-22 00:59:45 +01:00
assertUploadedFile(t, fs, branch)
})
}
2023-07-22 00:59:45 +01:00
}