|
|
package main
import ( "encoding/base64" "encoding/json" "net/http" "net/http/httptest" "testing" "time"
"github.com/geplauder/lithium/controllers"
"github.com/bxcodec/faker/v3" "github.com/geplauder/lithium/pipelines" "github.com/geplauder/lithium/settings" "github.com/geplauder/lithium/storage" "github.com/gorilla/mux" "github.com/spf13/afero" "github.com/stretchr/testify/assert" )
func TestIndexRoute(t *testing.T) { data := pipelines.Pipeline{} err := faker.FakeData(&data) assert.Nil(t, err)
t.Run("Index route returns valid response", func(t *testing.T) { request := httptest.NewRequest(http.MethodGet, "/", nil) responseRecorder := httptest.NewRecorder()
controllers.IndexHandler([]pipelines.IPipeline{}, responseRecorder, request, "")
assert.Equal(t, 200, responseRecorder.Code, "Response code should be 200") assert.NotNil(t, responseRecorder.Body, "Response should contain body") })
t.Run("Index route returns valid amount of pipelines", func(t *testing.T) { request := httptest.NewRequest(http.MethodGet, "/", nil) responseRecorder := httptest.NewRecorder()
controllers.IndexHandler([]pipelines.IPipeline{data}, responseRecorder, request, "")
assert.Equal(t, 200, responseRecorder.Code, "Response code should be 200")
var body = controllers.Metadata{} err = json.Unmarshal(responseRecorder.Body.Bytes(), &body) assert.Nil(t, err) assert.Equal(t, 1, len(body.Pipelines)) assert.Equal(t, data.Slug, body.Pipelines[0]) }) }
func TestEndpointRoute(t *testing.T) { data := pipelines.Pipeline{} err := faker.FakeData(&data) assert.Nil(t, err)
t.Run("Registered pipelines are valid routes", func(t *testing.T) { router := mux.NewRouter() fs := storage.GetMemoryStorageProvider() appSettings, _ := settings.LoadSettings(afero.NewMemMapFs())
RegisterRoutes(router, appSettings, []pipelines.IPipeline{data}, fs)
request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil) responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request)
assert.Equal(t, 200, responseRecorder.Code) body, _ := json.Marshal(data) assert.JSONEq(t, string(body), responseRecorder.Body.String()) })
t.Run("Unregistered pipelines return 404", func(t *testing.T) { router := mux.NewRouter()
request, _ := http.NewRequest("GET", "/pipelines/"+data.Slug, nil) responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request)
assert.Equal(t, 404, responseRecorder.Code) }) }
func TestUploadRoute(t *testing.T) { t.Run("Test uploads missing multipart boundary", func(t *testing.T) { router := mux.NewRouter() fs := storage.GetMemoryStorageProvider() appSettings, _ := settings.LoadSettings(afero.NewMemMapFs())
RegisterRoutes(router, appSettings, []pipelines.IPipeline{pipelines.Pipeline{ Name: "", Slug: "", Type: 0, RemoveMetadata: false, Steps: []pipelines.Step{}, Output: struct { Format string `json:"format"` Quality int `json:"quality"` }{"jpeg", 10}, }}, fs)
request, _ := http.NewRequest("POST", "/upload", nil) request.Header["Content-Type"] = []string{"multipart/form-data"} responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request)
assert.Equal(t, 0x1A6, responseRecorder.Code) str, _ := base64.StdEncoding.DecodeString("eyJlcnJvciI6Im5" + "vIG11bHRpcGFydCBib3VuZGFyeSBwYXJhbSBpbiBDb250ZW50LVR5cGUifQ==") assert.JSONEq(t, string(str), responseRecorder.Body.String()) })
t.Run("Test uploads missing multipart boundary", func(t *testing.T) { router := mux.NewRouter() fs := storage.GetMemoryStorageProvider() appSettings, _ := settings.LoadSettings(afero.NewMemMapFs())
RegisterRoutes(router, appSettings, []pipelines.IPipeline{pipelines.Pipeline{ Name: "", Slug: "", Type: 0, RemoveMetadata: false, Steps: []pipelines.Step{}, Output: struct { Format string `json:"format"` Quality int `json:"quality"` }{"jpeg", 10}, }}, fs)
request, _ := http.NewRequest("POST", "/upload", nil) request.Header["Content-Type"] = []string{"multipart/form-data", "boundary=X-INSOMNIA-BOUNDARY"} responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request)
assert.Equal(t, 0x1A6, responseRecorder.Code) str, _ := base64.StdEncoding.DecodeString("eyJlcnJvciI6Im5vIG11bHRpcGFydCBib3VuZGFyeSBwYXJhbSBpbiBDb250ZW50LVR5cGUifQ==") assert.JSONEq(t, string(str), responseRecorder.Body.String()) }) }
func TestFilesRoute(t *testing.T) { data := pipelines.Pipeline{} err := faker.FakeData(&data) assert.Nil(t, err)
t.Run("Files endpoint returns files", func(t *testing.T) { router := mux.NewRouter() fs := storage.GetMemoryStorageProvider() appSettings, _ := settings.LoadSettings(afero.NewMemMapFs())
RegisterRoutes(router, appSettings, []pipelines.IPipeline{data}, fs)
request, _ := http.NewRequest("GET", "/files", nil) responseRecorder := httptest.NewRecorder()
router.ServeHTTP(responseRecorder, request)
type responseBody struct { Files []struct { Bucket string `json:"bucket"` FileName string `json:"file_name"` Size int `json:"size"` ModifiedAt time.Time `json:"modified_at"` } `json:"files"` }
var res responseBody
assert.Equal(t, 200, responseRecorder.Code) err := json.Unmarshal(responseRecorder.Body.Bytes(), &res) assert.Nil(t, err) }) }
|