160 lines
3.7 KiB
Go
160 lines
3.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"gosimplenpm/internal/config"
|
|
"gosimplenpm/internal/storage"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestUnitTar(t *testing.T) {
|
|
t.Run("return `Bad request` error if tar package cannot be read from the filesystem", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/{name}/-/{tar}", nil)
|
|
wrt := httptest.NewRecorder()
|
|
|
|
lg := &logrus.Logger{
|
|
Out: os.Stdout,
|
|
// Level: "DEBUG",
|
|
Formatter: &logrus.TextFormatter{
|
|
FullTimestamp: true,
|
|
TimestampFormat: "2009-01-02 15:15:15",
|
|
},
|
|
}
|
|
|
|
cfg := config.Config{
|
|
RepoDir: "",
|
|
}
|
|
|
|
mfs := &storage.MockFs{}
|
|
mfs.GetTarFromStoreFunc = func(packageName string, tarFileName string, registryPath string, lg *logrus.Logger) (string, error) {
|
|
return "", fmt.Errorf("Filesystem error")
|
|
}
|
|
|
|
tag := "@test%2Fpackage-1.2.0.tgz"
|
|
|
|
//Hack to try to fake gorilla/mux vars
|
|
vars := map[string]string{
|
|
"name": "@test%2Fpackage",
|
|
"tar": tag,
|
|
}
|
|
|
|
req = mux.SetURLVars(req, vars)
|
|
|
|
PackageTarGet(lg, cfg, mfs)(wrt, req)
|
|
|
|
rs := wrt.Result()
|
|
|
|
assert.Equal(t, rs.StatusCode, http.StatusInternalServerError)
|
|
|
|
defer rs.Body.Close()
|
|
body, err := io.ReadAll(rs.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bytes.TrimSpace(body)
|
|
|
|
assert.Equal(t, string(body), "Filesystem error\n")
|
|
})
|
|
|
|
t.Run("return 200 OK if tar is found on the filesystem", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/{name}/-/{tar}", nil)
|
|
wrt := httptest.NewRecorder()
|
|
|
|
lg := &logrus.Logger{
|
|
Out: os.Stdout,
|
|
// Level: "DEBUG",
|
|
Formatter: &logrus.TextFormatter{
|
|
FullTimestamp: true,
|
|
TimestampFormat: "2009-01-02 15:15:15",
|
|
},
|
|
}
|
|
|
|
cfg := config.Config{
|
|
RepoDir: "",
|
|
}
|
|
|
|
mfs := &storage.MockFs{}
|
|
mfs.GetTarFromStoreFunc = func(packageName string, tarFileName string, registryPath string, lg *logrus.Logger) (string, error) {
|
|
return "OHnFFeCPAnb7E0jRLSuw4hVrNDVdDmKB4lbye6oZoBVItuRKy6ee43yAMaO6k0yhr2SU9HqWSZ", nil
|
|
}
|
|
|
|
tag := "@test%2Fpackage-1.2.0.tgz"
|
|
|
|
//Hack to try to fake gorilla/mux vars
|
|
vars := map[string]string{
|
|
"name": "@test%2Fpackage",
|
|
"tar": tag,
|
|
}
|
|
|
|
req = mux.SetURLVars(req, vars)
|
|
|
|
PackageTarGet(lg, cfg, mfs)(wrt, req)
|
|
|
|
rs := wrt.Result()
|
|
|
|
assert.Equal(t, rs.StatusCode, http.StatusOK)
|
|
|
|
defer rs.Body.Close()
|
|
body, err := io.ReadAll(rs.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bytes.TrimSpace(body)
|
|
|
|
assert.Equal(t, string(body), "OHnFFeCPAnb7E0jRLSuw4hVrNDVdDmKB4lbye6oZoBVItuRKy6ee43yAMaO6k0yhr2SU9HqWSZ")
|
|
})
|
|
}
|
|
|
|
func TestIntegrationTar(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("Skipping getTar integration test")
|
|
}
|
|
|
|
token := "0N89nr/hmKXoBzG]R{fKH%YE1X"
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
t.Logf("Temp Dir: %s", tmpDir)
|
|
|
|
// cpFolders(t, "intestdata/@df", fmt.Sprintf("%s/@df", tmpDir))
|
|
|
|
mkDir(t, fmt.Sprintf("%s/@df/simplepackone", tmpDir))
|
|
mkDir(t, fmt.Sprintf("%s/output", tmpDir))
|
|
|
|
indexJsonFp := "intestdata/tar/index.json"
|
|
tgzFp := "intestdata/tar/simplepackone-1.0.0.tgz"
|
|
|
|
cpFile(t, indexJsonFp, fmt.Sprintf("%s/@df/simplepackone/index.json", tmpDir))
|
|
cpFile(t, tgzFp, fmt.Sprintf("%s/@df/simplepackone/simplepackone-1.0.0.tgz", tmpDir))
|
|
|
|
cfg := config.Config{
|
|
RepoDir: tmpDir,
|
|
Token: token,
|
|
}
|
|
|
|
app := newTestApp(t, cfg)
|
|
app.Routes()
|
|
ts := newTestServer(t, app.Mux)
|
|
defer ts.Close()
|
|
|
|
tarUrlFragment := url.PathEscape("@df/simplepackone-1.0.0.tgz")
|
|
nameUrlFragment := url.PathEscape("@df/simplepackone")
|
|
|
|
code, _, body := ts.get(t, fmt.Sprintf("/%s/-/%s", nameUrlFragment, tarUrlFragment))
|
|
assert.Equal(t, code, http.StatusOK)
|
|
// assert.NotEmpty(t, body)
|
|
|
|
expected := readTestFileAsBase64(t, tgzFp)
|
|
assert.Equal(t, string(body), expected)
|
|
}
|