219 lines
4.8 KiB
Go
219 lines
4.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"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 TestUnitGet(t *testing.T) {
|
|
t.Run("return `Not Found` error if package is not found", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/{name}", nil)
|
|
wrt := httptest.NewRecorder()
|
|
|
|
log := &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.GetIndexJsonFromStoreFunc = func(packageName string, registryPath string, log *logrus.Logger) (string, bool, error) {
|
|
return "", false, nil
|
|
}
|
|
|
|
//Hack to try to fake gorilla/mux vars
|
|
vars := map[string]string{
|
|
"name": "test-package",
|
|
}
|
|
|
|
req = mux.SetURLVars(req, vars)
|
|
|
|
GetPackage(log, cfg, mfs)(wrt, req)
|
|
|
|
rs := wrt.Result()
|
|
|
|
assert.Equal(t, rs.StatusCode, http.StatusNotFound)
|
|
|
|
defer rs.Body.Close()
|
|
body, err := io.ReadAll(rs.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bytes.TrimSpace(body)
|
|
|
|
assert.Equal(t, string(body), "Package not found: test-package\n")
|
|
})
|
|
|
|
t.Run("return `Internal Server` error if package cannot be retrieved", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/test-package", nil)
|
|
wrt := httptest.NewRecorder()
|
|
|
|
log := &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.GetIndexJsonFromStoreFunc = func(packageName string, registryPath string, log *logrus.Logger) (string, bool, error) {
|
|
return "", true, fmt.Errorf("filesystem error")
|
|
}
|
|
|
|
//Hack to try to fake gorilla/mux vars
|
|
vars := map[string]string{
|
|
"name": "test-package",
|
|
}
|
|
|
|
req = mux.SetURLVars(req, vars)
|
|
|
|
GetPackage(log, 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 a file if package is found", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
|
|
f, err := os.CreateTemp(tmpDir, "foo.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(tmpDir)
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString("{data: \"test data\"}")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/test-oackage", nil)
|
|
wrt := httptest.NewRecorder()
|
|
|
|
log := &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.GetIndexJsonFromStoreFunc = func(packageName string, registryPath string, log *logrus.Logger) (string, bool, error) {
|
|
return f.Name(), true, nil
|
|
}
|
|
// mfs.SetRetrieved(true)
|
|
// mfs.SetFileToServe(f.Name())
|
|
|
|
//Hack to try to fake gorilla/mux vars
|
|
vars := map[string]string{
|
|
"name": "test-package",
|
|
}
|
|
|
|
req = mux.SetURLVars(req, vars)
|
|
|
|
GetPackage(log, 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), "{data: \"test data\"}")
|
|
})
|
|
}
|
|
|
|
func TestIntegrationGet(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("Skipping getPackage 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))
|
|
|
|
indexJsonFp := "intestdata/get/index.json"
|
|
tgzFile := "intestdata/get/simplepackone-1.0.0.tgz"
|
|
|
|
cpFile(t, indexJsonFp, fmt.Sprintf("%s/@df/simplepackone/index.json", tmpDir))
|
|
cpFile(t, tgzFile, fmt.Sprintf("%s/@df/simplepackone/simplepackone-1.0.0.tgz", tmpDir))
|
|
|
|
listDir(t, fmt.Sprintf("%s/", tmpDir), true)
|
|
|
|
cfg := config.Config{
|
|
RepoDir: tmpDir,
|
|
Token: token,
|
|
}
|
|
|
|
app := newTestApp(t, cfg)
|
|
app.Routes()
|
|
ts := newTestServer(t, app.Mux)
|
|
defer ts.Close()
|
|
|
|
code, _, body := ts.get(t, fmt.Sprintf("/%s", url.PathEscape("@df/simplepackone")))
|
|
|
|
assert.Equal(t, code, http.StatusOK)
|
|
|
|
expected := readTestFile(t, indexJsonFp)
|
|
var resultExpected map[string]interface{}
|
|
var resultBody map[string]interface{}
|
|
json.Unmarshal(expected, &resultExpected)
|
|
json.Unmarshal(body, &resultBody)
|
|
|
|
assert.Equal(t, resultBody, resultExpected)
|
|
}
|